@humanspeak/svelte-motion
Version:
Framer Motion for Svelte 5. Declarative motion.<tag> components with AnimatePresence exit animations, gestures (hover, tap, drag, focus, in-view), variants, FLIP layout animations, shared-layout transitions, spring physics, and scroll-linked motion values
44 lines (43 loc) • 1.78 kB
TypeScript
import { type BooleanSnapshot } from './booleanSnapshot.svelte.js';
/**
* State returned by {@link useReducedMotion}.
*/
export type ReducedMotionState = BooleanSnapshot;
/**
* Returns a `{ current, subscribe }` object that reflects the user's
* `prefers-reduced-motion` setting. Mirrors framer-motion's
* `useReducedMotion` adapted for Svelte 5 runes.
*
* - `state.current` is reactive — read it in templates / `$derived` /
* `$effect` and it tracks the underlying `matchMedia` listener.
* - `state.subscribe(run)` is the Svelte readable store contract:
* synchronously emits the current value, then re-emits on every change.
* Kept for compat with downstream hooks that still consume Svelte
* readables until the Tier 2 wave lands.
*
* Diverges from React framer-motion's plain `boolean | null` return for
* the same reason as `useCycle`: a `$state`-backed value must live on an
* object so reads inside getters preserve tracking under runes.
*
* SSR-safe: returns a static `{ current: false }` when `window` /
* `matchMedia` is unavailable, including when `matchMedia` throws.
*
* The media listener is bound to the surrounding reactive scope via
* `$effect` — call this from a component `<script>` block (the standard
* hook contract). On unmount the listener is detached automatically.
*
* @returns A `ReducedMotionState` reflecting the OS reduced-motion setting.
* @see https://motion.dev/docs/react-reduced-motion
*
* @example
* ```svelte
* <script lang="ts">
* import { useReducedMotion } from '@humanspeak/svelte-motion'
*
* const reduced = useReducedMotion()
* </script>
*
* <div style:transform={reduced.current ? 'none' : 'rotate(45deg)'} />
* ```
*/
export declare const useReducedMotion: () => ReducedMotionState;