import { useMotion } from "../lib/motion.js"; import { animate } from "animejs"; import { themedEase, themedSpring } from "../lib/motion-spring.js"; import { useId, useLayoutEffect, useRef } from "react"; import { GooeyInsetDefinition, GooeyShadowDefinition, GooeySurfaceDefinition, } from "./gooey.js"; /** @internal A selected bead emerging from a recessed radio well. */ export function RadioBead({ checked }: { checked: boolean }) { const id = `caps-radio-${useId().replaceAll(":", "")}`; const body = useRef(null); const initial = useRef(checked).current; const previous = useRef(checked); const root = useRef(null); const motion = useMotion(root); const { reduced } = motion; useLayoutEffect(() => { if (!body.current) return; const immediate = reduced || previous.current === checked; previous.current = checked; const radius = checked ? 4 : 0; // The radio's own lively settle, in the theme's feel. const press = themedEase(motion, { ease: "outCubic", duration: 75 }); const settle = checked ? themedSpring(motion, { bounce: 0.65, duration: 300 }) : themedEase(motion, { ease: "inCubic", duration: 100 }); const bead = animate( body.current, immediate ? { rx: radius, ry: radius, cy: 6, duration: 0, } : { rx: [ { to: checked ? 3.5 : 4.15, ...press }, { to: radius, ...settle }, ], ry: [ { to: checked ? 4.5 : 3.85, ...press }, { to: radius, ...settle }, ], cy: [ { to: checked ? 5.85 : 6.1, ...press }, { to: 6, ...settle }, ], }, ); return () => { bead.cancel(); }; }, [checked, reduced, motion]); return ( ); }