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.

Canvas resize handles (#1449)

Oleg Isonenfd979b73d3dd
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoints-settings.tsx b/apps/builder/app/builder/features/breakpoints/breakpoints-settings.tsx
index d247676292..b39befbfdd 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoints-settings.tsx
+++ b/apps/builder/app/builder/features/breakpoints/breakpoints-settings.tsx
@@ -27,7 +27,7 @@ import {
selectedBreakpointStore,
} from "~/shared/nano-states";
import { isFeatureEnabled } from "@webstudio-is/feature-flags";
-import { groupBreakpoints } from "~/shared/breakpoints";
+import { groupBreakpoints, minCanvasWidth } from "~/shared/breakpoints";
import { scaleStore } from "~/builder/shared/nano-states";
export const BreakpointsSettings = () => {
@@ -131,7 +131,7 @@ export const BreakpointsSettings = () => {
gap="3"
direction="row"
>
- <WidthInput />
+ <WidthInput min={minCanvasWidth} />
<Flex align="center" gap="2">
<Label>Scale</Label>
<Button color="neutral" css={{ width: theme.spacing[17] }}>
diff --git a/apps/builder/app/builder/features/breakpoints/width-input.tsx b/apps/builder/app/builder/features/breakpoints/width-input.tsx
index fdc007d6b4..aed8638e5f 100644
--- a/apps/builder/app/builder/features/breakpoints/width-input.tsx
+++ b/apps/builder/app/builder/features/breakpoints/width-input.tsx
@@ -24,29 +24,28 @@ import {
useEffect,
} from "react";
-// Doesn't make sense to allow resizing the canvas lower/higher than this.
-export const minWidth = 240;
-
const useEnhancedInput = ({
onChange,
onChangeComplete,
value,
+ min,
}: {
onChange: (value: NumericScrubValue) => void;
onChangeComplete: (value: NumericScrubValue) => void;
value: number;
+ min: number;
}) => {
const [intermediateValue, setIntermediateValue] = useState<number>();
const currentValue = intermediateValue ?? value;
const handleChange = (nextValue: number) => {
- onChange(Math.max(nextValue, minWidth));
+ onChange(Math.max(nextValue, min));
setIntermediateValue(undefined);
};
const handleChangeComplete = (nextValue: number) => {
- onChangeComplete(Math.max(nextValue, minWidth));
+ onChangeComplete(Math.max(nextValue, min));
setIntermediateValue(undefined);
};
@@ -80,7 +79,7 @@ const useEnhancedInput = ({
};
};
-export const WidthInput = () => {
+export const WidthInput = ({ min }: { min: number }) => {
const id = useId();
const [canvasWidth, setCanvasWidth] = useCanvasWidth();
const selectedBreakpoint = useStore(selectedBreakpointStore);
@@ -119,6 +118,7 @@ export const WidthInput = () => {
value: canvasWidth ?? 0,
onChange,
onChangeComplete,
+ min,
});
if (canvasWidth === undefined || selectedBreakpoint === undefined) {
diff --git a/apps/builder/app/builder/features/sidebar-left/panels/components/use-draggable.tsx b/apps/builder/app/builder/features/sidebar-left/panels/components/use-draggable.tsx
index d632ea0fac..29236b1c20 100644
--- a/apps/builder/app/builder/features/sidebar-left/panels/components/use-draggable.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/panels/components/use-draggable.tsx
@@ -21,9 +21,9 @@ import {
} from "~/shared/nano-states";
import { useSubscribe, type Publish } from "~/shared/pubsub";
import {
+ canvasRectStore,
isCanvasPointerEventsEnabledStore,
scaleStore,
- useCanvasRect,
} from "~/builder/shared/nano-states";
import { insertNewComponentInstance } from "~/shared/instance-utils";
@@ -104,7 +104,7 @@ export const useDraggable = ({
}) => {
const [dragComponent, setDragComponent] = useState<Instance["component"]>();
const [point, setPoint] = useState<Point>({ x: 0, y: 0 });
- const [canvasRect] = useCanvasRect();
+ const canvasRect = useStore(canvasRectStore);
const scale = useStore(scaleStore);
const dragHandlers = useDrag<Instance["component"]>({
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
index 83b7e4ce7b..e355e18637 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
@@ -13,6 +13,8 @@ import { useSubscribeSwitchPage } from "~/shared/pages";
import { Label } from "./outline/label";
import { Outline } from "./outline/outline";
import { useSubscribeDragAndDropState } from "./use-subscribe-drag-drop-state";
+import { ResizeHandles } from "./resize-handles";
+import { MediaBadge } from "./media-badge";
const toolsStyle = {
position: "absolute",
@@ -21,7 +23,6 @@ const toolsStyle = {
bottom: 0,
left: 0,
pointerEvents: "none",
- overflow: "hidden",
};
type CanvasToolsProps = {
@@ -29,7 +30,7 @@ type CanvasToolsProps = {
};
export const CanvasTools = ({ publish }: CanvasToolsProps) => {
- // @todo try to setup cross-frame atoms to vaoid this
+ // @todo try to setup cross-frame atoms to avoid this
useSubscribeDragAndDropState();
useSubscribeSwitchPage();
@@ -64,9 +65,11 @@ export const CanvasTools = ({ publish }: CanvasToolsProps) => {
}
return (
<Box css={toolsStyle}>
+ <MediaBadge />
<SelectedInstanceOutline />
<HoveredInstanceOutline />
<TextToolbar publish={publish} />
+ <ResizeHandles />
</Box>
);
};
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/media-badge.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/media-badge.tsx
new file mode 100644
index 0000000000..e8c7b5b90e
--- /dev/null
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/media-badge.tsx
@@ -0,0 +1,41 @@
+import { useStore } from "@nanostores/react";
+import { Flex, Text, css, theme } from "@webstudio-is/design-system";
+import { selectedBreakpointStore } from "~/shared/nano-states";
+
+const labelStyle = css({
+ position: "absolute",
+ top: "50%",
+ left: `calc(-${theme.spacing[23]})`,
+ transform: "rotate(-90deg)",
+});
+
+const badgeStyle = css({
+ background: theme.colors.backgroundTopbarHover,
+ px: theme.spacing[3],
+ py: theme.spacing[2],
+ borderRadius: theme.borderRadius[3],
+});
+
+export const MediaBadge = () => {
+ const breakpoint = useStore(selectedBreakpointStore);
+ if (breakpoint === undefined) {
+ return null;
+ }
+ const media =
+ breakpoint.maxWidth !== undefined
+ ? `@media (max-width: ${breakpoint.maxWidth}px)`
+ : breakpoint.minWidth !== undefined
+ ? `@media (min-width: ${breakpoint.minWidth}px)`
+ : undefined;
+ if (media === undefined) {
+ return null;
+ }
+ return (
+ <Flex gap="2" className={labelStyle()} align="center">
+ <Text variant="labelsTitleCase" color="contrast" className={badgeStyle()}>
+ {breakpoint.maxWidth ?? breakpoint.minWidth}
+ </Text>
+ <Text variant="labelsSentenceCase">{media}</Text>
+ </Flex>
+ );
+};
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/resize-handles.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/resize-handles.tsx
new file mode 100644
index 0000000000..668afc3923
--- /dev/null
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/resize-handles.tsx
@@ -0,0 +1,172 @@
+import { useStore } from "@nanostores/react";
+import { findApplicableMedia } from "@webstudio-is/css-engine";
+import { css, theme, useDrag } from "@webstudio-is/design-system";
+import { useState } from "react";
+import {
+ canvasRectStore,
+ canvasWidthStore,
+ isCanvasPointerEventsEnabledStore,
+} from "~/builder/shared/nano-states";
+import { minCanvasWidth } from "~/shared/breakpoints";
+import {
+ breakpointsStore,
+ isResizingCanvasStore,
+ selectedBreakpointIdStore,
+} from "~/shared/nano-states";
+
+const handleStyle = css({
+ position: "absolute",
+ top: 0,
+ width: 4,
+ bottom: 0,
+ cursor: "col-resize",
+ pointerEvents: "auto",
+ color: "transparent",
+ "&::before": {
+ position: "absolute",
+ content: '""',
+ inset: 0,
+ background: "currentColor",
+ },
+ "& svg": {
+ position: "absolute",
+ top: "50%",
+ right: 0,
+ transform: "translateX(100%)",
+ color: theme.colors.foregroundSubtle,
+ },
+ "&[data-state=hovering]": {
+ "&::before, & svg": {
+ color: theme.colors.backgroundPrimaryLight,
+ },
+ },
+ "&[data-align=left]": {
+ left: 0,
+ },
+ "&[data-align=right]": {
+ right: 0,
+ },
+ "&[data-state=dragging]::before": {
+ display: "none",
+ },
+});
+
+const handleIcon = (
+ <svg
+ width="14"
+ height="40"
+ viewBox="0 0 14 40"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ d="M0 0H10C12.2091 0 14 1.79086 14 4V36C14 38.2091 12.2091 40 10 40H0V0Z"
+ fill="currentColor"
+ />
+ <path
+ fillRule="evenodd"
+ clipRule="evenodd"
+ d="M4.5 8C4.77614 8 5 8.22386 5 8.5L5 31.5C5 31.7761 4.77614 32 4.5 32C4.22386 32 4 31.7761 4 31.5L4 8.5C4 8.22386 4.22386 8 4.5 8Z"
+ fill="#C1C8CD"
+ />
+ <path
+ fillRule="evenodd"
+ clipRule="evenodd"
+ d="M8.5 8C8.77614 8 9 8.22386 9 8.5L9 31.5C9 31.7761 8.77614 32 8.5 32C8.22386 32 8 31.7761 8 31.5L8 8.5C8 8.22386 8.22386 8 8.5 8Z"
+ fill="#C1C8CD"
+ />
+ </svg>
+);
+
+const baseDragProps = {
+ startDistanceThreashold: 0,
+ elementToData: () => true,
+ onStart() {
+ isCanvasPointerEventsEnabledStore.set(false);
+ isResizingCanvasStore.set(true);
+ },
+ onEnd() {
+ isCanvasPointerEventsEnabledStore.set(true);
+ isResizingCanvasStore.set(false);
+ },
+};
+
+const useResize = () => {
+ const isDragging = useStore(isCanvasPointerEventsEnabledStore) === false;
+ const [isHovering, setIsHovering] = useState(false);
+
+ const updateBreakpoint = (width: number) => {
+ const applicableBreakpoint = findApplicableMedia(
+ Array.from(breakpointsStore.get().values()),
+ width
+ );
+ if (applicableBreakpoint) {
+ selectedBreakpointIdStore.set(applicableBreakpoint.id);
+ }
+ };
+
+ const { rootRef: leftRef } = useDrag({
+ ...baseDragProps,
+ onMove({ x }) {
+ const rect = canvasRectStore.get();
+ if (rect) {
+ const width = Math.round(Math.max(rect.right - x, minCanvasWidth));
+ canvasWidthStore.set(width);
+ updateBreakpoint(width);
+ }
+ },
+ });
+ const { rootRef: rightRef } = useDrag({
+ ...baseDragProps,
+ onMove({ x }) {
+ const rect = canvasRectStore.get();
+ if (rect) {
+ const width = Math.max(Math.round(x - rect.left), minCanvasWidth);
+ canvasWidthStore.set(width);
+ updateBreakpoint(width);
+ }
+ },
+ });
+
+ const handleMouseEnter = () => setIsHovering(true);
+
+ const handleMouseOut = () => setIsHovering(false);
+
+ const state = isDragging ? "dragging" : isHovering ? "hovering" : "idle";
+
+ return {
+ state,
+ leftRef,
+ rightRef,
+ handleMouseEnter,
+ handleMouseOut,
+ };
+};
+
+export const ResizeHandles = () => {
+ const { state, leftRef, rightRef, handleMouseEnter, handleMouseOut } =
+ useResize();
+
+ return (
+ <>
+ <div
+ ref={leftRef}
+ onMouseEnter={handleMouseEnter}
+ onMouseOut={handleMouseOut}
+ data-state={state}
+ data-align="left"
+ className={handleStyle()}
+ />
+ <div
+ ref={rightRef}
+ onMouseEnter={handleMouseEnter}
+ onMouseOut={handleMouseOut}
+ data-state={state}
+ data-align="right"
+ className={handleStyle()}
+ >
+ {handleIcon}
+ </div>
+ </>
+ );
+};
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar.tsx
similarity index 100%
rename from apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar/text-toolbar.tsx
rename to apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar.tsx
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar/index.ts b/apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar/index.ts
deleted file mode 100644
index 3b4edfd2cd..0000000000
--- a/apps/builder/app/builder/features/workspace/canvas-tools/text-toolbar/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./text-toolbar";
diff --git a/apps/builder/app/builder/features/workspace/use-read-canvas-rect.ts b/apps/builder/app/builder/features/workspace/use-read-canvas-rect.ts
index bbac6a991f..7e4f00b47c 100644
--- a/apps/builder/app/builder/features/workspace/use-read-canvas-rect.ts
+++ b/apps/builder/app/builder/features/workspace/use-read-canvas-rect.ts
@@ -2,8 +2,8 @@ import { useCallback, useEffect, useState } from "react";
import {
scaleStore,
- canvasWidthContainer,
- canvasRectContainer,
+ canvasWidthStore,
+ canvasRectStore,
} from "~/builder/shared/nano-states";
import { useWindowResizeDebounced } from "~/shared/dom-hooks";
@@ -21,18 +21,17 @@ export const useReadCanvasRect = () => {
return;
}
const rect = iframeElement.getBoundingClientRect();
- canvasRectContainer.set(rect);
+ canvasRectStore.set(rect);
}, [iframeElement]);
useEffect(() => {
updateRect();
const scaleStoreUnsubscribe = scaleStore.listen(updateRect);
- const canvasWidthContainerUnsubscribe =
- canvasWidthContainer.listen(updateRect);
+ const canvasWidthStoreUnsubscribe = canvasWidthStore.listen(updateRect);
return () => {
scaleStoreUnsubscribe();
- canvasWidthContainerUnsubscribe();
+ canvasWidthStoreUnsubscribe();
};
}, [updateRect]);
diff --git a/apps/builder/app/builder/features/workspace/workspace.tsx b/apps/builder/app/builder/features/workspace/workspace.tsx
index f26d7fac8d..723cda8530 100644
--- a/apps/builder/app/builder/features/workspace/workspace.tsx
+++ b/apps/builder/app/builder/features/workspace/workspace.tsx
@@ -22,7 +22,6 @@ const workspaceStyle = css({
const canvasContainerStyle = css({
position: "relative",
- overflow: "hidden",
transformStyle: "preserve-3d",
transformOrigin: "0 0",
});
diff --git a/apps/builder/app/builder/shared/nano-states/index.ts b/apps/builder/app/builder/shared/nano-states/index.ts
index 66da0cdb03..4409424c96 100644
--- a/apps/builder/app/builder/shared/nano-states/index.ts
+++ b/apps/builder/app/builder/shared/nano-states/index.ts
@@ -7,18 +7,16 @@ const useValue = <T>(atom: WritableAtom<T>) => {
return [value, atom.set] as const;
};
-const isShareDialogOpenContainer = atom<boolean>(false);
-export const useIsShareDialogOpen = () => useValue(isShareDialogOpenContainer);
+const isShareDialogOpenStore = atom<boolean>(false);
+export const useIsShareDialogOpen = () => useValue(isShareDialogOpenStore);
-const isPublishDialogOpenContainer = atom<boolean>(false);
-export const useIsPublishDialogOpen = () =>
- useValue(isPublishDialogOpenContainer);
+const isPublishDialogOpenStore = atom<boolean>(false);
+export const useIsPublishDialogOpen = () => useValue(isPublishDialogOpenStore);
-export const canvasWidthContainer = atom<number | undefined>();
-export const useCanvasWidth = () => useValue(canvasWidthContainer);
+export const canvasWidthStore = atom<number | undefined>();
+export const useCanvasWidth = () => useValue(canvasWidthStore);
-export const canvasRectContainer = atom<DOMRect | undefined>();
-export const useCanvasRect = () => useValue(canvasRectContainer);
+export const canvasRectStore = atom<DOMRect | undefined>();
export const projectContainer = atom<Project | undefined>();
export const useProject = () => useValue(projectContainer);
@@ -28,7 +26,7 @@ export const isCanvasPointerEventsEnabledStore = atom<boolean>(true);
export const workspaceRectStore = atom<DOMRect | undefined>();
export const scaleStore = computed(
- [canvasWidthContainer, workspaceRectStore],
+ [canvasWidthStore, workspaceRectStore],
(canvasWidth, workspaceRect) => {
if (
canvasWidth === undefined ||
diff --git a/apps/builder/app/shared/breakpoints/constants.ts b/apps/builder/app/shared/breakpoints/constants.ts
new file mode 100644
index 0000000000..0ee84b4ccd
--- /dev/null
+++ b/apps/builder/app/shared/breakpoints/constants.ts
@@ -0,0 +1,2 @@
+// Doesn't make sense to allow resizing the canvas lower than this.
+export const minCanvasWidth = 240;
diff --git a/apps/builder/app/shared/breakpoints/index.ts b/apps/builder/app/shared/breakpoints/index.ts
index fa3dc47a41..c278183194 100644
--- a/apps/builder/app/shared/breakpoints/index.ts
+++ b/apps/builder/app/shared/breakpoints/index.ts
@@ -1,3 +1,4 @@
export * from "./is-base-breakpoint";
export * from "./select-breakpoint-by-order";
export * from "./group-breakpoints";
+export * from "./constants";
diff --git a/packages/design-system/src/components/__DEPRECATED__/text.tsx b/packages/design-system/src/components/__DEPRECATED__/text.tsx
index 19102d1acd..8dd05f7138 100644
--- a/packages/design-system/src/components/__DEPRECATED__/text.tsx
+++ b/packages/design-system/src/components/__DEPRECATED__/text.tsx
@@ -7,6 +7,7 @@ export const DeprecatedText = styled("div", {
margin: "0",
fontWeight: 400,
fontVariantNumeric: "tabular-nums",
+ WebkitFontSmoothing: "antialiased",
display: "block",
userSelect: "none",
diff --git a/packages/design-system/src/components/label.tsx b/packages/design-system/src/components/label.tsx
index 3682dc40e3..a1f67bb63b 100644
--- a/packages/design-system/src/components/label.tsx
+++ b/packages/design-system/src/components/label.tsx
@@ -15,6 +15,7 @@ const StyledLabel = styled(RadixLabel, {
all: "unset", // reset <button>
margin: 0,
WebkitAppearance: "none",
+ WebkitFontSmoothing: "antialiased",
display: "block",
boxSizing: "border-box",
diff --git a/packages/design-system/src/components/tabs.tsx b/packages/design-system/src/components/tabs.tsx
index e74c4fbabb..3bfba97c01 100644
--- a/packages/design-system/src/components/tabs.tsx
+++ b/packages/design-system/src/components/tabs.tsx
@@ -17,6 +17,7 @@ export const TabsTrigger = styled(TabsPrimitive.Trigger, {
display: "inline-flex",
lineHeight: 1,
fontFamily: "inherit",
+ WebkitFontSmoothing: "antialiased",
fontSize: theme.deprecatedFontSize[3],
px: theme.spacing[5],
userSelect: "none",
diff --git a/packages/design-system/src/components/text.ts b/packages/design-system/src/components/text.ts
index f04d78132c..b8c49ae52d 100644
--- a/packages/design-system/src/components/text.ts
+++ b/packages/design-system/src/components/text.ts
@@ -5,6 +5,7 @@ export { textVariants };
export const textStyle = css({
margin: 0, // in case it's used with <p>
+ WebkitFontSmoothing: "antialiased",
variants: {
variant: textVariants,
color: {
diff --git a/packages/design-system/src/components/toast.tsx b/packages/design-system/src/components/toast.tsx
index d138f9016a..14d7b51649 100644
--- a/packages/design-system/src/components/toast.tsx
+++ b/packages/design-system/src/components/toast.tsx
@@ -50,6 +50,7 @@ const StyledToast = styled(ToastPrimitive.Root, {
color: theme.colors.hiContrast,
fontWeight: 500,
fontSize: theme.deprecatedFontSize[3],
+ WebkitFontSmoothing: "antialiased",
background: theme.colors.backgroundPanel,
"@media (prefers-reduced-motion: no-preference)": {
diff --git a/packages/design-system/src/components/toggle.tsx b/packages/design-system/src/components/toggle.tsx
index cf8029dd65..214f747a3f 100644
--- a/packages/design-system/src/components/toggle.tsx
+++ b/packages/design-system/src/components/toggle.tsx
@@ -12,6 +12,7 @@ export const Toggle = styled(TogglePrimitive.Root, {
flexShrink: 0,
fontFamily: "inherit",
fontSize: theme.deprecatedFontSize[4],
+ WebkitFontSmoothing: "antialiased",
justifyContent: "center",
lineHeight: "1",
outline: "none",