-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLANET-7135: Fix country selector tab index (#2187)
* PLANET-7136: Fix country selector tab index Ref: https://jira.greenpeace.org/browse/PLANET-7135 - Implement a new script - Improve SEO on this element - Fix border bottom - Fix tabindex to country group - Fix focus tab - Change the order of elements programatically - Apply transition duration dynamic variable - Update JSDoc --------- Co-authored-by: Dan Tovbein <dtovbein@gmail.com>
- Loading branch information
1 parent
03ff5bb
commit a87ba2d
Showing
6 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* The script basically add/remove the tabindex attribute within the country selector | ||
* It helps to improve SEO | ||
* @return {void} | ||
*/ | ||
export const setupCountrySelector = () => { | ||
const targetNode = document.querySelector('#country-selector'); | ||
const countriesList = document.querySelector('.countries-list'); | ||
const footerMenu = document.querySelector('.footer-menu'); | ||
const footerSocialMedia = document.querySelector('.footer-social-media'); | ||
|
||
if (!targetNode || !countriesList || !footerMenu || !footerSocialMedia) { | ||
return; | ||
} | ||
|
||
const largeAndUpScreens = '(min-width: 992px)'; | ||
const countriesListTransitionDuration = ( | ||
parseFloat(getComputedStyle(countriesList).transitionDuration) * 1000 | ||
) || 500; | ||
|
||
/** | ||
* This funtion re-order the elements from the footer | ||
* @param {*} evt | ||
*/ | ||
const switchOrders = (evt = null) => { | ||
const event = evt ? evt : window.matchMedia(largeAndUpScreens); | ||
if (event.matches) { | ||
footerMenu.parentNode.prepend(footerMenu); | ||
} else { | ||
footerSocialMedia.parentNode.prepend(footerSocialMedia); | ||
} | ||
}; | ||
|
||
// Create an observer instance linked to the callback function | ||
const observer = new MutationObserver( | ||
// Callback function to execute when mutations are observed | ||
mutationList => { | ||
// Set default visibility | ||
for (const mutation of mutationList) { | ||
if (mutation.attributeName === 'class') { | ||
if (targetNode.classList.contains('open')) { | ||
countriesList.style.visibility = 'visible'; | ||
} else { | ||
setTimeout(() => { | ||
countriesList.style.visibility = 'hidden'; | ||
}, countriesListTransitionDuration); | ||
} | ||
} | ||
} | ||
} | ||
); | ||
|
||
// Start observing the target node for configured mutations | ||
observer.observe(targetNode, {attributes: true, childList: true, subtree: true}); | ||
|
||
window.matchMedia(largeAndUpScreens).addEventListener('change', event => { | ||
switchOrders(event); | ||
}); | ||
|
||
// Set default order | ||
switchOrders(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters