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 Isonenb0d5a95b16ec
diff --cc app/canvas/features/wrapper-component/use-css.tsx
index d73722cb18,c58ee18d14..81d26db465
--- a/app/canvas/features/wrapper-component/use-css.tsx
+++ b/app/canvas/features/wrapper-component/use-css.tsx
@@@ -6,7 -6,55 +6,7 @@@ import
useSubscribe,
toValue,
} from "@webstudio-is/sdk";
- import { type StyleUpdates } from "~/shared/component";
-import { useDropData, useSelectedInstance } from "~/canvas/shared/nano-states";
-import { primitives, type StyleUpdates } from "~/shared/canvas-components";
-
-// @todo this doesn't work with the node at the top edge of the iframe, tag gets hidden.
-const componentTagStyle = {
- "&::before": {
- display: "flex",
- content: "attr(data-label)",
- padding: "0 $1",
- marginTop: "-$4",
- height: "$4",
- position: "absolute",
- backgroundColor: "$blue10",
- color: "$loContrast",
- alignItems: "center",
- justifyContent: "center",
- fontSize: "$1",
- lineHeight: 1,
- minWidth: "$6",
- },
-};
-
-const hoverOutlineStyle = {
- "&:hover": {
- outline: "1px solid $blue10",
- outlineOffset: -1,
- ...componentTagStyle,
- },
-};
-
-const emptyOutlineStyle = {
- "&:empty": {
- outline: "1px dashed #555",
- outlineOffset: -1,
- },
- ...hoverOutlineStyle,
-};
-
-const selectedOutlineStyle = {
- outline: "2px solid $blue10",
- outlineOffset: -2,
- ...componentTagStyle,
-};
-
-const dragOverOutlineStyle = {
- outline: "2px solid $blue10",
- outlineOffset: -2,
- ...componentTagStyle,
-};
++import { type StyleUpdates } from "~/shared/canvas-components";
type UseCssProps = {
instance: Instance;
diff --cc app/canvas/shared/instance.ts
index 43337a7dea,3cd81ca944..0397c69016
--- a/app/canvas/shared/instance.ts
+++ b/app/canvas/shared/instance.ts
@@@ -19,11 -19,9 +19,11 @@@ import
findInstanceById,
} from "~/shared/tree-utils";
import store from "immerhin";
- import { DropData, HoveredInstanceData, type SelectedInstanceData } from "~/shared/component";
-import { DropData, type SelectedInstanceData } from "~/shared/canvas-components";
-import { useSelectedInstance, useSelectedElement } from "./nano-states";
++import { DropData, HoveredInstanceData, type SelectedInstanceData } from "~/shared/canvas-components";
+import { useSelectedInstance, useSelectedElement, useHoveredElement, useHoveredInstance } from "./nano-states";
import { rootInstanceContainer, useRootInstance } from "~/shared/nano-states";
+import { useAfterRender } from "./use-after-render";
+import { useWindowResize } from "~/shared/dom-hooks";
export const usePopulateRootInstance = (tree: Tree) => {
const [, setRootInstance] = useRootInstance();
diff --cc app/canvas/shared/use-drag-drop-handlers.ts
index 4ed7b79127,d736543817..ac1dc67045
--- a/app/canvas/shared/use-drag-drop-handlers.ts
+++ b/app/canvas/shared/use-drag-drop-handlers.ts
@@@ -1,6 -1,7 +1,6 @@@
import { useState } from "react";
-import memoize from "lodash.memoize";
import { publish, useSubscribe } from "@webstudio-is/sdk";
- import { type DragData, type DropData } from "~/shared/component";
+ import { type DragData, type DropData } from "~/shared/canvas-components";
import { findInstanceById } from "~/shared/tree-utils";
import {
findClosestChild,
diff --cc app/designer/designer.tsx
index 6dd192eadd,2e3f26be59..3d902f8ad4
--- a/app/designer/designer.tsx
+++ b/app/designer/designer.tsx
@@@ -3,7 -3,7 +3,7 @@@ import { DndProvider } from "react-dnd"
import { HTML5Backend } from "react-dnd-html5-backend";
import { type Project, useSubscribe, usePublish } from "@webstudio-is/sdk";
import type { Config } from "~/config";
- import type { HoveredInstanceData, SelectedInstanceData } from "~/shared/component";
-import type { SelectedInstanceData } from "~/shared/canvas-components";
++import type { HoveredInstanceData, SelectedInstanceData } from "~/shared/canvas-components";
import { Box, Flex, Grid, type CSS } from "~/shared/design-system";
import interStyles from "~/shared/font-faces/inter.css";
import { SidebarLeft } from "./features/sidebar-left";
diff --cc app/designer/features/workspace/canvas-tools/outline/label.tsx
index d6ff75fdbe,0000000000..12dcb54e6f
mode 100644,000000..100644
--- a/app/designer/features/workspace/canvas-tools/outline/label.tsx
+++ b/app/designer/features/workspace/canvas-tools/outline/label.tsx
@@@ -1,89 -1,0 +1,89 @@@
+import { useCallback, useState } from "react";
+import { styled } from "~/shared/design-system";
- import { primitives } from "~/shared/component";
++import { primitives } from "~/shared/canvas-components";
+import { type Instance } from "@webstudio-is/sdk";
+
+type LabelPosition = "top" | "inside" | "bottom";
+type LabelRefCallback = (element: HTMLElement | null) => void;
+
+/**
+ * Detects if there is no space on top and for the label and tells to show it inside.
+ * - if there is enough space for the label on top of the instance - top
+ * - else if instance height is more than 250px - bottom
+ * - else inside-top - last resort because it covers a bit of the instance content
+ */
+const useLabelPosition = (instanceRect: DOMRect): [LabelRefCallback, LabelPosition] => {
+ const [position, setPosition] = useState<LabelPosition>("top");
+
+ const ref = useCallback(
+ (element) => {
+ if (element === null || instanceRect === undefined) return;
+ const labelRect = element.getBoundingClientRect();
+ let nextPosition: LabelPosition = "top";
+ if (labelRect.height > instanceRect.top) {
+ nextPosition = instanceRect.height < 250 ? "bottom" : "inside";
+ }
+ setPosition(nextPosition);
+ },
+ [instanceRect]
+ );
+
+ return [ref, position];
+};
+
+const LabelContainer = 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: {
+ top: {
+ top: "-$4",
+ },
+ inside: {
+ top: 0,
+ },
+ bottom: {
+ bottom: "-$4",
+ },
+ },
+ },
+ }
+);
+
+type LabelProps = {
+ component: Instance['component'], instanceRect: DOMRect
+}
+
+export const Label = ({component, instanceRect}: LabelProps) => {
+ const [labelRef, position] = useLabelPosition(instanceRect);
+ const primitive = primitives[component];
+ const { Icon } = primitive;
+ return (
+ <LabelContainer state="selected" position={position} ref={labelRef}>
+ <Icon width="1em" height="1em" />
+ {primitive.label}
+ </LabelContainer>
+ );
+};
diff --cc app/designer/shared/nano-states/index.ts
index cef64d1892,d78fc692aa..f19f7b4f75
--- a/app/designer/shared/nano-states/index.ts
+++ b/app/designer/shared/nano-states/index.ts
@@@ -1,6 -1,6 +1,6 @@@
import { createValueContainer, useValue } from "react-nano-state";
import { type Breakpoint } from "@webstudio-is/sdk";
- import { type SelectedInstanceData, type HoveredInstanceData } from "~/shared/component";
-import { SelectedInstanceData } from "~/shared/canvas-components";
++import { type SelectedInstanceData, type HoveredInstanceData } from "~/shared/canvas-components";
import { type SyncStatus } from "~/shared/sync";
const selectedInstanceDataContainer = createValueContainer<