useReducedMotion
hooksFreeMITTrue when the visitor has asked the system for less motion.
npx shadcn@latest add https://mikkelmanniche.dk/lab/r/use-reduced-motion.jsonPreview
No live demo for this one. The code is under Code.
Code
Install
npx shadcn@latest add https://mikkelmanniche.dk/lab/r/use-reduced-motion.jsonWith the registry added (components.json)
npx shadcn@latest add @manniche/use-reduced-motionSource
use-reduced-motion.ts
import { useSyncExternalStore } from 'react'
const QUERY = '(prefers-reduced-motion: reduce)'
function subscribe(onChange: () => void) {
const mq = window.matchMedia(QUERY)
mq.addEventListener('change', onChange)
return () => mq.removeEventListener('change', onChange)
}
/** True when the visitor has asked the system for less motion. */
export function useReducedMotion() {
return useSyncExternalStore(
subscribe,
() => window.matchMedia(QUERY).matches,
() => false,
)
}Props
No props documented yet.
A11y
Reduced motion
Returns true while the system asks for less motion (prefers-reduced-motion: reduce) and re-renders when that setting changes. It returns false on the server.
Details
Dependencies
None beyond React and Tailwind.