Motion API as an alternative
If a project already uses Motion for most transitions, Motion's own useReducedMotion hook can replace the custom hook. Keep the UI state the same, then change the animated values that create large visual travel.
import { useReducedMotion, motion } from "motion/react"
export function Sidebar({ isOpen }) {
const shouldReduceMotion = useReducedMotion();
const closedX = shouldReduceMotion ? 0 : "-100%";
return (
<motion.div animate={{
opacity: isOpen ? 1 : 0,
x: isOpen ? 0 : closedX
}} />
)
}You can also set the behavior once for a subtree with MotionConfig. This is useful when Motion already owns the animation tree and should follow the user's device preference by default.
import { MotionConfig } from "motion/react";
// ...
<MotionConfig reducedMotion="user">{children}</MotionConfig>