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.

Merge branch 'main' into 56-better-instance-outline--outside-iframe

Oleg Isonen6c7983d841f8
diff --cc app/canvas/features/wrapper-component/use-set-hovered-instance.ts
index ecaeb7f30e,0000000000..db980f709d
mode 100644,000000..100644
--- a/app/canvas/features/wrapper-component/use-set-hovered-instance.ts
+++ b/app/canvas/features/wrapper-component/use-set-hovered-instance.ts
@@@ -1,20 -1,0 +1,20 @@@
+import { useMemo, type MouseEvent } from "react";
- import { useHoveredElement } from "~/canvas/shared/nano-values";
++import { useHoveredElement } from "~/canvas/shared/nano-states";
+
+export const useSetHoveredInstance = () => {
+ const [, setHoveredElement] = useHoveredElement();
+
+ return useMemo(
+ () => ({
+ onMouseOver(event: MouseEvent<HTMLElement>) {
+ if (event.target instanceof HTMLElement) {
+ setHoveredElement(event.target);
+ }
+ },
+ onMouseOut() {
+ setHoveredElement(undefined);
+ },
+ }),
+ [setHoveredElement]
+ );
+};
diff --cc app/canvas/shared/nano-states.ts
index f82ea1daa8,75b85f5b1d..2b1806a66d
--- a/app/canvas/shared/nano-states.ts
+++ b/app/canvas/shared/nano-states.ts
@@@ -11,22 -10,5 +10,10 @@@ export const selectedInstanceContainer
>();
export const useSelectedInstance = () => useValue(selectedInstanceContainer);
-const selectedElementContainer = createValueContainer<Element | undefined>();
+const selectedElementContainer = createValueContainer<
+ HTMLElement | undefined
+>();
export const useSelectedElement = () => useValue(selectedElementContainer);
+
- const isPreviewModeContainer = createValueContainer<boolean>(false);
- export const useIsPreviewMode = () => useValue(isPreviewModeContainer);
-
- export const rootInstanceContainer = createValueContainer<
- Instance | undefined
- >();
- export const useRootInstance = () => useValue(rootInstanceContainer);
- store.register("root", rootInstanceContainer);
-
- export const breakpointsContainer = createValueContainer<Array<Breakpoint>>([]);
- export const useBreakpoints = () => useValue(breakpointsContainer);
-
+const hoveredElementContainer = createValueContainer<HTMLElement | undefined>();
+export const useHoveredElement = () => useValue(hoveredElementContainer);
diff --cc app/canvas/shared/use-drag-drop-handlers.ts
index 37ac4cc253,3cdb88924e..e12e8d75e0
--- a/app/canvas/shared/use-drag-drop-handlers.ts
+++ b/app/canvas/shared/use-drag-drop-handlers.ts
@@@ -5,17 -6,19 +5,14 @@@ import { findInstanceById } from "~/sha
import {
findClosestChild,
findInsertionIndex,
+ getBoundingClientRect,
+ getComputedStyle,
getDragOverInfo,
} from "~/shared/dom-utils";
- import {
- useDropData,
- useRootInstance,
- useSelectedInstance,
- } from "./nano-values";
+ import { useDropData, useSelectedInstance } from "./nano-states";
+ import { useRootInstance } from "~/shared/nano-states";
//import {usePointerOutline} from './use-pointer-outline'
-// Avoid recalculating rects for each node during dragging.
-const getBoundingClientRect = memoize((element) =>
- element.getBoundingClientRect()
-);
-
-const getComputedStyle = memoize((element) => window.getComputedStyle(element));
-
export const useDragDropHandlers = () => {
const [rootInstance] = useRootInstance();
const [, setSelectedInstance] = useSelectedInstance();
diff --cc app/canvas/shared/use-outline.tsx
index 31bf7f7963,0000000000..0ec274bcae
mode 100644,000000..100644
--- a/app/canvas/shared/use-outline.tsx
+++ b/app/canvas/shared/use-outline.tsx
@@@ -1,159 -1,0 +1,159 @@@
+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 { useHoveredElement, useSelectedElement } from "./nano-states";
+import { styled, darkTheme } from "~/shared/design-system";
+import { useOnAnyUpdate } from "./use-on-any-update";
+import { useWindowResize } from "~/shared/dom-hooks";
+
+const useElement = (
+ currentInstance: Instance
+): { element: HTMLElement; type: "selected" | "hovered" } | undefined => {
+ const [selectedElement] = useSelectedElement();
+ const [hoveredElement] = useHoveredElement();
+
+ return useMemo(() => {
+ if (selectedElement?.id === currentInstance.id) {
+ return { element: selectedElement, type: "selected" };
+ }
+ if (hoveredElement?.id === currentInstance.id) {
+ return { element: hoveredElement, type: "hovered" };
+ }
+ }, [currentInstance, hoveredElement, selectedElement]);
+};
+
+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(() => {
+ if (element === undefined) return;
+ getBoundingClientRect.cache.delete(element);
+ forceRender(!rerenderFlag);
+ }, [element, rerenderFlag]);
+
+ useOnAnyUpdate(handleUpdate);
+ useWindowResize(handleUpdate);
+
+ return useMemo(
+ () => {
+ if (element === undefined) return;
+
+ const rect = getBoundingClientRect(element);
+
+ return {
+ top: rect.top,
+ left: rect.left,
+ width: rect.width,
+ height: rect.height,
+ };
+ },
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ [element, rerenderFlag]
+ );
+};
+
+/**
+ * Detects if there is no space on top and for the label and tells to show it inside.
+ */
+const useLabelPosition = () => {
+ const [position, setPosition] = useState<"outside" | "inside">("outside");
+
+ const ref = useCallback((element: HTMLElement | null) => {
+ if (element === null) return;
+ const rect = element.getBoundingClientRect();
+ const nextPosition = rect.height > rect.top ? "inside" : "outside";
+ setPosition(nextPosition);
+ }, []);
+
+ return { ref, position };
+};
+
+const Outline = styled(
+ "div",
+ {
+ position: "absolute",
+ pointerEvents: "none",
+ outline: "2px solid $blue9",
+ outlineOffset: -2,
+ },
+ {
+ variants: {
+ state: {
+ selected: {
+ zIndex: "$4",
+ },
+ hovered: {
+ zIndex: "$3",
+ },
+ },
+ },
+ }
+);
+
+const Label = styled(
+ "div",
+ {
+ position: "absolute",
+ display: "flex",
+ padding: "0 $1",
+ height: "$4",
+ color: "$hiContrast",
+ alignItems: "center",
+ justifyContent: "center",
+ gap: "$1",
+ fontSize: "$2",
+ fontFamily: "$sans",
+ lineHeight: 1,
+ minWidth: "$6",
+ },
+ {
+ variants: {
+ state: {
+ selected: {
+ backgroundColor: "$blue9",
+ },
+ hovered: {
+ color: "$blue9",
+ },
+ },
+ position: {
+ outside: {
+ top: "-$4",
+ },
+ inside: {
+ top: 0,
+ },
+ },
+ },
+ }
+);
+
+export const useOutline = (currentInstance: Instance) => {
+ const { element, type } = useElement(currentInstance) ?? {};
+ const style = useStyle(element);
+ const { ref: labelRef, position } = useLabelPosition();
+
+ const component = element?.dataset?.component;
+
+ if (component === undefined || style === undefined) {
+ return null;
+ }
+
+ const primitive = primitives[component as Instance["component"]];
+ const { Icon } = primitive;
+
+ return createPortal(
+ <Outline state={type} style={style} className={darkTheme}>
+ <Label state={type} position={position} ref={labelRef}>
+ <Icon width="1em" height="1em" />
+ {primitive.label}
+ </Label>
+ </Outline>,
+ document.body
+ );
+};