Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Filter custom selectors to current instance only (#5575)
Oleg Isonen7859eded641e
diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.mdindex 5cad1d1e14..713b912bf0 100644--- a/.github/copilot-instructions.md+++ b/.github/copilot-instructions.md@@ -191,3 +191,6 @@ When writing social posts for new releases:- After completing changes, review your own code against all instructions in this document.- If a component or module needs refactoring (e.g., too many props, too complex, violates guidelines), ask the user before proceeding.+- Always perform these two checks:+ 1. What needs to be purified? (extract pure functions from side-effect code)+ 2. Which of the pure functions need tests? (see "When to Test" section)diff --git a/apps/builder/app/builder/features/style-panel/style-source-section.test.ts b/apps/builder/app/builder/features/style-panel/style-source-section.test.tsindex ca965adc1a..ed8f56d707 100644--- a/apps/builder/app/builder/features/style-panel/style-source-section.test.ts+++ b/apps/builder/app/builder/features/style-panel/style-source-section.test.ts@@ -1,5 +1,5 @@import { enableMapSet } from "immer";-import { expect, test } from "vitest";+import { expect, test, describe } from "vitest";import { registerContainers } from "~/shared/sync/sync-stores";import {$instances,@@ -7,12 +7,115 @@ import {$styleSourceSelections,$styleSources,} from "~/shared/nano-states";-import { addStyleSourceToInstance } from "./style-source-section";+import { addStyleSourceToInstance, __testing__ } from "./style-source-section";import { $awareness } from "~/shared/awareness";+const { getComponentStates } = __testing__;+enableMapSet();registerContainers();+describe("getComponentStates", () => {+ test("returns predefined states for tag", () => {+ const result = getComponentStates({+ predefinedStates: [":visited", ":active"],+ componentStates: [],+ instanceStyleSourceIds: new Set(),+ styles: [],+ selectedStyleState: undefined,+ });++ // Should include universal states (:hover, :focus, etc.) and tag-specific states+ expect(result.some((s) => s.selector === ":hover")).toBe(true);+ expect(result.some((s) => s.selector === ":visited")).toBe(true);+ expect(result.some((s) => s.selector === ":active")).toBe(true);+ expect(result.every((s) => s.source === "native")).toBe(true);+ });++ test("includes selectors from instance styles only", () => {+ const result = getComponentStates({+ predefinedStates: [],+ componentStates: [],+ instanceStyleSourceIds: new Set(["style1"]),+ styles: [+ { styleSourceId: "style1", state: "::before" },+ { styleSourceId: "other", state: "::after" },+ ],+ selectedStyleState: undefined,+ });++ // Should include ::before from instance's style source+ expect(result.some((s) => s.selector === "::before")).toBe(true);+ // Should NOT include ::after from other style source+ expect(result.some((s) => s.selector === "::after")).toBe(false);+ });++ test("marks custom selectors correctly", () => {+ const result = getComponentStates({+ predefinedStates: [],+ componentStates: [],+ instanceStyleSourceIds: new Set(["style1"]),+ styles: [{ styleSourceId: "style1", state: "::before" }],+ selectedStyleState: undefined,+ });++ const beforeSelector = result.find((s) => s.selector === "::before");+ expect(beforeSelector?.source).toBe("custom");+ expect(beforeSelector?.type).toBe("pseudoElement");+ });++ test("includes currently selected state even without styles", () => {+ const result = getComponentStates({+ predefinedStates: [],+ componentStates: [],+ instanceStyleSourceIds: new Set(),+ styles: [],+ selectedStyleState: "::marker",+ });++ expect(result.some((s) => s.selector === "::marker")).toBe(true);+ });++ test("includes component states", () => {+ const result = getComponentStates({+ predefinedStates: [],+ componentStates: [+ { label: "Open", selector: "[data-state=open]" },+ { label: "Closed", selector: "[data-state=closed]" },+ ],+ instanceStyleSourceIds: new Set(),+ styles: [],+ selectedStyleState: undefined,+ });++ const openState = result.find((s) => s.selector === "[data-state=open]");+ expect(openState?.source).toBe("component");+ expect(openState?.label).toBe("Open");+ });++ test("removes selector when styles are cleared", () => {+ // First, with styles+ const withStyles = getComponentStates({+ predefinedStates: [],+ componentStates: [],+ instanceStyleSourceIds: new Set(["style1"]),+ styles: [{ styleSourceId: "style1", state: "::before" }],+ selectedStyleState: undefined,+ });+ expect(withStyles.some((s) => s.selector === "::before")).toBe(true);++ // After clearing styles (empty styles array)+ const withoutStyles = getComponentStates({+ predefinedStates: [],+ componentStates: [],+ instanceStyleSourceIds: new Set(["style1"]),+ styles: [],+ selectedStyleState: undefined,+ });+ expect(withoutStyles.some((s) => s.selector === "::before")).toBe(false);+ });+});+test("add style source to instance", () => {$instances.set(new Map([diff --git a/apps/builder/app/builder/features/style-panel/style-source-section.tsx b/apps/builder/app/builder/features/style-panel/style-source-section.tsxindex 5766b7b288..99a61ae6fd 100644--- a/apps/builder/app/builder/features/style-panel/style-source-section.tsx+++ b/apps/builder/app/builder/features/style-panel/style-source-section.tsx@@ -87,7 +87,7 @@ const getOrCreateStyleSourceSelectionMutable = (return styleSourceSelection;};-const addStyleSourceToInstaceMutable = (+const addStyleSourceToInstanceMutable = (styleSourceSelections: StyleSourceSelections,styleSources: StyleSources,instanceId: Instance["id"],@@ -126,7 +126,7 @@ const createStyleSource = (id: StyleSource["id"], name: string) => {[$styleSources, $styleSourceSelections],(styleSources, styleSourceSelections) => {styleSources.set(newStyleSource.id, newStyleSource);- addStyleSourceToInstaceMutable(+ addStyleSourceToInstanceMutable(styleSourceSelections,styleSources,instanceId,@@ -147,7 +147,7 @@ export const addStyleSourceToInstance = (serverSyncStore.createTransaction([$styleSourceSelections, $styleSources],(styleSourceSelections, styleSources) => {- addStyleSourceToInstaceMutable(+ addStyleSourceToInstanceMutable(styleSourceSelections,styleSources,instanceId,@@ -278,6 +278,70 @@ const clearStyles = (styleSourceId: StyleSource["id"]) => {});};+type SelectorConfig = {+ type: "state" | "pseudoElement";+ selector: string;+ label: string;+ source: "native" | "component" | "custom";+};++const getComponentStates = ({+ predefinedStates,+ componentStates,+ instanceStyleSourceIds,+ styles,+ selectedStyleState,+}: {+ predefinedStates: string[];+ componentStates: Array<{ label: string; selector: string }>;+ instanceStyleSourceIds: Set<StyleSource["id"]>;+ styles: Iterable<Pick<StyleDecl, "state" | "styleSourceId">>;+ selectedStyleState: string | undefined;+}): SelectorConfig[] => {+ const allStates = [...pseudoClassesByTag["*"], ...predefinedStates];++ const usedSelectors = new Set<string>();+ for (const styleDecl of styles) {+ if (+ styleDecl.state &&+ styleDecl.state.trim() &&+ instanceStyleSourceIds.has(styleDecl.styleSourceId)+ ) {+ usedSelectors.add(styleDecl.state);+ }+ }++ // Show selected state in menu immediately, before any styles are added+ if (selectedStyleState && selectedStyleState.trim()) {+ usedSelectors.add(selectedStyleState);+ }++ const allStateSelectors = new Set([...allStates, ...usedSelectors]);++ const toConfig = (selector: string): SelectorConfig => ({+ type: isPseudoElement(selector) ? "pseudoElement" : "state",+ label: selector,+ selector,+ source: allStates.includes(selector) ? "native" : "custom",+ });++ const states = Array.from(allStateSelectors)+ .filter((state) => !isPseudoElement(state))+ .map(toConfig);++ const pseudoElements = Array.from(allStateSelectors)+ .filter(isPseudoElement)+ .map(toConfig);++ const componentStatesConfig = componentStates.map((item) => ({+ type: "state" as const,+ ...item,+ source: "component" as const,+ }));++ return [...states, ...componentStatesConfig, ...pseudoElements];+};+const $componentStates = computed([$selectedInstance,@@ -285,70 +349,31 @@ const $componentStates = computed($instanceTags,$styles,$selectedStyleState,+ $styleSourceSelections,],(selectedInstance,registeredComponentMetas,instanceTags,styles,- selectedStyleState+ selectedStyleState,+ styleSourceSelections) => {if (selectedInstance === undefined) {return;}const tag = instanceTags.get(selectedInstance.id);- const allStates = [- ...pseudoClassesByTag["*"],- ...(pseudoClassesByTag[tag ?? ""] ?? []),- ];-- // Get recently used selectors from project styles- const usedSelectors = new Set<string>();- for (const styleDecl of styles.values()) {- if (styleDecl.state && styleDecl.state.trim()) {- usedSelectors.add(styleDecl.state);- }- }-- // Include currently selected state so it appears in the menu immediately- // even before any styles are added to it- if (selectedStyleState && selectedStyleState.trim()) {- usedSelectors.add(selectedStyleState);- }-- // Combine predefined states with recently used, removing duplicates- const allStateSelectors = new Set([...allStates, ...usedSelectors]);-- const states = Array.from(allStateSelectors)- .filter((state) => !isPseudoElement(state))- .map((state) => ({- type: "state" as const,- label: state,- selector: state,- source: allStates.includes(state)- ? ("native" as const)- : ("custom" as const),- }));-- const pseudoElements = Array.from(allStateSelectors)- .filter((state) => isPseudoElement(state))- .map((state) => ({- type: "pseudoElement" as const,- label: state,- selector: state,- source: allStates.includes(state)- ? ("native" as const)- : ("custom" as const),- }));-const meta = registeredComponentMetas.get(selectedInstance.component);- const componentStates = (meta?.states ?? []).map((item) => ({- type: "state" as const,- ...item,- source: "component" as const,- }));- return [...states, ...componentStates, ...pseudoElements];+ return getComponentStates({+ predefinedStates: pseudoClassesByTag[tag ?? ""] ?? [],+ componentStates: meta?.states ?? [],+ instanceStyleSourceIds: new Set(+ styleSourceSelections.get(selectedInstance.id)?.values+ ),+ styles: styles.values(),+ selectedStyleState,+ });});@@ -373,9 +398,6 @@ const convertToInputItem = (};};-/**- * find all non-local and component style sources- */const $availableStyleSources = computed([$styleSources], (styleSources) => {const availableStylesSources: StyleSourceInputItem[] = [];for (const styleSource of styleSources.values()) {@@ -504,3 +526,5 @@ export const StyleSourcesSection = () => {</>);};++export const __testing__ = { getComponentStates };