* Reinitialize variables in instantclick context
* Refactor and follow instantclick loading pattern elsewhere
* Actually fix conflicts oops
* Add navigation links to e2e seeds
* Add e2e tests for More button with instantclick
* Set element's onclick directly as opposed to use addEventListener
* Remove unnecessary null check
* Check for visibility instead of class list
Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
* Chain .should since we can 👍
* Update test and seed data to account for merge
* Use .findAllByLabelText instead of .get
Co-authored-by: Nick Taylor <nick@iamdeveloper.com>
Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
Co-authored-by: Nick Taylor <nick@iamdeveloper.com>
178 lines
5.1 KiB
JavaScript
178 lines
5.1 KiB
JavaScript
function closeHeaderMenu(memberMenu, menuNavButton) {
|
|
if (menuNavButton) {
|
|
menuNavButton.setAttribute('aria-expanded', 'false');
|
|
}
|
|
|
|
if (memberMenu) {
|
|
setTimeout(() => {
|
|
memberMenu.classList.remove('showing');
|
|
}, 5);
|
|
}
|
|
}
|
|
|
|
function blurHeaderMenu(memberMenu, menuNavButton, potentiallyActiveElement) {
|
|
setTimeout(() => {
|
|
if (document.activeElement !== potentiallyActiveElement) {
|
|
closeHeaderMenu(memberMenu, menuNavButton);
|
|
}
|
|
}, 10);
|
|
}
|
|
|
|
function toggleHeaderMenu(memberMenu, navigationButton) {
|
|
if (!memberMenu || !navigationButton) {
|
|
return;
|
|
}
|
|
|
|
const crayonsHeaderMenuClassList = memberMenu.classList;
|
|
if (crayonsHeaderMenuClassList.contains('showing')) {
|
|
crayonsHeaderMenuClassList.remove('showing');
|
|
navigationButton.setAttribute('aria-expanded', 'false');
|
|
} else {
|
|
crayonsHeaderMenuClassList.add('showing');
|
|
navigationButton.setAttribute('aria-expanded', 'true');
|
|
|
|
const firstNavLink = document.getElementById('first-nav-link');
|
|
if (firstNavLink) {
|
|
setTimeout(() => {
|
|
// focus first item on open
|
|
firstNavLink.focus();
|
|
}, 100);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function isTouchDevice() {
|
|
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|DEV-Native-ios/i.test(
|
|
navigator.userAgent,
|
|
);
|
|
}
|
|
|
|
export function initializeTouchDevice(memberTopMenu, menuNavButton) {
|
|
if (navigator.userAgent === 'DEV-Native-ios') {
|
|
document.body.classList.add('dev-ios-native-body');
|
|
}
|
|
|
|
if (memberTopMenu) {
|
|
const crayonsHeaderMenuClassList = memberTopMenu.classList;
|
|
|
|
setTimeout(() => {
|
|
closeHeaderMenu(memberTopMenu, menuNavButton);
|
|
|
|
if (isTouchDevice()) {
|
|
// Use a named function instead of anonymous so duplicate event handlers are discarded
|
|
menuNavButton.addEventListener('click', (_event) => {
|
|
toggleHeaderMenu(memberTopMenu, menuNavButton);
|
|
});
|
|
window.InstantClick.on('change', () => {
|
|
memberTopMenu.classList.remove('showing');
|
|
});
|
|
} else {
|
|
crayonsHeaderMenuClassList.add('desktop');
|
|
menuNavButton.addEventListener('click', (_event) => {
|
|
toggleHeaderMenu(memberTopMenu, menuNavButton);
|
|
});
|
|
memberTopMenu.addEventListener('keyup', (e) => {
|
|
if (
|
|
e.key === 'Escape' &&
|
|
crayonsHeaderMenuClassList.contains('showing')
|
|
) {
|
|
crayonsHeaderMenuClassList.remove('showing');
|
|
menuNavButton.focus();
|
|
}
|
|
});
|
|
document
|
|
.getElementById('last-nav-link')
|
|
.addEventListener('blur', (_event) => {
|
|
blurHeaderMenu(
|
|
memberTopMenu,
|
|
menuNavButton,
|
|
document.getElementById('second-last-nav-link'),
|
|
);
|
|
});
|
|
document.addEventListener('click', (_event) => {
|
|
// if clicking outside of the menu, close it
|
|
if (!memberTopMenu.contains(document.activeElement)) {
|
|
blurHeaderMenu(
|
|
memberTopMenu,
|
|
menuNavButton,
|
|
document.getElementById('first-nav-link'),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}, 10);
|
|
}
|
|
}
|
|
|
|
function toggleBurgerMenu() {
|
|
const { leftNavState = 'closed' } = document.body.dataset;
|
|
document.body.dataset.leftNavState =
|
|
leftNavState === 'open' ? 'closed' : 'open';
|
|
}
|
|
|
|
function showMoreMenu({ target }) {
|
|
target.nextElementSibling.classList.remove('hidden');
|
|
target.classList.add('hidden');
|
|
}
|
|
|
|
/**
|
|
* Gets a reference to InstantClick
|
|
*
|
|
* @param {number} [waitTime=2000] The amount of time to wait
|
|
* until giving up waiting for InstantClick to exist
|
|
*
|
|
* @returns {Promise<object>} The global instance of InstantClick.
|
|
*/
|
|
export async function getInstantClick(waitTime = 2000) {
|
|
return new Promise((resolve, reject) => {
|
|
const failTimer = setTimeout(() => {
|
|
clearInterval(timer);
|
|
reject(new Error('Unable to resolve InstantClick'));
|
|
}, waitTime);
|
|
|
|
const timer = setInterval(() => {
|
|
if (typeof InstantClick !== 'undefined') {
|
|
clearTimeout(failTimer);
|
|
clearInterval(timer);
|
|
resolve(InstantClick);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Initializes the hamburger menu for mobile navigation
|
|
*
|
|
* @param {HTMLElement[]} menuTriggers
|
|
* @param {HTMLElement[]} moreMenus
|
|
*/
|
|
export function initializeMobileMenu(menuTriggers, moreMenus) {
|
|
menuTriggers.forEach((trigger) => {
|
|
trigger.onclick = toggleBurgerMenu;
|
|
});
|
|
|
|
moreMenus.forEach((trigger) => {
|
|
trigger.onclick = showMoreMenu;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Sets the icon link visually for the current page if the current page
|
|
* is one of the main icon links of the top navigation.
|
|
*
|
|
* @param {string} currentPage
|
|
* @param {[string, HTMLElement][]} pageEntries
|
|
*/
|
|
export function setCurrentPageIconLink(currentPage, pageEntries) {
|
|
pageEntries
|
|
// Filter out nulls (means the user is logged out so most icons are not in the logged out view)
|
|
.filter(([, iconLink]) => iconLink)
|
|
.forEach(([page, iconLink]) => {
|
|
if (currentPage === page) {
|
|
iconLink.blur();
|
|
iconLink.classList.add('crayons-header__link--current');
|
|
} else {
|
|
iconLink.classList.remove('crayons-header__link--current');
|
|
}
|
|
});
|
|
}
|