Skip to content

Commit ec3ad42

Browse files
committed
feat(studio): wire the flat Style group into the one-open accordion
1 parent a5c8d39 commit ec3ad42

3 files changed

Lines changed: 99 additions & 15 deletions

File tree

‎packages/studio/src/components/editor/PropertyPanel.test.tsx‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ function multiFieldTextElement() {
100100
};
101101
}
102102

103+
// Style-only fixture: no text fields (Text group must not render), but
104+
// canEditStyles stays true (inherited from baseElement()) so the Style group
105+
// is gated in.
106+
function styleOnlyElement() {
107+
return {
108+
...baseElement(),
109+
id: "stat-card",
110+
selector: ".stat-card",
111+
label: "Stat Card",
112+
textFields: [],
113+
inlineStyles: { "background-color": "#0D0C09" },
114+
};
115+
}
116+
103117
async function renderPanel(
104118
flatEnabled: boolean,
105119
elementOverride: ReturnType<typeof baseElement> = baseElement(),
@@ -185,9 +199,19 @@ describe("PropertyPanel — STUDIO_FLAT_INSPECTOR_ENABLED on", () => {
185199
it(
186200
"renders no Text group at all for a non-text element (bug 1)",
187201
async () => {
202+
// nonTextElement() inherits canEditStyles: true from baseElement(), so
203+
// the Style group (Task 10) renders and opens by default here — the
204+
// invariant under test is narrower than "no flat group at all": no
205+
// group titled "Text" may appear, open or collapsed.
188206
const { host, root } = await renderPanel(true, nonTextElement());
189-
expect(host.querySelector('[data-flat-group-open="true"]')).toBeNull();
190-
expect(host.querySelector('[data-flat-group-collapsed="true"]')).toBeNull();
207+
const openTitle = host.querySelector(
208+
'[data-flat-group-open="true"] .text-panel-text-0',
209+
)?.textContent;
210+
const collapsedTitles = Array.from(
211+
host.querySelectorAll('[data-flat-group-collapsed="true"] .text-panel-text-2'),
212+
).map((el) => el.textContent);
213+
expect(openTitle).not.toBe("Text");
214+
expect(collapsedTitles).not.toContain("Text");
191215
act(() => root.unmount());
192216
},
193217
RENDER_TIMEOUT_MS,
@@ -209,3 +233,36 @@ describe("PropertyPanel — STUDIO_FLAT_INSPECTOR_ENABLED on", () => {
209233
RENDER_TIMEOUT_MS,
210234
);
211235
});
236+
237+
describe("PropertyPanel — Style group (flag on)", () => {
238+
it(
239+
"renders the Style group for a style-editable, non-text element",
240+
async () => {
241+
const { host, root } = await renderPanel(true, styleOnlyElement());
242+
expect(host.textContent).toContain("Style");
243+
expect(host.textContent).toContain("Fill");
244+
act(() => root.unmount());
245+
},
246+
RENDER_TIMEOUT_MS,
247+
);
248+
249+
it(
250+
"one-open accordion: opening Style closes Text",
251+
async () => {
252+
// baseElement() is text-editable and has capabilities.canEditStyles:
253+
// true, so both the Text and Style groups render for it.
254+
const { host, root } = await renderPanel(true);
255+
const textGroup = () => host.querySelector('[data-flat-group-open="true"]');
256+
expect(textGroup()?.textContent).toContain("Text");
257+
const styleCollapsedRow = Array.from(
258+
host.querySelectorAll('[data-flat-group-collapsed="true"]'),
259+
).find((el) => el.textContent?.includes("Style"));
260+
if (!styleCollapsedRow) throw new Error("expected a collapsed Style row");
261+
act(() => styleCollapsedRow.dispatchEvent(new MouseEvent("click", { bubbles: true })));
262+
expect(textGroup()?.textContent).not.toContain("Text");
263+
expect(host.querySelector('[data-flat-group-open="true"]')?.textContent).toContain("Style");
264+
act(() => root.unmount());
265+
},
266+
RENDER_TIMEOUT_MS,
267+
);
268+
});

‎packages/studio/src/components/editor/PropertyPanel.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export const PropertyPanel = memo(function PropertyPanel(props: PropertyPanelPro
268268
return (
269269
<PropertyPanelFlat
270270
{...props}
271+
key={element.id ?? element.selector}
271272
element={element}
272273
styles={styles}
273274
sections={sections}

‎packages/studio/src/components/editor/PropertyPanelFlat.tsx‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { PropertyPanelFlatHeader } from "./PropertyPanelFlatHeader";
77
import { PropertyPanelFlatFooter } from "./PropertyPanelFlatFooter";
88
import { FlatGroup } from "./propertyPanelFlatPrimitives";
99
import { FlatTextSection } from "./propertyPanelFlatTextSection";
10+
import { FlatStyleSection } from "./propertyPanelFlatStyleSections";
1011
import { formatTextFieldPreview, StyleSections } from "./propertyPanelSections";
1112
import { TimingSection } from "./propertyPanelTimingSection";
1213
import { ColorGradingSection } from "./propertyPanelColorGradingSection";
@@ -102,15 +103,25 @@ export function PropertyPanelFlat({
102103
clipboardCopied: boolean;
103104
onCopyElementInfo: () => void;
104105
}) {
105-
// Defaulting to "text" is harmless for a non-text element even though the
106-
// Text FlatGroup won't render (nothing else reads openGroupId yet) — this
107-
// only matters once a second FlatGroup exists (Plan 2+), at which point a
108-
// non-text element should default-open that group instead.
109-
const [openGroupId, setOpenGroupId] = useState<string>("text");
106+
// Lazy initializer: pick whichever group actually renders for this element
107+
// (Text if text-editable, else Style if style-editable, else none open) so a
108+
// style-only element doesn't start with everything collapsed. Only runs on
109+
// mount — PropertyPanel.tsx keys <PropertyPanelFlat> by element identity so
110+
// switching the selection re-mounts this component and re-derives the
111+
// default instead of preserving stale state across unrelated elements.
112+
const [openGroupId, setOpenGroupId] = useState<string>(() =>
113+
isTextEditableSelection(element) ? "text" : showEditableSections ? "style" : "",
114+
);
110115
const [pinnedGroupIds, setPinnedGroupIds] = useState<string[]>([]);
111116

112117
const isTextEditable = isTextEditableSelection(element);
113118
const elementKind = sections.media ? "media" : element.textFields.length > 0 ? "text" : "other";
119+
const toggleOpen = (groupId: string) =>
120+
setOpenGroupId((current) => (current === groupId ? "" : groupId));
121+
const togglePin = (groupId: string) =>
122+
setPinnedGroupIds((current) =>
123+
current.includes(groupId) ? current.filter((id) => id !== groupId) : [...current, groupId],
124+
);
114125

115126
return (
116127
<div className="flex h-full min-h-0 flex-col overflow-hidden bg-panel-bg text-panel-text-1">
@@ -136,14 +147,8 @@ export function PropertyPanelFlat({
136147
title="Text"
137148
isOpen={openGroupId === "text" || pinnedGroupIds.includes("text")}
138149
isPinned={pinnedGroupIds.includes("text")}
139-
onToggleOpen={() => setOpenGroupId((current) => (current === "text" ? "" : "text"))}
140-
onTogglePin={() =>
141-
setPinnedGroupIds((current) =>
142-
current.includes("text")
143-
? current.filter((id) => id !== "text")
144-
: [...current, "text"],
145-
)
146-
}
150+
onToggleOpen={() => toggleOpen("text")}
151+
onTogglePin={() => togglePin("text")}
147152
summary={formatTextFieldPreview(element.textFields[0]?.value ?? "")}
148153
>
149154
<FlatTextSection
@@ -159,6 +164,27 @@ export function PropertyPanelFlat({
159164
</FlatGroup>
160165
)}
161166

167+
{showEditableSections && (
168+
<FlatGroup
169+
title="Style"
170+
isOpen={openGroupId === "style" || pinnedGroupIds.includes("style")}
171+
isPinned={pinnedGroupIds.includes("style")}
172+
onToggleOpen={() => toggleOpen("style")}
173+
onTogglePin={() => togglePin("style")}
174+
summary={`fill ${styles["background-image"] && styles["background-image"] !== "none" ? "image/gradient" : styles["background-color"] ? "set" : "none"} · ${Math.round((parseFloat(styles.opacity ?? "1") || 1) * 100)}%`}
175+
>
176+
<FlatStyleSection
177+
projectId={projectId}
178+
element={element}
179+
styles={styles}
180+
assets={assets}
181+
onSetStyle={onSetStyle}
182+
onImportAssets={onImportAssets}
183+
gsapBorderRadius={gsapBorderRadius}
184+
/>
185+
</FlatGroup>
186+
)}
187+
162188
{sections.timing && (
163189
<TimingSection
164190
element={element}

0 commit comments

Comments
 (0)