Skip to content

Instantly share code, notes, and snippets.

@brunodbo
Created March 26, 2024 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunodbo/85550489ee7b5ef97056b4d1885228b2 to your computer and use it in GitHub Desktop.
Save brunodbo/85550489ee7b5ef97056b4d1885228b2 to your computer and use it in GitHub Desktop.
This code adds a back button to the site header when our site is viewed in the MobiLoud app. When testing locally, everything works fine, but in the app, the `window.history.back()` method doesn't seem to work. I tested whether the event listener is being added in the app, and it is.
(function (Drupal, FontAwesome) {
'use strict';
Drupal.behaviors.appBackButton = {
attach: function (context) {
const mobileNavIconWrapper = document.getElementById('nav-mobile-show');
const isApp = navigator.userAgent.toLowerCase().includes('canvas');
if (!isApp) {
if (mobileNavIconWrapper) {
mobileNavIconWrapper.style.visibility = 'visible';
}
return;
}
const isFront = document.body.classList.contains('front');
if (isFront) {
return;
}
const backIcon = FontAwesome.icon({
prefix: 'far',
iconName: 'chevron-left'
}).node[0];
// Replace the mobile menu icon with a back button in the mobile app.
if (mobileNavIconWrapper) {
const backButtonWrapper = document.createElement('div');
backButtonWrapper.classList.add('mobile-back-button-wrapper');
const backButton = document.createElement('button');
backButton.classList.add('mobile-back-button');
backButton.prepend(backIcon);
backButton.setAttribute('aria-label', 'Back to previous page');
backButton.setAttribute('title', 'Back to previous page');
backButton.addEventListener('click', function () {
window.history.back();
});
backButtonWrapper.appendChild(backButton);
mobileNavIconWrapper.parentNode.replaceChild(backButtonWrapper, mobileNavIconWrapper);
}
}
};
})(Drupal, FontAwesome);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment