fix: Improve Advanced styles feedback and search (#6682)

Trying to change a source heading in Advanced styles now explains that
the heading identifies a style source. The centered Styles editor opens
the shared search controls from a header button.

### What changed

- Give protected source headings their own near-cursor message and
screen-reader announcement. Read-only declarations keep their existing
explanation.
- Keep search state and navigation with the shared style block editor.
The centered editor shows the controls in a popover, and Cmd/Ctrl+F
opens search from either editor.
- Start the centered search empty whenever the dialog opens. Local
declarations remain editable while searching.

### Checklist

- [x] Fix popover width, dialog-wide search shortcut, and query reset on
close
- [x] Show centered editor search in a header popover
- [x] Replace vague “mixed” names with StyleBlockEditor and style source
names
- [x] Clarify editor state and callback names
- [x] Use one style block editor for inline and centered views

- [x] Reproduce and fix source-heading feedback in inline and centered
editors
- [x] Reuse the inline search controls in the centered dialog popover
- [x] Verify navigation, Local editing during search, and empty search
on reopen
- [x] Review documentation impact: the existing Advanced guide already
describes search in the code editor; no wording change is needed
- [x] Run focused browser tests, Builder typecheck, lint, format, and
diff checks
- [x] Inspect the centered dialog in light and dark Storybook views
- [x] Required CI checks

### Verification

The 21 focused editor browser tests pass. New behavior tests failed
before the changes and passed afterward. Builder typecheck, focused
lint, formatting, and diff checks pass. The code cleanup also passed the
29 style block formatting tests. No fixture inputs or generated files
changed.

All required CI checks passed. One end-to-end shard initially failed
because Chromium did not start in its MCP fixture; that shard passed on
retry. The visual comparison showed only the intended search icon in the
centered editor header and passed after approval. Storybook screenshots
for the popover were skipped at the user's request.

### Known limitation

The existing shared SearchField has no explicit screen-reader name. This
PR reuses it unchanged in both views; adding new accessible copy needs
approval.

The header-click request in this issue was already merged in #6668.

Closes #6659
This commit is contained in:
Oleg Isonen authored and GitHub committed 2026-11-12 15:47:50 +01:00
1 parent 9883a20742
commit d08dbcf827
5 files changed
+786 -375

No files matched your search

@@ -39,10 +39,7 @@ import {
} from "~/shared/nano-states";
import { $styles } from "~/shared/sync/data-stores";
import { useReadonly } from "../../shared/readonly";
import {
InlineMixedStyleBlockEditor,
MixedStyleBlockDialog,
} from "./style-block-dialog";
import { StyleBlockEditor } from "./style-block-editor";
import type { ApplyChanges } from "~/builder/shared/css-editor/style-block-parse";
import { $settings, setSetting } from "~/builder/shared/client-settings";
import type { ComputedStyleDecl } from "~/shared/style-object-model";
@@ -253,8 +250,9 @@ export const Section = () => {
}
>
{selectedInstanceKey !== undefined && (
<InlineMixedStyleBlockEditor
<StyleBlockEditor
key={selectionKey}
inline
declarations={blockDeclarations}
sourceDeclarations={visibleAdvancedStyleDeclarations}
readonly={readonly}
@@ -266,7 +264,7 @@ export const Section = () => {
)}
</AdvancedStyleSection>
{selectedInstanceKey !== undefined && (
<MixedStyleBlockDialog
<StyleBlockEditor
key={selectionKey}
declarations={blockDeclarations}
sourceDeclarations={visibleAdvancedStyleDeclarations}
@@ -2,16 +2,13 @@ import { useState, type ComponentProps } from "react";
import { Box, cssVar, theme } from "@webstudio-is/design-system";
import { ROOT_INSTANCE_ID } from "@webstudio-is/sdk";
import type { ComputedStyleDecl } from "~/shared/style-object-model";
import {
InlineMixedStyleBlockEditor,
MixedStyleBlockDialog,
} from "./style-block-dialog";
import { StyleBlockEditor } from "./style-block-editor";
const cascadedValue = { type: "keyword", value: "blue" } as const;
const presetValue = { type: "keyword", value: "block" } as const;
const globalValue = { type: "keyword", value: "red" } as const;
const localDeclarations: ComponentProps<
typeof InlineMixedStyleBlockEditor
typeof StyleBlockEditor
>["declarations"] = [
{ property: "opacity", value: { type: "unit", unit: "number", value: 0.8 } },
];
@@ -50,7 +47,8 @@ export const InlineCode = () => (
background: cssVar("--background-primary"),
}}
>
<InlineMixedStyleBlockEditor
<StyleBlockEditor
inline
declarations={localDeclarations}
sourceDeclarations={inheritedDeclarations}
readonly={false}
@@ -60,7 +58,7 @@ export const InlineCode = () => (
);
export const CenteredCode = () => (
<MixedStyleBlockDialog
<StyleBlockEditor
declarations={localDeclarations}
sourceDeclarations={inheritedDeclarations}
readonly={false}
@@ -72,10 +70,11 @@ export const CenteredCode = () => (
export const EditableCode = () => {
const [declarations, setDeclarations] = useState<
ComponentProps<typeof InlineMixedStyleBlockEditor>["declarations"]
ComponentProps<typeof StyleBlockEditor>["declarations"]
>([]);
return (
<InlineMixedStyleBlockEditor
<StyleBlockEditor
inline
declarations={declarations}
sourceDeclarations={inheritedDeclarations}
readonly={false}
@@ -5,14 +5,12 @@ import { userEvent } from "@vitest/browser/context";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { CompletionContext } from "@codemirror/autocomplete";
import { getSearchQuery } from "@codemirror/search";
import { TooltipProvider } from "@webstudio-is/design-system";
import { $availableVariables } from "../../shared/model";
import { parseStyleInput } from "~/builder/shared/css-editor/parse-style-input";
import type { ApplyChanges } from "~/builder/shared/css-editor/style-block-parse";
import {
InlineMixedStyleBlockEditor,
MixedStyleBlockDialog,
} from "./style-block-dialog";
import { StyleBlockEditor } from "./style-block-editor";
import {
parseBlock,
cssBlockLanguage,
@@ -173,7 +171,8 @@ test("keeps hidden and uploaded image layers when editing a gradient", async ()
};
let changes: ApplyChanges | undefined;
const container = render(
<InlineMixedStyleBlockEditor
<StyleBlockEditor
inline
declarations={[{ property: "background-image", value: stored }]}
sourceDeclarations={[]}
readonly={false}
@@ -207,6 +206,15 @@ const declarations = Array.from(
parseStyleInput("margin: 0; border: 1px solid red", new Map()).styleMap,
([property, value]) => ({ property, value })
);
const sourceDeclarations = [
{
property: "background-color" as const,
source: { name: "remote" as const },
cascadedValue: { type: "keyword" as const, value: "blue" },
computedValue: { type: "keyword" as const, value: "blue" },
usedValue: { type: "keyword" as const, value: "blue" },
},
];
const render = (content: React.ReactNode) => {
const container = document.createElement("div");
@@ -218,7 +226,8 @@ const render = (content: React.ReactNode) => {
test("inline Advanced editor renders stored longhands as editable shorthands", async () => {
const container = render(
<InlineMixedStyleBlockEditor
<StyleBlockEditor
inline
declarations={declarations}
sourceDeclarations={[]}
readonly={false}
@@ -235,7 +244,7 @@ test("inline Advanced editor renders stored longhands as editable shorthands", a
test("centered Local editor renders stored longhands as editable shorthands", async () => {
render(
<MixedStyleBlockDialog
<StyleBlockEditor
declarations={declarations}
sourceDeclarations={[]}
readonly={false}
@@ -251,6 +260,286 @@ test("centered Local editor renders stored longhands as editable shorthands", as
});
});
test.each(["inline", "centered"])(
"%s editor explains protected source headings without changing them",
async (mode) => {
const props = {
declarations,
sourceDeclarations,
readonly: false,
onApply: () => {},
};
const container = render(
mode === "inline" ? (
<StyleBlockEditor {...props} inline />
) : (
<StyleBlockEditor {...props} open onOpenChange={() => {}} />
)
);
const editor = await vi.waitFor(() => {
const element = (
mode === "inline" ? container : document
).querySelector<HTMLElement>(".cm-content");
expect(element).not.toBeNull();
return element!;
});
const view = EditorView.findFromDOM(editor)!;
const before = view.state.doc.toString();
act(() => {
view.dispatch({ changes: { from: 0, to: 1 } });
});
expect(view.state.doc.toString()).toBe(before);
await vi.waitFor(() => {
expect(
document.querySelector(".cm-style-block-blocked-tooltip")?.textContent
).toContain("This heading marks a style source and cannot be changed.");
expect(document.querySelector("[aria-live]")?.textContent).toContain(
"This heading marks a style source and cannot be changed."
);
});
const remoteHeading = before.indexOf("/* Cascaded or inherited */");
act(() => {
view.dispatch({
changes: { from: remoteHeading, to: remoteHeading + 1 },
});
});
expect(view.state.doc.toString()).toBe(before);
expect(
document.querySelector(".cm-style-block-blocked-tooltip")?.textContent
).toContain("This heading marks a style source and cannot be changed.");
const remoteDeclaration = before.indexOf("background-color:");
act(() => {
view.dispatch({
changes: { from: remoteDeclaration, to: remoteDeclaration + 1 },
});
});
expect(view.state.doc.toString()).toBe(before);
expect(
document.querySelector(".cm-style-block-blocked-tooltip")?.textContent
).toContain("This declaration comes from another source.");
}
);
test("centered search finds every source and keeps Local editable", async () => {
render(
<StyleBlockEditor
declarations={Array.from(
parseStyleInput("color: red", new Map()).styleMap,
([property, value]) => ({ property, value })
)}
sourceDeclarations={sourceDeclarations}
readonly={false}
open
onOpenChange={() => {}}
onApply={() => {}}
/>
);
const trigger = document.querySelector<HTMLButtonElement>(
'[data-floating-panel] button[aria-label="Search styles"]'
);
expect(trigger).not.toBeNull();
expect(
document.querySelector("[data-floating-panel] input[type=text]")
).toBeNull();
await act(async () => {
await userEvent.click(trigger!);
});
const input = document.querySelector<HTMLInputElement>("input[type=text]");
expect(input).not.toBeNull();
expect(document.activeElement).toBe(input);
await act(async () => {
await userEvent.type(input!, "color");
});
const editor = document.querySelector<HTMLElement>(".cm-content")!;
const view = EditorView.findFromDOM(editor)!;
expect(getSearchQuery(view.state).search).toBe("color");
expect(document.querySelectorAll(".cm-style-searchMatch")).toHaveLength(2);
const firstMatch = view.state.selection.main.from;
const next = document.querySelector<HTMLButtonElement>(
'button[aria-label="Next match"]'
);
expect(next?.disabled).toBe(false);
await act(async () => {
await userEvent.click(next!);
});
expect(view.state.selection.main.from).toBeGreaterThan(firstMatch);
const previous = document.querySelector<HTMLButtonElement>(
'button[aria-label="Previous match"]'
);
await act(async () => {
await userEvent.click(previous!);
});
expect(view.state.selection.main.from).toBe(firstMatch);
const localValue = view.state.doc.toString().indexOf("red");
act(() => {
view.dispatch({
changes: { from: localValue, to: localValue + 3, insert: "green" },
});
});
expect(view.state.doc.toString()).toContain("green");
});
test("centered search starts empty each time the dialog opens", async () => {
const dialog = (open: boolean) => (
<TooltipProvider>
<StyleBlockEditor
declarations={declarations}
sourceDeclarations={sourceDeclarations}
readonly={false}
open={open}
onOpenChange={() => {}}
onApply={() => {}}
/>
</TooltipProvider>
);
const container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
act(() => root?.render(dialog(true)));
const trigger = document.querySelector<HTMLButtonElement>(
'[data-floating-panel] button[aria-label="Search styles"]'
)!;
await act(async () => {
await userEvent.click(trigger);
});
const input = document.querySelector<HTMLInputElement>("input[type=text]")!;
await act(async () => {
await userEvent.type(input, "color");
});
expect(input.value).toBe("color");
act(() => root?.render(dialog(false)));
act(() => root?.render(dialog(true)));
await act(async () => {
await userEvent.click(
document.querySelector<HTMLButtonElement>(
'[data-floating-panel] button[aria-label="Search styles"]'
)!
);
});
const reopenedInput =
document.querySelector<HTMLInputElement>("input[type=text]")!;
expect(reopenedInput.value).toBe("");
const view = EditorView.findFromDOM(
document.querySelector<HTMLElement>(".cm-content")!
)!;
expect(getSearchQuery(view.state).search).toBe("");
});
test.each(["metaKey", "ctrlKey"] as const)(
"%s+F opens the centered editor search",
(modifier) => {
render(
<StyleBlockEditor
declarations={declarations}
sourceDeclarations={sourceDeclarations}
readonly={false}
open
onOpenChange={() => {}}
onApply={() => {}}
/>
);
const editor = document.querySelector<HTMLElement>(".cm-content")!;
const event = new KeyboardEvent("keydown", {
key: "f",
[modifier]: true,
bubbles: true,
cancelable: true,
});
act(() => editor.dispatchEvent(event));
expect(event.defaultPrevented).toBe(true);
expect(
document.querySelector<HTMLInputElement>("input[type=text]")
).not.toBeNull();
}
);
test("Cmd+F opens search from the centered dialog header", () => {
render(
<StyleBlockEditor
declarations={declarations}
sourceDeclarations={sourceDeclarations}
readonly={false}
open
onOpenChange={() => {}}
onApply={() => {}}
/>
);
const headerButton = document.querySelector<HTMLButtonElement>(
'[data-floating-panel] button[aria-label="Expand"]'
)!;
const event = new KeyboardEvent("keydown", {
key: "f",
metaKey: true,
bubbles: true,
cancelable: true,
});
act(() => headerButton.dispatchEvent(event));
expect(event.defaultPrevented).toBe(true);
expect(
document.querySelector<HTMLInputElement>("input[type=text]")
).not.toBeNull();
});
test("closing the centered search clears its query", async () => {
render(
<StyleBlockEditor
declarations={declarations}
sourceDeclarations={sourceDeclarations}
readonly={false}
open
onOpenChange={() => {}}
onApply={() => {}}
/>
);
const trigger = document.querySelector<HTMLButtonElement>(
'[data-floating-panel] button[aria-label="Search styles"]'
)!;
await act(async () => {
await userEvent.click(trigger);
});
const input = document.querySelector<HTMLInputElement>("input[type=text]")!;
await act(async () => {
await userEvent.type(input, "color");
});
const view = EditorView.findFromDOM(
document.querySelector<HTMLElement>(".cm-content")!
)!;
expect(getSearchQuery(view.state).search).toBe("color");
await act(async () => {
await userEvent.click(trigger);
});
expect(getSearchQuery(view.state).search).toBe("");
await act(async () => {
await userEvent.click(trigger);
});
expect(
document.querySelector<HTMLInputElement>("input[type=text]")?.value
).toBe("");
});
test("Cmd+F focuses the inline editor search", () => {
const container = render(
<StyleBlockEditor
inline
declarations={declarations}
sourceDeclarations={sourceDeclarations}
readonly={false}
onApply={() => {}}
/>
);
const editor = container.querySelector<HTMLElement>(".cm-content")!;
const input = container.querySelector<HTMLInputElement>("input[type=text]")!;
const event = new KeyboardEvent("keydown", {
key: "f",
metaKey: true,
bubbles: true,
cancelable: true,
});
act(() => editor.dispatchEvent(event));
expect(event.defaultPrevented).toBe(true);
expect(document.activeElement).toBe(input);
});
test("keeps an unknown declaration as a custom property", () => {
const parsed = parseBlock("bla: 123");
expect(parsed.styleMap.get("--bla")).toEqual({
@@ -283,7 +572,7 @@ test("reports malformed pasted CSS without saving the broken declaration", () =>
test("Enter adds a separator before a new declaration line", async () => {
render(
<MixedStyleBlockDialog
<StyleBlockEditor
declarations={[]}
sourceDeclarations={[]}
readonly={false}
@@ -22,14 +22,14 @@ const sourceTitles: Record<StyleBlockEditorSource, string> = {
globalVariables: "Global variables",
};
export type MixedStyleGroup = {
export type StyleSourceGroup = {
source: StyleBlockEditorSource;
from: number;
contentFrom: number;
to: number;
};
export type MixedPropertyRange = {
export type StyleSourcePropertyRange = {
property: CssProperty;
source: StyleBlockEditorSource;
from: number;
@@ -45,7 +45,7 @@ const formatDeclarations = (declarations: ComputedStyleDecl[]) =>
)
.join("\n");
export const formatMixedStyleBlock = (
export const formatStyleSources = (
localStyles: CssStyleMap,
declarations: ComputedStyleDecl[],
cssVars: Map<string, string>
@@ -73,8 +73,8 @@ export const formatMixedStyleBlock = (
return [localGroup, readOnlyGroups.join("\n\n")].filter(Boolean).join("\n");
};
export const getMixedStyleGroups = (code: string): MixedStyleGroup[] => {
const groups: MixedStyleGroup[] = [];
export const getStyleSourceGroups = (code: string): StyleSourceGroup[] => {
const groups: StyleSourceGroup[] = [];
let offset = 0;
for (const line of code.split("\n")) {
const source = (
@@ -97,7 +97,7 @@ export const getMixedStyleGroups = (code: string): MixedStyleGroup[] => {
return groups;
};
export const getProtectedMixedRanges = (groups: MixedStyleGroup[]) => {
export const getProtectedStyleSourceRanges = (groups: StyleSourceGroup[]) => {
return groups.map((group) => ({
from: group.from,
to: group.source === "local" ? group.contentFrom : group.to,
@@ -105,9 +105,11 @@ export const getProtectedMixedRanges = (groups: MixedStyleGroup[]) => {
}));
};
export const getMixedPropertyRanges = (code: string): MixedPropertyRange[] => {
const ranges: MixedPropertyRange[] = [];
for (const group of getMixedStyleGroups(code)) {
export const getStyleSourcePropertyRanges = (
code: string
): StyleSourcePropertyRange[] => {
const ranges: StyleSourcePropertyRange[] = [];
for (const group of getStyleSourceGroups(code)) {
let ast;
try {
ast = parse(code.slice(group.contentFrom, group.to), {
@@ -135,11 +137,11 @@ export const getMixedPropertyRanges = (code: string): MixedPropertyRange[] => {
return ranges;
};
export const parseMixedStyleBlock = (
export const parseLocalStyleGroup = (
code: string,
cssVars = new Map<string, string>()
) => {
const group = getMixedStyleGroups(code).find(
const group = getStyleSourceGroups(code).find(
(group) => group.source === "local"
);
const parsed = parseBlock(