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.

Base breakpoint (#1140)

Oleg Isonenc50c39b0b756
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index af2218f9c9..a408b6327c 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -37,7 +37,6 @@ import { Navigator } from "./features/sidebar-left";
import { getBuildUrl } from "~/shared/router-utils";
import { useCopyPaste } from "~/shared/copy-paste";
import { AssetsProvider } from "./shared/assets";
-import { minWidth } from "./features/breakpoints/width-setting";
registerContainers();
@@ -332,7 +331,7 @@ export const Builder = ({
css={{
height: "100%",
width: "100%",
- minWidth,
+ //minWidth,
}}
/>
</Workspace>
diff --git a/apps/builder/app/builder/features/breakpoints/use-update-canvas-width.ts b/apps/builder/app/builder/features/breakpoints/use-update-canvas-width.ts
index 1c42d5b2ae..dd968ccb79 100644
--- a/apps/builder/app/builder/features/breakpoints/use-update-canvas-width.ts
+++ b/apps/builder/app/builder/features/breakpoints/use-update-canvas-width.ts
@@ -1,14 +1,25 @@
import { useStore } from "@nanostores/react";
-import { useCallback, useEffect } from "react";
+import { useCallback, useEffect, useRef } from "react";
import { useCanvasWidth } from "~/builder/shared/nano-states";
import { useIsPreviewMode } from "~/shared/nano-states";
-import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";
+import {
+ selectedBreakpointStore,
+ workspaceRectStore,
+} from "~/shared/nano-states/breakpoints";
+import { useNextBreakpoint } from "./use-next-breakpoint";
import { minWidth } from "./width-setting";
export const useUpdateCanvasWidth = () => {
const selectedBreakpoint = useStore(selectedBreakpointStore);
const [canvasWidth, setCanvasWidth] = useCanvasWidth();
const [isPreviewMode] = useIsPreviewMode();
+ const nextBreakpoint = useNextBreakpoint();
+ const workspaceRect = useStore(workspaceRectStore);
+
+ // We don't won't workspace rect resize observer to trigger canvas width update.
+ // We only need the initial workspace width here.
+ const initialWorkspaceRect = useRef(workspaceRect);
+ initialWorkspaceRect.current = workspaceRect;
// Ensure the size is within currently selected breakpoint when returning to design mode out of preview mode,
// because preview mode enables resizing without constraining to the selected breakpoint.
@@ -16,8 +27,27 @@ export const useUpdateCanvasWidth = () => {
if (isPreviewMode || selectedBreakpoint === undefined) {
return;
}
- setCanvasWidth(Math.max(selectedBreakpoint.minWidth, minWidth));
- }, [isPreviewMode, selectedBreakpoint, setCanvasWidth]);
+ const maxWidthBelowNextBreakpoint = (nextBreakpoint?.minWidth ?? 0) - 1;
+ const initialMaxAvailableWidth = initialWorkspaceRect.current
+ ? Math.min(
+ maxWidthBelowNextBreakpoint,
+ initialWorkspaceRect.current.width
+ )
+ : maxWidthBelowNextBreakpoint;
+
+ const width = Math.max(
+ selectedBreakpoint.minWidth,
+ initialMaxAvailableWidth,
+ minWidth
+ );
+ setCanvasWidth(width);
+ }, [
+ isPreviewMode,
+ selectedBreakpoint,
+ nextBreakpoint,
+ setCanvasWidth,
+ initialWorkspaceRect,
+ ]);
// This fallback is needed for cases when something unexpected loads in the iframe.
// In that case the width remains 0, and user is unable to see what has loaded,
@@ -45,7 +75,6 @@ export const useUpdateCanvasWidth = () => {
) {
return;
}
-
setCanvasWidth(minWidth);
},
[canvasWidth, selectedBreakpoint, setCanvasWidth]
diff --git a/apps/builder/app/builder/features/breakpoints/width-setting.tsx b/apps/builder/app/builder/features/breakpoints/width-setting.tsx
index 7d1b9be797..9a13448470 100644
--- a/apps/builder/app/builder/features/breakpoints/width-setting.tsx
+++ b/apps/builder/app/builder/features/breakpoints/width-setting.tsx
@@ -28,6 +28,7 @@ export const WidthSetting = () => {
const min = isPreviewMode
? minWidth
: Math.max(minWidth, selectedBreakpoint.minWidth);
+
const max = isPreviewMode
? maxWidth
: Math.min(
diff --git a/apps/builder/app/builder/features/workspace/workspace.tsx b/apps/builder/app/builder/features/workspace/workspace.tsx
index 9e9b7d476c..2153a0fe39 100644
--- a/apps/builder/app/builder/features/workspace/workspace.tsx
+++ b/apps/builder/app/builder/features/workspace/workspace.tsx
@@ -3,15 +3,18 @@ import { theme, Box, Flex, Toaster } from "@webstudio-is/design-system";
import { useCanvasWidth } from "~/builder/shared/nano-states";
import type { Publish } from "~/shared/pubsub";
import { selectedInstanceIdStore } from "~/shared/nano-states";
-import { zoomStore } from "~/shared/nano-states/breakpoints";
+import {
+ workspaceRectStore,
+ zoomStore,
+} from "~/shared/nano-states/breakpoints";
import { CanvasTools } from "./canvas-tools";
+import { useMeasure } from "react-use";
+import { useEffect } from "react";
const workspaceStyle = {
flexGrow: 1,
background: theme.colors.gray3,
- // scroll behaviour should be derived from the iframe
- overflow: "hidden",
- scrollbarGutter: "stable",
+ overflow: "scroll",
position: "relative",
};
@@ -25,6 +28,20 @@ const zoomStyle = {
const canvasContainerStyle = {
position: "relative",
height: "100%",
+ overflow: "hidden",
+};
+
+const useSetWorkspaceRect = () => {
+ const workspaceRect = useStore(workspaceRectStore);
+ const [ref, rect] = useMeasure<HTMLDivElement>();
+ useEffect(() => {
+ if (rect.width === 0 || rect.height === 0) {
+ return;
+ }
+ // Little lie to safe the trouble of importing the type it uses everywhere.
+ workspaceRectStore.set(rect as DOMRect);
+ }, [workspaceRect, rect]);
+ return ref;
};
type WorkspaceProps = {
@@ -40,13 +57,14 @@ export const Workspace = ({
}: WorkspaceProps) => {
const zoom = useStore(zoomStore);
const [canvasWidth] = useCanvasWidth();
+ const workspaceRef = useSetWorkspaceRect();
const handleWorkspaceClick = () => {
selectedInstanceIdStore.set(undefined);
};
return (
- <Box css={workspaceStyle} onClick={handleWorkspaceClick}>
+ <Box css={workspaceStyle} onClick={handleWorkspaceClick} ref={workspaceRef}>
<Flex
direction="column"
align="center"
diff --git a/apps/builder/app/builder/shared/nano-states/index.ts b/apps/builder/app/builder/shared/nano-states/index.ts
index 048e897e48..aa2d17e0e8 100644
--- a/apps/builder/app/builder/shared/nano-states/index.ts
+++ b/apps/builder/app/builder/shared/nano-states/index.ts
@@ -3,7 +3,6 @@ import { useStore } from "@nanostores/react";
import type { Pages } from "@webstudio-is/project-build";
import type { Project } from "@webstudio-is/project";
import type { AssetContainer, DeletingAssetContainer } from "../assets";
-import { minWidth } from "~/builder/features/breakpoints/width-setting";
const useValue = <T>(atom: WritableAtom<T>) => {
const value = useStore(atom);
@@ -17,7 +16,7 @@ const isPublishDialogOpenContainer = atom<boolean>(false);
export const useIsPublishDialogOpen = () =>
useValue(isPublishDialogOpenContainer);
-const canvasWidthContainer = atom<number>(minWidth);
+const canvasWidthContainer = atom<number>(0);
export const useCanvasWidth = () => useValue(canvasWidthContainer);
const canvasRectContainer = atom<DOMRect | undefined>();
diff --git a/apps/builder/app/shared/nano-states/breakpoints.ts b/apps/builder/app/shared/nano-states/breakpoints.ts
index d63d8c40e3..daa1ef1796 100644
--- a/apps/builder/app/shared/nano-states/breakpoints.ts
+++ b/apps/builder/app/shared/nano-states/breakpoints.ts
@@ -50,3 +50,5 @@ export const synchronizedBreakpointsStores = [
["zoomStore", zoomStore],
["selectedBreakpointId", selectedBreakpointIdStore],
] as const;
+
+export const workspaceRectStore = atom<DOMRect | undefined>();
diff --git a/packages/project-build/src/schema/breakpoints.ts b/packages/project-build/src/schema/breakpoints.ts
index 6f2a553c2d..aab5cd8b1a 100644
--- a/packages/project-build/src/schema/breakpoints.ts
+++ b/packages/project-build/src/schema/breakpoints.ts
@@ -21,7 +21,7 @@ export type Breakpoints = z.infer<typeof Breakpoints>;
type BaseBreakpoint = Pick<Breakpoint, "label" | "minWidth">;
export const initialBreakpoints: BaseBreakpoint[] = [
- { label: "Mobile", minWidth: 0 },
+ { label: "Base", minWidth: 0 },
{ label: "Tablet", minWidth: 768 },
{ label: "Laptop", minWidth: 1024 },
{ label: "Desktop", minWidth: 1280 },