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
refactor: switch to style object model in color control (#3993)
Bogdan Chadkin39637c60d9f6
diff --git a/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsx b/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsxindex 73a9665017..f0021f8d5a 100644--- a/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsx+++ b/apps/builder/app/builder/features/style-panel/controls/color/color-control.tsx@@ -1,4 +1,5 @@-import { useState } from "react";+import { useStore } from "@nanostores/react";+import { useMemo, useState } from "react";import { Flex } from "@webstudio-is/design-system";import { toValue } from "@webstudio-is/css-engine";import type { RgbValue, StyleValue } from "@webstudio-is/css-engine";@@ -11,6 +12,7 @@ import { getStyleSource } from "../../shared/style-info";import { styleConfigByName } from "../../shared/configs";import type { ControlProps } from "../types";import { AdvancedValueTooltip } from "../advanced-value-tooltip";+import { createComputedStyleDeclStore } from "../../shared/model";const parseColor = (color?: StyleValue): RgbValue => {const colordValue = colord(toValue(color));@@ -45,25 +47,26 @@ export const ColorControl = ({deleteProperty,isAdvanced,}: ControlProps) => {+ const $computedStyleDecl = useMemo(+ () => createComputedStyleDeclStore(property),+ [property]+ );+ const computedStyleDecl = useStore($computedStyleDecl);+const [intermediateValue, setIntermediateValue] =useState<CssColorPickerValueInput>();const { items: defaultItems } = styleConfigByName(property);const styleInfo = currentStyle[property];- let value = currentStyle[property]?.value ?? {- type: "keyword" as const,- value: "black",- };-const setValue = setProperty(property);+ let value = computedStyleDecl.cascadedValue;if (value.type !== "rgb" && value.type !== "keyword") {// Support previously set colorsvalue = parseColor(value);}-- const currentColor = parseColor(currentStyle["color"]?.currentColor);+ const currentColor = parseColor(computedStyleDecl.usedValue);return (<Flex align="center" gap="1">diff --git a/apps/builder/app/builder/features/style-panel/shared/model.tsx b/apps/builder/app/builder/features/style-panel/shared/model.tsxindex 11c8cfaf1a..e2d95b139d 100644--- a/apps/builder/app/builder/features/style-panel/shared/model.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/model.tsx@@ -14,6 +14,7 @@ import {$styleSourceSelections,} from "~/shared/nano-states";import {+ getComputedStyleDecl,getPresetStyleDeclKey,type StyleObjectModel,} from "~/shared/style-object-model";@@ -61,17 +62,6 @@ const $matchingBreakpoints = computed($breakpoints, (breakpoints) => {.map((breakpoint) => breakpoint.id);});-const $matchingStates = computed(- [$selectedInstanceStates, $selectedOrLastStyleSourceSelector],- (instanceStates, styleSourceSelector) => {- const matchingStates = new Set(instanceStates);- if (styleSourceSelector?.state) {- matchingStates.add(styleSourceSelector.state);- }- return matchingStates;- }-);-const $model = computed([$styles,@@ -80,7 +70,7 @@ const $model = computed($selectedInstanceIntanceToTag,$instanceComponents,$matchingBreakpoints,- $matchingStates,+ $selectedInstanceStates,],(styles,@@ -103,6 +93,21 @@ const $model = computed(});+export const createComputedStyleDeclStore = (property: string) => {+ return computed(+ [$model, $selectedInstanceSelector, $selectedOrLastStyleSourceSelector],+ (model, instanceSelector, styleSourceSelector) => {+ return getComputedStyleDecl({+ model,+ instanceSelector,+ styleSourceId: styleSourceSelector?.styleSourceId,+ state: styleSourceSelector?.state,+ property,+ });+ }+ );+};+export const useStyleObjectModel = () => {return useStore($model);};diff --git a/apps/builder/app/shared/style-object-model.test.tsx b/apps/builder/app/shared/style-object-model.test.tsxindex b10e90c492..0f06ed13d8 100644--- a/apps/builder/app/shared/style-object-model.test.tsx+++ b/apps/builder/app/shared/style-object-model.test.tsx@@ -803,6 +803,46 @@ test("preset styles are more specific than browser styles", () => {).toEqual({ type: "keyword", value: "flex" });});+test("access cascaded value without resolving", () => {+ const model = createModel({+ css: `+ local {+ color: initial;+ }+ `,+ jsx: <$.Body ws:id="body" class="local"></$.Body>,+ });+ expect(+ getComputedStyleDecl({+ model,+ instanceSelector: ["body"],+ property: "color",+ }).cascadedValue+ ).toEqual({ type: "keyword", value: "initial" });+});++test("fallback cascaded value to inherited computed value", () => {+ const model = createModel({+ css: `+ body {+ border-top-color: currentcolor;+ }+ `,+ jsx: (+ <$.Body ws:id="body" class="body">+ <$.Box ws:id="box" class="box"></$.Box>+ </$.Body>+ ),+ });+ expect(+ getComputedStyleDecl({+ model,+ instanceSelector: ["box", "body"],+ property: "borderTopColor",+ }).cascadedValue+ ).toEqual({ type: "keyword", value: "currentColor" });+});+describe("selected style", () => {test("access selected style source value within cascade", () => {const model = createModel({@@ -984,4 +1024,37 @@ describe("selected style", () => {}).usedValue).toEqual({ type: "keyword", value: "red" });});++ test("prefer selected state from preset", () => {+ const model = createModel({+ presets: {+ Body: `+ body {+ color: red;+ }+ body:hover {+ color: blue;+ }+ `,+ },+ css: "",+ jsx: <$.Body ws:id="body" ws:tag="body" class="local"></$.Body>,+ matchingBreakpoints: ["base", "small"],+ });+ expect(+ getComputedStyleDecl({+ model,+ instanceSelector: ["body"],+ property: "color",+ }).usedValue+ ).toEqual({ type: "keyword", value: "red" });+ expect(+ getComputedStyleDecl({+ model,+ instanceSelector: ["body"],+ state: ":hover",+ property: "color",+ }).usedValue+ ).toEqual({ type: "keyword", value: "blue" });+ });});diff --git a/apps/builder/app/shared/style-object-model.ts b/apps/builder/app/shared/style-object-model.tsindex 18159320ef..0a1bd9a508 100644--- a/apps/builder/app/shared/style-object-model.ts+++ b/apps/builder/app/shared/style-object-model.ts@@ -143,13 +143,6 @@ const getCascadedValue = ({// preset component stylesif (component && tag) {- // stateless- const key = getPresetStyleDeclKey({ component, tag, property });- const styleValue = presetStyles.get(key);- if (styleValue) {- declaredValues.push({ value: styleValue });- }- // statefulfor (const state of states) {const key = getPresetStyleDeclKey({ component, tag, state, property });const styleValue = presetStyles.get(key);@@ -210,14 +203,14 @@ const customPropertyData = {*/export const getComputedStyleDecl = ({model,- instanceSelector,+ instanceSelector = [],styleSourceId,state,property,customPropertiesGraph = new Map(),}: {model: StyleObjectModel;- instanceSelector: InstanceSelector;+ instanceSelector?: InstanceSelector;styleSourceId?: StyleDecl["styleSourceId"];state?: StyleDecl["state"];property: Property;@@ -226,6 +219,7 @@ export const getComputedStyleDecl = ({*/customPropertiesGraph?: Map<Instance["id"], Set<Property>>;}): {+ cascadedValue: StyleValue;computedValue: StyleValue;usedValue: StyleValue;} => {@@ -236,6 +230,7 @@ export const getComputedStyleDecl = ({const inherited = propertyData.inherited;const initialValue: StyleValue = propertyData.initial;let computedValue: StyleValue = initialValue;+ let cascadedValue: undefined | StyleValue;// start computing from the rootfor (let index = instanceSelector.length - 1; index >= 0; index -= 1) {@@ -250,13 +245,14 @@ export const getComputedStyleDecl = ({const inheritedValue: StyleValue = computedValue;// https://drafts.csswg.org/css-cascade-5/#cascaded- const { cascadedValue } = getCascadedValue({+ const cascaded = getCascadedValue({model,instanceId,styleSourceId,state,property,});+ cascadedValue = cascaded.cascadedValue;// resolve specified value// https://drafts.csswg.org/css-cascade-5/#specified@@ -337,5 +333,8 @@ export const getComputedStyleDecl = ({usedValue = currentColor.usedValue;}- return { computedValue, usedValue };+ // fallback to inherited value+ cascadedValue ??= computedValue;++ return { cascadedValue, computedValue, usedValue };};