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.

Use normalized styles in style panel (#773)

Bogdan Chadkin145155f12e37
diff --git a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
index 066814a7d3..5fedf1da61 100644
--- a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
+++ b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
@@ -132,7 +132,6 @@ export const SelectedInstanceConnector = ({
payload: {
id: instance.id,
component: instance.component,
- cssRules: instance.cssRules,
browserStyle: getBrowserStyle(element),
props: instanceProps,
},
diff --git a/apps/designer/app/designer/features/props-panel/props-panel.stories.tsx b/apps/designer/app/designer/features/props-panel/props-panel.stories.tsx
index 30f73e6cc9..d078d456ee 100644
--- a/apps/designer/app/designer/features/props-panel/props-panel.stories.tsx
+++ b/apps/designer/app/designer/features/props-panel/props-panel.stories.tsx
@@ -12,7 +12,6 @@ export const NoProps: ComponentStoryObj<typeof PropsPanel> = {
selectedInstanceData: {
id: "1",
component: "Button",
- cssRules: [],
browserStyle: {},
props: {
id: "1",
@@ -36,7 +35,6 @@ export const RequiredProps: ComponentStoryObj<typeof PropsPanel> = {
selectedInstanceData: {
id: "1",
component: "Link",
- cssRules: [],
browserStyle: {},
props: {
id: "1",
@@ -53,7 +51,6 @@ export const DefaultProps: ComponentStoryObj<typeof PropsPanel> = {
selectedInstanceData: {
id: "1",
component: "Button",
- cssRules: [],
browserStyle: {},
props: {
id: "1",
@@ -72,7 +69,6 @@ export const AllProps: ComponentStoryObj<typeof PropsPanel> = {
selectedInstanceData: {
id: "3",
component: "Heading",
- cssRules: [],
browserStyle: {},
props: {
id: "2",
diff --git a/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts b/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
index 09d0821efe..5265f3ec6e 100644
--- a/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
+++ b/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
@@ -16,7 +16,6 @@ const getSelectedInstanceData = (
return {
id: nanoid(8),
component: componentName,
- cssRules: [],
browserStyle: {},
props: {
id: nanoid(8),
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
index 4364690e20..ba529fffa4 100644
--- 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
@@ -1,5 +1,5 @@
-import type { Breakpoint, CssRule } from "@webstudio-is/css-data";
-import type { Instance } from "@webstudio-is/react-sdk";
+import type { Breakpoint } from "@webstudio-is/css-data";
+import type { Instance, Styles } from "@webstudio-is/react-sdk";
import {
getCascadedBreakpointIds,
getCascadedInfo,
@@ -30,37 +30,43 @@ const breakpoints: Breakpoint[] = [
];
const selectedBreakpointId = "3";
+const selectedInstanceId = "3";
const cascadedBreakpointIds = getCascadedBreakpointIds(
breakpoints,
selectedBreakpointId
);
-const cssRules: CssRule[] = [
+const cascadingStyles: Styles = [
{
- breakpoint: "1",
- style: {
- width: { type: "unit", value: 100, unit: "px" },
- height: { type: "unit", value: 50, unit: "px" },
- },
+ breakpointId: "1",
+ instanceId: selectedInstanceId,
+ property: "width",
+ value: { type: "unit", value: 100, unit: "px" },
},
{
- breakpoint: "2",
- style: {
- width: { type: "unit", value: 200, unit: "px" },
- },
+ breakpointId: "1",
+ instanceId: selectedInstanceId,
+ property: "height",
+ value: { type: "unit", value: 50, unit: "px" },
},
{
- breakpoint: "3",
- style: {
- // should not be computed because current breakpoint
- height: { type: "unit", value: 150, unit: "px" },
- },
+ breakpointId: "2",
+ instanceId: selectedInstanceId,
+ property: "width",
+ value: { type: "unit", value: 200, unit: "px" },
},
{
- breakpoint: "4",
- style: {
- width: { type: "unit", value: 400, unit: "px" },
- },
+ breakpointId: "3",
+ instanceId: selectedInstanceId,
+ // should not be computed because current breakpoint
+ property: "height",
+ value: { type: "unit", value: 150, unit: "px" },
+ },
+ {
+ breakpointId: "4",
+ instanceId: selectedInstanceId,
+ property: "width",
+ value: { type: "unit", value: 400, unit: "px" },
},
];
@@ -68,56 +74,63 @@ 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" },
- },
- },
- ],
+ cssRules: [],
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" },
- },
- },
- ],
+ cssRules: [],
children: [
{
type: "instance",
id: "3",
component: "Box",
- cssRules: [
- {
- breakpoint: "3",
- style: {
- // should not show selected style as inherited
- fontWeight: { type: "keyword", value: "500" },
- },
- },
- ],
+ cssRules: [],
children: [],
},
],
},
],
};
-const selectedInstanceId = "3";
+
+const inheritingStyles: Styles = [
+ // should be inherited even from another breakpoint
+ {
+ breakpointId: "1",
+ instanceId: "1",
+ property: "fontSize",
+ value: { type: "unit", value: 20, unit: "px" },
+ },
+
+ // should not be inherited because width is not inheritable
+ {
+ breakpointId: "3",
+ instanceId: "2",
+ property: "width",
+ value: { type: "unit", value: 100, unit: "px" },
+ },
+ // should be inherited from selected breakpoint
+ {
+ breakpointId: "3",
+ instanceId: "2",
+ property: "fontWeight",
+ value: { type: "keyword", value: "600" },
+ },
+
+ // should not show selected style as inherited
+ {
+ breakpointId: "3",
+ instanceId: "3",
+ property: "fontWeight",
+ value: { type: "keyword", value: "500" },
+ },
+];
test("compute cascaded styles", () => {
- expect(getCascadedInfo(cssRules, cascadedBreakpointIds))
- .toMatchInlineSnapshot(`
+ expect(
+ getCascadedInfo(cascadingStyles, selectedInstanceId, cascadedBreakpointIds)
+ ).toMatchInlineSnapshot(`
{
"height": {
"breakpointId": "1",
@@ -143,6 +156,7 @@ test("compute inherited styles", () => {
expect(
getInheritedInfo(
rootInstance,
+ inheritingStyles,
selectedInstanceId,
cascadedBreakpointIds,
selectedBreakpointId
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 790696beab..f0462956be 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,15 +1,18 @@
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 type { Instance, Styles } from "@webstudio-is/react-sdk";
+import {
+ useBreakpoints,
+ useRootInstance,
+ useStyles,
+} from "~/shared/nano-states";
import {
useSelectedBreakpoint,
useSelectedInstanceData,
@@ -72,6 +75,23 @@ for (const [property, value] of Object.entries(properties)) {
}
}
+export const getLocalStyle = (
+ styles: Styles,
+ breakpointId: string,
+ instanceId: string
+) => {
+ const style: Style = {};
+ for (const styleItem of styles) {
+ if (
+ styleItem.breakpointId === breakpointId &&
+ styleItem.instanceId === instanceId
+ ) {
+ style[styleItem.property] = styleItem.value;
+ }
+ }
+ return style;
+};
+
/**
* find all breakpoints which may affect current view
*/
@@ -95,23 +115,20 @@ export const getCascadedBreakpointIds = (
* affecting current view
*/
export const getCascadedInfo = (
- cssRules: CssRule[],
+ styles: Styles,
+ instanceId: string,
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] = {
+ for (const styleItem of styles) {
+ if (
+ styleItem.breakpointId === breakpointId &&
+ styleItem.instanceId === instanceId
+ ) {
+ cascadedStyle[styleItem.property] = {
breakpointId,
- value,
+ value: styleItem.value,
};
}
}
@@ -125,6 +142,7 @@ export const getCascadedInfo = (
*/
export const getInheritedInfo = (
rootInstance: Instance,
+ styles: Styles,
instanceId: string,
cascadedBreakpointIds: string[],
selectedBreakpointId: string
@@ -136,16 +154,24 @@ export const getInheritedInfo = (
if (ancestorInstance.id === instanceId) {
continue;
}
- const cascadedStyle = getCascadedInfo(ancestorInstance.cssRules, [
+ const cascadedAndSelectedBreakpointIds = [
...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,
- };
+ ];
+
+ // extract styles from all active breakpoints
+ for (const breakpointId of cascadedAndSelectedBreakpointIds) {
+ for (const styleItem of styles) {
+ if (
+ styleItem.breakpointId === breakpointId &&
+ styleItem.instanceId === ancestorInstance.id &&
+ inheritableProperties.has(styleItem.property)
+ ) {
+ inheritedStyle[styleItem.property] = {
+ instanceId: ancestorInstance.id,
+ value: styleItem.value,
+ };
+ }
}
}
}
@@ -155,20 +181,25 @@ export const getInheritedInfo = (
/**
* combine all local, cascaded, inherited and browser styles
*/
-export const useStyleInfo = ({
- localStyle,
- browserStyle,
-}: {
- localStyle?: Style;
- browserStyle?: Style;
-}) => {
+export const useStyleInfo = () => {
const [breakpoints] = useBreakpoints();
const [selectedBreakpoint] = useSelectedBreakpoint();
const selectedBreakpointId = selectedBreakpoint?.id;
const [selectedInstanceData] = useSelectedInstanceData();
const selectedInstanceId = selectedInstanceData?.id;
- const selectedInstanceCssRules = selectedInstanceData?.cssRules;
+ const browserStyle = selectedInstanceData?.browserStyle;
const [rootInstance] = useRootInstance();
+ const [styles] = useStyles();
+
+ const localStyle = useMemo(() => {
+ if (
+ selectedBreakpointId === undefined ||
+ selectedInstanceId === undefined
+ ) {
+ return {};
+ }
+ return getLocalStyle(styles, selectedBreakpointId, selectedInstanceId);
+ }, [styles, selectedBreakpointId, selectedInstanceId]);
const cascadedBreakpointIds = useMemo(
() => getCascadedBreakpointIds(breakpoints, selectedBreakpointId),
@@ -185,23 +216,25 @@ export const useStyleInfo = ({
}
return getInheritedInfo(
rootInstance,
+ styles,
selectedInstanceId,
cascadedBreakpointIds,
selectedBreakpointId
);
}, [
rootInstance,
+ styles,
cascadedBreakpointIds,
selectedBreakpointId,
selectedInstanceId,
]);
const cascadedInfo = useMemo(() => {
- if (selectedInstanceCssRules === undefined) {
+ if (selectedInstanceId === undefined) {
return {};
}
- return getCascadedInfo(selectedInstanceCssRules, cascadedBreakpointIds);
- }, [selectedInstanceCssRules, cascadedBreakpointIds]);
+ return getCascadedInfo(styles, selectedInstanceId, cascadedBreakpointIds);
+ }, [styles, selectedInstanceId, cascadedBreakpointIds]);
const styleInfoData = useMemo(() => {
const styleInfoData: StyleInfo = {};
diff --git a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
index 2a0bdeb177..1ad5c084f0 100644
--- a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
@@ -1,10 +1,10 @@
-import { useMemo, useCallback } from "react";
+import { useCallback } from "react";
import store from "immerhin";
import warnOnce from "warn-once";
import type { SelectedInstanceData, StyleUpdates } from "@webstudio-is/project";
-import type { Style, StyleProperty, StyleValue } from "@webstudio-is/css-data";
+import type { StyleProperty, StyleValue } from "@webstudio-is/css-data";
import { type Publish } from "~/shared/pubsub";
-import { stylesContainer, useStyles } from "~/shared/nano-states";
+import { stylesContainer } from "~/shared/nano-states";
import { useSelectedBreakpoint } from "~/designer/shared/nano-states";
// @todo: must be removed, now it's only for compatibility with existing code
import { parseCssValue } from "./parse-css-value";
@@ -47,26 +47,8 @@ export const useStyleData = ({
publish,
}: UseStyleData) => {
const [selectedBreakpoint] = useSelectedBreakpoint();
- const selectedBreakpointId = selectedBreakpoint?.id;
- const selectedInstanceId = selectedInstanceData?.id;
- const [styles] = useStyles();
- const localStyle = useMemo(() => {
- const style: Style = {};
- for (const styleItem of styles) {
- if (
- styleItem.breakpointId === selectedBreakpointId &&
- styleItem.instanceId === selectedInstanceId
- ) {
- style[styleItem.property] = styleItem.value;
- }
- }
- return style;
- }, [styles, selectedBreakpointId, selectedInstanceId]);
- const currentStyle = useStyleInfo({
- localStyle,
- browserStyle: selectedInstanceData?.browserStyle,
- });
+ const currentStyle = useStyleInfo();
const publishUpdates = useCallback(
(type: "update" | "preview", updates: StyleUpdates["updates"]) => {
diff --git a/packages/project/src/shared/canvas-components/instance-data.ts b/packages/project/src/shared/canvas-components/instance-data.ts
index ddf6e43bc8..cf22164419 100644
--- a/packages/project/src/shared/canvas-components/instance-data.ts
+++ b/packages/project/src/shared/canvas-components/instance-data.ts
@@ -1,6 +1,5 @@
import type { Instance, InstanceProps } from "@webstudio-is/react-sdk";
import type {
- CssRule,
Style,
StyleProperty,
StyleValue,
@@ -10,7 +9,6 @@ import type {
export type SelectedInstanceData = {
id: Instance["id"];
component: Instance["component"];
- cssRules: Array<CssRule>;
browserStyle: Style;
props?: InstanceProps;
};