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.

recalculate outline position on any pubsub event that may change elements on the canvas

Oleg Isonenb4bfc983f890
diff --git a/app/canvas/shared/use-on-render.ts b/app/canvas/shared/use-on-render.ts
new file mode 100644
index 0000000000..6b96f6235c
--- /dev/null
+++ b/app/canvas/shared/use-on-render.ts
@@ -0,0 +1,22 @@
+import { useCallback } from "react";
+import { useSubscribe } from "@webstudio-is/sdk";
+import { useSelectedElement } from "./nano-values";
+
+/**
+ * This hook lets you subscribe any rendering event we know that is triggered by pubsub.
+ * It calls the callback after the updates should have already happened.
+ */
+export const useOnRender = (callback: () => void) => {
+ const [selectedElement] = useSelectedElement();
+ const handleUpdate = useCallback(() => {
+ requestAnimationFrame(callback);
+ }, [callback]);
+ useSubscribe("updateStyle", handleUpdate);
+ useSubscribe("updateProps", handleUpdate);
+ useSubscribe(`previewStyle:${selectedElement?.id}`, handleUpdate);
+ useSubscribe("insertInstance", handleUpdate);
+ useSubscribe("reparentInstance", handleUpdate);
+ useSubscribe("deleteInstance", handleUpdate);
+ useSubscribe("breakpointChange", handleUpdate);
+ useSubscribe("breakpointDelete", handleUpdate);
+};
diff --git a/app/canvas/shared/use-outline.tsx b/app/canvas/shared/use-outline.tsx
index 2150ad491c..c8c037f7cd 100644
--- a/app/canvas/shared/use-outline.tsx
+++ b/app/canvas/shared/use-outline.tsx
@@ -1,10 +1,11 @@
-import { useMemo } from "react";
+import { useCallback, useMemo, useState } from "react";
import { createPortal } from "react-dom";
+import { type Instance } from "@webstudio-is/sdk";
import { getBoundingClientRect } from "~/shared/dom-utils";
import { primitives } from "~/shared/component";
import { useHoveredElement, useSelectedElement } from "./nano-values";
import { styled, darkTheme } from "~/shared/design-system";
-import { type Instance } from "@webstudio-is/sdk";
+import { useOnRender } from "./use-on-render";
const useElement = (currentInstance: Instance) => {
const [selectedElement] = useSelectedElement();
@@ -17,16 +18,30 @@ const useElement = (currentInstance: Instance) => {
};
const useStyle = (element?: HTMLElement) => {
+ const [rerenderFlag, forceRender] = useState(false);
+
+ // We need to recalculate the client rect the the element if any
+ // style on the page changes because we have no idea how any layout changes
+ // can impact the position or size of the outline.
+ const handleUpdate = useCallback(() => {
+ getBoundingClientRect.cache.delete(element);
+ forceRender(!rerenderFlag);
+ }, [element, rerenderFlag]);
+
+ useOnRender(handleUpdate);
+
return useMemo(() => {
- if (element === undefined) return undefined;
+ if (element === undefined) return;
+
const rect = getBoundingClientRect(element);
+
return {
top: rect.top,
left: rect.left,
width: rect.width,
height: rect.height,
};
- }, [element]);
+ }, [element, rerenderFlag]);
};
const Outline = styled("div", {