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.

Fix switching to base breakpoint on a small screen (#1504)

Oleg Isonen119d8410c3a5
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
index 17f856aae1..712a492952 100644
--- a/apps/builder/app/builder/features/breakpoints/find-initial-width.test.ts
+++ b/apps/builder/app/builder/features/breakpoints/find-initial-width.test.ts
@@ -13,11 +13,52 @@ const breakpoints = [
];
describe("Find initial width", () => {
- test("base", () => {
+ 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)
diff --git a/apps/builder/app/builder/features/breakpoints/find-initial-width.ts b/apps/builder/app/builder/features/breakpoints/find-initial-width.ts
index e91b0ab898..740ec7229e 100644
--- a/apps/builder/app/builder/features/breakpoints/find-initial-width.ts
+++ b/apps/builder/app/builder/features/breakpoints/find-initial-width.ts
@@ -1,5 +1,6 @@
import { compareMedia } from "@webstudio-is/css-engine";
import type { Breakpoint } from "@webstudio-is/project-build";
+import { groupBreakpoints, isBaseBreakpoint } from "~/shared/breakpoints";
const defaultWidth = 320;
@@ -11,12 +12,29 @@ export const findInitialWidth = (
selectedBreakpoint: Breakpoint,
workspaceWidth: number
) => {
- // When on a base breakpoint, we want to show maximum width canvas
- if (
- selectedBreakpoint.minWidth === undefined &&
- selectedBreakpoint.maxWidth === undefined
- ) {
- return workspaceWidth;
+ // 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) {
diff --git a/apps/builder/app/builder/features/breakpoints/trigger-button.tsx b/apps/builder/app/builder/features/breakpoints/trigger-button.tsx
index 86d2edbe08..d0d0ced92c 100644
--- a/apps/builder/app/builder/features/breakpoints/trigger-button.tsx
+++ b/apps/builder/app/builder/features/breakpoints/trigger-button.tsx
@@ -38,6 +38,7 @@ export const TriggerButton = (props: TriggerButtonProps) => {
if (breakpoint === undefined || canvasWidth === undefined) {
return null;
}
+ const roundedScale = Math.round(scale);
return (
<DropdownMenuTrigger
aria-label="Show breakpoints"
@@ -48,9 +49,9 @@ export const TriggerButton = (props: TriggerButtonProps) => {
<Value unit="PX" minWidth={55}>
{Math.round(canvasWidth)}
</Value>
- {scale !== 100 && (
+ {roundedScale !== 100 && (
<Value unit="%" minWidth={30}>
- {Math.round(scale)}
+ {roundedScale}
</Value>
)}
</DropdownMenuTrigger>
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 87a31e4f71..ab02e93364 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,17 +1,13 @@
import { useStore } from "@nanostores/react";
import { useCallback, useEffect, useRef } from "react";
-import { findApplicableMedia } from "@webstudio-is/css-engine";
import type { Breakpoint } from "@webstudio-is/project-build";
import {
useCanvasWidth,
workspaceRectStore,
} from "~/builder/shared/nano-states";
-import {
- breakpointsStore,
- selectedBreakpointIdStore,
-} from "~/shared/nano-states";
+import { breakpointsStore } from "~/shared/nano-states";
import { findInitialWidth } from "./find-initial-width";
-import { groupBreakpoints, isBaseBreakpoint } from "~/shared/breakpoints";
+import { isBaseBreakpoint } from "~/shared/breakpoints";
// Set canvas width based on workspace width, breakpoints and passed breakpoint id.
export const useSetInitialCanvasWidth = () => {
@@ -24,6 +20,7 @@ export const useSetInitialCanvasWidth = () => {
if (workspaceRect === undefined || breakpoint === undefined) {
return false;
}
+
const width = findInitialWidth(
Array.from(breakpoints.values()),
breakpoint,
@@ -56,24 +53,12 @@ export const useSetInitialCanvasWidthOnce = () => {
// When there is base breakpoint, we want to find the lowest possible size
// that is bigger than all max breakpoints and smaller than all min breakpoints.
if (baseBreakpoint) {
- const grouped = groupBreakpoints(breakpointValues);
- const baseIndex = grouped.findIndex(isBaseBreakpoint);
- const nextAfterBase = grouped[baseIndex + 1];
- if (nextAfterBase?.maxWidth !== undefined) {
- const width = nextAfterBase.maxWidth + 1;
- setCanvasWidth(width);
- return;
- }
- }
-
- // Find the breakpoint that applies according to the workspace width.
- const applicableBreakpoint = findApplicableMedia(
- breakpointValues,
- workspaceRect.width
- );
- if (applicableBreakpoint) {
- selectedBreakpointIdStore.set(applicableBreakpoint.id);
+ const width = findInitialWidth(
+ breakpointValues,
+ baseBreakpoint,
+ workspaceRect.width
+ );
+ setCanvasWidth(width);
}
- setCanvasWidth(workspaceRect.width);
}, [breakpoints, setCanvasWidth, workspaceRect]);
};