Download code/web/src/hooks/motion.ts from MiniMaxAI/oss-ecosystem-data: direct link, hf CLI and curl.
- Browser
- Download file 993 Bytes
-
https://huggingface.co/datasets/MiniMaxAI/oss-ecosystem-data/resolve/61db3dcff00709b0c7af75b417f98ea7d6f08366/code/web/src/hooks/motion.ts
- Command line
-
hf download hf://datasets/MiniMaxAI/oss-ecosystem-data@61db3dcff00709b0c7af75b417f98ea7d6f08366/code/web/src/hooks/motion.ts
-
curl -L -o motion.ts https://huggingface.co/datasets/MiniMaxAI/oss-ecosystem-data/resolve/61db3dcff00709b0c7af75b417f98ea7d6f08366/code/web/src/hooks/motion.ts
993 Bytes
| import { useEffect, useState, type RefObject } from "react" | |
| /** Whether the visitor asked for less motion (videos then hold their first frame). */ | |
| export function useReducedMotion() { | |
| const q = "(prefers-reduced-motion: reduce)" | |
| const [on, setOn] = useState(() => matchMedia(q).matches) | |
| useEffect(() => { | |
| const mq = matchMedia(q) | |
| const f = () => setOn(mq.matches) | |
| mq.addEventListener("change", f) | |
| return () => mq.removeEventListener("change", f) | |
| }, []) | |
| return on | |
| } | |
| /** Plays a muted loop only while it is on screen and motion is welcome. */ | |
| export function useLoop(ref: RefObject<HTMLVideoElement | null>) { | |
| const still = useReducedMotion() | |
| useEffect(() => { | |
| const v = ref.current | |
| if (!v) return | |
| if (still) { | |
| v.pause() | |
| return | |
| } | |
| const io = new IntersectionObserver(([e]) => (e.isIntersecting ? v.play().catch(() => {}) : v.pause()), { rootMargin: "120px" }) | |
| io.observe(v) | |
| return () => io.disconnect() | |
| }, [ref, still]) | |
| } | |