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: Fullscreen preview when on base breakpoint (#3276)

Oleg Isonenba1f58232428
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoints-popover-toolbar-button.tsx b/apps/builder/app/builder/features/breakpoints/breakpoints-popover-toolbar-button.tsx
index 92732e6829..27362110bb 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoints-popover-toolbar-button.tsx
+++ b/apps/builder/app/builder/features/breakpoints/breakpoints-popover-toolbar-button.tsx
@@ -1,7 +1,7 @@
import { forwardRef, type ComponentProps } from "react";
import { useStore } from "@nanostores/react";
import { Flex, theme, ToolbarButton } from "@webstudio-is/design-system";
-import { $scale, useCanvasWidth } from "~/builder/shared/nano-states";
+import { $canvasWidth, $scale } from "~/builder/shared/nano-states";
import { $selectedBreakpoint } from "~/shared/nano-states";
const Value = ({
@@ -29,7 +29,7 @@ export const BreakpointsPopoverToolbarButton = forwardRef<
>((props, ref) => {
const scale = useStore($scale);
const breakpoint = useStore($selectedBreakpoint);
- const [canvasWidth] = useCanvasWidth();
+ const canvasWidth = useStore($canvasWidth);
if (breakpoint === undefined || canvasWidth === undefined) {
return null;
}
diff --git a/apps/builder/app/builder/features/breakpoints/calc-canvas-width.test.ts b/apps/builder/app/builder/features/breakpoints/calc-canvas-width.test.ts
new file mode 100644
index 0000000000..fcc54e9218
--- /dev/null
+++ b/apps/builder/app/builder/features/breakpoints/calc-canvas-width.test.ts
@@ -0,0 +1,240 @@
+import { test, expect, describe } from "@jest/globals";
+import { calcCanvasWidth } from "./calc-canvas-width";
+
+const breakpoints = [
+ { id: "0", label: "Base" },
+ { id: "1", label: "Tablet", maxWidth: 991 },
+ { id: "2", label: "Mobile landscape", maxWidth: 767 },
+ { id: "3", label: "Mobile portrait", maxWidth: 479 },
+ { id: "4", label: "Large", minWidth: 1280 },
+ { id: "5", label: "Extra Large", minWidth: 1440 },
+];
+
+describe("base breakpoint", () => {
+ test("without canvas width", () => {
+ const workspaceWidth = 1000;
+ const breakpoints = [{ id: "0", label: "Base" }];
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ })
+ ).toStrictEqual(workspaceWidth);
+ });
+
+ test("canvas width bigger than workspace", () => {
+ const workspaceWidth = 1000;
+ const breakpoints = [{ id: "0", label: "Base" }];
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ canvasWidth: 1200,
+ })
+ ).toStrictEqual(workspaceWidth);
+ });
+
+ test("canvas width smaller than workspace", () => {
+ const workspaceWidth = 1000;
+ const breakpoints = [{ id: "0", label: "Base" }];
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ canvasWidth: 900,
+ })
+ ).toStrictEqual(workspaceWidth);
+ });
+
+ test("keep current canvas width when there is min-width breakpoint", () => {
+ const workspaceWidth = 1200;
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ canvasWidth: 1000,
+ })
+ ).toStrictEqual(1000);
+ });
+
+ test("keep a custom canvas width when there is no min-width breakpoint", () => {
+ const workspaceWidth = 1000;
+ const breakpoints = [
+ { id: "0", label: "Base" },
+ { id: "1", label: "Tablet", maxWidth: 991 },
+ ];
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[1],
+ workspaceWidth,
+ canvasWidth: 993,
+ })
+ ).toStrictEqual(993);
+ });
+
+ test("using maximum available workspace while not entering any of the breakpoints", () => {
+ const workspaceWidth = 1000;
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ })
+ ).toStrictEqual(workspaceWidth);
+ });
+
+ test("workspace is smaller than the base breakpoint range, so we need to use minimum available", () => {
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth: 500,
+ })
+ ).toStrictEqual(992);
+ });
+
+ test("min-width only", () => {
+ const workspaceWidth = 1000;
+
+ const breakpoints = [
+ { id: "0", label: "Base" },
+ { id: "4", label: "Large", minWidth: 1280 },
+ { id: "5", label: "Extra Large", minWidth: 1440 },
+ ];
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ })
+ ).toStrictEqual(workspaceWidth);
+ });
+
+ test("max-width only", () => {
+ const workspaceWidth = 1000;
+
+ const breakpoints = [
+ { id: "0", label: "Base" },
+ { id: "1", label: "Tablet", maxWidth: 991 },
+ { id: "2", label: "Mobile landscape", maxWidth: 767 },
+ { id: "3", label: "Mobile portrait", maxWidth: 479 },
+ ];
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ })
+ ).toStrictEqual(workspaceWidth);
+ });
+});
+
+describe("other breakpoints", () => {
+ test("tablet", () => {
+ const workspaceWidth = 1000;
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[1],
+ workspaceWidth,
+ })
+ ).toStrictEqual(768);
+ });
+
+ test("tablet - keep the custom width", () => {
+ const workspaceWidth = 1000;
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[1],
+ workspaceWidth,
+ canvasWidth: 993,
+ })
+ ).toStrictEqual(993);
+ });
+
+ test("mobile landscape", () => {
+ const workspaceWidth = 1000;
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[2],
+ workspaceWidth,
+ })
+ ).toStrictEqual(480);
+ });
+
+ test("mobile portrait", () => {
+ const workspaceWidth = 1000;
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[3],
+ workspaceWidth,
+ })
+ ).toStrictEqual(320);
+ });
+
+ test("large", () => {
+ const workspaceWidth = 1000;
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[4],
+ workspaceWidth,
+ })
+ ).toStrictEqual(1280);
+ });
+
+ test("extra large", () => {
+ const workspaceWidth = 1000;
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[5],
+ workspaceWidth,
+ })
+ ).toStrictEqual(1440);
+ });
+
+ test("0 min-width", () => {
+ const workspaceWidth = 1000;
+ const breakpoints = [{ id: "0", label: "x", minWidth: 0 }];
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ })
+ ).toStrictEqual(320);
+ });
+
+ test("low min-width", () => {
+ const workspaceWidth = 1000;
+ const breakpoints = [{ id: "0", label: "x", minWidth: 123 }];
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ })
+ ).toStrictEqual(123);
+ });
+
+ test("no previous max-width", () => {
+ const workspaceWidth = 1000;
+ const breakpoints = [{ id: "0", label: "x", maxWidth: 100 }];
+ expect(
+ calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint: breakpoints[0],
+ workspaceWidth,
+ })
+ ).toStrictEqual(320);
+ });
+});
diff --git a/apps/builder/app/builder/features/breakpoints/calc-canvas-width.ts b/apps/builder/app/builder/features/breakpoints/calc-canvas-width.ts
new file mode 100644
index 0000000000..a625f8124f
--- /dev/null
+++ b/apps/builder/app/builder/features/breakpoints/calc-canvas-width.ts
@@ -0,0 +1,124 @@
+import { compareMedia } from "@webstudio-is/css-engine";
+import type { Breakpoint } from "@webstudio-is/sdk";
+import {
+ groupBreakpoints,
+ isBaseBreakpoint,
+ minCanvasWidth,
+} from "~/shared/breakpoints";
+
+const defaultWidth = 320;
+
+// We want to know if user resized the canvas to a custom value.
+const isCustomCanvasWidth = (
+ breakpoints: Array<Breakpoint>,
+ selectedBreakpoint: Breakpoint,
+ canvasWidth?: number
+) => {
+ if (canvasWidth === undefined) {
+ return false;
+ }
+ const hasMinWidth = breakpoints.some(
+ (breakpoint) => breakpoint.minWidth !== undefined
+ );
+
+ // Only in case of base breakpoint and whithout min-width breakpoint, we ignore the custom width,
+ // because we want it to go full viewport width
+ if (isBaseBreakpoint(selectedBreakpoint) && hasMinWidth === false) {
+ return false;
+ }
+
+ for (const breakpoint of breakpoints) {
+ if (
+ breakpoint.minWidth !== undefined &&
+ canvasWidth === breakpoint.minWidth
+ ) {
+ return false;
+ }
+ if (
+ breakpoint.maxWidth !== undefined &&
+ canvasWidth === breakpoint.maxWidth + 1
+ ) {
+ return false;
+ }
+ }
+ return true;
+};
+
+// Find a canvas width that is within the selected breakpoint's range, but is at it's minimum.
+// Goal is to allow user to consistently know the direction they want to resize to after they switched to a breakpoint.
+// In this case they will want to always increase the width after switching.
+export const calcCanvasWidth = ({
+ breakpoints,
+ selectedBreakpoint,
+ workspaceWidth,
+ canvasWidth,
+}: {
+ breakpoints: Array<Breakpoint>;
+ selectedBreakpoint: Breakpoint;
+ workspaceWidth: number;
+ canvasWidth?: number;
+}) => {
+ // When user has resized the canvas to a custom value, we want to keep it.
+ if (isCustomCanvasWidth(breakpoints, selectedBreakpoint, canvasWidth)) {
+ return canvasWidth;
+ }
+
+ // Finding the canvas width when user selects base breakpoint is a bit more complicated.
+ // We want to find the lowest possible size that is bigger than all max breakpoints and smaller than all min breakpoints.
+ // Note: it is still possible to get intersecting min and max breakpoints.
+ if (isBaseBreakpoint(selectedBreakpoint)) {
+ // Base is the only breakpoint
+ if (breakpoints.length === 1) {
+ return workspaceWidth;
+ }
+
+ const grouped = groupBreakpoints(breakpoints).filter(
+ ({ minWidth, maxWidth }) => {
+ // We don't want to grow the canvas beyond the workspace width.
+ if (minWidth && minWidth < workspaceWidth) {
+ return true;
+ }
+ // Max width can not be smaller than the minimum canvas width.
+ if (maxWidth && maxWidth > minCanvasWidth) {
+ return true;
+ }
+ }
+ );
+ let lowestMinWidth = grouped
+ .filter(({ minWidth }) => minWidth !== undefined)
+ .at(-1)?.minWidth;
+
+ lowestMinWidth =
+ lowestMinWidth === undefined ? workspaceWidth : lowestMinWidth - 1;
+
+ let highestMaxWidth = grouped
+ .filter(({ maxWidth }) => maxWidth !== undefined)
+ .at(0)?.maxWidth;
+
+ highestMaxWidth =
+ highestMaxWidth === undefined ? minCanvasWidth : highestMaxWidth + 1;
+
+ return Math.max(lowestMinWidth, highestMaxWidth);
+ }
+
+ if (selectedBreakpoint.minWidth !== undefined) {
+ if (selectedBreakpoint.minWidth === 0) {
+ return defaultWidth;
+ }
+ return selectedBreakpoint.minWidth;
+ }
+
+ const sorted = breakpoints
+ .filter((breakpoint) => breakpoint.maxWidth !== undefined)
+ .sort(compareMedia)
+ .reverse();
+
+ const index = sorted.findIndex(
+ (breakpoint) => breakpoint.id === selectedBreakpoint.id
+ );
+ const previousBreakpointMaxWidth = sorted[index - 1]?.maxWidth;
+ if (previousBreakpointMaxWidth === undefined) {
+ return defaultWidth;
+ }
+ return previousBreakpointMaxWidth + 1;
+};
diff --git a/apps/builder/app/builder/features/breakpoints/find-initial-width.test.ts b/apps/builder/app/builder/features/breakpoints/find-initial-width.test.ts
deleted file mode 100644
index 712a492952..0000000000
--- a/apps/builder/app/builder/features/breakpoints/find-initial-width.test.ts
+++ /dev/null
@@ -1,108 +0,0 @@
-import { describe, test, expect } from "@jest/globals";
-import { findInitialWidth } from "./find-initial-width";
-
-const workspaceWidth = 1000;
-
-const breakpoints = [
- { id: "0", label: "Base" },
- { id: "1", label: "Tablet", maxWidth: 991 },
- { id: "2", label: "Mobile landscape", maxWidth: 767 },
- { id: "3", label: "Mobile portrait", maxWidth: 479 },
- { id: "4", label: "Large", minWidth: 1280 },
- { id: "5", label: "Extra Large", minWidth: 1440 },
-];
-
-describe("Find initial width", () => {
- test("base default", () => {
- expect(
- findInitialWidth(breakpoints, breakpoints[0], workspaceWidth)
- ).toStrictEqual(992);
- });
-
- test("base while workspace is small", () => {
- expect(findInitialWidth(breakpoints, breakpoints[0], 500)).toStrictEqual(
- 992
- );
- });
-
- test("base and min-width only", () => {
- const workspaceWidth = 1000;
-
- const breakpoints = [
- { id: "0", label: "Base" },
- { id: "4", label: "Large", minWidth: 1280 },
- { id: "5", label: "Extra Large", minWidth: 1440 },
- ];
- expect(
- findInitialWidth(breakpoints, breakpoints[0], workspaceWidth)
- ).toStrictEqual(workspaceWidth);
- });
-
- test("base and max-width only", () => {
- const workspaceWidth = 1000;
-
- const breakpoints = [
- { id: "0", label: "Base" },
- { id: "1", label: "Tablet", maxWidth: 991 },
- { id: "2", label: "Mobile landscape", maxWidth: 767 },
- { id: "3", label: "Mobile portrait", maxWidth: 479 },
- ];
- expect(
- findInitialWidth(breakpoints, breakpoints[0], workspaceWidth)
- ).toStrictEqual(992);
- });
-
- test("base only", () => {
- const breakpoints = [{ id: "0", label: "Base" }];
- expect(
- findInitialWidth(breakpoints, breakpoints[0], workspaceWidth)
- ).toStrictEqual(workspaceWidth);
- });
-
- test("tablet", () => {
- expect(
- findInitialWidth(breakpoints, breakpoints[1], workspaceWidth)
- ).toStrictEqual(768);
- });
- test("mobile landscape", () => {
- expect(
- findInitialWidth(breakpoints, breakpoints[2], workspaceWidth)
- ).toStrictEqual(480);
- });
- test("mobile portrait", () => {
- expect(
- findInitialWidth(breakpoints, breakpoints[3], workspaceWidth)
- ).toStrictEqual(320);
- });
- test("large", () => {
- expect(
- findInitialWidth(breakpoints, breakpoints[4], workspaceWidth)
- ).toStrictEqual(1280);
- });
- test("extra large", () => {
- expect(
- findInitialWidth(breakpoints, breakpoints[5], workspaceWidth)
- ).toStrictEqual(1440);
- });
-
- test("0 min-width", () => {
- const breakpoints = [{ id: "0", label: "x", minWidth: 0 }];
- expect(
- findInitialWidth(breakpoints, breakpoints[0], workspaceWidth)
- ).toStrictEqual(320);
- });
-
- test("low min-width", () => {
- const breakpoints = [{ id: "0", label: "x", minWidth: 123 }];
- expect(
- findInitialWidth(breakpoints, breakpoints[0], workspaceWidth)
- ).toStrictEqual(123);
- });
-
- test("no previous max-width", () => {
- const breakpoints = [{ id: "0", label: "x", maxWidth: 100 }];
- expect(
- findInitialWidth(breakpoints, breakpoints[0], workspaceWidth)
- ).toStrictEqual(320);
- });
-});
diff --git a/apps/builder/app/builder/features/breakpoints/find-initial-width.ts b/apps/builder/app/builder/features/breakpoints/find-initial-width.ts
deleted file mode 100644
index eea1edbe17..0000000000
--- a/apps/builder/app/builder/features/breakpoints/find-initial-width.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-import { compareMedia } from "@webstudio-is/css-engine";
-import type { Breakpoint } from "@webstudio-is/sdk";
-import { groupBreakpoints, isBaseBreakpoint } from "~/shared/breakpoints";
-
-const defaultWidth = 320;
-
-// Find a canvas width that is within the selected breakpoint's range, but is at it's minimum.
-// Goal is to allow user to consistently know the direction they want to resize to after they switched to a breakpoint.
-// In this case they will want to always increase the width after switching.
-export const findInitialWidth = (
- breakpoints: Array<Breakpoint>,
- selectedBreakpoint: Breakpoint,
- workspaceWidth: number
-) => {
- // Finding the canvas width when user selects base breakpoint is a bit more complicated.
- // We want to find the lowest possible size that is bigger than all max breakpoints and smaller than all min breakpoints.
- // Note: it is still possible to get intersecting min and max breakpoints.
- if (isBaseBreakpoint(selectedBreakpoint)) {
- // Base is the only breakpoint
- if (breakpoints.length === 1) {
- return workspaceWidth;
- }
- const grouped = groupBreakpoints(breakpoints);
- const baseIndex = grouped.findIndex(
- ({ id }) => selectedBreakpoint.id === id
- );
- const next = grouped[baseIndex + 1];
- if (next?.maxWidth !== undefined) {
- return Math.max(next.maxWidth + 1, defaultWidth);
- }
- const prev = grouped[baseIndex - 1];
- if (prev?.minWidth !== undefined) {
- if (prev.minWidth < workspaceWidth) {
- return Math.max(prev.minWidth - 1, defaultWidth);
- }
- return workspaceWidth;
- }
- }
-
- if (selectedBreakpoint.minWidth !== undefined) {
- if (selectedBreakpoint.minWidth === 0) {
- return defaultWidth;
- }
- return selectedBreakpoint.minWidth;
- }
-
- const sorted = breakpoints
- .filter((breakpoint) => breakpoint.maxWidth !== undefined)
- .sort(compareMedia)
- .reverse();
-
- const index = sorted.findIndex(
- (breakpoint) => breakpoint.id === selectedBreakpoint.id
- );
- const previousBreakpointMaxWidth = sorted[index - 1]?.maxWidth;
- if (previousBreakpointMaxWidth === undefined) {
- return defaultWidth;
- }
- return previousBreakpointMaxWidth + 1;
-};
diff --git a/apps/builder/app/builder/features/breakpoints/use-set-initial-canvas-width.ts b/apps/builder/app/builder/features/breakpoints/use-set-initial-canvas-width.ts
index 057978ae13..eded73d1c3 100644
--- a/apps/builder/app/builder/features/breakpoints/use-set-initial-canvas-width.ts
+++ b/apps/builder/app/builder/features/breakpoints/use-set-initial-canvas-width.ts
@@ -1,24 +1,28 @@
import { useEffect } from "react";
import type { Breakpoint } from "@webstudio-is/sdk";
import { $workspaceRect, $canvasWidth } from "~/builder/shared/nano-states";
-import { $breakpoints, $selectedBreakpoint } from "~/shared/nano-states";
-import { findInitialWidth } from "./find-initial-width";
+import {
+ $breakpoints,
+ $isPreviewMode,
+ $selectedBreakpoint,
+} from "~/shared/nano-states";
+import { calcCanvasWidth } from "./calc-canvas-width";
// Fixes initial canvas width jump on wide screens.
// Calculate canvas width during SSR based on known initial width for wide screens.
export const setInitialCanvasWidth = (breakpointId: Breakpoint["id"]) => {
const workspaceRect = $workspaceRect.get();
const breakpoints = $breakpoints.get();
- const breakpoint = breakpoints.get(breakpointId);
- if (workspaceRect === undefined || breakpoint === undefined) {
+ const selectedBreakpoint = breakpoints.get(breakpointId);
+ if (workspaceRect === undefined || selectedBreakpoint === undefined) {
return false;
}
- const width = findInitialWidth(
- Array.from(breakpoints.values()),
- breakpoint,
- workspaceRect.width
- );
+ const width = calcCanvasWidth({
+ breakpoints: Array.from(breakpoints.values()),
+ selectedBreakpoint,
+ workspaceWidth: workspaceRect.width,
+ });
$canvasWidth.set(width);
return true;
@@ -35,18 +39,20 @@ export const useSetCanvasWidth = () => {
if (workspaceRect === undefined || breakpoints.size === 0) {
return;
}
- const breakpointValues = Array.from(breakpoints.values());
const selectedBreakpoint = $selectedBreakpoint.get();
- // When there is selected breakpoint, we want to find the lowest possible size
- // that is bigger than all max breakpoints and smaller than all min breakpoints.
+ // When there is selected breakpoint, we want to find the smallest possible size
+ // that is bigger than any max-width breakpoints and smaller than any min-width breakpoints.
+ // When on base breakpoint it will be the biggest possible but smaller than the workspace.
if (selectedBreakpoint) {
- const width = findInitialWidth(
- breakpointValues,
+ const canvasWidth = $canvasWidth.get();
+ const nextWidth = calcCanvasWidth({
+ breakpoints: Array.from(breakpoints.values()),
selectedBreakpoint,
- workspaceRect.width
- );
- $canvasWidth.set(width);
+ workspaceWidth: workspaceRect.width,
+ canvasWidth,
+ });
+ $canvasWidth.set(nextWidth);
}
};
@@ -55,13 +61,18 @@ export const useSetCanvasWidth = () => {
if (workspaceRect === undefined) {
return;
}
- unsubscribeRectStore?.();
update();
});
+ const unsubscribeIsPreviewMode = $isPreviewMode.listen((isPreviewMode) => {
+ if (isPreviewMode) {
+ update();
+ }
+ });
return () => {
unsubscribeBreakpointStore();
- unsubscribeRectStore?.();
+ unsubscribeRectStore();
+ unsubscribeIsPreviewMode();
};
}, []);
};
diff --git a/apps/builder/app/builder/features/breakpoints/width-input.tsx b/apps/builder/app/builder/features/breakpoints/width-input.tsx
index d1f650c3e1..fc77fabd79 100644
--- a/apps/builder/app/builder/features/breakpoints/width-input.tsx
+++ b/apps/builder/app/builder/features/breakpoints/width-input.tsx
@@ -11,13 +11,13 @@ import {
useScrub,
handleNumericInputArrowKeys,
} from "@webstudio-is/design-system";
-import { useCanvasWidth } from "~/builder/shared/nano-states";
import { $breakpoints, $isResizingCanvas } from "~/shared/nano-states";
import {
$selectedBreakpointId,
$selectedBreakpoint,
} from "~/shared/nano-states";
import { useState, type KeyboardEvent, useEffect } from "react";
+import { $canvasWidth } from "~/builder/shared/nano-states";
const useEnhancedInput = ({
onChange,
@@ -85,12 +85,12 @@ const useEnhancedInput = ({
export const WidthInput = ({ min }: { min: number }) => {
const id = useId();
- const [canvasWidth, setCanvasWidth] = useCanvasWidth();
+ const canvasWidth = useStore($canvasWidth);
const selectedBreakpoint = useStore($selectedBreakpoint);
const breakpoints = useStore($breakpoints);
const onChange = (value: number) => {
- setCanvasWidth(value);
+ $canvasWidth.set(value);
const applicableBreakpoint = findApplicableMedia(
Array.from(breakpoints.values()),
value
diff --git a/apps/builder/app/builder/features/topbar/menu/menu.tsx b/apps/builder/app/builder/features/topbar/menu/menu.tsx
index f3393c220c..7137a3cce0 100644
--- a/apps/builder/app/builder/features/topbar/menu/menu.tsx
+++ b/apps/builder/app/builder/features/topbar/menu/menu.tsx
@@ -16,10 +16,10 @@ import {
Kbd,
} from "@webstudio-is/design-system";
import {
- useIsShareDialogOpen,
- useIsPublishDialogOpen,
$userPlanFeatures,
$isCloneDialogOpen,
+ $isShareDialogOpen,
+ $isPublishDialogOpen,
} from "~/builder/shared/nano-states";
import { useClientSettings } from "~/builder/shared/client-settings";
import { dashboardPath } from "~/shared/router-utils";
@@ -55,8 +55,6 @@ const ViewMenuItem = () => {
export const Menu = () => {
const navigate = useNavigate();
- const [, setIsShareOpen] = useIsShareDialogOpen();
- const [, setIsPublishOpen] = useIsPublishDialogOpen();
const { hasProPlan } = useStore($userPlanFeatures);
const authPermit = useStore($authPermit);
const authTokenPermission = useStore($authTokenPermissions);
@@ -156,7 +154,7 @@ export const Menu = () => {
>
<DropdownMenuItem
onSelect={() => {
- setIsShareOpen(true);
+ $isShareDialogOpen.set(true);
}}
disabled={isShareEnabled === false}
>
@@ -171,7 +169,7 @@ export const Menu = () => {
>
<DropdownMenuItem
onSelect={() => {
- setIsPublishOpen(true);
+ $isPublishDialogOpen.set(true);
}}
disabled={isPublishEnabled === false}
>
diff --git a/apps/builder/app/builder/features/topbar/publish.tsx b/apps/builder/app/builder/features/topbar/publish.tsx
index 8e5e0d5f9a..e8d2cfa162 100644
--- a/apps/builder/app/builder/features/topbar/publish.tsx
+++ b/apps/builder/app/builder/features/topbar/publish.tsx
@@ -28,8 +28,8 @@ import {
} from "@webstudio-is/design-system";
import stripIndent from "strip-indent";
import {
+ $isPublishDialogOpen,
$userPlanFeatures,
- useIsPublishDialogOpen,
} from "../../shared/nano-states";
import { validateProjectDomain, type Project } from "@webstudio-is/project";
import { $authPermit, $publishedOrigin } from "~/shared/nano-states";
@@ -687,7 +687,7 @@ type PublishProps = {
};
export const PublishButton = ({ projectId }: PublishProps) => {
- const [isOpen, setIsOpen] = useIsPublishDialogOpen();
+ const isPublishDialogOpen = useStore($isPublishDialogOpen);
const authPermit = useStore($authPermit);
const [dialogContentType, setDialogContentType] = useState<
"publish" | "export"
@@ -707,11 +707,15 @@ export const PublishButton = ({ projectId }: PublishProps) => {
if (isOpen === false) {
setDialogContentType("publish");
}
- setIsOpen(isOpen);
+ $isPublishDialogOpen.set(isOpen);
};
return (
- <FloatingPanelPopover modal open={isOpen} onOpenChange={handleOpenChange}>
+ <FloatingPanelPopover
+ modal
+ open={isPublishDialogOpen}
+ onOpenChange={handleOpenChange}
+ >
<FloatingPanelAnchor>
<Tooltip
side="bottom"
diff --git a/apps/builder/app/builder/features/topbar/share.tsx b/apps/builder/app/builder/features/topbar/share.tsx
index 194185fed2..34d0a228ef 100644
--- a/apps/builder/app/builder/features/topbar/share.tsx
+++ b/apps/builder/app/builder/features/topbar/share.tsx
@@ -13,7 +13,7 @@ import {
import type { Project } from "@webstudio-is/prisma-client";
import { ShareProjectContainer } from "~/shared/share-project";
import { $authPermit } from "~/shared/nano-states";
-import { useIsShareDialogOpen } from "~/builder/shared/nano-states";
+import { $isShareDialogOpen } from "~/builder/shared/nano-states";
export const ShareButton = ({
projectId,
@@ -22,7 +22,7 @@ export const ShareButton = ({
projectId: Project["id"];
hasProPlan: boolean;
}) => {
- const [isShareOpen, setIsShareOpen] = useIsShareDialogOpen();
+ const isShareDialogOpen = useStore($isShareDialogOpen);
const authPermit = useStore($authPermit);
const isShareDisabled = authPermit !== "own";
@@ -33,8 +33,10 @@ export const ShareButton = ({
return (
<FloatingPanelPopover
modal
- open={isShareOpen}
- onOpenChange={setIsShareOpen}
+ open={isShareDialogOpen}
+ onOpenChange={(isOpen) => {
+ $isShareDialogOpen.set(isOpen);
+ }}
>
<FloatingPanelAnchor>
<Tooltip
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
index 010ea9588f..cfa67c492d 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/resize-handles.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/resize-handles.tsx
@@ -4,6 +4,7 @@ import {
css,
disableCanvasPointerEvents,
numericScrubControl,
+ rawTheme,
theme,
} from "@webstudio-is/design-system";
import { useEffect, useRef } from "react";
@@ -18,7 +19,7 @@ import {
const handlesContainerStyle = css({
position: "absolute",
top: 0,
- width: 4,
+ width: theme.spacing[3],
bottom: 0,
cursor: "col-resize",
pointerEvents: "auto",
@@ -32,15 +33,15 @@ const handlesContainerStyle = css({
"& svg": {
position: "absolute",
top: "50%",
- right: 0,
+ right: theme.spacing[3],
transform: "translateX(100%)",
color: theme.colors.foregroundSubtle,
},
"&[data-align=left]": {
- left: 0,
+ left: `-${rawTheme.spacing[3]}`,
},
"&[data-align=right]": {
- right: 0,
+ right: `-${rawTheme.spacing[3]}`,
},
"&[data-state=resizing]::before": {
display: "none",
@@ -135,7 +136,6 @@ const useScrub = ({ side }: { side: "right" | "left" }) => {
return () => {
enableCanvasPointerEvents?.();
-
disposeScrubControl();
};
}, [side]);
diff --git a/apps/builder/app/builder/features/workspace/workspace.tsx b/apps/builder/app/builder/features/workspace/workspace.tsx
index e2b6fcef52..d120c6023a 100644
--- a/apps/builder/app/builder/features/workspace/workspace.tsx
+++ b/apps/builder/app/builder/features/workspace/workspace.tsx
@@ -1,8 +1,8 @@
import { useStore } from "@nanostores/react";
import { theme, Toaster, css } from "@webstudio-is/design-system";
import {
+ $canvasWidth,
$scale,
- useCanvasWidth,
$workspaceRect,
} from "~/builder/shared/nano-states";
import { $selectedInstanceSelector } from "~/shared/nano-states";
@@ -11,7 +11,7 @@ import { CanvasTools } from "./canvas-tools";
import { useEffect, useRef } from "react";
import { useSetCanvasWidth } from "../breakpoints";
import type { Breakpoint } from "@webstudio-is/sdk";
-import { findInitialWidth } from "../breakpoints/find-initial-width";
+import { calcCanvasWidth } from "../breakpoints/calc-canvas-width";
import { isBaseBreakpoint } from "~/shared/breakpoints";
const workspaceStyle = css({
@@ -20,6 +20,9 @@ const workspaceStyle = css({
position: "relative",
// Prevent scrollIntoView from scrolling the whole page
overflow: "clip",
+ // This spacing is needed to still be able to grab the canvas edge, otherwise you will always drag
+ // the browser window instead of the canvas.
+ px: theme.spacing[5],
});
const canvasContainerStyle = css({
@@ -53,16 +56,17 @@ const useMeasureWorkspace = () => {
const getCanvasInitialMaxWidth = (
initialBreakpoints: [Breakpoint["id"], Breakpoint][]
) => {
- const breakpointsArray = [...new Map(initialBreakpoints).values()];
- const initialSelectedBreakpoint =
- breakpointsArray.find(isBaseBreakpoint) ?? initialBreakpoints[0]?.[1];
-
- if (initialSelectedBreakpoint) {
- const initialWidth = findInitialWidth(
- [...new Map(initialBreakpoints).values()],
- initialSelectedBreakpoint,
- Number.POSITIVE_INFINITY
- );
+ const breakpoints = Array.from(new Map(initialBreakpoints).values());
+ const selectedBreakpoint =
+ breakpoints.find(isBaseBreakpoint) ?? initialBreakpoints[0]?.[1];
+
+ if (selectedBreakpoint) {
+ const initialWidth = calcCanvasWidth({
+ breakpoints,
+ selectedBreakpoint,
+ // Just some reasonable initial guess, will be overwritten later as soon as detected.
+ workspaceWidth: 2000,
+ });
return initialWidth;
}
};
@@ -98,7 +102,7 @@ const useCanvasStyle = (
) => {
const scale = useStore($scale);
const workspaceRect = useStore($workspaceRect);
- const [canvasWidth] = useCanvasWidth();
+ const canvasWidth = useStore($canvasWidth);
return getCanvasStyle(initialBreakpoints, scale, workspaceRect, canvasWidth);
};
@@ -108,7 +112,7 @@ const useOutlineStyle = (
) => {
const scale = useStore($scale);
const workspaceRect = useStore($workspaceRect);
- const [canvasWidth] = useCanvasWidth();
+ const canvasWidth = useStore($canvasWidth);
const style = getCanvasStyle(
initialBreakpoints,
100,
diff --git a/apps/builder/app/builder/shared/nano-states/index.ts b/apps/builder/app/builder/shared/nano-states/index.ts
index c31e48a555..6d1ba8fdea 100644
--- a/apps/builder/app/builder/shared/nano-states/index.ts
+++ b/apps/builder/app/builder/shared/nano-states/index.ts
@@ -1,21 +1,12 @@
-import { atom, computed, type WritableAtom } from "nanostores";
-import { useStore } from "@nanostores/react";
+import { atom, computed } from "nanostores";
import type { TabName } from "~/builder/features/sidebar-left/types";
import type { UserPlanFeatures } from "~/shared/db/user-plan-features.server";
-const useValue = <T>(atom: WritableAtom<T>) => {
- const value = useStore(atom);
- return [value, atom.set] as const;
-};
+export const $isShareDialogOpen = atom<boolean>(false);
-const $isShareDialogOpen = atom<boolean>(false);
-export const useIsShareDialogOpen = () => useValue($isShareDialogOpen);
-
-const $isPublishDialogOpen = atom<boolean>(false);
-export const useIsPublishDialogOpen = () => useValue($isPublishDialogOpen);
+export const $isPublishDialogOpen = atom<boolean>(false);
export const $canvasWidth = atom<number | undefined>();
-export const useCanvasWidth = () => useValue($canvasWidth);
export const $isCloneDialogOpen = atom<boolean>(false);