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: show active style states as remote (#2654)
Bogdan Chadkin847a042ceba3
diff --git a/apps/builder/app/builder/features/style-panel/shared/property-name.tsx b/apps/builder/app/builder/features/style-panel/shared/property-name.tsxindex a51a6c57bd..d2d8990e93 100644--- a/apps/builder/app/builder/features/style-panel/shared/property-name.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/property-name.tsx@@ -37,11 +37,13 @@ import {import { humanizeString } from "~/shared/string-utils";import { getInstanceLabel } from "~/shared/instance-utils";import { StyleSourceBadge } from "../style-source";+import type { WsComponentMeta } from "@webstudio-is/react-sdk";// We don't return source name only in case of preset or default value.const getSourceName = (styleSources: StyleSources,styleValueInfo: StyleValueInfo,+ meta?: WsComponentMeta,selectedStyleSource?: StyleSource) => {if (styleValueInfo.nextSource) {@@ -55,12 +57,22 @@ const getSourceName = (}}- if (styleValueInfo.local) {+ if (styleValueInfo.local || styleValueInfo.stateless) {return selectedStyleSource?.type === "token"? selectedStyleSource.name: "Local";}+ if (styleValueInfo.stateful) {+ const selector = styleValueInfo.stateful.state;+ const state =+ meta?.states?.find((item) => item.selector === selector)?.label ??+ humanizeString(selector);+ return selectedStyleSource?.type === "token"+ ? `${selectedStyleSource.name} (${state})`+ : `Local (${state})`;+ }+if (styleValueInfo.previousSource) {const { styleSourceId } = styleValueInfo.previousSource;const styleSource = styleSources.get(styleSourceId);@@ -148,10 +160,12 @@ const TooltipContent = ({if (styleValueInfo === undefined) {continue;}+ const meta = instance ? metas.get(instance.component) : undefined;const sourceName = getSourceName(styleSources,styleValueInfo,+ meta,selectedStyleSource);@@ -168,11 +182,8 @@ const TooltipContent = ({breakpointSet.add(`${breakpointName}`);let instanceTitle: undefined | string;- if (instance) {- const meta = metas.get(instance.component);- if (meta) {- instanceTitle = getInstanceLabel(instance, meta);- }+ if (instance && meta) {+ instanceTitle = getInstanceLabel(instance, meta);}if (styleValueInfo.inherited && styleValueInfo.local === undefined) {const localInstance = instances.get(styleValueInfo.inherited.instanceId);@@ -232,7 +243,11 @@ const TooltipContent = ({))}{Array.from(styleSourceNameSet).map((sourceName) => (- <StyleSourceBadge key={sourceName} source="token" variant="small">+ <StyleSourceBadge+ key={sourceName}+ source={sourceName === "Local" ? "local" : "token"}+ variant="small"+ >{sourceName}</StyleSourceBadge>))}diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.test.tsindex 225e3a629b..562e98a277 100644--- a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts@@ -1,5 +1,5 @@import { test, expect, describe } from "@jest/globals";-import { renderHook } from "@testing-library/react-hooks";+import { act, renderHook } from "@testing-library/react-hooks";import * as defaultMetas from "@webstudio-is/sdk-components-react/metas";import {getStyleDeclKey,@@ -7,8 +7,11 @@ import {type Instance,type Instances,type StyleDecl,+ StyleSourceSelection,} from "@webstudio-is/sdk";import {+ $selectedBreakpointId,+ $selectedInstanceStates,breakpointsStore,instancesStore,registeredComponentMetasStore,@@ -25,9 +28,19 @@ import {getInheritedInfo,getNextSourceInfo,getPreviousSourceInfo,+ getStyleSource,useStyleInfo,} from "./style-info";+const toMap = <T extends { id: string }>(list: T[]) =>+ new Map(list.map((item) => [item.id, item]));++const toStyleSourceSelectionsMap = (list: StyleSourceSelection[]) =>+ new Map(list.map((item) => [item.instanceId, item]));++const toStylesMap = (list: StyleDecl[]) =>+ new Map(list.map((item) => [getStyleDeclKey(item), item]));+const metas = new Map(Object.entries(defaultMetas));const breakpoints: Breakpoints = new Map([@@ -150,7 +163,8 @@ test("compute cascaded styles", () => {getCascadedInfo(cascadingStylesByInstanceId,selectedInstanceSelector[0],- cascadedBreakpointIds+ cascadedBreakpointIds,+ new Set())).toMatchInlineSnapshot(`{@@ -188,7 +202,8 @@ test("compute inherited styles", () => {]),cascadedBreakpointIds,- selectedBreakpointId+ selectedBreakpointId,+ new Set())).toMatchInlineSnapshot(`{@@ -293,7 +308,8 @@ test("compute styles from previous sources", () => {stylesByInstanceId,selectedInstanceSelector,selectedStyleSourceSelector,- "bp"+ "bp",+ new Set())).toMatchInlineSnapshot(`{@@ -360,7 +376,8 @@ test("compute styles from next sources", () => {stylesByInstanceId,selectedInstanceSelector,selectedStyleSourceSelector,- "bp"+ "bp",+ new Set())).toMatchInlineSnapshot(`{@@ -386,6 +403,7 @@ const resetStores = () => {selectedInstanceSelectorStore.set(undefined);selectedInstanceIntanceToTagStore.set(new Map());selectedStyleSourceSelectorStore.set(undefined);+ $selectedInstanceStates.set(new Set());};describe("color and currentColor", () => {@@ -568,3 +586,365 @@ describe("color and currentColor", () => {});});});++describe("active states", () => {+ test("show stateless style as remote when state is selected", () => {+ resetStores();+ instancesStore.set(+ toMap([{ type: "instance", id: "box", component: "Box", children: [] }])+ );+ breakpointsStore.set(toMap([{ id: "base", label: "" }]));+ styleSourcesStore.set(toMap([{ id: "box.local", type: "local" }]));+ styleSourceSelectionsStore.set(+ toStyleSourceSelectionsMap([{ instanceId: "box", values: ["box.local"] }])+ );+ stylesStore.set(+ toStylesMap([+ {+ styleSourceId: "box.local",+ breakpointId: "base",+ property: "color",+ value: { type: "keyword", value: "green" },+ },+ ])+ );+ selectedInstanceSelectorStore.set(["box"]);+ selectedStyleSourceSelectorStore.set({ styleSourceId: "box.local" });++ const { result } = renderHook(() => useStyleInfo());+ expect(getStyleSource(result.current.color)).toEqual("local");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "green",+ });++ act(() => {+ selectedStyleSourceSelectorStore.set({+ styleSourceId: "box.local",+ state: ":hover",+ });+ });+ expect(getStyleSource(result.current.color)).toEqual("remote");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "green",+ });+ });++ test("show active state style as local when selected", () => {+ resetStores();+ instancesStore.set(+ toMap([{ type: "instance", id: "box", component: "Box", children: [] }])+ );+ breakpointsStore.set(toMap([{ id: "base", label: "" }]));+ styleSourcesStore.set(toMap([{ id: "box.local", type: "local" }]));+ styleSourceSelectionsStore.set(+ toStyleSourceSelectionsMap([{ instanceId: "box", values: ["box.local"] }])+ );+ stylesStore.set(+ toStylesMap([+ {+ styleSourceId: "box.local",+ breakpointId: "base",+ state: ":hover",+ property: "color",+ value: { type: "keyword", value: "green" },+ },+ ])+ );+ selectedInstanceSelectorStore.set(["box"]);+ selectedStyleSourceSelectorStore.set({ styleSourceId: "box.local" });+ $selectedInstanceStates.set(new Set([":hover"]));++ const { result } = renderHook(() => useStyleInfo());+ expect(getStyleSource(result.current.color)).toEqual("remote");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "green",+ });++ act(() => {+ selectedStyleSourceSelectorStore.set({+ styleSourceId: "box.local",+ state: ":hover",+ });+ });+ expect(getStyleSource(result.current.color)).toEqual("local");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "green",+ });+ });++ test("show active state from another breakpoint as remote", () => {+ resetStores();+ instancesStore.set(+ toMap([{ type: "instance", id: "box", component: "Box", children: [] }])+ );+ breakpointsStore.set(+ toMap([+ { id: "base", label: "" },+ { id: "small", label: "", minWidth: 768 },+ ])+ );+ styleSourcesStore.set(toMap([{ id: "box.local", type: "local" }]));+ styleSourceSelectionsStore.set(+ toStyleSourceSelectionsMap([{ instanceId: "box", values: ["box.local"] }])+ );+ stylesStore.set(+ toStylesMap([+ {+ styleSourceId: "box.local",+ breakpointId: "base",+ state: ":hover",+ property: "color",+ value: { type: "keyword", value: "green" },+ },+ // check stateless declaration does not override state+ {+ styleSourceId: "box.local",+ breakpointId: "base",+ property: "color",+ value: { type: "keyword", value: "red" },+ },+ ])+ );+ selectedInstanceSelectorStore.set(["box"]);+ selectedStyleSourceSelectorStore.set({ styleSourceId: "box.local" });+ $selectedBreakpointId.set("small");++ const { result } = renderHook(() => useStyleInfo());+ expect(getStyleSource(result.current.color)).toEqual("remote");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "red",+ });++ act(() => {+ $selectedInstanceStates.set(new Set([":hover"]));+ });+ expect(getStyleSource(result.current.color)).toEqual("remote");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "green",+ });+ });++ test("show active state inherited from parent as remote", () => {+ resetStores();+ instancesStore.set(+ toMap([+ {+ type: "instance",+ id: "body",+ component: "Body",+ children: [{ type: "id", value: "box" }],+ },+ { type: "instance", id: "box", component: "Box", children: [] },+ ])+ );+ breakpointsStore.set(toMap([{ id: "base", label: "" }]));+ styleSourcesStore.set(toMap([{ id: "body.local", type: "local" }]));+ styleSourceSelectionsStore.set(+ toStyleSourceSelectionsMap([+ { instanceId: "body", values: ["body.local"] },+ ])+ );+ stylesStore.set(+ toStylesMap([+ {+ styleSourceId: "body.local",+ breakpointId: "base",+ state: ":hover",+ property: "color",+ value: { type: "keyword", value: "green" },+ },+ // check stateless declaration does not override state+ {+ styleSourceId: "body.local",+ breakpointId: "base",+ property: "color",+ value: { type: "keyword", value: "red" },+ },+ ])+ );+ selectedInstanceSelectorStore.set(["box", "body"]);++ const { result } = renderHook(() => useStyleInfo());+ expect(getStyleSource(result.current.color)).toEqual("remote");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "red",+ });++ act(() => {+ $selectedInstanceStates.set(new Set([":hover"]));+ });+ expect(getStyleSource(result.current.color)).toEqual("remote");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "green",+ });+ });++ test("show active state from the previous token as remote", () => {+ resetStores();+ instancesStore.set(+ toMap([{ type: "instance", id: "box", component: "Box", children: [] }])+ );+ breakpointsStore.set(toMap([{ id: "base", label: "" }]));+ styleSourcesStore.set(+ toMap([+ { id: "box.token", type: "token", name: "" },+ { id: "box.local", type: "local" },+ ])+ );+ styleSourceSelectionsStore.set(+ toStyleSourceSelectionsMap([+ { instanceId: "box", values: ["box.token", "box.local"] },+ ])+ );+ stylesStore.set(+ toStylesMap([+ {+ styleSourceId: "box.token",+ breakpointId: "base",+ state: ":hover",+ property: "color",+ value: { type: "keyword", value: "green" },+ },+ // check stateless declaration does not override state+ {+ styleSourceId: "box.token",+ breakpointId: "base",+ property: "color",+ value: { type: "keyword", value: "red" },+ },+ ])+ );+ selectedInstanceSelectorStore.set(["box"]);+ selectedStyleSourceSelectorStore.set({ styleSourceId: "box.local" });++ const { result } = renderHook(() => useStyleInfo());+ expect(getStyleSource(result.current.color)).toEqual("remote");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "red",+ });++ act(() => {+ $selectedInstanceStates.set(new Set([":hover"]));+ });+ expect(getStyleSource(result.current.color)).toEqual("remote");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "green",+ });+ });++ test("show active state from the next token as overwritten", () => {+ resetStores();+ instancesStore.set(+ toMap([{ type: "instance", id: "box", component: "Box", children: [] }])+ );+ breakpointsStore.set(toMap([{ id: "base", label: "" }]));+ styleSourcesStore.set(+ toMap([+ { id: "box.first", type: "token", name: "" },+ { id: "box.second", type: "token", name: "" },+ { id: "box.third", type: "token", name: "" },+ ])+ );+ styleSourceSelectionsStore.set(+ toStyleSourceSelectionsMap([+ { instanceId: "box", values: ["box.first", "box.second", "box.third"] },+ ])+ );+ stylesStore.set(+ toStylesMap([+ {+ styleSourceId: "box.third",+ breakpointId: "base",+ state: ":hover",+ property: "color",+ value: { type: "keyword", value: "green" },+ },+ // check stateless declaration does not override state+ {+ styleSourceId: "box.second",+ breakpointId: "base",+ state: ":hover",+ property: "color",+ value: { type: "keyword", value: "blue" },+ },+ ])+ );+ selectedInstanceSelectorStore.set(["box"]);+ selectedStyleSourceSelectorStore.set({ styleSourceId: "box.first" });+ $selectedInstanceStates.set(new Set([":hover"]));++ const { result } = renderHook(() => useStyleInfo());+ expect(getStyleSource(result.current.color)).toEqual("remote");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "green",+ });++ act(() => {+ selectedStyleSourceSelectorStore.set({ styleSourceId: "box.second" });+ });+ expect(getStyleSource(result.current.color)).toEqual("overwritten");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "blue",+ });+ });++ test("show active state from preset", () => {+ resetStores();+ instancesStore.set(+ toMap([{ type: "instance", id: "box", component: "Box", children: [] }])+ );+ breakpointsStore.set(toMap([{ id: "base", label: "" }]));+ styleSourcesStore.set(toMap([{ id: "box.local", type: "local" }]));+ styleSourceSelectionsStore.set(+ toStyleSourceSelectionsMap([{ instanceId: "box", values: ["box.local"] }])+ );+ stylesStore.set(new Map());+ registeredComponentMetasStore.set(+ new Map([+ [+ "Box",+ {+ type: "container",+ icon: "",+ presetStyle: {+ div: [+ {+ state: ":hover",+ property: "color",+ value: { type: "keyword", value: "green" },+ },+ {+ property: "color",+ value: { type: "keyword", value: "red" },+ },+ ],+ },+ },+ ],+ ])+ );+ selectedInstanceSelectorStore.set(["box"]);+ $selectedInstanceStates.set(new Set([":hover"]));+ selectedStyleSourceSelectorStore.set({ styleSourceId: "box.local" });+ selectedInstanceIntanceToTagStore.set(new Map([["box", "div"]]));++ const { result } = renderHook(() => useStyleInfo());+ expect(getStyleSource(result.current.color)).toEqual("preset");+ expect(result.current.color?.value).toEqual({+ type: "keyword",+ value: "green",+ });+ });+});diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.tsindex 069eeaba20..8cbee9aed7 100644--- a/apps/builder/app/builder/features/style-panel/shared/style-info.ts+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.ts@@ -27,6 +27,7 @@ import {breakpointsStore,styleSourceSelectionsStore,registeredComponentMetasStore,+ $selectedInstanceStates,} from "~/shared/nano-states";import { selectedBreakpointStore } from "~/shared/nano-states";import type { InstanceSelector } from "~/shared/tree-utils";@@ -69,9 +70,15 @@ const CUSTOM_DEFAULT_VALUES: Partial<Record<StyleProperty, StyleValue>> = {outlineWidth: { value: 0, type: "unit", unit: "px" },};+type StatefulValue = { state: string; value: StyleValue };+export type StyleValueInfo = {value: StyleValue;+ // either stateful and local exist or stateless and local or just local+ // @todo improve with more clear structure+ stateful?: StatefulValue;local?: StyleValue;+ stateless?: StyleValue;previousSource?: SourceValueInfo;nextSource?: SourceValueInfo;cascaded?: CascadedValueInfo;@@ -109,7 +116,12 @@ export const getStyleSource = (// show source to use if at least one of control properties matches// so user could see if something is set or something is inheritedfor (const info of styleValueInfos) {- if (info?.nextSource && info.local) {+ if (info?.nextSource && (info.local || info.stateful)) {+ return "overwritten";+ }+ }+ for (const info of styleValueInfos) {+ if (info?.stateful && info.local) {return "overwritten";}}@@ -119,26 +131,27 @@ export const getStyleSource = (}}for (const info of styleValueInfos) {- if (- info?.previousSource !== undefined ||- info?.nextSource !== undefined ||- info?.cascaded !== undefined- ) {+ if (info?.stateless || info?.stateful) {return "remote";}}for (const info of styleValueInfos) {- if (info?.preset !== undefined) {+ if (info?.previousSource || info?.nextSource || info?.cascaded) {+ return "remote";+ }+ }+ for (const info of styleValueInfos) {+ if (info?.preset) {return "preset";}}for (const info of styleValueInfos) {- if (info?.htmlValue !== undefined) {+ if (info?.htmlValue) {return "default";}}for (const info of styleValueInfos) {- if (info?.inherited !== undefined) {+ if (info?.inherited) {return "remote";}}@@ -153,36 +166,44 @@ for (const [property, value] of Object.entries(properties)) {}}-const getSelectedStyle = (+const getLocalStyles = (stylesByStyleSourceId: Map<StyleSourceType["id"], StyleDecl[]>,breakpointId: string,- styleSourceSelector: StyleSourceSelector+ styleSourceSelector: StyleSourceSelector,+ activeStates: Set<string>) => {- const style: Style = {};+ const statelessStyles = new Map<StyleProperty, StyleValue>();+ const localStyles = new Map<StyleProperty, StyleValue>();+ const statefulStyles = new Map<StyleProperty, StatefulValue>();const instanceStyles = stylesByStyleSourceId.get(styleSourceSelector.styleSourceId);if (instanceStyles === undefined) {- return style;+ return { statelessStyles, localStyles, statefulStyles };}for (const styleDecl of instanceStyles) {- // @todo consider making stateless styles remote when state is selected- if (- styleDecl.breakpointId === breakpointId &&- styleDecl.state === undefined- ) {- style[styleDecl.property] = styleDecl.value;+ if (styleDecl.breakpointId !== breakpointId) {+ continue;}- }- for (const styleDecl of instanceStyles) {- if (- styleDecl.breakpointId === breakpointId &&- styleDecl.state === styleSourceSelector.state- ) {- style[styleDecl.property] = styleDecl.value;+ if (styleDecl.state === undefined) {+ if (styleSourceSelector.state === undefined) {+ localStyles.set(styleDecl.property, styleDecl.value);+ } else {+ statelessStyles.set(styleDecl.property, styleDecl.value);+ }+ }+ if (styleDecl.state && activeStates.has(styleDecl.state)) {+ if (styleDecl.state === styleSourceSelector.state) {+ localStyles.set(styleDecl.property, styleDecl.value);+ } else {+ statefulStyles.set(styleDecl.property, {+ state: styleDecl.state,+ value: styleDecl.value,+ });+ }}}- return style;+ return { statelessStyles, localStyles, statefulStyles };};/**@@ -210,7 +231,8 @@ export const getCascadedBreakpointIds = (export const getCascadedInfo = (stylesByInstanceId: Map<Instance["id"], StyleDecl[]>,instanceId: string,- cascadedBreakpointIds: string[]+ cascadedBreakpointIds: string[],+ activeStates: Set<string>) => {const cascadedStyle: CascadedProperties = {};const instanceStyles = stylesByInstanceId.get(instanceId);@@ -229,6 +251,18 @@ export const getCascadedInfo = (};}}+ for (const styleDecl of instanceStyles) {+ if (+ styleDecl.breakpointId === breakpointId &&+ styleDecl.state &&+ activeStates.has(styleDecl.state)+ ) {+ cascadedStyle[styleDecl.property] = {+ breakpointId,+ value: styleDecl.value,+ };+ }+ }}return cascadedStyle;};@@ -250,7 +284,7 @@ export const getInstanceComponent = (export const getPresetStyleRule = (meta: undefined | WsComponentMeta,tagName: HtmlTags,- styleSourceSelector?: StyleSourceSelector+ activeStates: Set<string>) => {const presetStyles = meta?.presetStyle?.[tagName];if (presetStyles === undefined) {@@ -258,11 +292,12 @@ export const getPresetStyleRule = (}const presetStyle: Style = {};for (const styleDecl of presetStyles) {- // @todo consider making stateless styles remote when state is selectedif (styleDecl.state === undefined) {presetStyle[styleDecl.property] = styleDecl.value;}- if (styleDecl.state === styleSourceSelector?.state) {+ }+ for (const styleDecl of presetStyles) {+ if (styleDecl.state && activeStates.has(styleDecl.state)) {presetStyle[styleDecl.property] = styleDecl.value;}}@@ -280,7 +315,8 @@ export const getInheritedInfo = (instanceSelector: InstanceSelector,selectedInstanceIntanceToTag: Map<Instance["id"], HtmlTags>,cascadedBreakpointIds: string[],- selectedBreakpointId: string+ selectedBreakpointId: string,+ activeStates: Set<string>) => {const inheritedStyle: InheritedProperties = {};// skip current instance and start with root til parent@@ -324,8 +360,22 @@ export const getInheritedInfo = (for (const styleDecl of ancestorInstanceStyles) {if (styleDecl.breakpointId === breakpointId &&- styleDecl.state === undefined &&- inheritableProperties.has(styleDecl.property)+ inheritableProperties.has(styleDecl.property) &&+ styleDecl.state === undefined+ ) {+ inheritedStyle[styleDecl.property] = {+ instanceId: ancestorInstance.id,+ styleSourceId: styleDecl.styleSourceId,+ value: styleDecl.value,+ };+ }+ }+ for (const styleDecl of ancestorInstanceStyles) {+ if (+ styleDecl.breakpointId === breakpointId &&+ inheritableProperties.has(styleDecl.property) &&+ styleDecl.state &&+ activeStates.has(styleDecl.state)) {inheritedStyle[styleDecl.property] = {instanceId: ancestorInstance.id,@@ -344,7 +394,8 @@ export const getPreviousSourceInfo = (stylesByInstanceId: Map<Instance["id"], StyleDecl[]>,selectedInstanceSelector: InstanceSelector,selectedStyleSourceSelector: StyleSourceSelector,- breakpointId: Breakpoint["id"]+ breakpointId: Breakpoint["id"],+ activeStates: Set<string>) => {const previousSourceStyle: SourceProperties = {};const [selectedInstanceId] = selectedInstanceSelector;@@ -362,7 +413,21 @@ export const getPreviousSourceInfo = (for (const styleDecl of instanceStyles) {if (styleDecl.breakpointId === breakpointId &&- previousSourceIds.includes(styleDecl.styleSourceId)+ previousSourceIds.includes(styleDecl.styleSourceId) &&+ styleDecl.state === undefined+ ) {+ previousSourceStyle[styleDecl.property] = {+ styleSourceId: styleDecl.styleSourceId,+ value: styleDecl.value,+ };+ }+ }+ for (const styleDecl of instanceStyles) {+ if (+ styleDecl.breakpointId === breakpointId &&+ previousSourceIds.includes(styleDecl.styleSourceId) &&+ styleDecl.state &&+ activeStates.has(styleDecl.state)) {previousSourceStyle[styleDecl.property] = {styleSourceId: styleDecl.styleSourceId,@@ -378,7 +443,8 @@ export const getNextSourceInfo = (stylesByInstanceId: Map<Instance["id"], StyleDecl[]>,selectedInstanceSelector: InstanceSelector,selectedStyleSourceSelector: StyleSourceSelector,- breakpointId: Breakpoint["id"]+ breakpointId: Breakpoint["id"],+ activeStates: Set<string>) => {const nextSourceStyle: SourceProperties = {};const [selectedInstanceId] = selectedInstanceSelector;@@ -396,7 +462,21 @@ export const getNextSourceInfo = (for (const styleDecl of instanceStyles) {if (styleDecl.breakpointId === breakpointId &&- nextSourceIds.includes(styleDecl.styleSourceId)+ nextSourceIds.includes(styleDecl.styleSourceId) &&+ styleDecl.state === undefined+ ) {+ nextSourceStyle[styleDecl.property] = {+ styleSourceId: styleDecl.styleSourceId,+ value: styleDecl.value,+ };+ }+ }+ for (const styleDecl of instanceStyles) {+ if (+ styleDecl.breakpointId === breakpointId &&+ nextSourceIds.includes(styleDecl.styleSourceId) &&+ styleDecl.state &&+ activeStates.has(styleDecl.state)) {nextSourceStyle[styleDecl.property] = {styleSourceId: styleDecl.styleSourceId,@@ -427,20 +507,42 @@ const useStyleInfoByInstanceAndStyleSource = (const { stylesByInstanceId, stylesByStyleSourceId } =useStore(stylesIndexStore);const styleSourceSelections = useStore(styleSourceSelectionsStore);-- const selectedStyle = useMemo(() => {+ const selectedInstanceStates = useStore($selectedInstanceStates);+ const activeStates = useMemo(() => {+ const activeStates = new Set(selectedInstanceStates);+ if (styleSourceSelector?.state !== undefined) {+ activeStates.add(styleSourceSelector.state);+ }+ return activeStates;+ }, [styleSourceSelector, selectedInstanceStates]);++ const selectedStyle = useMemo((): {+ statelessStyles: Map<StyleProperty, StyleValue>;+ localStyles: Map<StyleProperty, StyleValue>;+ statefulStyles: Map<StyleProperty, StatefulValue>;+ } => {if (selectedBreakpointId === undefined ||styleSourceSelector === undefined) {- return {};+ return {+ statelessStyles: new Map(),+ localStyles: new Map(),+ statefulStyles: new Map(),+ };}- return getSelectedStyle(+ return getLocalStyles(stylesByStyleSourceId,selectedBreakpointId,- styleSourceSelector+ styleSourceSelector,+ activeStates);- }, [stylesByStyleSourceId, selectedBreakpointId, styleSourceSelector]);+ }, [+ stylesByStyleSourceId,+ selectedBreakpointId,+ activeStates,+ styleSourceSelector,+ ]);const cascadedBreakpointIds = useMemo(() => getCascadedBreakpointIds(breakpoints, selectedBreakpointId),@@ -462,7 +564,8 @@ const useStyleInfoByInstanceAndStyleSource = (instanceSelector,instanceToTag,cascadedBreakpointIds,- selectedBreakpointId+ selectedBreakpointId,+ activeStates);}, [instances,@@ -472,6 +575,7 @@ const useStyleInfoByInstanceAndStyleSource = (selectedBreakpointId,instanceSelector,instanceToTag,+ activeStates,]);const cascadedInfo = useMemo(() => {@@ -481,9 +585,15 @@ const useStyleInfoByInstanceAndStyleSource = (return getCascadedInfo(stylesByInstanceId,instanceSelector[0],- cascadedBreakpointIds+ cascadedBreakpointIds,+ activeStates);- }, [stylesByInstanceId, instanceSelector, cascadedBreakpointIds]);+ }, [+ stylesByInstanceId,+ instanceSelector,+ cascadedBreakpointIds,+ activeStates,+ ]);const previousSourceInfo = useMemo(() => {if (@@ -498,7 +608,8 @@ const useStyleInfoByInstanceAndStyleSource = (stylesByInstanceId,instanceSelector,styleSourceSelector,- selectedBreakpointId+ selectedBreakpointId,+ activeStates);}, [styleSourceSelections,@@ -506,6 +617,7 @@ const useStyleInfoByInstanceAndStyleSource = (instanceSelector,styleSourceSelector,selectedBreakpointId,+ activeStates,]);const nextSourceInfo = useMemo(() => {@@ -521,7 +633,8 @@ const useStyleInfoByInstanceAndStyleSource = (stylesByInstanceId,instanceSelector,styleSourceSelector,- selectedBreakpointId+ selectedBreakpointId,+ activeStates);}, [styleSourceSelections,@@ -529,6 +642,7 @@ const useStyleInfoByInstanceAndStyleSource = (instanceSelector,styleSourceSelector,selectedBreakpointId,+ activeStates,]);const presetStyle = useMemo(() => {@@ -541,12 +655,15 @@ const useStyleInfoByInstanceAndStyleSource = (if (tagName === undefined || component === undefined) {return;}- return getPresetStyleRule(- metas.get(component),- tagName,- styleSourceSelector- );- }, [instances, metas, instanceSelector, instanceToTag, styleSourceSelector]);+ return getPresetStyleRule(metas.get(component), tagName, activeStates);+ }, [+ instances,+ metas,+ instanceSelector,+ instanceToTag,+ activeStates,+ styleSourceSelector,+ ]);const htmlStyle = useMemo(() => {const instanceId = instanceSelector?.[0];@@ -581,9 +698,14 @@ const useStyleInfoByInstanceAndStyleSource = (const cascaded = cascadedInfo[property];const previousSource = previousSourceInfo[property];const nextSource = nextSourceInfo[property];- const local = selectedStyle?.[property];+ const { statelessStyles, localStyles, statefulStyles } = selectedStyle;+ const stateful = statefulStyles.get(property);+ const local = localStyles.get(property);+ const stateless = statelessStyles.get(property);const ownValue =local ??+ stateful?.value ??+ stateless ??nextSource?.value ??previousSource?.value ??cascaded?.value ??@@ -607,7 +729,9 @@ const useStyleInfoByInstanceAndStyleSource = (const currentColor = ownColor ?? inheritedColor ?? defaultValue;styleInfoData[property] = {value,+ stateful,local,+ stateless,nextSource,previousSource,cascaded,@@ -619,7 +743,9 @@ const useStyleInfoByInstanceAndStyleSource = (} else {styleInfoData[property] = {value,+ stateful,local,+ stateless,nextSource,previousSource,cascaded,diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-badge.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-badge.tsxindex 92dbebe78f..e71e808e47 100644--- a/apps/builder/app/builder/features/style-panel/style-source/style-source-badge.tsx+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-badge.tsx@@ -15,6 +15,9 @@ export const StyleSourceBadge = styled(Text, {textOverflow: "ellipsis",variants: {source: {+ local: {+ backgroundColor: theme.colors.backgroundStyleSourceLocal,+ },token: {backgroundColor: theme.colors.backgroundStyleSourceToken,},diff --git a/apps/builder/app/canvas/collapsed.ts b/apps/builder/app/canvas/collapsed.tsindex ade7d13d40..de86e74c43 100644--- a/apps/builder/app/canvas/collapsed.ts+++ b/apps/builder/app/canvas/collapsed.ts@@ -78,13 +78,15 @@ const getInstanceSize = (instanceId: string, tagName: HtmlTags | undefined) => {const component = getInstanceComponent(instances, instanceId);const presetStyle =tagName !== undefined && component !== undefined- ? getPresetStyleRule(metas.get(component), tagName)+ ? getPresetStyleRule(metas.get(component), tagName, new Set()): undefined;- const cascadedStyle = getCascadedInfo(stylesByInstanceId, instanceId, [- ...cascadedBreakpointIds,- selectedBreakpointId,- ]);+ const cascadedStyle = getCascadedInfo(+ stylesByInstanceId,+ instanceId,+ [...cascadedBreakpointIds, selectedBreakpointId],+ new Set()+ );const widthStyle = cascadedStyle?.width?.value ?? presetStyle?.width;const heightStyle = cascadedStyle?.height?.value ?? presetStyle?.height;diff --git a/apps/builder/app/canvas/instance-selected.ts b/apps/builder/app/canvas/instance-selected.tsindex 4213f08c3b..b9ac6eabcb 100644--- a/apps/builder/app/canvas/instance-selected.ts+++ b/apps/builder/app/canvas/instance-selected.ts@@ -11,6 +11,9 @@ import {instancesStore,selectedInstanceSelectorStore,$propValuesByInstanceSelector,+ $styles,+ $selectedInstanceStates,+ styleSourceSelectionsStore,} from "~/shared/nano-states";import htmlTags, { type htmlTags as HtmlTags } from "html-tags";import {@@ -135,7 +138,7 @@ const subscribeSelectedInstance = (return;}- const element = elements[0];+ const [element] = elements;// trigger style recomputing every time instance styles are changedselectedInstanceBrowserStyleStore.set(getBrowserStyle(element));@@ -158,6 +161,27 @@ const subscribeSelectedInstance = (const unitSizes = calculateUnitSizes(element);selectedInstanceUnitSizesStore.set(unitSizes);++ const availableStates = new Set<string>();+ const instanceStyleSourceIds = new Set(+ styleSourceSelectionsStore.get().get(instanceId)?.values+ );+ const styles = $styles.get();+ for (const styleDecl of styles.values()) {+ if (+ instanceStyleSourceIds.has(styleDecl.styleSourceId) &&+ styleDecl.state+ ) {+ availableStates.add(styleDecl.state);+ }+ }+ const activeStates = new Set<string>();+ for (const state of availableStates) {+ if (element.matches(state)) {+ activeStates.add(state);+ }+ }+ $selectedInstanceStates.set(activeStates);};let updateStoreTimeouHandle: undefined | ReturnType<typeof setTimeout>;diff --git a/apps/builder/app/shared/nano-states/breakpoints.ts b/apps/builder/app/shared/nano-states/breakpoints.tsindex f78eaaadeb..a59f7f5496 100644--- a/apps/builder/app/shared/nano-states/breakpoints.ts+++ b/apps/builder/app/shared/nano-states/breakpoints.ts@@ -7,6 +7,7 @@ export const breakpointsStore = atom<Breakpoints>(new Map());export const selectedBreakpointIdStore = atom<undefined | Breakpoint["id"]>(undefined);+export const $selectedBreakpointId = selectedBreakpointIdStore;export const selectedBreakpointStore = computed([breakpointsStore, selectedBreakpointIdStore],diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.tsindex 8c3218fe81..4e166192ba 100644--- a/apps/builder/app/shared/nano-states/nano-states.ts+++ b/apps/builder/app/shared/nano-states/nano-states.ts@@ -67,6 +67,7 @@ export const propsIndexStore = computed(propsStore, (props) => {});export const stylesStore = atom<Styles>(new Map());+export const $styles = stylesStore;export const useInstanceStyles = (instanceId: undefined | Instance["id"]) => {const instanceStylesStore = useMemo(() => {@@ -263,6 +264,13 @@ export const selectedStyleSourceStore = computed();});+export const $selectedStyleSource = selectedStyleSourceStore;++/**+ * Store the list of active states inferred from dom element+ * to display style values as remote+ */+export const $selectedInstanceStates = atom(new Set<string>());export const hoveredInstanceSelectorStore = atom<undefined | InstanceSelector>(undefineddiff --git a/apps/builder/app/shared/sync/sync-stores.ts b/apps/builder/app/shared/sync/sync-stores.tsindex 80ad8d5a3f..312c4fb5f5 100644--- a/apps/builder/app/shared/sync/sync-stores.ts+++ b/apps/builder/app/shared/sync/sync-stores.ts@@ -31,6 +31,7 @@ import {synchronizedComponentsMetaStores,dataSourceVariablesStore,$dragAndDropState,+ $selectedInstanceStates,} from "~/shared/nano-states";import { $ephemeralStyles } from "~/canvas/stores";@@ -109,6 +110,8 @@ export const registerContainers = () => {);clientStores.set("dragAndDropState", $dragAndDropState);clientStores.set("ephemeralStyles", $ephemeralStyles);+ clientStores.set("selectedInstanceStates", $selectedInstanceStates);+for (const [name, store] of synchronizedBreakpointsStores) {clientStores.set(name, store);}