1. The three ways React animations break under real users
Most UI bugs happen during interruption, not during clean happy-path demos. Three failure modes cause almost every broken motion bug in production:
- Hard-resetting start position: starting a new timeline from 0 instead of the current rendered value causes a visible teleport.
- Velocity discontinuity: reversing direction with a fixed easing curve kills existing momentum and feels robotic.
- Stale timers & orphaned rAF loops: calling setState or mutating styles after unmount or after a newer interaction supersedes an older one.
2. Keep physical state in refs, semantic state in React
Store your semantic truth (open: boolean, activeTab: number) in React state so rendering and ARIA attributes stay declarative. Store continuous motion state (value, velocity, target, rafId) in a mutable ref so interrupting an animation only updates spring.target—never resetting value or velocity.
import { useEffect, useRef, useState } from "react";
export function InterruptibleToggle() {
const [on, setOn] = useState(false);
const knobRef = useRef<HTMLSpanElement>(null);
const sim = useRef({ x: 0, v: 0, target: 0, raf: 0, last: 0 });
useEffect(() => {
const s = sim.current;
s.target = on ? 24 : 0; // Only update target — keep current x and v!
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
s.x = s.target;
s.v = 0;
if (knobRef.current) knobRef.current.style.transform = `translate3d(${s.x}px, 0, 0)`;
return;
}
const step = (now: number) => {
const dt = Math.min(0.05, (now - s.last) / 1000);
s.last = now;
const force = -420 * (s.x - s.target) - 28 * s.v;
s.v += force * dt;
s.x += s.v * dt;
if (knobRef.current) {
knobRef.current.style.transform = `translate3d(${s.x.toFixed(2)}px, 0, 0)`;
}
if (Math.abs(s.v) > 0.05 || Math.abs(s.x - s.target) > 0.05) {
s.raf = requestAnimationFrame(step);
} else {
s.x = s.target;
s.v = 0;
if (knobRef.current) knobRef.current.style.transform = `translate3d(${s.x}px, 0, 0)`;
s.raf = 0;
}
};
if (!s.raf) {
s.last = performance.now();
s.raf = requestAnimationFrame(step);
}
}, [on]);
useEffect(() => () => cancelAnimationFrame(sim.current.raf), []);
return (
<button
type="button"
role="switch"
aria-checked={on}
onClick={() => setOn((v) => !v)}
className="relative inline-flex h-7 w-13 items-center rounded-full bg-zinc-200 p-0.5"
>
<span ref={knobRef} className="size-6 rounded-full bg-zinc-900 will-change-transform" />
</button>
);
}3. Stress-test every component with 10x rapid spam
Before shipping any interactive widget, hammer it in Playwright: click 12 times in 600ms, press Escape mid-animation, and assert 0 uncaught errors and exact final ARIA state. If a component survives 12x spam and mid-flight unmount, it will survive real users.
Accessibility checklist
Tick each one off as you check your build. Your ticks stay in this browser.
0 of 2 checked
Build it yourself or install it
Build it yourself when
You are hardening an existing custom toggle or modal inside your own design system.
Install Kipluo when
Every Kipluo component is built around ref-driven interruptible springs and verified with rapid-spam & mid-flight unmount Playwright tests.