Created
April 24, 2024 12:49
-
-
Save VelichkoAlexander/e77021afeb8efb91ed00702e99252486 to your computer and use it in GitHub Desktop.
How we can catch `prefers-reduced-motion` by js
This file contains hidden or 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
const MEDIA_QUERY = '(prefers-reduced-motion: reduce)'; | |
const CSS_PROPERTY = '--reduced-motion-play-state'; | |
const documentRoot = document.documentElement; | |
const handleMediaChange = (event) => { | |
if (event.matches) { | |
documentRoot.style.setProperty(CSS_PROPERTY, 'paused'); | |
} else { | |
documentRoot.style.removeProperty(CSS_PROPERTY); | |
} | |
}; | |
const setReducedMotion = () => { | |
const mediaQueryList = window.matchMedia(MEDIA_QUERY); | |
handleMediaChange(mediaQueryList); | |
mediaQueryList.addEventListener('change', handleMediaChange); | |
}; | |
export default setReducedMotion; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment