Use Forge Motion
useForgeMotion returns hydrated and animate (respecting prefers-reduced-motion unless told to ignore it); useForgeGlow returns the element's computed --forge-glow. Both read page-wide stores, so a page with many Forge blocks registers one reduced-motion listener and one MutationObserver in total.
pnpm dlx shadcn@latest add @leo/use-forge-motionAPI
| Name | Type | Default | Description |
|---|---|---|---|
| ForgeMotion.animate* | boolean | - | hydrated && (reducedMotion === "ignore" || the user does not prefer reduced motion). |
| ForgeMotion.hydrated* | boolean | - | False on the server and during hydration, true after. |
| parseGlow* | (value: string | null | undefined) => number | - | Unset or unparsable counts as fully on (1), matching CSS `var(--forge-glow, 1)`. |
| useForgeGlow* | (ref: RefObject<HTMLElement | null>) => number | - | The element's computed --forge-glow as set by the theme (0..1, not clamped); 1 on the server and when unset. `ref` must be a stable ref object (e.g. from `useRef`) whose element is rendered at mount. The value is read on mount and again only when the class, style or data-theme of <html> or <body> changes; an element attached later, or a change on the element itself, is not picked up. |
| useForgeMotion* | (reducedMotion?: "respect" | "ignore") => ForgeMotion | - | Page-wide motion state: `animate` is false until hydrated and, with "respect" (default), while the user prefers reduced motion. |
Source
"use client";
import { type RefObject, useEffect, useState, useSyncExternalStore } from "react";
export interface ForgeMotion {
/** False on the server and during hydration, true after. */
hydrated: boolean;
/** hydrated && (reducedMotion === "ignore" || the user does not prefer reduced motion). */
animate: boolean;
}
const REDUCED_QUERY = "(prefers-reduced-motion: reduce)";
const OBSERVED_ATTRIBUTES = ["class", "style", "data-theme"];
// Both stores are module-level so a page with many Forge blocks attaches one listener and one
// observer in total; each is released when its last subscriber leaves.
const reducedSubscribers = new Set<() => void>();
let reducedList: MediaQueryList | null = null;
function notifyReduced() {
for (const cb of reducedSubscribers) cb();
}
function subscribeReduced(cb: () => void) {
if (typeof window.matchMedia !== "function") return () => {};
reducedSubscribers.add(cb);
if (reducedSubscribers.size === 1) {
reducedList = window.matchMedia(REDUCED_QUERY);
reducedList.addEventListener("change", notifyReduced);
}
return () => {
reducedSubscribers.delete(cb);
if (reducedSubscribers.size === 0 && reducedList) {
reducedList.removeEventListener("change", notifyReduced);
reducedList = null;
}
};
}
// Unsubscribed reads query afresh rather than caching, so a re-mount never sees a stale value.
function getReduced() {
if (reducedList) return reducedList.matches;
return typeof window.matchMedia === "function" && window.matchMedia(REDUCED_QUERY).matches;
}
const themeSubscribers = new Set<() => void>();
let themeObserver: MutationObserver | null = null;
function subscribeTheme(cb: () => void) {
if (typeof MutationObserver !== "function") return () => {};
themeSubscribers.add(cb);
if (themeSubscribers.size === 1) {
themeObserver = new MutationObserver(() => {
for (const sub of themeSubscribers) sub();
});
const options = { attributes: true, attributeFilter: OBSERVED_ATTRIBUTES };
themeObserver.observe(document.documentElement, options);
themeObserver.observe(document.body, options);
}
return () => {
themeSubscribers.delete(cb);
if (themeSubscribers.size === 0 && themeObserver) {
themeObserver.disconnect();
themeObserver = null;
}
};
}
const noopSubscribe = () => () => {};
/** Page-wide motion state: `animate` is false until hydrated and, with "respect" (default), while
* the user prefers reduced motion. */
export function useForgeMotion(reducedMotion: "respect" | "ignore" = "respect"): ForgeMotion {
const hydrated = useSyncExternalStore(
noopSubscribe,
() => true,
() => false,
);
const reduced = useSyncExternalStore(subscribeReduced, getReduced, () => false);
return { hydrated, animate: hydrated && (reducedMotion === "ignore" || !reduced) };
}
/** Unset or unparsable counts as fully on (1), matching CSS `var(--forge-glow, 1)`. */
export function parseGlow(value: string | null | undefined): number {
const n = Number.parseFloat((value ?? "").trim());
return Number.isFinite(n) ? n : 1;
}
/** The element's computed --forge-glow as set by the theme (0..1, not clamped); 1 on the server and when unset.
*
* `ref` must be a stable ref object (e.g. from `useRef`) whose element is rendered at mount. The
* value is read on mount and again only when the class, style or data-theme of <html> or <body>
* changes; an element attached later, or a change on the element itself, is not picked up. */
export function useForgeGlow(ref: RefObject<HTMLElement | null>): number {
const [glow, setGlow] = useState(1);
// biome-ignore lint/correctness/useExhaustiveDependencies: ref is a stable object by contract (see the doc comment)
useEffect(() => {
const read = () => {
const el = ref.current;
setGlow(el ? parseGlow(window.getComputedStyle(el).getPropertyValue("--forge-glow")) : 1);
};
read();
return subscribeTheme(read);
}, []);
return glow;
}