Skip to content

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.

Fallback to cascaded and inherited when value not set (#712)

Bogdan Chadkin6f1df5378d8c
diff --git a/apps/designer/app/designer/features/style-panel/shared/style-info.test.ts b/apps/designer/app/designer/features/style-panel/shared/style-info.test.ts
new file mode 100644
index 0000000000..4364690e20
--- /dev/null
+++ b/apps/designer/app/designer/features/style-panel/shared/style-info.test.ts
@@ -0,0 +1,169 @@
+import type { Breakpoint, CssRule } from "@webstudio-is/css-data";
+import type { Instance } from "@webstudio-is/react-sdk";
+import {
+ getCascadedBreakpointIds,
+ getCascadedInfo,
+ getInheritedInfo,
+} from "./style-info";
+
+const breakpoints: Breakpoint[] = [
+ {
+ id: "1",
+ label: "1",
+ minWidth: 0,
+ },
+ {
+ id: "2",
+ label: "2",
+ minWidth: 768,
+ },
+ {
+ id: "3",
+ label: "3",
+ minWidth: 1280,
+ },
+ {
+ id: "4",
+ label: "4",
+ minWidth: 1920,
+ },
+];
+
+const selectedBreakpointId = "3";
+const cascadedBreakpointIds = getCascadedBreakpointIds(
+ breakpoints,
+ selectedBreakpointId
+);
+
+const cssRules: CssRule[] = [
+ {
+ breakpoint: "1",
+ style: {
+ width: { type: "unit", value: 100, unit: "px" },
+ height: { type: "unit", value: 50, unit: "px" },
+ },
+ },
+ {
+ breakpoint: "2",
+ style: {
+ width: { type: "unit", value: 200, unit: "px" },
+ },
+ },
+ {
+ breakpoint: "3",
+ style: {
+ // should not be computed because current breakpoint
+ height: { type: "unit", value: 150, unit: "px" },
+ },
+ },
+ {
+ breakpoint: "4",
+ style: {
+ width: { type: "unit", value: 400, unit: "px" },
+ },
+ },
+];
+
+const rootInstance: Instance = {
+ type: "instance",
+ id: "1",
+ component: "Body",
+ cssRules: [
+ {
+ breakpoint: "1",
+ style: {
+ // should be inherited even from another breakpoint
+ fontSize: { type: "unit", value: 20, unit: "px" },
+ },
+ },
+ ],
+ children: [
+ {
+ type: "instance",
+ id: "2",
+ component: "Box",
+ cssRules: [
+ {
+ breakpoint: "3",
+ style: {
+ // should not be inherited because width is not inheritable
+ width: { type: "unit", value: 100, unit: "px" },
+ // should be inherited from selected breakpoint
+ fontWeight: { type: "keyword", value: "600" },
+ },
+ },
+ ],
+ children: [
+ {
+ type: "instance",
+ id: "3",
+ component: "Box",
+ cssRules: [
+ {
+ breakpoint: "3",
+ style: {
+ // should not show selected style as inherited
+ fontWeight: { type: "keyword", value: "500" },
+ },
+ },
+ ],
+ children: [],
+ },
+ ],
+ },
+ ],
+};
+const selectedInstanceId = "3";
+
+test("compute cascaded styles", () => {
+ expect(getCascadedInfo(cssRules, cascadedBreakpointIds))
+ .toMatchInlineSnapshot(`
+ {
+ "height": {
+ "breakpointId": "1",
+ "value": {
+ "type": "unit",
+ "unit": "px",
+ "value": 50,
+ },
+ },
+ "width": {
+ "breakpointId": "2",
+ "value": {
+ "type": "unit",
+ "unit": "px",
+ "value": 200,
+ },
+ },
+ }
+ `);
+});
+
+test("compute inherited styles", () => {
+ expect(
+ getInheritedInfo(
+ rootInstance,
+ selectedInstanceId,
+ cascadedBreakpointIds,
+ selectedBreakpointId
+ )
+ ).toMatchInlineSnapshot(`
+ {
+ "fontSize": {
+ "instanceId": "1",
+ "value": {
+ "type": "unit",
+ "unit": "px",
+ "value": 20,
+ },
+ },
+ "fontWeight": {
+ "instanceId": "2",
+ "value": {
+ "type": "keyword",
+ "value": "600",
+ },
+ },
+ }
+ `);
+});
diff --git a/apps/designer/app/designer/features/style-panel/shared/style-info.ts b/apps/designer/app/designer/features/style-panel/shared/style-info.ts
index f794b210ec..14f49280f1 100644
--- a/apps/designer/app/designer/features/style-panel/shared/style-info.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/style-info.ts
@@ -1,9 +1,43 @@
-import type { Style, StyleProperty, StyleValue } from "@webstudio-is/css-data";
import { useMemo } from "react";
+import type {
+ Breakpoint,
+ CssRule,
+ Style,
+ StyleProperty,
+ StyleValue,
+} from "@webstudio-is/css-data";
+import { properties } from "@webstudio-is/css-data";
+import { utils } from "@webstudio-is/project";
+import type { Instance } from "@webstudio-is/react-sdk";
+import { useBreakpoints, useRootInstance } from "~/shared/nano-states";
+import {
+ useSelectedBreakpoint,
+ useSelectedInstanceData,
+} from "~/designer/shared/nano-states";
+
+type CascadedValueInfo = {
+ breakpointId: string;
+ value: StyleValue;
+};
+
+type CascadedProperties = {
+ [property in StyleProperty]?: CascadedValueInfo;
+};
+
+type InheritedValueInfo = {
+ instanceId: string;
+ value: StyleValue;
+};
+
+type InheritedProperties = {
+ [property in StyleProperty]?: InheritedValueInfo;
+};
export type StyleValueInfo = {
value: StyleValue;
local?: StyleValue;
+ cascaded?: CascadedValueInfo;
+ inherited?: InheritedValueInfo;
};
export type StyleInfo = {
@@ -23,6 +57,97 @@ export const getStyleSource = (
return "preset";
};
+const styleProperties = Object.keys(properties) as StyleProperty[];
+const inheritableProperties = new Set<string>();
+for (const [property, value] of Object.entries(properties)) {
+ if (value.inherited) {
+ inheritableProperties.add(property);
+ }
+}
+
+/**
+ * find all breakpoints which may affect current view
+ */
+export const getCascadedBreakpointIds = (
+ breakpoints: Breakpoint[],
+ selectedBreakpointId?: string
+) => {
+ const sortedBreakpoints = utils.breakpoints.sort(breakpoints);
+ const cascadedBreakpointIds: string[] = [];
+ for (const breakpoint of sortedBreakpoints) {
+ if (breakpoint.id === selectedBreakpointId) {
+ break;
+ }
+ cascadedBreakpointIds.push(breakpoint.id);
+ }
+ return cascadedBreakpointIds;
+};
+
+/**
+ * extract all styles from breakpoints
+ * affecting current view
+ */
+export const getCascadedInfo = (
+ cssRules: CssRule[],
+ cascadedBreakpointIds: string[]
+) => {
+ const styles = new Map<string, Style>();
+ for (const rule of cssRules) {
+ if (rule.breakpoint !== undefined) {
+ styles.set(rule.breakpoint, rule.style);
+ }
+ }
+ const cascadedStyle: CascadedProperties = {};
+ for (const breakpointId of cascadedBreakpointIds) {
+ const style = styles.get(breakpointId);
+ if (style !== undefined) {
+ for (const [property, value] of Object.entries(style)) {
+ cascadedStyle[property as StyleProperty] = {
+ breakpointId,
+ value,
+ };
+ }
+ }
+ }
+ return cascadedStyle;
+};
+
+/**
+ * extract all inheritable styles from ancestor instances
+ * including active breakpoints
+ */
+export const getInheritedInfo = (
+ rootInstance: Instance,
+ instanceId: string,
+ cascadedBreakpointIds: string[],
+ selectedBreakpointId: string
+) => {
+ const inheritedStyle: InheritedProperties = {};
+ const ancestors = utils.tree.getInstancePath(rootInstance, instanceId);
+ for (const ancestorInstance of ancestors) {
+ // skip current element
+ if (ancestorInstance.id === instanceId) {
+ continue;
+ }
+ const cascadedStyle = getCascadedInfo(ancestorInstance.cssRules, [
+ ...cascadedBreakpointIds,
+ selectedBreakpointId,
+ ]);
+ for (const [property, cascaded] of Object.entries(cascadedStyle)) {
+ if (cascaded !== undefined && inheritableProperties.has(property)) {
+ inheritedStyle[property as StyleProperty] = {
+ instanceId: ancestorInstance.id,
+ value: cascaded.value,
+ };
+ }
+ }
+ }
+ return inheritedStyle;
+};
+
+/**
+ * combine all local, cascaded, inherited and browser styles
+ */
export const useStyleInfo = ({
localStyle,
browserStyle,
@@ -30,24 +155,67 @@ export const useStyleInfo = ({
localStyle: Style;
browserStyle?: Style;
}) => {
+ const [breakpoints] = useBreakpoints();
+ const [selectedBreakpoint] = useSelectedBreakpoint();
+ const selectedBreakpointId = selectedBreakpoint?.id;
+ const [selectedInstanceData] = useSelectedInstanceData();
+ const selectedInstanceId = selectedInstanceData?.id;
+ const selectedInstanceCssRules = selectedInstanceData?.cssRules;
+ const [rootInstance] = useRootInstance();
+
+ const cascadedBreakpointIds = useMemo(
+ () => getCascadedBreakpointIds(breakpoints, selectedBreakpointId),
+ [breakpoints, selectedBreakpointId]
+ );
+
+ const inheritedInfo = useMemo(() => {
+ if (
+ rootInstance === undefined ||
+ selectedBreakpointId === undefined ||
+ selectedInstanceId === undefined
+ ) {
+ return {};
+ }
+ return getInheritedInfo(
+ rootInstance,
+ selectedInstanceId,
+ cascadedBreakpointIds,
+ selectedBreakpointId
+ );
+ }, [
+ rootInstance,
+ cascadedBreakpointIds,
+ selectedBreakpointId,
+ selectedInstanceId,
+ ]);
+
+ const cascadedInfo = useMemo(() => {
+ if (selectedInstanceCssRules === undefined) {
+ return {};
+ }
+ return getCascadedInfo(selectedInstanceCssRules, cascadedBreakpointIds);
+ }, [selectedInstanceCssRules, cascadedBreakpointIds]);
+
const styleInfoData = useMemo(() => {
const styleInfoData: StyleInfo = {};
- // temporary solution until we start computing all styles from data
- if (browserStyle) {
- for (const [property, value] of Object.entries(browserStyle)) {
- styleInfoData[property as StyleProperty] = {
+ for (const property of styleProperties) {
+ // temporary solution until we start computing all styles from data
+ const computed = browserStyle?.[property];
+ const inherited = inheritedInfo[property];
+ const cascaded = cascadedInfo[property];
+ const local = localStyle[property];
+ const value = local ?? cascaded?.value ?? inherited?.value ?? computed;
+ if (value) {
+ styleInfoData[property] = {
value,
+ local,
+ cascaded,
+ inherited,
};
}
}
- for (const [property, value] of Object.entries(localStyle)) {
- styleInfoData[property as StyleProperty] = {
- value,
- local: value,
- };
- }
return styleInfoData;
- }, [browserStyle, localStyle]);
+ }, [browserStyle, inheritedInfo, cascadedInfo, localStyle]);
return styleInfoData;
};