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.

feat: Resizable and draggable inspector dialog (#3232)

Oleg Isonen90db933b6b5d
diff --git a/apps/builder/app/builder/shared/code-editor.tsx b/apps/builder/app/builder/shared/code-editor.tsx
index bac182ea22..6043385919 100644
--- a/apps/builder/app/builder/shared/code-editor.tsx
+++ b/apps/builder/app/builder/shared/code-editor.tsx
@@ -31,7 +31,6 @@ import {
textVariants,
css,
SmallIconButton,
- rawTheme,
Dialog,
DialogTrigger,
DialogContent,
@@ -299,18 +298,17 @@ export const EditorDialog = ({
content: ReactNode;
children: ReactNode;
}) => {
- const [isExpanded, setIsExpanded] = useState(false);
- const width = isExpanded ? "80vw" : "640px";
- const height = isExpanded ? "80vh" : "480px";
- const padding = rawTheme.spacing[7];
+ const [isMaximized, setIsMaximized] = useState(false);
return (
<Dialog open={open} onOpenChange={onOpenChange} modal={false}>
<DialogTrigger asChild>{children}</DialogTrigger>
<DialogContent
- css={{
- maxWidth: "none",
- maxHeight: "none",
- }}
+ draggable
+ resize="auto"
+ width={640}
+ height={480}
+ minHeight={240}
+ isMaximized={isMaximized}
onInteractOutside={(event) => {
event.preventDefault();
}}
@@ -318,9 +316,8 @@ export const EditorDialog = ({
<Grid
align="stretch"
css={{
- padding,
- width,
- height,
+ padding: theme.spacing[7],
+ height: "100%",
overflow: "hidden",
boxSizing: "content-box",
}}
@@ -335,9 +332,9 @@ export const EditorDialog = ({
<Flex gap="1">
<Button
color="ghost"
- prefix={isExpanded ? <MinimizeIcon /> : <MaximizeIcon />}
+ prefix={isMaximized ? <MinimizeIcon /> : <MaximizeIcon />}
aria-label="Expand"
- onClick={() => setIsExpanded(isExpanded ? false : true)}
+ onClick={() => setIsMaximized(isMaximized ? false : true)}
/>
<DialogClose asChild>
<Button
diff --git a/packages/design-system/src/components/dialog.tsx b/packages/design-system/src/components/dialog.tsx
index 593d3c13a4..6d4cfefbc9 100644
--- a/packages/design-system/src/components/dialog.tsx
+++ b/packages/design-system/src/components/dialog.tsx
@@ -3,12 +3,16 @@ import {
type ComponentProps,
type Ref,
forwardRef,
+ useRef,
+ type DragEventHandler,
} from "react";
import * as Primitive from "@radix-ui/react-dialog";
import { css, theme, keyframes, type CSS } from "../stitches.config";
import { PanelTitle } from "./panel-title";
import { floatingPanelStyle, CloseButton, TitleSlot } from "./floating-panel";
import { Flex } from "./flex";
+import { useDisableCanvasPointerEvents } from "../utilities";
+import type { CSSProperties } from "@stitches/react";
export const Dialog = Primitive.Root;
export const DialogTrigger = Primitive.Trigger;
@@ -21,23 +25,137 @@ export const DialogClose = Primitive.Close;
// https://www.radix-ui.com/docs/primitives/components/dialog#description
export const DialogDescription = Primitive.Description;
+const placeholderImage =
+ typeof Image !== "undefined" ? new Image(0, 0) : undefined;
+// It's important to set the src early, because it has to be loaded by the time drag starts.
+if (placeholderImage) {
+ placeholderImage.src =
+ "data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
+}
+
+type UseDraggableProps = {
+ isDraggable?: boolean;
+ isMaximized?: boolean;
+ width?: number;
+ height?: number;
+ minWidth?: number;
+ minHeight?: number;
+};
+
+const useDraggable = ({
+ isDraggable = false,
+ isMaximized = false,
+ width,
+ height,
+ minHeight,
+ minWidth,
+}: UseDraggableProps) => {
+ const initialDataRef = useRef<{
+ point: { x: number; y: number };
+ rect: DOMRect;
+ }>();
+ const { enableCanvasPointerEvents, disableCanvasPointerEvents } =
+ useDisableCanvasPointerEvents();
+
+ const handleDragStart: DragEventHandler = (event) => {
+ disableCanvasPointerEvents();
+ if (placeholderImage) {
+ event.dataTransfer.setDragImage(placeholderImage, 0, 0);
+ }
+ const target = event.target as HTMLElement;
+ const rect = target.getBoundingClientRect();
+ target.style.left = `${rect.left}px`;
+ target.style.top = `${rect.top}px`;
+ target.style.transform = "none";
+ initialDataRef.current = {
+ point: { x: event.pageX, y: event.pageY },
+ rect,
+ };
+ };
+
+ const handleDrag: DragEventHandler = (event) => {
+ if (
+ event.pageX <= 0 ||
+ event.pageY <= 0 ||
+ initialDataRef.current === undefined
+ ) {
+ return;
+ }
+ const { rect, point } = initialDataRef.current;
+ const movementX = point.x - event.pageX;
+ const movementY = point.y - event.pageY;
+ let left = Math.max(rect.x - movementX, 0);
+ left = Math.min(left, window.innerWidth - rect.width);
+ let top = Math.max(rect.y - movementY, 0);
+ // We want some part of the dialog to be visible but otherwise let it go off screen.
+ top = Math.min(top, window.innerHeight - 40);
+ const target = event.target as HTMLElement;
+ target.style.left = `${left}px`;
+ target.style.top = `${top}px`;
+ };
+
+ const style: CSSProperties = isMaximized
+ ? {
+ ...centeredContent,
+ width: "100vw",
+ height: "100vh",
+ }
+ : {
+ width,
+ height,
+ };
+
+ if (minWidth !== undefined) {
+ style.minWidth = minWidth;
+ }
+ if (minHeight !== undefined) {
+ style.minHeight = minHeight;
+ }
+
+ return {
+ onDragStart: handleDragStart,
+ onDrag: handleDrag,
+ onDragEnd: enableCanvasPointerEvents,
+ draggable: isDraggable,
+ style,
+ };
+};
+
export const DialogContent = forwardRef(
(
{
children,
className,
css,
+ resize,
+ isDraggable,
+ isMaximized,
+ width,
+ height,
+ minWidth,
+ minHeight,
...props
- }: ComponentProps<typeof Primitive.Content> & {
- css?: CSS;
- },
+ }: ComponentProps<typeof Primitive.Content> &
+ UseDraggableProps & {
+ css?: CSS;
+ resize?: "auto";
+ },
forwardedRef: Ref<HTMLDivElement>
) => {
+ const draggableProps = useDraggable({
+ isDraggable,
+ width,
+ height,
+ minWidth,
+ minHeight,
+ isMaximized,
+ });
return (
<Primitive.Portal>
<Primitive.Overlay className={overlayStyle()} />
<Primitive.Content
- className={contentStyle({ className, css })}
+ className={contentStyle({ className, css, resize, isDraggable })}
+ {...draggableProps}
{...props}
ref={forwardedRef}
>
@@ -107,16 +225,31 @@ const contentShow = keyframes({
from: { opacity: 0, transform: "translate(-50%, -48%) scale(0.96)" },
to: { opacity: 1, transform: "translate(-50%, -50%) scale(1)" },
});
-const contentStyle = css(floatingPanelStyle, {
- position: "fixed",
+
+const centeredContent: CSSProperties = {
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
+};
+
+const contentStyle = css(floatingPanelStyle, {
+ ...centeredContent,
+ position: "fixed",
width: "min-content",
- minWidth: theme.spacing[30],
- maxWidth: theme.spacing[35],
- maxHeight: "85vh",
+ minWidth: theme.spacing[33],
+ maxWidth: "calc(100vw - 40px)",
+ maxHeight: "calc(100vh - 40px)",
+ userSelect: "none",
animation: `${contentShow} 150ms ${theme.easing.easeOut}`,
+
+ overflow: "hidden",
+ variants: {
+ resize: {
+ auto: {
+ resize: "auto",
+ },
+ },
+ },
});
const titleStyle = css({