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.

unset custom var when final value arrives

Oleg Isonen8a80fb40a85c
diff --git a/apps/designer/app/canvas/features/wrapper-component/use-css.tsx b/apps/designer/app/canvas/features/wrapper-component/use-css.tsx
index 3e62f710e9..cc1e519cfa 100644
--- a/apps/designer/app/canvas/features/wrapper-component/use-css.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/use-css.tsx
@@ -1,18 +1,10 @@
import { useMemo } from "react";
-import {
- type Instance,
- css as createCss,
- type CSS,
-} from "@webstudio-is/react-sdk";
-
-type UseCssProps = {
- css: CSS;
-};
+import { css as createCss, type CSS } from "@webstudio-is/react-sdk";
const voidElements =
"area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr";
-const defaultStyle = {
+const defaultStyle: CSS = {
"&": {
// When double clicking into an element to edit text, it should not select the word.
userSelect: "none",
@@ -35,6 +27,6 @@ const defaultStyle = {
},
};
-export const useCss = ({ css }: UseCssProps): string => {
+export const useCss = (css: CSS): string => {
return useMemo(() => createCss(css)(defaultStyle), [css]);
};
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 f4fdfcd845..6cf687af22 100644
--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -52,7 +52,7 @@ export const WrapperComponentDev = ({
onChangeChildren = noop,
...rest
}: WrapperComponentDevProps) => {
- const className = useCss({ css });
+ const className = useCss(css);
const [editingInstanceId] = useTextEditingInstanceId();
const [, setSelectedElement] = useSelectedElement();
diff --git a/apps/designer/app/canvas/shared/style.ts b/apps/designer/app/canvas/shared/style.ts
index 9e805848f8..c6c016e9bf 100644
--- a/apps/designer/app/canvas/shared/style.ts
+++ b/apps/designer/app/canvas/shared/style.ts
@@ -3,7 +3,20 @@ import { useSubscribe } from "~/shared/pubsub";
import { setInstanceStyleMutable } from "~/shared/tree-utils";
import { useSelectedInstance } from "./nano-states";
import { rootInstanceContainer } from "~/shared/nano-states";
-import { toValue, toVarNamespace } from "@webstudio-is/react-sdk";
+import {
+ type StyleValue,
+ toValue,
+ toVarNamespace,
+} from "@webstudio-is/react-sdk";
+
+const setCssVar = (id: string, property: string, value?: StyleValue) => {
+ const customProperty = `--${toVarNamespace(id, property)}`;
+ if (value === undefined) {
+ document.body.style.removeProperty(customProperty);
+ return;
+ }
+ document.body.style.setProperty(customProperty, toValue(value));
+};
export const useUpdateStyle = () => {
const [selectedInstance] = useSelectedInstance();
@@ -12,6 +25,10 @@ export const useUpdateStyle = () => {
// It can potentially happen that we selected a difference instance right after we changed the style in style panel.
if (id !== selectedInstance?.id) return;
+ for (const update of updates) {
+ setCssVar(id, update.property, undefined);
+ }
+
store.createTransaction([rootInstanceContainer], (rootInstance) => {
if (rootInstance === undefined) {
return;
@@ -24,12 +41,7 @@ export const useUpdateStyle = () => {
export const usePreviewStyle = () => {
useSubscribe("previewStyle", ({ id, updates }) => {
for (const update of updates) {
- const property = `--${toVarNamespace(id, update.property)}`;
- if (update.value === undefined) {
- document.body.style.removeProperty(property);
- continue;
- }
- document.body.style.setProperty(property, toValue(update.value));
+ setCssVar(id, update.property, update.value);
}
});
};