Skip to content

Instantly share code, notes, and snippets.

@stekhn
Created June 10, 2022 07:55
Show Gist options
  • Save stekhn/11ec30523c79b146a4755bb4d67b5baa to your computer and use it in GitHub Desktop.
Save stekhn/11ec30523c79b146a4755bb4d67b5baa to your computer and use it in GitHub Desktop.
React hook to disable animations for users which prefer reduced motion
import { useState, useEffect } from "react";
export function useReducedMotion() {
const [matches, setMatch] = useState(
window.matchMedia("(prefers-reduced-motion: reduce)").matches
);
useEffect(() => {
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
const handleChange = () => {
setMatch(mediaQuery.matches);
};
handleChange();
mediaQuery.addEventListener("change", handleChange);
return () => {
mediaQuery.removeEventListener("change", handleChange);
};
}, []);
return matches;
}
// Usage
// const prefersReducedMotion = useReducedMotion();
// useEffect(() => {
// Globals.assign({
// skipAnimation: prefersReducedMotion,
// });
// }, [prefersReducedMotion]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment