Skip to content

Instantly share code, notes, and snippets.

@VelichkoAlexander
Created April 24, 2024 12:49
Show Gist options
  • Save VelichkoAlexander/e77021afeb8efb91ed00702e99252486 to your computer and use it in GitHub Desktop.
Save VelichkoAlexander/e77021afeb8efb91ed00702e99252486 to your computer and use it in GitHub Desktop.
How we can catch `prefers-reduced-motion` by js
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