# usePrefersReducedMotion

Hook that reports the prefers-reduced-motion media query and stays true on the server so motion starts off.

Source: https://www.ui95.dev/ui/use-prefers-reduced-motion

## Installation

```bash
npx shadcn@latest add https://www.ui95.dev/r/use-prefers-reduced-motion.json
```

Run it in a Radix shadcn project that already has the UI95 theme.

`hooks/use-prefers-reduced-motion.ts`

```tsx
import * as React from "react"

const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)"

function subscribe(onChange: () => void): () => void {
  const query = window.matchMedia(REDUCED_MOTION_QUERY)
  query.addEventListener("change", onChange)
  return () => query.removeEventListener("change", onChange)
}

function readSnapshot(): boolean {
  return window.matchMedia(REDUCED_MOTION_QUERY).matches
}

function readServerSnapshot(): boolean {
  return true
}

export function usePrefersReducedMotion(): boolean {
  return React.useSyncExternalStore(subscribe, readSnapshot, readServerSnapshot)
}
```

## Used by

- [cta-button](https://www.ui95.dev/ui/cta-button)
- [crt-screen](https://www.ui95.dev/ui/crt-screen)
