-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathAnimationCard.tsx
More file actions
587 lines (556 loc) · 20.3 KB
/
Copy pathAnimationCard.tsx
File metadata and controls
587 lines (556 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import { memo, useCallback, useMemo, useState } from "react";
import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
import { SUPPORTED_EASES, SUPPORTED_PROPS } from "@hyperframes/core/gsap-constants";
import { RESPONSIVE_GRID } from "./propertyPanelHelpers";
import { MetricField, SelectField } from "./propertyPanelPrimitives";
import { controlPointsForGsapEase } from "./studioMotion";
import {
EASE_LABELS,
METHOD_LABELS,
METHOD_TOOLTIPS,
PERCENT_PROPS,
PROP_CONSTRAINTS,
PROP_LABELS,
PROP_TOOLTIPS,
PROP_UNITS,
clampPropertyValue,
} from "./gsapAnimationConstants";
import { buildTweenSummary } from "./gsapAnimationHelpers";
import { EaseCurveSection } from "./EaseCurveSection";
import { ArcPathControls } from "./ArcPathControls";
import type { GsapAnimationEditCallbacks } from "./gsapAnimationCallbacks";
import { ComputedTweenNotice } from "./ComputedTweenNotice";
import { P } from "./panelTokens";
const BOOLEAN_PROPS = new Set(["visibility"]);
const STRING_PROPS = new Set(["filter", "clipPath"]);
const FILTER_PRESETS = [
{ label: "Blur", value: "blur(4px)" },
{ label: "Bright", value: "brightness(1.5)" },
{ label: "Gray", value: "grayscale(1)" },
{ label: "None", value: "none" },
];
const CLIP_PATH_PRESETS = [
{ label: "Circle", value: "circle(50% at 50% 50%)" },
{ label: "Inset", value: "inset(10%)" },
{ label: "None", value: "none" },
];
function isPercentProp(prop: string): boolean {
return PERCENT_PROPS.has(prop);
}
function displayValue(prop: string, val: number | string): string {
if (isPercentProp(prop)) return String(Math.round(Math.max(0, Math.min(1, Number(val))) * 100));
return String(val);
}
function adjustedValue(prop: string, raw: string): string {
if (isPercentProp(prop)) return String(clampPropertyValue(prop, Number(raw) / 100));
const num = Number(raw);
if (!Number.isNaN(num) && PROP_CONSTRAINTS[prop]) {
return String(clampPropertyValue(prop, num));
}
return raw;
}
function RemoveButton({ onClick, title }: { onClick: () => void; title: string }) {
return (
<button
type="button"
onClick={onClick}
className="flex-shrink-0 rounded p-0.5 text-neutral-600 transition-colors hover:bg-neutral-800 hover:text-red-400"
title={title}
>
<svg
width="12"
height="12"
viewBox="0 0 12 12"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
>
<path d="M3 3l6 6M9 3l-6 6" />
</svg>
</button>
);
}
function PropertyRow({
prop,
val,
onCommit,
onRemove,
removeTitle,
}: {
prop: string;
val: number | string;
onCommit: (adjusted: string) => void;
onRemove: () => void;
removeTitle: string;
}) {
if (BOOLEAN_PROPS.has(prop)) {
const isVisible = val === "visible" || val === 1;
return (
<div className="flex items-center gap-1">
<div className="min-w-0 flex-1 flex items-center gap-2 px-2 py-1 rounded-lg bg-neutral-900 border border-neutral-800">
<span className="flex-1 text-[11px] font-medium text-neutral-500">
{PROP_LABELS[prop] ?? prop}
</span>
<button
type="button"
onClick={() => onCommit(isVisible ? "hidden" : "visible")}
className={`flex-shrink-0 rounded-full transition-all duration-150 relative`}
style={{ width: 28, height: 16, background: isVisible ? P.accent : P.borderInput }}
title={isVisible ? "Visible — click to hide" : "Hidden — click to show"}
>
<span
className="absolute top-[2px] left-0 rounded-full transition-transform duration-150"
style={{
width: 12,
height: 12,
background: isVisible ? P.white : P.textMuted,
transform: isVisible ? "translateX(14px)" : "translateX(2px)",
}}
/>
</button>
</div>
<RemoveButton onClick={onRemove} title={removeTitle} />
</div>
);
}
if (STRING_PROPS.has(prop)) {
const presets =
prop === "filter" ? FILTER_PRESETS : prop === "clipPath" ? CLIP_PATH_PRESETS : [];
return (
<div className="flex flex-col gap-1">
<div className="flex items-center gap-1">
<div className="min-w-0 flex-1 flex items-center gap-2 px-2 py-1 rounded-lg bg-neutral-900 border border-neutral-800">
<span className="flex-shrink-0 text-[11px] font-medium text-neutral-500">
{PROP_LABELS[prop] ?? prop}
</span>
<input
type="text"
defaultValue={String(val)}
className="flex-1 bg-transparent text-[11px] text-neutral-200 outline-none"
onBlur={(e) => onCommit(e.currentTarget.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.currentTarget.blur();
}
}}
/>
</div>
<RemoveButton onClick={onRemove} title={removeTitle} />
</div>
{presets.length > 0 && (
<div className="flex gap-1 pl-1">
{presets.map((p) => (
<button
key={p.value}
type="button"
onClick={() => onCommit(p.value)}
className="px-1.5 py-0.5 rounded text-[9px] font-medium text-neutral-500 bg-neutral-800/50 hover:bg-neutral-800 hover:text-neutral-300 transition-colors"
>
{p.label}
</button>
))}
</div>
)}
</div>
);
}
return (
<div className="flex items-center gap-1">
<div className="min-w-0 flex-1">
<MetricField
label={PROP_LABELS[prop] ?? prop}
value={displayValue(prop, val)}
suffix={PROP_UNITS[prop]}
tooltip={PROP_TOOLTIPS[prop]}
scrub
liveCommit
onCommit={(raw) => onCommit(adjustedValue(prop, raw))}
/>
</div>
<RemoveButton onClick={onRemove} title={removeTitle} />
</div>
);
}
function AddPropertyTrigger({
adding,
available,
addLabel,
addTitle,
onAdd,
onOpen,
onClose,
buttonClassName,
}: {
adding: boolean;
available: string[];
addLabel: string;
addTitle: string;
onAdd: (prop: string) => void;
onOpen: () => void;
onClose: () => void;
buttonClassName: string;
}) {
if (adding && available.length > 0) {
return (
<select
autoFocus
className="min-w-0 rounded-lg border border-neutral-700 bg-neutral-900 px-2 py-1 text-[11px] text-neutral-100 outline-none"
defaultValue=""
onChange={(e) => {
if (e.target.value) onAdd(e.target.value);
onClose();
}}
onBlur={onClose}
>
<option value="" disabled>
Choose property…
</option>
{available.map((p) => (
<option key={p} value={p}>
{PROP_LABELS[p] ?? p}
</option>
))}
</select>
);
}
if (available.length === 0) return null;
return (
<button type="button" onClick={onOpen} className={buttonClassName} title={addTitle}>
{addLabel}
</button>
);
}
function parseNumericOrString(raw: string): number | string {
const num = Number(raw);
return Number.isFinite(num) ? num : raw;
}
interface AnimationCardProps extends GsapAnimationEditCallbacks {
animation: GsapAnimation;
defaultExpanded: boolean;
}
// fallow-ignore-next-line complexity
export const AnimationCard = memo(function AnimationCard({
animation,
defaultExpanded,
onUpdateProperty,
onUpdateMeta,
onDeleteAnimation,
onAddProperty,
onRemoveProperty,
onUpdateFromProperty,
onAddFromProperty,
onRemoveFromProperty,
onLivePreview,
onLivePreviewEnd,
onSetArcPath,
onUpdateArcSegment,
onUnroll,
}: AnimationCardProps) {
const [expanded, setExpanded] = useState(defaultExpanded);
const [addingProp, setAddingProp] = useState(false);
const [addingFromProp, setAddingFromProp] = useState(false);
const usedProps = useMemo(
() => new Set(Object.keys(animation.properties)),
[animation.properties],
);
const availableProps = useMemo(
() =>
SUPPORTED_PROPS.filter(
(p) => !usedProps.has(p) && (animation.method === "set" || !BOOLEAN_PROPS.has(p)),
),
[usedProps, animation.method],
);
const usedFromProps = useMemo(
() => new Set(Object.keys(animation.fromProperties ?? {})),
[animation.fromProperties],
);
const availableFromProps = useMemo(
() => SUPPORTED_PROPS.filter((p) => !usedFromProps.has(p) && !BOOLEAN_PROPS.has(p)),
[usedFromProps],
);
const commitProperty = useCallback(
(prop: string, raw: string) => {
const value = parseNumericOrString(raw);
onUpdateProperty(animation.id, prop, value);
onLivePreviewEnd?.();
},
[animation.id, onUpdateProperty, onLivePreviewEnd],
);
const scrubProperty = useCallback(
(prop: string, raw: string) => {
onLivePreview?.(prop, parseNumericOrString(raw));
},
[onLivePreview],
);
const commitFromProperty = useCallback(
(prop: string, raw: string) => {
const value = parseNumericOrString(raw);
onUpdateFromProperty?.(animation.id, prop, value);
onLivePreviewEnd?.();
},
[animation.id, onUpdateFromProperty, onLivePreviewEnd],
);
const commitDuration = useCallback(
(raw: string) => {
const num = Number(raw);
if (Number.isFinite(num) && num >= 0)
onUpdateMeta(animation.id, { duration: Math.max(0, num) });
},
[animation.id, onUpdateMeta],
);
const commitPosition = useCallback(
(raw: string) => {
const num = Number(raw);
if (Number.isFinite(num) && num >= 0)
onUpdateMeta(animation.id, { position: Math.max(0, num) });
},
[animation.id, onUpdateMeta],
);
const [copied, setCopied] = useState(false);
const [optimisticEase, setOptimisticEase] = useState<string | null>(null);
const methodLabel = METHOD_LABELS[animation.method] ?? animation.method;
const propEase =
(animation.keyframes ? animation.keyframes.easeEach : undefined) ?? animation.ease ?? "none";
const easeName = optimisticEase ?? propEase;
if (optimisticEase && propEase === optimisticEase) setOptimisticEase(null);
const easeLabel = easeName.startsWith("custom(")
? "Custom curve"
: (EASE_LABELS[easeName] ?? easeName);
const endTime =
typeof animation.position === "number"
? animation.position + (animation.duration ?? 0)
: animation.position;
const summary = useMemo(() => buildTweenSummary(animation), [animation]);
return (
<div className="border-b border-neutral-800 pb-3">
<button
type="button"
onClick={() => setExpanded((v) => !v)}
className="flex w-full items-center gap-2 py-1.5"
>
<span
className="rounded bg-panel-accent/10 px-1.5 py-0.5 text-[10px] font-semibold text-panel-accent"
title={METHOD_TOOLTIPS[animation.method]}
>
{methodLabel}
</span>
<span className="text-[11px] font-medium text-neutral-400" title="When this effect plays">
{typeof animation.position === "number"
? `${parseFloat(animation.position.toFixed(3))}s`
: animation.position}{" "}
– {typeof endTime === "number" ? `${parseFloat(endTime.toFixed(3))}s` : endTime}
</span>
<span className="ml-auto text-[10px] text-neutral-500" title={easeName}>
{easeLabel}
</span>
<svg
width="10"
height="10"
viewBox="0 0 10 10"
fill="currentColor"
className={`flex-shrink-0 text-neutral-500 transition-transform ${expanded ? "" : "-rotate-90"}`}
>
<path d="M2 3l3 4 3-4z" />
</svg>
</button>
{expanded && (
<div className="pt-2">
<div className="space-y-3">
<ComputedTweenNotice
provenance={animation.provenance}
onUnroll={onUnroll ? () => onUnroll(animation.id) : undefined}
/>
<div className="flex items-start gap-2">
<div className="flex-1">
<p className="text-[10px] leading-relaxed text-neutral-400 italic">{summary}</p>
{animation.keyframes && (
<p className="mt-1 text-[9px] text-neutral-500">
<span
className="inline-block w-2 h-2 mr-1 align-middle"
style={{
background: "currentColor",
clipPath: "polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%)",
}}
/>
Keyframed — edit values in the Layout panel above
</p>
)}
</div>
<button
type="button"
onClick={() => {
void navigator.clipboard.writeText(summary);
setCopied(true);
setTimeout(() => setCopied(false), 1500);
}}
className="flex-shrink-0 rounded px-1.5 py-0.5 text-[9px] font-medium text-neutral-500 transition-colors hover:bg-neutral-800 hover:text-neutral-300"
title="Copy description to clipboard — paste into agent prompts"
>
{copied ? "Copied" : "Copy"}
</button>
</div>
<div className={RESPONSIVE_GRID}>
{animation.method !== "set" && (
<MetricField
label="Length"
value={String(Math.max(0, animation.duration ?? 0))}
suffix="s"
tooltip="How long this effect lasts"
onCommit={commitDuration}
/>
)}
<MetricField
label="Starts at"
value={
typeof animation.position === "string"
? animation.position
: String(parseFloat(Math.max(0, animation.position).toFixed(3)))
}
suffix={typeof animation.position === "number" ? "s" : undefined}
tooltip="When this effect begins on the timeline"
onCommit={commitPosition}
/>
</div>
{animation.method !== "set" && (
<>
<SelectField
label="Speed"
value={easeName.startsWith("custom(") ? "custom" : easeName}
options={[...SUPPORTED_EASES, "custom"]}
onChange={(next) => {
const easeKey = animation.keyframes ? "easeEach" : "ease";
if (next === "custom") {
const points = controlPointsForGsapEase(
easeName !== "none" ? easeName : "power2.out",
);
const path = `M0,0 C${points.x1},${points.y1} ${points.x2},${points.y2} 1,1`;
setOptimisticEase(`custom(${path})`);
onUpdateMeta(animation.id, { [easeKey]: `custom(${path})` });
} else {
setOptimisticEase(next);
onUpdateMeta(animation.id, { [easeKey]: next });
}
}}
/>
<EaseCurveSection
ease={easeName}
duration={animation.duration}
onCustomEaseCommit={(customEase) => {
const easeKey = animation.keyframes ? "easeEach" : "ease";
setOptimisticEase(customEase);
onUpdateMeta(animation.id, { [easeKey]: customEase });
}}
/>
</>
)}
{animation.method === "fromTo" && (
<div className="space-y-1">
<p className="text-[9px] font-semibold uppercase tracking-wider text-orange-400/70">
From
</p>
<div className="space-y-1.5">
{Object.entries(animation.fromProperties ?? {}).map(([prop, val]) => (
<PropertyRow
key={prop}
prop={prop}
val={val}
onCommit={(adjusted) => commitFromProperty(prop, adjusted)}
onRemove={() => onRemoveFromProperty?.(animation.id, prop)}
removeTitle={`Remove from-${PROP_LABELS[prop] ?? prop}`}
/>
))}
</div>
<div className="pt-0.5">
<AddPropertyTrigger
adding={addingFromProp}
available={availableFromProps}
addLabel="+ From property"
addTitle="Add a from-state property"
onAdd={(prop) => onAddFromProperty?.(animation.id, prop)}
onOpen={() => setAddingFromProp(true)}
onClose={() => setAddingFromProp(false)}
buttonClassName="text-[11px] font-medium text-orange-400/70 transition-colors hover:text-orange-300"
/>
</div>
</div>
)}
{animation.method === "fromTo" && Object.keys(animation.properties).length > 0 && (
<p className="text-[9px] font-semibold uppercase tracking-wider text-panel-accent/70">
To
</p>
)}
{Object.keys(animation.properties).length > 0 && (
<div className="space-y-1.5">
{Object.entries(animation.properties).map(([prop, val]) => (
<PropertyRow
key={prop}
prop={prop}
val={val}
onCommit={(adjusted) => {
scrubProperty(prop, adjusted);
commitProperty(prop, adjusted);
}}
onRemove={() => onRemoveProperty(animation.id, prop)}
removeTitle={`Remove ${PROP_LABELS[prop] ?? prop}`}
/>
))}
</div>
)}
{onSetArcPath &&
(animation.properties.x != null ||
animation.properties.y != null ||
animation.keyframes) && (
<div className="border-t border-neutral-800 pt-3">
<ArcPathControls
arcPath={
animation.arcPath ?? { enabled: false, autoRotate: false, segments: [] }
}
segmentCount={Math.max(
animation.properties.x != null || animation.properties.y != null ? 1 : 0,
(animation.keyframes?.keyframes?.length ?? 0) - 1,
)}
onToggle={(enabled) =>
onSetArcPath(animation.id, {
enabled,
segments: animation.arcPath?.segments,
})
}
onUpdateSegment={(index, update) =>
onUpdateArcSegment?.(animation.id, index, update)
}
onToggleAutoRotate={(autoRotate) =>
onSetArcPath(animation.id, {
enabled: true,
autoRotate,
segments: animation.arcPath?.segments,
})
}
/>
</div>
)}
<div className="flex items-center gap-2 pt-1">
<AddPropertyTrigger
adding={addingProp}
available={availableProps}
addLabel="+ Effect"
addTitle="Add another animated property to this effect"
onAdd={(prop) => onAddProperty(animation.id, prop)}
onOpen={() => setAddingProp(true)}
onClose={() => setAddingProp(false)}
buttonClassName="text-[11px] font-medium text-neutral-400 transition-colors hover:text-neutral-200"
/>
<button
type="button"
onClick={() => onDeleteAnimation(animation.id)}
className="ml-auto text-[11px] font-medium text-red-400 transition-colors hover:text-red-300"
title="Remove this animation"
>
Remove
</button>
</div>
</div>
</div>
)}
</div>
);
});