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.

Update store in style panel directly (#771)

Bogdan Chadkin9ec401f83424
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 362f3c4276..066814a7d3 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
@@ -47,10 +47,12 @@ const hideOutline = () => {
export const SelectedInstanceConnector = ({
instanceElementRef,
instance,
+ instanceStylesKey,
instanceProps,
}: {
instanceElementRef: { current: undefined | HTMLElement };
instance: Instance;
+ instanceStylesKey: unknown;
instanceProps: undefined | InstanceProps;
}) => {
useEffect(() => {
@@ -146,7 +148,7 @@ export const SelectedInstanceConnector = ({
};
// instance props may change dom element
- }, [instanceElementRef, instance, instanceProps]);
+ }, [instanceElementRef, instance, instanceStylesKey, instanceProps]);
return null;
};
diff --git a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
index 172798a15f..1b6a9cab1d 100644
--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -53,7 +53,7 @@ export const WrapperComponentDev = ({
children,
onChangeChildren,
}: WrapperComponentDevProps) => {
- useCssRules({ instanceId: instance.id });
+ const instanceStylesKey = useCssRules({ instanceId: instance.id });
const [editingInstanceId, setTextEditingInstanceId] =
useTextEditingInstanceId();
@@ -110,6 +110,7 @@ export const WrapperComponentDev = ({
<SelectedInstanceConnector
instanceElementRef={instanceElementRef}
instance={instance}
+ instanceStylesKey={instanceStylesKey}
instanceProps={instanceProps}
/>
)}
diff --git a/apps/designer/app/canvas/shared/styles.ts b/apps/designer/app/canvas/shared/styles.ts
index 3b9ac36c91..ad7b180466 100644
--- a/apps/designer/app/canvas/shared/styles.ts
+++ b/apps/designer/app/canvas/shared/styles.ts
@@ -10,7 +10,6 @@ import { useSelectedInstance } from "./nano-states";
import {
designTokensContainer,
rootInstanceContainer,
- stylesContainer,
useBreakpoints,
useDesignTokens,
usePresetStyles,
@@ -193,7 +192,7 @@ export const useCssRules = ({ instanceId }: { instanceId: string }) => {
const [styles] = useStyles();
const [breakpoints] = useBreakpoints();
- const [key, instanceStyles] = useMemo(() => {
+ const [instanceStylesKey, instanceStyles] = useMemo(() => {
const instanceStyles = styles.filter(
(item) => item.instanceId === instanceId
);
@@ -237,7 +236,9 @@ export const useCssRules = ({ instanceId }: { instanceId: string }) => {
cssEngine.render();
// run effect only when serialized instanceStyles changed
// to avoid rerendering on other instances change
- }, [instanceId, key, breakpoints]);
+ }, [instanceId, instanceStylesKey, breakpoints]);
+
+ return instanceStylesKey;
};
const toVarNamespace = (id: string, property: string) => {
@@ -272,37 +273,6 @@ const useUpdateStyle = () => {
}
utils.tree.setInstanceStyleMutable(rootInstance, id, updates, breakpoint);
});
-
- store.createTransaction([stylesContainer], (styles) => {
- const instanceId = id;
- const breakpointId = breakpoint.id;
- for (const update of updates) {
- const matchedIndex = styles.findIndex(
- (item) =>
- item.breakpointId === breakpointId &&
- item.instanceId === instanceId &&
- item.property === update.property
- );
-
- if (update.operation === "set") {
- const newItem = {
- breakpointId,
- instanceId,
- property: update.property,
- value: update.value,
- };
- if (matchedIndex === -1) {
- styles.push(newItem);
- } else {
- styles[matchedIndex] = newItem;
- }
- }
-
- if (update.operation === "delete" && matchedIndex !== -1) {
- styles.splice(matchedIndex, 1);
- }
- }
- });
});
};
diff --git a/apps/designer/app/designer/features/style-panel/shared/get-css-rule-for-breakpoint.ts b/apps/designer/app/designer/features/style-panel/shared/get-css-rule-for-breakpoint.ts
deleted file mode 100644
index f548a2bd67..0000000000
--- a/apps/designer/app/designer/features/style-panel/shared/get-css-rule-for-breakpoint.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import type { CssRule, Breakpoint } from "@webstudio-is/css-data";
-
-/**
- * Find a instance css rule that is set on the provided breakpoint.
- */
-export const getCssRuleForBreakpoint = (
- cssRules: Array<CssRule> = [],
- breakpoint?: Breakpoint
-) => {
- if (breakpoint === undefined) {
- return;
- }
- return cssRules.find((cssRule) => cssRule.breakpoint === breakpoint.id);
-};
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 c397a6dd63..2a0bdeb177 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,11 @@
-import { useState, useMemo, useEffect, useCallback } from "react";
+import { useMemo, useCallback } from "react";
+import store from "immerhin";
import warnOnce from "warn-once";
import type { SelectedInstanceData, StyleUpdates } from "@webstudio-is/project";
-import type { StyleProperty, StyleValue } from "@webstudio-is/css-data";
+import type { Style, StyleProperty, StyleValue } from "@webstudio-is/css-data";
import { type Publish } from "~/shared/pubsub";
+import { stylesContainer, useStyles } from "~/shared/nano-states";
import { useSelectedBreakpoint } from "~/designer/shared/nano-states";
-import { getCssRuleForBreakpoint } from "./get-css-rule-for-breakpoint";
// @todo: must be removed, now it's only for compatibility with existing code
import { parseCssValue } from "./parse-css-value";
import { useStyleInfo } from "./style-info";
@@ -46,26 +47,27 @@ export const useStyleData = ({
publish,
}: UseStyleData) => {
const [selectedBreakpoint] = useSelectedBreakpoint();
- const cssRule = useMemo(
- () =>
- getCssRuleForBreakpoint(
- selectedInstanceData?.cssRules,
- selectedBreakpoint
- ),
- [selectedInstanceData?.cssRules, selectedBreakpoint]
- );
-
- const [breakpointStyle, setBreakpointStyle] = useState(() => cssRule?.style);
+ 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: breakpointStyle,
+ localStyle,
browserStyle: selectedInstanceData?.browserStyle,
});
- useEffect(() => {
- setBreakpointStyle({ ...cssRule?.style });
- }, [cssRule?.style]);
-
const publishUpdates = useCallback(
(type: "update" | "preview", updates: StyleUpdates["updates"]) => {
if (
@@ -83,6 +85,41 @@ export const useStyleData = ({
breakpoint: selectedBreakpoint,
},
});
+
+ if (type !== "update") {
+ return;
+ }
+
+ store.createTransaction([stylesContainer], (styles) => {
+ const instanceId = selectedInstanceData.id;
+ const breakpointId = selectedBreakpoint.id;
+ for (const update of updates) {
+ const matchedIndex = styles.findIndex(
+ (item) =>
+ item.breakpointId === breakpointId &&
+ item.instanceId === instanceId &&
+ item.property === update.property
+ );
+
+ if (update.operation === "set") {
+ const newItem = {
+ breakpointId,
+ instanceId,
+ property: update.property,
+ value: update.value,
+ };
+ if (matchedIndex === -1) {
+ styles.push(newItem);
+ } else {
+ styles[matchedIndex] = newItem;
+ }
+ }
+
+ if (update.operation === "delete" && matchedIndex !== -1) {
+ styles.splice(matchedIndex, 1);
+ }
+ }
+ });
},
[publish, selectedBreakpoint, selectedInstanceData]
);
@@ -117,13 +154,6 @@ export const useStyleData = ({
publishUpdates(type, updates);
}
-
- if (options.isEphemeral === false) {
- setBreakpointStyle((prevStyle) => ({
- ...prevStyle,
- [property]: nextValue,
- }));
- }
};
},
[publishUpdates]
@@ -134,14 +164,6 @@ export const useStyleData = ({
const updates = [{ operation: "delete" as const, property }];
const type = options.isEphemeral ? "preview" : "update";
publishUpdates(type, updates);
-
- if (options.isEphemeral === false) {
- setBreakpointStyle((prevStyle) => {
- const nextStyle = { ...prevStyle };
- delete nextStyle[property];
- return nextStyle;
- });
- }
},
[publishUpdates]
);
@@ -180,24 +202,6 @@ export const useStyleData = ({
}
const type = options.isEphemeral ? "preview" : "update";
publishUpdates(type, updates);
-
- if (options.isEphemeral === false) {
- setBreakpointStyle((prevStyle) => {
- const nextStyle = updates.reduce(
- (reduceStyle, update) => {
- if (update.operation === "delete") {
- delete reduceStyle[update.property];
- }
- if (update.operation === "set") {
- reduceStyle[update.property] = update.value;
- }
- return reduceStyle;
- },
- { ...prevStyle }
- );
- return nextStyle;
- });
- }
updates = [];
};