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: Speedup resize, part 1 (#4543)

Ivan Starkovcd4041e0d1ab
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index 2d0f853657..baf0cfd01f 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useState, type ReactNode } from "react";
+import { useEffect, useMemo, useState, type ReactNode } from "react";
import { useStore } from "@nanostores/react";
import { TooltipProvider } from "@radix-ui/react-tooltip";
import { usePublish, $publisher } from "~/shared/pubsub";
@@ -20,7 +20,6 @@ import { Footer } from "./features/footer";
import {
CanvasIframe,
CanvasToolsContainer,
- useReadCanvasRect,
Workspace,
} from "./features/workspace";
import {
@@ -300,19 +299,20 @@ export const Builder = ({
const isDesignMode = useStore($isDesignMode);
const isContentMode = useStore($isContentMode);
- const { onRef: onRefReadCanvas, onTransitionEnd } = useReadCanvasRect();
-
useSetWindowTitle();
- const iframeRefCallback = mergeRefs((element: HTMLIFrameElement | null) => {
- if (element?.contentWindow) {
- // added to iframe window and stored in local variable right away to prevent
- // overriding in emebedded scripts on canvas
- element.contentWindow.__webstudioSharedSyncEmitter__ =
- builderClient.emitter;
- }
- onRefReadCanvas(element);
- }, publishRef);
+ const iframeRefCallback = useMemo(
+ () =>
+ mergeRefs((element: HTMLIFrameElement | null) => {
+ if (element?.contentWindow) {
+ // added to iframe window and stored in local variable right away to prevent
+ // overriding in emebedded scripts on canvas
+ element.contentWindow.__webstudioSharedSyncEmitter__ =
+ builderClient.emitter;
+ }
+ }, publishRef),
+ [publishRef]
+ );
const { navigatorLayout } = useStore($settings);
const dataLoadingState = useStore($dataLoadingState);
@@ -369,7 +369,7 @@ export const Builder = ({
>
<ProjectSettings />
<Main>
- <Workspace onTransitionEnd={onTransitionEnd}>
+ <Workspace>
{dataLoadingState === "loaded" && (
<CanvasIframe
ref={iframeRefCallback}
diff --git a/apps/builder/app/builder/features/workspace/canvas-iframe.tsx b/apps/builder/app/builder/features/workspace/canvas-iframe.tsx
index 63e1a17c27..3b0b770b93 100644
--- a/apps/builder/app/builder/features/workspace/canvas-iframe.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-iframe.tsx
@@ -1,10 +1,18 @@
-import { forwardRef } from "react";
+import { forwardRef, useMemo, useRef, type RefObject } from "react";
import {
css,
canvasPointerEventsPropertyName,
} from "@webstudio-is/design-system";
import { useUnmount } from "~/shared/hook-utils/use-mount";
import { $canvasIframeState } from "~/shared/nano-states";
+import { useCallback, useEffect, useState } from "react";
+import {
+ $scale,
+ $canvasWidth,
+ $canvasRect,
+} from "~/builder/shared/nano-states";
+import { useWindowResizeDebounced } from "~/shared/dom-hooks";
+import { mergeRefs } from "@react-aria/utils";
const iframeStyle = css({
border: "none",
@@ -16,20 +24,72 @@ const iframeStyle = css({
type CanvasIframeProps = JSX.IntrinsicElements["iframe"];
+const CanvasRectUpdater = ({
+ iframeRef,
+}: {
+ iframeRef: RefObject<HTMLIFrameElement>;
+}) => {
+ const [updateCallback, setUpdateCallback] = useState<
+ undefined | (() => void)
+ >(undefined);
+
+ useEffect(() => {
+ updateCallback?.();
+ }, [updateCallback]);
+
+ const updateRect = useCallback(() => {
+ // create new function to trigger effect
+ const task = () => {
+ if (iframeRef.current === null) {
+ return;
+ }
+
+ const rect = iframeRef.current.getBoundingClientRect();
+ $canvasRect.set(rect);
+ };
+
+ setUpdateCallback(() => task);
+ }, [iframeRef]);
+
+ useEffect(() => {
+ updateRect();
+ const $scaleUnsubscribe = $scale.listen(updateRect);
+ const $canvasWidthUnsubscribe = $canvasWidth.listen(updateRect);
+
+ return () => {
+ $scaleUnsubscribe();
+ $canvasWidthUnsubscribe();
+ };
+ }, [updateRect]);
+
+ useWindowResizeDebounced(() => {
+ updateRect();
+ });
+
+ return null;
+};
+
export const CanvasIframe = forwardRef<HTMLIFrameElement, CanvasIframeProps>(
(props, ref) => {
+ const iframeRef = useRef<HTMLIFrameElement | null>(null);
+
+ const merrgedRef = useMemo(() => mergeRefs(ref, iframeRef), [ref]);
+
useUnmount(() => {
// Unmount does't work inside iframe.
$canvasIframeState.set("idle");
});
return (
- <iframe
- {...props}
- ref={ref}
- className={iframeStyle()}
- credentialless="true"
- />
+ <>
+ <iframe
+ {...props}
+ ref={merrgedRef}
+ className={iframeStyle()}
+ credentialless="true"
+ />
+ <CanvasRectUpdater iframeRef={iframeRef} />
+ </>
);
}
);
diff --git a/apps/builder/app/builder/features/workspace/index.ts b/apps/builder/app/builder/features/workspace/index.ts
index 202eb336a2..09d40f79fd 100644
--- a/apps/builder/app/builder/features/workspace/index.ts
+++ b/apps/builder/app/builder/features/workspace/index.ts
@@ -1,3 +1,2 @@
export * from "./workspace";
-export * from "./use-read-canvas-rect";
export * from "./canvas-iframe";
diff --git a/apps/builder/app/builder/features/workspace/use-read-canvas-rect.ts b/apps/builder/app/builder/features/workspace/use-read-canvas-rect.ts
deleted file mode 100644
index edba3200aa..0000000000
--- a/apps/builder/app/builder/features/workspace/use-read-canvas-rect.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import { useCallback, useEffect, useState } from "react";
-
-import {
- $scale,
- $canvasWidth,
- $canvasRect,
-} from "~/builder/shared/nano-states";
-import { useWindowResizeDebounced } from "~/shared/dom-hooks";
-
-/**
- * Reads the canvas iframe dom rect and puts it into nano state
- * so that this is the only place we do it.
- */
-export const useReadCanvasRect = () => {
- const [iframeElement, setIframeElement] = useState<HTMLIFrameElement | null>(
- null
- );
-
- // react updates dom asynchronously so here schedule rect
- // computing into effect when react done with dom
- const [updateCallback, setUpdateCallback] = useState<
- undefined | (() => void)
- >(undefined);
- useEffect(() => {
- updateCallback?.();
- }, [updateCallback]);
-
- const updateRect = useCallback(() => {
- if (iframeElement === null) {
- return;
- }
- // create new function to trigger effect
- const task = () => {
- const rect = iframeElement.getBoundingClientRect();
- $canvasRect.set(rect);
- };
- setUpdateCallback(() => task);
- }, [iframeElement]);
-
- useEffect(() => {
- updateRect();
- const $scaleUnsubscribe = $scale.listen(updateRect);
- const $canvasWidthUnsubscribe = $canvasWidth.listen(updateRect);
-
- return () => {
- $scaleUnsubscribe();
- $canvasWidthUnsubscribe();
- };
- }, [updateRect]);
-
- useWindowResizeDebounced(() => {
- updateRect();
- });
-
- return {
- onRef: setIframeElement,
- onTransitionEnd: updateRect,
- };
-};
diff --git a/apps/builder/app/builder/features/workspace/workspace.tsx b/apps/builder/app/builder/features/workspace/workspace.tsx
index 958c5e1266..3a5625dc72 100644
--- a/apps/builder/app/builder/features/workspace/workspace.tsx
+++ b/apps/builder/app/builder/features/workspace/workspace.tsx
@@ -92,10 +92,9 @@ const useOutlineStyle = () => {
type WorkspaceProps = {
children: ReactNode;
- onTransitionEnd: () => void;
};
-export const Workspace = ({ children, onTransitionEnd }: WorkspaceProps) => {
+export const Workspace = ({ children }: WorkspaceProps) => {
const canvasStyle = useCanvasStyle();
const workspaceRef = useMeasureWorkspace();
useSetCanvasWidth();
@@ -112,11 +111,7 @@ export const Workspace = ({ children, onTransitionEnd }: WorkspaceProps) => {
onClick={handleWorkspaceClick}
ref={workspaceRef}
>
- <div
- className={canvasContainerStyle()}
- style={canvasStyle}
- onTransitionEnd={onTransitionEnd}
- >
+ <div className={canvasContainerStyle()} style={canvasStyle}>
{children}
</div>
<div