'use client'; import { useEffect, useRef, useState } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'; import { Play, Pause, RotateCcw, Square, Circle, Type } from 'lucide-react'; import { buildCSS } from '@/lib/animate/cssBuilder'; import type { AnimationConfig, PreviewElement } from '@/types/animate'; interface Props { config: AnimationConfig; element: PreviewElement; onElementChange: (e: PreviewElement) => void; } const SPEEDS: { label: string; value: string }[] = [ { label: '0.25×', value: '0.25' }, { label: '0.5×', value: '0.5' }, { label: '1×', value: '1' }, { label: '2×', value: '2' }, ]; export function AnimationPreview({ config, element, onElementChange }: Props) { const styleRef = useRef(null); const elementRef = useRef(null); const [restartKey, setRestartKey] = useState(0); const [paused, setPaused] = useState(false); const [speed, setSpeed] = useState('1'); // Inject @keyframes CSS into document head useEffect(() => { if (!styleRef.current) { styleRef.current = document.createElement('style'); styleRef.current.id = 'kit-animate-preview'; document.head.appendChild(styleRef.current); } styleRef.current.textContent = buildCSS(config); }, [config]); // Cleanup on unmount useEffect(() => { return () => { styleRef.current?.remove(); }; }, []); const restart = () => { setPaused(false); setRestartKey((k) => k + 1); }; const scaledDuration = Math.round(config.duration / Number(speed)); return ( Preview {/* Preview canvas */}
{/* Grid overlay */}
{/* Animated element */}
{element === 'box' && (
)} {element === 'circle' && (
)} {element === 'text' && ( Hello )}
{/* Controls */}
v && onElementChange(v as PreviewElement)} className="gap-1" >
); }