From 1cc1a2e121c8d77dedb264f32a75d9979ff1908a Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Wed, 15 Jul 2026 19:24:35 -0400 Subject: [PATCH] fix(studio): remove trap options and wrong defaults from the inspector --- .../editor/propertyPanel3dTransform.test.tsx | 101 ++++++++++ .../editor/propertyPanel3dTransform.tsx | 45 +---- .../editor/propertyPanelPrimitives.test.tsx | 172 ++++++++++++++++++ .../editor/propertyPanelPrimitives.tsx | 11 +- .../editor/propertyPanelSections.tsx | 2 +- .../editor/propertyPanelStyleSections.tsx | 23 +-- 6 files changed, 292 insertions(+), 62 deletions(-) create mode 100644 packages/studio/src/components/editor/propertyPanel3dTransform.test.tsx create mode 100644 packages/studio/src/components/editor/propertyPanelPrimitives.test.tsx diff --git a/packages/studio/src/components/editor/propertyPanel3dTransform.test.tsx b/packages/studio/src/components/editor/propertyPanel3dTransform.test.tsx new file mode 100644 index 0000000000..f2ab064bf3 --- /dev/null +++ b/packages/studio/src/components/editor/propertyPanel3dTransform.test.tsx @@ -0,0 +1,101 @@ +// @vitest-environment happy-dom + +import React, { act } from "react"; +import { createRoot } from "react-dom/client"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import type { DomEditSelection } from "./domEditingTypes"; +import { PropertyPanel3dTransform } from "./propertyPanel3dTransform"; + +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + +afterEach(() => { + vi.useRealTimers(); + document.body.innerHTML = ""; +}); + +function createSelection(): DomEditSelection { + const composition = document.createElement("div"); + composition.dataset.compositionId = "main"; + Object.defineProperty(composition, "offsetHeight", { value: 720 }); + const element = document.createElement("div"); + composition.append(element); + document.body.append(composition); + + return { + element, + id: "card", + selector: "#card", + sourceFile: "index.html", + compositionPath: "index.html", + label: "Card", + tagName: "div", + isCompositionHost: false, + isInsideLockedComposition: false, + boundingBox: { x: 0, y: 0, width: 100, height: 100 }, + textContent: null, + dataAttributes: {}, + inlineStyles: {}, + computedStyles: {}, + textFields: [], + capabilities: { + canSelect: true, + canEditStyles: true, + canCrop: true, + canMove: true, + canResize: true, + canApplyManualOffset: true, + canApplyManualSize: true, + canApplyManualRotation: true, + }, + }; +} + +describe("PropertyPanel3dTransform depth control", () => { + it("previews and commits only z when scrolling for depth", () => { + vi.useFakeTimers(); + const previews: Array> = []; + const commits: Array> = []; + const host = document.createElement("div"); + document.body.append(host); + const root = createRoot(host); + + act(() => { + root.render( + { + commits.push(props); + }} + onLivePreviewProps={(_element, props) => { + previews.push(props); + }} + />, + ); + }); + + const cube = host.querySelector('svg[role="slider"]'); + if (!cube) throw new Error("Missing 3D transform cube"); + + act(() => { + cube.dispatchEvent(new WheelEvent("wheel", { deltaY: -400, cancelable: true })); + }); + expect(previews).toEqual([{ z: 100 }]); + + act(() => vi.advanceTimersByTime(160)); + expect(commits).toEqual([{ z: 100 }]); + + act(() => root.unmount()); + }); +}); diff --git a/packages/studio/src/components/editor/propertyPanel3dTransform.tsx b/packages/studio/src/components/editor/propertyPanel3dTransform.tsx index ad29d80d29..51bcceab1c 100644 --- a/packages/studio/src/components/editor/propertyPanel3dTransform.tsx +++ b/packages/studio/src/components/editor/propertyPanel3dTransform.tsx @@ -76,15 +76,8 @@ function Cube3dControl({ rotationY: gsapRuntimeValues.rotationY ?? 0, rotationZ: gsapRuntimeValues.rotationZ ?? 0, }; - // Comp-derived lens (see naturalDepthPerspective) applied the first time depth is - // set, so the scene's foreshortening scales with the canvas instead of a magic 800. + // Comp-derived lens for the cube widget's depth preview when the element has no perspective. const depthPerspective = naturalDepthPerspective(element.element); - // A gentle, fixed "depth pose" tilt (degrees) dropped on a flat element the first - // time it gets depth, so translateZ reads as 3D foreshortening instead of a plain - // resize — small enough to look like a premium card, not a flip. - const DEPTH_POSE_X = 10; - const DEPTH_POSE_Y = -15; - const isFlat = Math.round(pose.rotationX) === 0 && Math.round(pose.rotationY) === 0; // Commit only the rotation axes the drag actually changed (each rounded to a // whole degree). Reuses the keyframe-aware animated-property commit, so a drag // at the playhead writes/updates a keyframe just like the numeric fields. @@ -133,42 +126,10 @@ function Cube3dControl({ onPoseDraft={livePreview} onPoseCommit={commitPose} onDepthDraft={(z) => { - // Preview WITH a lens so depth is visible while scrolling — the same - // default the commit applies, so the element doesn't snap on release. - const preview: Record = gsapRuntimeValues.transformPerspective - ? { z } - : { z, transformPerspective: depthPerspective }; - // Depth-pose preview: a flat element only scales under Z, so mirror the - // commit and preview the gentle tilt that makes the depth read as 3D. - if (isFlat) { - preview.rotationX = DEPTH_POSE_X; - preview.rotationY = DEPTH_POSE_Y; - } - onLivePreviewProps?.(element, preview); + onLivePreviewProps?.(element, { z }); }} onDepthCommit={(z) => { - // Best-UX depth: scroll moves Z, and a 3D transform always has a lens — - // like an After Effects camera. translateZ is invisible without a - // perspective, so the FIRST time depth is added (Perspective still 0) we - // set a sensible comp-derived lens ONCE. Every later scroll touches Z - // only, and Perspective stays an independent, editable field. The cube's - // scroll is clamped in front of the lens, so Z can't run away past it. - const props: Record = { z }; - if (!gsapRuntimeValues.transformPerspective && depthPerspective > 0) { - props.transformPerspective = depthPerspective; - } - // Depth-pose: a flat element (no tilt) only scales under Z — it can't read - // as depth. So the first time depth lands on a flat element, also drop a - // gentle fixed tilt; the foreshortening makes depth read as 3D IN PLACE - // (no screen travel, per-element lens unchanged). Once the element has any - // tilt, depth scrolls touch Z only. Reset tilt to 0 to go flat again. - if (isFlat) { - props.rotationX = DEPTH_POSE_X; - props.rotationY = DEPTH_POSE_Y; - } - // One commit for all props so the writes can't race read-modify-write on - // the same script (which dropped a prop and reverted after a seek). - void onCommitAnimatedProperties(element, props); + void onCommitAnimatedProperties(element, { z }); }} onRecenter={recenter} onKeyframe={onKeyframe} diff --git a/packages/studio/src/components/editor/propertyPanelPrimitives.test.tsx b/packages/studio/src/components/editor/propertyPanelPrimitives.test.tsx new file mode 100644 index 0000000000..82a3b9691d --- /dev/null +++ b/packages/studio/src/components/editor/propertyPanelPrimitives.test.tsx @@ -0,0 +1,172 @@ +// @vitest-environment happy-dom + +import React, { act } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, describe, expect, it } from "vitest"; +import type { DomEditSelection } from "./domEditingTypes"; +import { SelectField } from "./propertyPanelPrimitives"; +import { StyleSections } from "./propertyPanelStyleSections"; + +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + +const roots: Root[] = []; + +afterEach(() => { + for (const root of roots) { + act(() => root.unmount()); + } + roots.length = 0; + document.body.innerHTML = ""; +}); + +function render(node: React.ReactNode): HTMLElement { + const host = document.createElement("div"); + document.body.append(host); + const root = createRoot(host); + roots.push(root); + act(() => root.render(node)); + return host; +} + +function findSelect(host: HTMLElement, label: string): HTMLSelectElement { + const field = Array.from(host.querySelectorAll("label")).find( + (candidate) => candidate.querySelector("span")?.textContent === label, + ); + const select = field?.querySelector("select"); + if (!select) throw new Error(`Missing select field: ${label}`); + return select; +} + +function expandSection(host: HTMLElement, title: string): void { + const section = host.querySelector(`[data-panel-section="${title}"]`); + const button = section?.querySelector("button"); + if (!button) throw new Error(`Missing panel section: ${title}`); + act(() => button.click()); +} + +function createSelection(): DomEditSelection { + return { + element: document.createElement("div"), + id: "card", + selector: "#card", + sourceFile: "index.html", + compositionPath: "index.html", + label: "Card", + tagName: "div", + isCompositionHost: false, + isInsideLockedComposition: false, + boundingBox: { x: 0, y: 0, width: 100, height: 100 }, + textContent: null, + dataAttributes: {}, + inlineStyles: {}, + computedStyles: {}, + textFields: [], + capabilities: { + canSelect: true, + canEditStyles: true, + canCrop: true, + canMove: true, + canResize: true, + canApplyManualOffset: true, + canApplyManualSize: true, + canApplyManualRotation: true, + }, + }; +} + +describe("SelectField unlisted values", () => { + it("disables an unlisted display-only value only when requested", () => { + const host = render( +
+ undefined} + /> + undefined} + /> +
, + ); + + const shadow = findSelect(host, "Shadow"); + expect(shadow.value).toBe("custom"); + expect(shadow.options[0]?.value).toBe("custom"); + expect(shadow.options[0]?.disabled).toBe(true); + expect( + Array.from(shadow.options) + .slice(1) + .every((option) => !option.disabled), + ).toBe(true); + + const style = findSelect(host, "Style"); + expect(style.value).toBe("groove"); + expect(style.options[0]?.value).toBe("groove"); + expect(style.options[0]?.disabled).toBe(false); + }); +}); + +describe("StyleSections curated options", () => { + it("keeps authored off-list values visible and commits remaining options", () => { + const calls: Array<[property: string, value: string]> = []; + const host = render( + { + calls.push([property, value]); + }} + />, + ); + + expandSection(host, "stroke"); + expandSection(host, "effects"); + expandSection(host, "clip"); + + const strokeStyle = findSelect(host, "Style"); + expect(strokeStyle.value).toBe("groove"); + expect(Array.from(strokeStyle.options, (option) => option.value)).toEqual([ + "groove", + "none", + "solid", + "dashed", + "dotted", + "double", + ]); + + const overflow = findSelect(host, "Overflow"); + expect(overflow.value).toBe("auto"); + expect(Array.from(overflow.options, (option) => option.value)).toEqual([ + "auto", + "visible", + "hidden", + "clip", + ]); + expect(findSelect(host, "Shadow").options[0]?.disabled).toBe(true); + expect(findSelect(host, "Mask").options[0]?.disabled).toBe(true); + expect(calls).toEqual([]); + + act(() => { + strokeStyle.value = "dashed"; + strokeStyle.dispatchEvent(new Event("change", { bubbles: true })); + overflow.value = "hidden"; + overflow.dispatchEvent(new Event("change", { bubbles: true })); + }); + + expect(calls).toContainEqual(["border-style", "dashed"]); + expect(calls).toContainEqual(["overflow", "hidden"]); + }); +}); diff --git a/packages/studio/src/components/editor/propertyPanelPrimitives.tsx b/packages/studio/src/components/editor/propertyPanelPrimitives.tsx index f2916c4668..43f54dfabd 100644 --- a/packages/studio/src/components/editor/propertyPanelPrimitives.tsx +++ b/packages/studio/src/components/editor/propertyPanelPrimitives.tsx @@ -305,16 +305,19 @@ export function SelectField({ label, value, disabled, + disableUnlistedValue, options, onChange, }: { label: string; value: string; disabled?: boolean; + disableUnlistedValue?: boolean; options: string[]; onChange: (nextValue: string) => void; }) { - const renderedOptions = value && !options.includes(value) ? [value, ...options] : options; + const hasUnlistedValue = Boolean(value && !options.includes(value)); + const renderedOptions = hasUnlistedValue ? [value, ...options] : options; return (