I see there have been topics about having mobile back button navigation for mobile browsers and I didn’t find solution though maybe I missed it
I’m not a coder and proficient enough with Discourse to dangerous but I did fumble through with ChatGPT to make the working BLUE colored back button in this image with my pup.
I want to hide on specific pages but haven’t figured out how to hide elements yet. If anyone knows I will adjust and add to instructions.
How to Add a Floating Back Button in Discourse (For Mobile Users)
This guide helps you add a floating “Back” button for mobile devices in your Discourse forum. It ensures the button appears only for mobile users and improves navigation for apps or browsers lacking built-in back buttons.
Step 1: Create a New Theme Component
- Log in as Admin:
- Go to your Discourse forum and log in with an admin account.
- Access Theme Customization:
- Navigate to
Admin > Customize > Themes
.
- Create a Theme Component:
- Click Components.
- Click Install > Create New.
- Name the component “Mobile Back Button” and click Create.
Step 2: Add the Back Button Code
- Edit the Theme Component:
- Click on the newly created theme component.
- Go to the CSS/HTML section.
- Add JavaScript for the Back Button:
- Switch to the Mobile tab.
- Click on the
</head>
section. - Paste the following code:
html
Copy code
<script type="text/discourse-plugin" version="0.8">
function addMobileBackButton() {
const isMobileView = api.container.lookup('site:main').mobileView;
if (isMobileView) {
const existingButton = document.querySelector('.floating-back-button');
if (!existingButton) {
const backButton = document.createElement('button');
backButton.className = 'floating-back-button';
backButton.innerHTML = '←'; // Unicode for left arrow
backButton.onclick = (event) => {
event.preventDefault();
window.history.back();
};
document.body.appendChild(backButton);
}
}
}
// Add the button on initial page load
document.addEventListener('DOMContentLoaded', addMobileBackButton);
// Re-check on subsequent page transitions
api.onPageChange(addMobileBackButton);
</script>
Step 3: Add CSS for Styling
- Add the Back Button Styles:
- In the Mobile tab, click on the CSS section.
- Paste the following CSS:
css
Copy code
.floating-back-button {
position: fixed;
bottom: 20px;
left: 20px;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #0088cc; /* Customize the color */
color: #fff;
border: none;
font-size: 24px;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
z-index: 1000;
cursor: pointer;
}
Step 4: Apply the Theme Component
- Go Back to Themes:
- Navigate back to
Admin > Customize > Themes
.
- Edit Your Active Theme:
- Click on your active theme.
- Under Theme Components, add the “Mobile Back Button” component from the dropdown.
- Click Add and then Save.
Step 5: Test Your Changes
- Open Your Forum on Mobile:
- Use a mobile browser or simulate mobile view using developer tools.
- Verify the Back Button:
- The button should appear in the bottom-left corner of the screen.
- Clicking the button should navigate you back in your browser history.
Notes:
- For Mobile Only: Since the code and CSS are in the Mobile tab, the button will only appear on mobile devices.
- Customization:
- You can adjust the button’s position, size, or color in the CSS to match your forum’s design.
- Functionality:
- The back button uses
window.history.back()
. If there’s no previous page, it won’t navigate anywhere.
- The back button uses