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 perf on scrub (#1425)

Ivan Starkova4c9bb8603ed
diff --git a/apps/builder/app/builder/features/style-panel/style-panel.tsx b/apps/builder/app/builder/features/style-panel/style-panel.tsx
index e2ff3301b0..5b874fb95a 100644
--- a/apps/builder/app/builder/features/style-panel/style-panel.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-panel.tsx
@@ -10,15 +10,40 @@ import type { Publish } from "~/shared/pubsub";
import { selectedBreakpointStore } from "~/shared/nano-states";
import { useStyleData } from "./shared/use-style-data";
import { StyleSettings } from "./style-settings";
-import { useCanvasWidth } from "~/builder/shared/nano-states";
+import { canvasWidthContainer } from "~/builder/shared/nano-states";
import { StyleSourcesSection } from "./style-source-section";
import { matchMedia } from "@webstudio-is/css-engine";
+import { useEffect, useState } from "react";
type StylePanelProps = {
publish: Publish;
selectedInstance: Instance;
};
+const useMatchMedia = () => {
+ const [matched, setMatched] = useState(false);
+
+ useEffect(() => {
+ return canvasWidthContainer.subscribe((canvasWidth) => {
+ const breakpoint = selectedBreakpointStore.get();
+ if (breakpoint === undefined) {
+ return;
+ }
+ if (canvasWidth === undefined) {
+ return;
+ }
+
+ if (matchMedia(breakpoint, canvasWidth) === false) {
+ setMatched(true);
+ }
+
+ setMatched(false);
+ });
+ }, []);
+
+ return matched;
+};
+
export const StylePanel = ({ selectedInstance, publish }: StylePanelProps) => {
const { currentStyle, setProperty, deleteProperty, createBatchUpdate } =
useStyleData({
@@ -26,19 +51,19 @@ export const StylePanel = ({ selectedInstance, publish }: StylePanelProps) => {
publish,
});
+ const renderWarning = useMatchMedia();
+
const breakpoint = useStore(selectedBreakpointStore);
- const [canvasWidth] = useCanvasWidth();
if (
currentStyle === undefined ||
selectedInstance === undefined ||
- breakpoint === undefined ||
- canvasWidth === undefined
+ breakpoint === undefined
) {
return null;
}
- if (matchMedia(breakpoint, canvasWidth) === false) {
+ if (renderWarning) {
return (
<Box css={{ p: theme.spacing[5] }}>
<Card css={{ p: theme.spacing[9], mt: theme.spacing[9] }}>