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.

Hide outline when window is resizing (#688)

Bogdan Chadkinc696078796ca
diff --git a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
index 04034e2278..362f3c4276 100644
--- a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
+++ b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
@@ -2,7 +2,10 @@ import { useEffect } from "react";
import type { Instance, InstanceProps } from "@webstudio-is/react-sdk";
import { getBrowserStyle } from "@webstudio-is/react-sdk";
import { publish, subscribe, subscribeAll } from "~/shared/pubsub";
-import { subscribeScrollState } from "~/shared/dom-hooks";
+import {
+ subscribeScrollState,
+ subscribeWindowResize,
+} from "~/shared/dom-hooks";
declare module "~/shared/pubsub" {
export interface PubsubMap {
@@ -112,6 +115,15 @@ export const SelectedInstanceConnector = ({
},
});
+ const unsubscribeWindowResize = subscribeWindowResize({
+ onResizeStart() {
+ hideOutline();
+ },
+ onResizeEnd() {
+ showOutline(element);
+ },
+ });
+
// trigger style recomputing every time instance styles are changed
publish({
type: "selectInstance",
@@ -130,6 +142,7 @@ export const SelectedInstanceConnector = ({
unsubscribeTreeChange?.();
unsubscribePreviewStyle();
unsubscribeScrollState();
+ unsubscribeWindowResize();
};
// instance props may change dom element
diff --git a/apps/designer/app/shared/dom-hooks/use-window-resize.ts b/apps/designer/app/shared/dom-hooks/use-window-resize.ts
index 8c07e4a445..461a5c2098 100644
--- a/apps/designer/app/shared/dom-hooks/use-window-resize.ts
+++ b/apps/designer/app/shared/dom-hooks/use-window-resize.ts
@@ -4,15 +4,54 @@ import mitt from "mitt";
const emitter = mitt();
if (typeof window === "object") {
+ let timeoutId = 0;
+ let isResizing = false;
window.addEventListener(
"resize",
() => {
+ if (isResizing === false) {
+ emitter.emit("resizeStart");
+ }
emitter.emit("resize");
+ isResizing = true;
+ clearTimeout(timeoutId);
+ timeoutId = window.setTimeout(() => {
+ if (isResizing === false) {
+ return;
+ }
+ isResizing = false;
+ emitter.emit("resizeEnd");
+ }, 150);
},
false
);
}
+// eslint-disable-next-line @typescript-eslint/no-empty-function
+const noop = () => {};
+
+type UseWindowResize = {
+ onResizeStart?: () => void;
+ onResize?: () => void;
+ onResizeEnd?: () => void;
+};
+
+export const subscribeWindowResize = ({
+ onResizeStart = noop,
+ onResize = noop,
+ onResizeEnd = noop,
+}: UseWindowResize) => {
+ emitter.on("resizeStart", onResizeStart);
+ emitter.on("resize", onResize);
+ emitter.on("resizeEnd", onResizeEnd);
+
+ return () => {
+ emitter.off("resizeStart", onResizeStart);
+ emitter.off("resize", onResize);
+ emitter.off("resizeEnd", onResizeEnd);
+ };
+};
+
/**
* Subscribe to DOM resize event only once and then notify all listeners over js only.
* This allows us to subscribe to resize by many listeners without perf issues, since its going to be the same resize event.