Skip to content

Commit 20ef798

Browse files
committed
fix(studio): stop a uniform resize writing a scale GSAP ignores
A uniform drag committed the `scale` shorthand. If the tween's keyframes already stated `scaleX` and `scaleY`, the commit left both forms in the same keyframe, and GSAP animates each property name independently, so the longhands ran alongside the shorthand and won. The resize therefore computed the right number, wrote it to the file, and did nothing: the element snapped back to its old size the moment the handle was released. Reproduced from a real session, where a drop at 384px on a 630px element wrote {scaleX: 1, scaleY: 1, scale: 0.61} and rendered at the original size. The mixing hazard was already known in the other direction, where a non-uniform drag takes a rewrite path that normalizes every keyframe to the longhands. This makes the condition symmetric: whenever the tween already speaks longhands, a uniform drag speaks them too.
1 parent 477f776 commit 20ef798

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎packages/studio/src/hooks/gsapResizeIntercept.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,53 @@ it("scales from the element's real box, not a hardcoded fallback", async () => {
331331
expect(scale).toBeCloseTo(2, 1);
332332
expect(scale).toBeLessThan(3);
333333
});
334+
335+
/**
336+
* The bug: a uniform drag committed the `scale` shorthand into a tween whose
337+
* keyframes already stated `scaleX`/`scaleY`. GSAP animates each property name
338+
* independently, so the keyframe ran as `{ scaleX: 1, scaleY: 1, scale: 0.61 }`
339+
* and the longhands won. The resize computed the right number, wrote it, and
340+
* the element snapped straight back to its old size on release.
341+
*/
342+
it("does not mix the scale shorthand into a tween that speaks longhands", async () => {
343+
const el = document.createElement("div");
344+
el.id = "clip";
345+
el.setAttribute("data-hf-studio-original-box-width", "630");
346+
el.setAttribute("data-hf-studio-original-box-height", "252");
347+
document.body.append(el);
348+
const selection = { id: "clip", selector: "#clip", element: el } as DomEditSelection;
349+
const longhandTween = {
350+
...scaleFromTween(),
351+
keyframes: {
352+
keyframes: [
353+
{ percentage: 0, properties: { scaleX: 1, scaleY: 1 } },
354+
{ percentage: 100, properties: { scaleX: 1.15, scaleY: 1.15 } },
355+
],
356+
},
357+
} as unknown as GsapAnimation;
358+
const commitMutation = vi.fn();
359+
360+
// A uniform drop, so the old code took the shorthand branch and wrote
361+
// `scale` into keyframes that already stated the longhands.
362+
await tryGsapResizeIntercept(
363+
selection,
364+
{ width: 384, height: 216 },
365+
[longhandTween],
366+
fakeIframe(el, { scaleX: 1, scaleY: 1 }),
367+
commitMutation,
368+
);
369+
370+
const frames = commitMutation.mock.calls
371+
.map((call) => call[1] as { keyframes?: Array<{ properties: Record<string, number> }> })
372+
.flatMap((mutation) => mutation.keyframes ?? []);
373+
expect(frames.length).toBeGreaterThan(0);
374+
for (const frame of frames) {
375+
const names = Object.keys(frame.properties);
376+
const hasShorthand = names.includes("scale");
377+
const hasLonghand = names.includes("scaleX") || names.includes("scaleY");
378+
expect(hasShorthand && hasLonghand).toBe(false);
379+
}
380+
// And the resize still lands: 384/630 is about 0.61.
381+
const resized = frames.find((frame) => frame.properties.scaleX != null);
382+
expect(resized?.properties.scaleX).toBeCloseTo(0.61, 1);
383+
});

‎packages/studio/src/hooks/gsapResizeIntercept.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ function originalBoxSize(
7373
return Number.isFinite(inline) && inline > 0 ? inline : null;
7474
}
7575

76+
/**
77+
* Whether this tween already states scale as `scaleX`/`scaleY`.
78+
*
79+
* Both forms are legal, and either alone is fine. A tween holding both is not:
80+
* GSAP animates each property name independently, so the longhands run
81+
* alongside the shorthand and win, which silently discards whatever the
82+
* shorthand was set to.
83+
*/
84+
function tweenUsesScaleLonghands(anim: GsapAnimation | null): boolean {
85+
const isLonghand = (name: string) => name === "scaleX" || name === "scaleY";
86+
const inKeyframes = (anim?.keyframes?.keyframes ?? []).some((frame) =>
87+
Object.keys(frame.properties ?? {}).some(isLonghand),
88+
);
89+
return inKeyframes || Object.keys(anim?.properties ?? {}).some(isLonghand);
90+
}
91+
7692
// ── Resize intercept ──────────────────────────────────────────────────────
7793

7894
// fallow-ignore-next-line complexity
@@ -180,6 +196,8 @@ export async function tryGsapResizeIntercept(
180196
let scaleDraftEl: HTMLElement | null = null;
181197
let scaleDraftDropPoint: { x: number; y: number } | null = null;
182198
let nonUniformScale = false;
199+
/** Whether this commit writes scaleX/scaleY rather than the `scale` shorthand. */
200+
let useScaleLonghands = false;
183201
if (resizeGroup === "scale") {
184202
// Iframe-realm element — instanceof HTMLElement fails across realms; the
185203
// selector targets composition elements, and every use below is duck-typed.
@@ -210,8 +228,18 @@ export async function tryGsapResizeIntercept(
210228
// can't represent it — committing width-derived scale used to snap the
211229
// height at drop. Commit scaleX/scaleY longhands instead; keep the uniform
212230
// shorthand when the two agree (aspect-true drags, shift-drags).
231+
//
232+
// Unless the tween already speaks longhands, in which case a uniform drag
233+
// has to as well. GSAP animates each property name on its own, so a
234+
// keyframe holding `{ scaleX: 1, scaleY: 1, scale: 0.61 }` runs all three
235+
// and the longhands win: the resize commits correctly and then does
236+
// nothing, and the element snaps back to its old size on release. The
237+
// tween never mixes the two forms in either direction.
213238
nonUniformScale = Math.abs(newScaleX - newScaleY) > 0.01;
214-
resizeProps = nonUniformScale ? { scaleX: newScaleX, scaleY: newScaleY } : { scale: newScaleX };
239+
useScaleLonghands = nonUniformScale || tweenUsesScaleLonghands(anim);
240+
resizeProps = useScaleLonghands
241+
? { scaleX: newScaleX, scaleY: newScaleY }
242+
: { scale: newScaleX };
215243
logResize("intercept-route", {
216244
route: "scale-tween",
217245
cssW,
@@ -370,7 +398,7 @@ export async function tryGsapResizeIntercept(
370398
// normalizes every keyframe to the longhands. For an in-range resize the
371399
// min/max window math below degenerates to the tween's own start/duration,
372400
// so timing is unchanged.
373-
if ((outsideRange || nonUniformScale) && ts !== null) {
401+
if ((outsideRange || useScaleLonghands) && ts !== null) {
374402
// For flat tweens, synthesize the keyframes from the tween's properties
375403
const kfs =
376404
anim.keyframes?.keyframes ??

0 commit comments

Comments
 (0)