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.

Fix property reset glitches (#620)

Bogdan Chadkin984e8902ac3a
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index 197c9c2ca6..f63a29e281 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useState } from "react";
+import { useCallback, useEffect, useReducer, useState } from "react";
import {
type Instance,
type UserProp,
@@ -193,6 +193,15 @@ export const usePublishSelectedInstanceData = (treeId: Tree["id"]) => {
const [selectedElement] = useSelectedElement();
const [allUserProps] = useAllUserProps();
+ // trigger style recomputing every time
+ // new updates for this node are sent from designer
+ const [styleKey, recomputeStyles] = useReducer((d) => d + 1, 0);
+ useSubscribe("updateStyle", ({ id }) => {
+ if (id === instance?.id) {
+ recomputeStyles();
+ }
+ });
+
useEffect(() => {
// Unselects the instance by `undefined`
let payload;
@@ -213,7 +222,7 @@ export const usePublishSelectedInstanceData = (treeId: Tree["id"]) => {
type: "selectInstance",
payload,
});
- }, [instance, allUserProps, treeId, selectedElement]);
+ }, [instance, allUserProps, treeId, selectedElement, styleKey]);
};
export const usePublishHoveredInstanceData = () => {
diff --git a/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx b/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx
index e501528551..1425eaf273 100644
--- a/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx
@@ -15,11 +15,15 @@ export const ColorControl = ({
currentStyle,
inheritedStyle,
property: styleConfig.property,
- });
-
- if (value === undefined) {
- return null;
- }
+ }) ?? {
+ // provide default value to avoid control hiding
+ // when value is recomputed
+ type: "rgb" as const,
+ r: 0,
+ g: 0,
+ b: 0,
+ alpha: 0,
+ };
const setValue = setProperty(styleConfig.property);
diff --git a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx
index e5d54ef0e5..20a6b77084 100644
--- a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx
@@ -20,10 +20,6 @@ export const FontFamilyControl = ({
});
const [isOpen, setIsOpen] = useState(false);
- if (value === undefined) {
- return null;
- }
-
const setValue = setProperty(styleConfig.property);
return (
diff --git a/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsx b/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsx
index 40a82a3eb2..6d1c81c7aa 100644
--- a/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/font-weight/font-weight-control.tsx
@@ -87,14 +87,13 @@ export const FontWeightControl = ({
toValue(fontWeight)
);
- if (fontWeight === undefined) {
- return null;
- }
-
const setValue = setProperty(styleConfig.property);
return (
<Select
+ // show empty field instead of radix placeholder
+ // like css value input does
+ placeholder=""
options={labels}
// We use a weight as a value, because there are only 9 weights and they are unique.
value={selectedLabel}
diff --git a/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx b/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx
index a08b36c77d..14e21be4d3 100644
--- a/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx
@@ -16,14 +16,13 @@ export const SelectControl = ({
property: styleConfig.property,
});
- if (value === undefined) {
- return null;
- }
-
const setValue = setProperty(styleConfig.property);
return (
<Select
+ // show empty field instead of radix placeholder
+ // like css value input does
+ placeholder=""
options={styleConfig.items.map(({ label }) => label)}
value={toValue(value)}
onChange={setValue}