Reputation: 113
I'm writing a frontend application using React, and am rendering an animation on one of the components. If the user has animations disabled in their settings, I'd like to display a static image in its place. Is there a way to tell if animations have been disabled so I can do some conditional rendering?
Upvotes: 0
Views: 1087
Reputation: 3130
@Phix gives the relevant standard.
To check in JavaScript as requested, you can use
const query = window.matchMedia(
'(prefers-reduced-motion: reduce)');
if(query.matches) ... else ...
In React, you could use @react-hook/media-query as follows:
import {useMediaQuery} from '@react-hook/media-query'
const Component = () => {
const matches = useMediaQuery(
'(prefers-reduced-motion: reduce)');
return matches ? ... : ...;
}
Upvotes: 6