fix: track animation ended state and wire preview controls correctly

- Replace boolean paused with AnimState ('playing'|'paused'|'ended')
- Use onAnimationEnd to detect when finite animations finish
- Play re-enables after end and restarts the animation (replay)
- Pause only active while playing; Restart always available
- Config changes auto-restart preview so edits are instantly visible

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-28 17:17:20 +01:00
parent 27c7372a31
commit 0e95b7e543

View File

@@ -14,6 +14,8 @@ interface Props {
onElementChange: (e: PreviewElement) => void; onElementChange: (e: PreviewElement) => void;
} }
type AnimState = 'playing' | 'paused' | 'ended';
const SPEEDS: { label: string; value: string }[] = [ const SPEEDS: { label: string; value: string }[] = [
{ label: '0.25×', value: '0.25' }, { label: '0.25×', value: '0.25' },
{ label: '0.5×', value: '0.5' }, { label: '0.5×', value: '0.5' },
@@ -23,9 +25,8 @@ const SPEEDS: { label: string; value: string }[] = [
export function AnimationPreview({ config, element, onElementChange }: Props) { export function AnimationPreview({ config, element, onElementChange }: Props) {
const styleRef = useRef<HTMLStyleElement | null>(null); const styleRef = useRef<HTMLStyleElement | null>(null);
const elementRef = useRef<HTMLDivElement>(null);
const [restartKey, setRestartKey] = useState(0); const [restartKey, setRestartKey] = useState(0);
const [paused, setPaused] = useState(false); const [animState, setAnimState] = useState<AnimState>('playing');
const [speed, setSpeed] = useState('1'); const [speed, setSpeed] = useState('1');
// Inject @keyframes CSS into document head // Inject @keyframes CSS into document head
@@ -36,21 +37,32 @@ export function AnimationPreview({ config, element, onElementChange }: Props) {
document.head.appendChild(styleRef.current); document.head.appendChild(styleRef.current);
} }
styleRef.current.textContent = buildCSS(config); styleRef.current.textContent = buildCSS(config);
// Restart preview whenever config changes so changes are immediately visible
setAnimState('playing');
setRestartKey((k) => k + 1);
}, [config]); }, [config]);
// Cleanup on unmount // Cleanup on unmount
useEffect(() => { useEffect(() => {
return () => { return () => { styleRef.current?.remove(); };
styleRef.current?.remove();
};
}, []); }, []);
const restart = () => { const restart = () => {
setPaused(false); setAnimState('playing');
setRestartKey((k) => k + 1); setRestartKey((k) => k + 1);
}; };
const handlePlay = () => {
if (animState === 'ended') {
// Animation finished — restart it
restart();
} else {
setAnimState('playing');
}
};
const scaledDuration = Math.round(config.duration / Number(speed)); const scaledDuration = Math.round(config.duration / Number(speed));
const isInfinite = config.iterationCount === 'infinite';
return ( return (
<Card className="h-full flex flex-col"> <Card className="h-full flex flex-col">
@@ -79,12 +91,12 @@ export function AnimationPreview({ config, element, onElementChange }: Props) {
{/* Animated element */} {/* Animated element */}
<div <div
key={restartKey} key={restartKey}
ref={elementRef}
className="animated relative z-10" className="animated relative z-10"
style={{ style={{
animationDuration: `${scaledDuration}ms`, animationDuration: `${scaledDuration}ms`,
animationPlayState: paused ? 'paused' : 'running', animationPlayState: animState === 'paused' ? 'paused' : 'running',
}} }}
onAnimationEnd={() => !isInfinite && setAnimState('ended')}
> >
{element === 'box' && ( {element === 'box' && (
<div className="w-20 h-20 rounded-xl bg-gradient-to-br from-violet-500 to-purple-600 shadow-lg shadow-purple-500/30" /> <div className="w-20 h-20 rounded-xl bg-gradient-to-br from-violet-500 to-purple-600 shadow-lg shadow-purple-500/30" />
@@ -118,17 +130,17 @@ export function AnimationPreview({ config, element, onElementChange }: Props) {
<Button <Button
size="icon-xs" size="icon-xs"
variant="outline" variant="outline"
onClick={() => { setPaused(false); }} onClick={handlePlay}
disabled={!paused} disabled={animState === 'playing'}
title="Play" title={animState === 'ended' ? 'Replay' : 'Play'}
> >
<Play className="h-3 w-3" /> <Play className="h-3 w-3" />
</Button> </Button>
<Button <Button
size="icon-xs" size="icon-xs"
variant="outline" variant="outline"
onClick={() => setPaused(true)} onClick={() => setAnimState('paused')}
disabled={paused} disabled={animState !== 'playing'}
title="Pause" title="Pause"
> >
<Pause className="h-3 w-3" /> <Pause className="h-3 w-3" />