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: Limit breakpoint amount (#5776)

Oleg Isonend6362b22ba89
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoint-editor-utils.test.ts b/apps/builder/app/builder/features/breakpoints/breakpoint-editor-utils.test.ts
index 68666206be..7cde1b4b27 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoint-editor-utils.test.ts
+++ b/apps/builder/app/builder/features/breakpoints/breakpoint-editor-utils.test.ts
@@ -149,7 +149,7 @@ describe("breakpoint-editor-utils", () => {
});
});
- test("uses original label if new label is empty", () => {
+ test("does not build breakpoint when label is empty", () => {
const original = {
id: "id6",
label: "Original",
@@ -165,11 +165,7 @@ describe("breakpoint-editor-utils", () => {
original
);
- expect(result).toEqual({
- id: "id6",
- label: "Original",
- minWidth: 1024,
- });
+ expect(result).toBeUndefined();
});
});
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoint-editor-utils.ts b/apps/builder/app/builder/features/breakpoints/breakpoint-editor-utils.ts
index 73b44f6325..17af70b297 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoint-editor-utils.ts
+++ b/apps/builder/app/builder/features/breakpoints/breakpoint-editor-utils.ts
@@ -50,9 +50,13 @@ export const buildBreakpointFromEditorState = (
const trimmedCondition = conditionValue.trim();
const hasCondition = trimmedCondition !== "";
+ if (trimmedLabel === "") {
+ return;
+ }
+
const newBreakpoint: Breakpoint = {
id,
- label: trimmedLabel || originalBreakpoint?.label || "",
+ label: trimmedLabel,
};
if (hasCondition) {
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx b/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx
index d40a87f45f..0ca1767ba7 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx
+++ b/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx
@@ -15,11 +15,18 @@ import {
Popover,
PopoverTrigger,
PopoverContent,
+ ScrollAreaNative,
+ toast,
} from "@webstudio-is/design-system";
-import { MinusIcon, PlusIcon } from "@webstudio-is/icons";
+import { PlusIcon, TrashIcon } from "@webstudio-is/icons";
import { useStore } from "@nanostores/react";
import { $breakpoints } from "~/shared/sync/data-stores";
-import { groupBreakpoints, isBaseBreakpoint } from "~/shared/breakpoints";
+import {
+ breakpointLimitWarning,
+ groupBreakpoints,
+ hasReachedBreakpointLimit,
+ isBaseBreakpoint,
+} from "~/shared/breakpoints";
import { serverSyncStore } from "~/shared/sync/sync-stores";
import { ConditionInput } from "./condition-input";
import { CssValueInput } from "~/builder/features/style-panel/shared/css-value-input";
@@ -77,11 +84,22 @@ const BreakpointEditorItem = ({
);
const hasCondition = localValue.value.condition.trim() !== "";
+ const hasName = localValue.value.label.trim() !== "";
return (
- <Flex gap="2">
- <Flex direction="column" gap="1">
+ <Flex
+ direction="column"
+ gap="2"
+ css={{
+ [`&:hover [data-breakpoint-delete], &:focus-within [data-breakpoint-delete]`]:
+ {
+ visibility: "visible",
+ },
+ }}
+ >
+ <Flex gap="2">
<InputField
+ variant="chromeless"
type="text"
value={localValue.value.label}
onChange={(event) =>
@@ -93,6 +111,29 @@ const BreakpointEditorItem = ({
required
autoFocus={autoFocus}
/>
+ <IconButton
+ data-breakpoint-delete
+ disabled={hasName === false}
+ css={{ visibility: "hidden" }}
+ onClick={() => {
+ const breakpointToDelete = buildBreakpointFromEditorState(
+ breakpoint.id,
+ localValue.value.label,
+ localValue.value.condition,
+ type,
+ localValue.value.width,
+ breakpoint
+ );
+ if (breakpointToDelete === undefined) {
+ return;
+ }
+ onDelete(breakpointToDelete);
+ }}
+ >
+ <TrashIcon />
+ </IconButton>
+ </Flex>
+ <Flex direction="column" gap="1">
<Flex gap="2" css={{ width: theme.spacing[26] }}>
<Select
css={{ width: theme.spacing[28] }}
@@ -166,13 +207,6 @@ const BreakpointEditorItem = ({
onBlur={localValue.save}
/>
</Flex>
- <IconButton
- onClick={() => {
- onDelete(breakpoint);
- }}
- >
- <MinusIcon />
- </IconButton>
</Flex>
);
};
@@ -233,6 +267,10 @@ export const BreakpointsEditor = ({
suffix={
<IconButton
onClick={() => {
+ if (hasReachedBreakpointLimit(allBreakpoints.length)) {
+ toast.warn(breakpointLimitWarning);
+ return;
+ }
const newBreakpoint: Breakpoint = {
id: nanoid(),
label: "",
@@ -248,28 +286,30 @@ export const BreakpointsEditor = ({
{"Breakpoints"}
</PanelTitle>
<Separator />
- <Fragment>
- {allBreakpoints.map((breakpoint, index, all) => {
- return (
- <Fragment key={breakpoint.id}>
- <Box css={{ p: theme.panel.padding }}>
- <BreakpointEditorItem
- breakpoint={breakpoint}
- onChangeComplete={handleChangeComplete}
- onDelete={onDelete}
- autoFocus={index === 0}
- />
- </Box>
- {index < all.length - 1 && <PopoverSeparator />}
- </Fragment>
- );
- })}
- </Fragment>
- {allBreakpoints.length === 0 && (
- <Text css={{ margin: theme.spacing[10] }}>
- No breakpoints found
- </Text>
- )}
+ <ScrollAreaNative css={{ maxHeight: "80vh" }}>
+ <Fragment>
+ {allBreakpoints.map((breakpoint, index, all) => {
+ return (
+ <Fragment key={breakpoint.id}>
+ <Box css={{ p: theme.panel.padding }}>
+ <BreakpointEditorItem
+ breakpoint={breakpoint}
+ onChangeComplete={handleChangeComplete}
+ onDelete={onDelete}
+ autoFocus={index === 0}
+ />
+ </Box>
+ {index < all.length - 1 && <PopoverSeparator />}
+ </Fragment>
+ );
+ })}
+ </Fragment>
+ {allBreakpoints.length === 0 && (
+ <Text css={{ margin: theme.spacing[10] }}>
+ No breakpoints found
+ </Text>
+ )}
+ </ScrollAreaNative>
</Flex>
</PopoverContent>
</Popover>
diff --git a/apps/builder/app/shared/breakpoints-utils.test.ts b/apps/builder/app/shared/breakpoints-utils.test.ts
index 1a81bbf619..b4456f651f 100644
--- a/apps/builder/app/shared/breakpoints-utils.test.ts
+++ b/apps/builder/app/shared/breakpoints-utils.test.ts
@@ -3,6 +3,9 @@ import {
isBaseBreakpoint,
groupBreakpoints,
buildMergedBreakpointIds,
+ findClosestBreakpoint,
+ hasReachedBreakpointLimit,
+ maxBreakpoints,
} from "./breakpoints-utils";
import type { Breakpoint, Breakpoints } from "@webstudio-is/sdk";
@@ -252,4 +255,138 @@ describe("buildMergedBreakpointIds", () => {
expect(result.size).toBe(1);
expect(result.get("frag-1")).toBe("exist-1");
});
+
+ test("keeps total breakpoints within max count by merging overflow into closest existing breakpoint", () => {
+ const existingBreakpoints: Breakpoints = new Map([
+ ["base", { id: "base", label: "" }],
+ ["desktop", { id: "desktop", label: "Desktop", minWidth: 1200 }],
+ ["tablet", { id: "tablet", label: "Tablet", maxWidth: 900 }],
+ ["mobile", { id: "mobile", label: "Mobile", maxWidth: 480 }],
+ ["wide", { id: "wide", label: "Wide", minWidth: 1600 }],
+ ["large", { id: "large", label: "Large", minWidth: 2000 }],
+ ["print", { id: "print", label: "Print", condition: "print" }],
+ ["hover", { id: "hover", label: "Hover", condition: "hover: hover" }],
+ ]);
+ const fragmentBreakpoints: Breakpoint[] = [
+ { id: "frag-existing", label: "Same", maxWidth: 480 },
+ { id: "frag-new", label: "New", minWidth: 1440 },
+ { id: "frag-overflow", label: "Overflow", maxWidth: 760 },
+ ];
+
+ const result = buildMergedBreakpointIds(
+ fragmentBreakpoints,
+ existingBreakpoints,
+ { maxBreakpointCount: maxBreakpoints }
+ );
+
+ expect(result.get("frag-existing")).toBe("mobile");
+ expect(result.has("frag-new")).toBe(false);
+ expect(result.get("frag-overflow")).toBe("tablet");
+ });
+
+ test("merges all new breakpoints when existing non-base breakpoints are already at the max count", () => {
+ const existingBreakpoints: Breakpoints = new Map([
+ ["base", { id: "base", label: "" }],
+ ["0", { id: "0", label: "320", maxWidth: 320 }],
+ ["1", { id: "1", label: "480", maxWidth: 480 }],
+ ["2", { id: "2", label: "768", maxWidth: 768 }],
+ ["3", { id: "3", label: "1024", maxWidth: 1024 }],
+ ["4", { id: "4", label: "1280", minWidth: 1280 }],
+ ["5", { id: "5", label: "1440", minWidth: 1440 }],
+ ["6", { id: "6", label: "1920", minWidth: 1920 }],
+ ["7", { id: "7", label: "2560", minWidth: 2560 }],
+ ]);
+
+ const result = buildMergedBreakpointIds(
+ [{ id: "frag", label: "New", minWidth: 1500 }],
+ existingBreakpoints,
+ { maxBreakpointCount: maxBreakpoints }
+ );
+
+ expect(result.get("frag")).toBe("5");
+ });
+
+ test("reports when breakpoint is merged because of max count", () => {
+ const existingBreakpoints: Breakpoints = new Map([
+ ["base", { id: "base", label: "" }],
+ ["0", { id: "0", label: "320", maxWidth: 320 }],
+ ["1", { id: "1", label: "480", maxWidth: 480 }],
+ ["2", { id: "2", label: "768", maxWidth: 768 }],
+ ["3", { id: "3", label: "1024", maxWidth: 1024 }],
+ ["4", { id: "4", label: "1280", minWidth: 1280 }],
+ ["5", { id: "5", label: "1440", minWidth: 1440 }],
+ ["6", { id: "6", label: "1920", minWidth: 1920 }],
+ ["7", { id: "7", label: "2560", minWidth: 2560 }],
+ ]);
+ let mergeCount = 0;
+
+ buildMergedBreakpointIds(
+ [{ id: "frag", label: "New", minWidth: 1500 }],
+ existingBreakpoints,
+ {
+ maxBreakpointCount: maxBreakpoints,
+ onBreakpointMergedDueToLimit: () => {
+ mergeCount += 1;
+ },
+ }
+ );
+
+ expect(mergeCount).toBe(1);
+ });
+
+ test("does not count base breakpoint against max count", () => {
+ const existingBreakpoints: Breakpoints = new Map([
+ ["base", { id: "base", label: "" }],
+ ["0", { id: "0", label: "320", maxWidth: 320 }],
+ ["1", { id: "1", label: "480", maxWidth: 480 }],
+ ["2", { id: "2", label: "768", maxWidth: 768 }],
+ ["3", { id: "3", label: "1024", maxWidth: 1024 }],
+ ["4", { id: "4", label: "1280", minWidth: 1280 }],
+ ["5", { id: "5", label: "1440", minWidth: 1440 }],
+ ["6", { id: "6", label: "1920", minWidth: 1920 }],
+ ]);
+
+ const result = buildMergedBreakpointIds(
+ [{ id: "frag", label: "New", minWidth: 1500 }],
+ existingBreakpoints,
+ { maxBreakpointCount: maxBreakpoints }
+ );
+
+ expect(result.has("frag")).toBe(false);
+ });
+});
+
+describe("findClosestBreakpoint", () => {
+ test("finds the closest width-based breakpoint", () => {
+ const result = findClosestBreakpoint(
+ { id: "new", label: "New", maxWidth: 760 },
+ [
+ { id: "base", label: "" },
+ { id: "mobile", label: "Mobile", maxWidth: 480 },
+ { id: "tablet", label: "Tablet", maxWidth: 768 },
+ ]
+ );
+
+ expect(result?.id).toBe("tablet");
+ });
+
+ test("falls back to base for condition breakpoints without comparable widths", () => {
+ const result = findClosestBreakpoint(
+ { id: "new", label: "Dark", condition: "prefers-color-scheme: dark" },
+ [
+ { id: "base", label: "" },
+ { id: "hover", label: "Hover", condition: "hover: hover" },
+ ]
+ );
+
+ expect(result?.id).toBe("base");
+ });
+});
+
+describe("hasReachedBreakpointLimit", () => {
+ test("counts saved and pending breakpoints", () => {
+ expect(hasReachedBreakpointLimit(maxBreakpoints - 1)).toBe(false);
+ expect(hasReachedBreakpointLimit(maxBreakpoints)).toBe(true);
+ expect(hasReachedBreakpointLimit(maxBreakpoints - 1, 1)).toBe(true);
+ });
});
diff --git a/apps/builder/app/shared/breakpoints-utils.ts b/apps/builder/app/shared/breakpoints-utils.ts
index ccbb6312b7..a35bf2e4e4 100644
--- a/apps/builder/app/shared/breakpoints-utils.ts
+++ b/apps/builder/app/shared/breakpoints-utils.ts
@@ -1,6 +1,18 @@
import { compareMedia, equalMedia } from "@webstudio-is/css-engine";
import type { Breakpoint, Breakpoints } from "@webstudio-is/sdk";
+export const maxBreakpoints = 8;
+
+export const breakpointLimitWarning =
+ "You can have up to 8 breakpoints. More breakpoints can make the project harder to maintain.";
+
+export const breakpointPasteLimitWarning = `${breakpointLimitWarning} Pasted breakpoint styles were merged into the closest existing breakpoints.`;
+
+export const hasReachedBreakpointLimit = (
+ breakpointCount: number,
+ pendingBreakpointCount = 0
+) => breakpointCount + pendingBreakpointCount >= maxBreakpoints;
+
/**
* Check if a breakpoint is the base breakpoint (no min or max width).
* Note: Does not check for custom conditions.
@@ -43,6 +55,46 @@ export const groupBreakpoints = <
};
};
+const getBreakpointWidthValue = (breakpoint: Breakpoint) => {
+ if (breakpoint.minWidth !== undefined && breakpoint.maxWidth !== undefined) {
+ return (breakpoint.minWidth + breakpoint.maxWidth) / 2;
+ }
+ return breakpoint.minWidth ?? breakpoint.maxWidth;
+};
+
+export const findClosestBreakpoint = (
+ breakpoint: Breakpoint,
+ existingBreakpoints: Breakpoint[]
+): Breakpoint | undefined => {
+ let closestBreakpoint: Breakpoint | undefined;
+ let closestDistance = Number.POSITIVE_INFINITY;
+ const widthValue = getBreakpointWidthValue(breakpoint);
+
+ if (widthValue !== undefined) {
+ for (const existingBreakpoint of existingBreakpoints) {
+ const existingWidthValue = getBreakpointWidthValue(existingBreakpoint);
+ if (existingWidthValue === undefined) {
+ continue;
+ }
+ const distance = Math.abs(widthValue - existingWidthValue);
+ if (distance < closestDistance) {
+ closestBreakpoint = existingBreakpoint;
+ closestDistance = distance;
+ }
+ }
+ }
+
+ return (
+ closestBreakpoint ??
+ existingBreakpoints.find(
+ (existingBreakpoint) =>
+ existingBreakpoint.condition === undefined &&
+ isBaseBreakpoint(existingBreakpoint)
+ ) ??
+ existingBreakpoints[0]
+ );
+};
+
/**
* Build a map of merged breakpoint IDs from fragment breakpoints to existing breakpoints.
* Breakpoints are merged when they have matching minWidth, maxWidth, condition, and label.
@@ -50,20 +102,47 @@ export const groupBreakpoints = <
*
* @param fragmentBreakpoints - Breakpoints from the fragment being inserted
* @param existingBreakpoints - Existing breakpoints in the project
+ * @param options.maxBreakpointCount - When provided, overflow fragment breakpoints are merged into the closest existing breakpoint.
* @returns Map of fragment breakpoint IDs to existing breakpoint IDs
*/
export const buildMergedBreakpointIds = (
fragmentBreakpoints: Breakpoint[],
- existingBreakpoints: Breakpoints
+ existingBreakpoints: Breakpoints,
+ options?: {
+ maxBreakpointCount?: number;
+ onBreakpointMergedDueToLimit?: () => void;
+ }
): Map<Breakpoint["id"], Breakpoint["id"]> => {
const mergedBreakpointIds = new Map<Breakpoint["id"], Breakpoint["id"]>();
+ const acceptedBreakpoints = Array.from(existingBreakpoints.values());
+
for (const newBreakpoint of fragmentBreakpoints) {
- for (const breakpoint of existingBreakpoints.values()) {
+ for (const breakpoint of acceptedBreakpoints) {
if (equalMedia(breakpoint, newBreakpoint)) {
mergedBreakpointIds.set(newBreakpoint.id, breakpoint.id);
break;
}
}
+ if (mergedBreakpointIds.has(newBreakpoint.id)) {
+ continue;
+ }
+
+ if (
+ options?.maxBreakpointCount !== undefined &&
+ acceptedBreakpoints.length >= options.maxBreakpointCount + 1
+ ) {
+ const closestBreakpoint = findClosestBreakpoint(
+ newBreakpoint,
+ acceptedBreakpoints
+ );
+ if (closestBreakpoint !== undefined) {
+ mergedBreakpointIds.set(newBreakpoint.id, closestBreakpoint.id);
+ options.onBreakpointMergedDueToLimit?.();
+ }
+ continue;
+ }
+
+ acceptedBreakpoints.push(newBreakpoint);
}
return mergedBreakpointIds;
};
diff --git a/apps/builder/app/shared/breakpoints/index.ts b/apps/builder/app/shared/breakpoints/index.ts
index 753170937d..e059e9020a 100644
--- a/apps/builder/app/shared/breakpoints/index.ts
+++ b/apps/builder/app/shared/breakpoints/index.ts
@@ -2,6 +2,11 @@ export {
isBaseBreakpoint,
groupBreakpoints,
buildMergedBreakpointIds,
+ breakpointLimitWarning,
+ breakpointPasteLimitWarning,
+ findClosestBreakpoint,
+ hasReachedBreakpointLimit,
+ maxBreakpoints,
minCanvasWidth,
} from "../breakpoints-utils";
export * from "./select-breakpoint-by-order";
diff --git a/apps/builder/app/shared/copy-paste/plugin-html.ts b/apps/builder/app/shared/copy-paste/plugin-html.ts
index 4ea856da36..c86bc74a69 100644
--- a/apps/builder/app/shared/copy-paste/plugin-html.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-html.ts
@@ -5,6 +5,7 @@ import { generateFragmentFromTailwind } from "../tailwind/tailwind";
import { denormalizeSrcProps } from "./asset-upload";
import type { Plugin } from "./init-copy-paste";
import { builderApi } from "../builder-api";
+import { breakpointPasteLimitWarning } from "../breakpoints";
const inceptionMark = `<!-- @webstudio/inception/1 -->`;
@@ -19,7 +20,11 @@ export const html: Plugin = {
if (html.includes(inceptionMark)) {
fragment = await generateFragmentFromTailwind(fragment);
}
- const result = insertWebstudioFragmentAt(fragment);
+ const result = insertWebstudioFragmentAt(fragment, undefined, undefined, {
+ onBreakpointLimitMerge: () => {
+ builderApi.toast.warn(breakpointPasteLimitWarning);
+ },
+ });
if (skippedSelectors.length > 0) {
builderApi.toast.info(
`Skipped nested selectors (no matching elements): ${skippedSelectors.join(", ")}`
diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.ts
index 2a0befd584..33794b330f 100644
--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts
@@ -29,6 +29,7 @@ import {
import { findAvailableVariables } from "../data-variables";
import { builderApi } from "../builder-api";
import type { Plugin } from "./init-copy-paste";
+import { breakpointPasteLimitWarning } from "../breakpoints";
const version = "@webstudio/instance/v0.1";
@@ -188,6 +189,9 @@ const onPaste = async (clipboardData: string) => {
}),
projectId: project.id,
conflictResolution,
+ onBreakpointLimitMerge: () => {
+ toast.warn(breakpointPasteLimitWarning);
+ },
});
const newRootInstanceId = newInstanceIds.get(fragment.instances[0].id);
if (newRootInstanceId === undefined) {
diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.ts b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
index 69301e46a5..082a5a70dc 100644
--- a/apps/builder/app/shared/copy-paste/plugin-markdown.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
@@ -4,6 +4,8 @@ import { insertWebstudioFragmentAt } from "../instance-utils";
import { denormalizeSrcProps } from "./asset-upload";
import { generateFragmentFromHtml } from "../html";
import type { Plugin } from "./init-copy-paste";
+import { builderApi } from "../builder-api";
+import { breakpointPasteLimitWarning } from "../breakpoints";
const parse = (clipboardData: string) => {
const html = micromark(clipboardData, "utf-8", {
@@ -32,7 +34,11 @@ export const markdown: Plugin = {
return false;
}
fragment = await denormalizeSrcProps(fragment);
- return insertWebstudioFragmentAt(fragment);
+ return insertWebstudioFragmentAt(fragment, undefined, undefined, {
+ onBreakpointLimitMerge: () => {
+ builderApi.toast.warn(breakpointPasteLimitWarning);
+ },
+ });
},
};
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts
index f6fda77577..b3086d65e4 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.ts
@@ -20,6 +20,7 @@ import { denormalizeSrcProps } from "../asset-upload";
import { nanoHash } from "~/shared/nano-hash";
import { findAvailableVariables } from "~/shared/data-variables";
import type { Plugin } from "../init-copy-paste";
+import { breakpointPasteLimitWarning } from "~/shared/breakpoints";
const { toast } = builderApi;
@@ -191,6 +192,9 @@ const onPaste = async (clipboardData: string) => {
startingInstanceId: insertable.parentSelector[0],
}),
projectId: project.id,
+ onBreakpointLimitMerge: () => {
+ toast.warn(breakpointPasteLimitWarning);
+ },
});
const children = fragment.children
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsx
index 7207bab592..1afda156e7 100644
--- a/apps/builder/app/shared/instance-utils.test.tsx
+++ b/apps/builder/app/shared/instance-utils.test.tsx
@@ -1385,6 +1385,40 @@ describe("insert webstudio fragment copy", () => {
]);
});
+ test("reports breakpoint limit merge once when inserting fragment", () => {
+ const breakpoints = toMap<Breakpoint>([
+ { id: "base", label: "base" },
+ { id: "0", label: "320", maxWidth: 320 },
+ { id: "1", label: "480", maxWidth: 480 },
+ { id: "2", label: "768", maxWidth: 768 },
+ { id: "3", label: "1024", maxWidth: 1024 },
+ { id: "4", label: "1280", minWidth: 1280 },
+ { id: "5", label: "1440", minWidth: 1440 },
+ { id: "6", label: "1920", minWidth: 1920 },
+ { id: "7", label: "2560", minWidth: 2560 },
+ ]);
+ const data = getWebstudioDataStub({ breakpoints });
+ let mergeCount = 0;
+
+ insertWebstudioFragmentCopy({
+ data,
+ fragment: {
+ ...emptyFragment,
+ breakpoints: [
+ { id: "new_medium", label: "Medium", minWidth: 1500 },
+ { id: "new_large", label: "Large", minWidth: 1700 },
+ ],
+ },
+ availableVariables: [],
+ projectId: "",
+ onBreakpointLimitMerge: () => {
+ mergeCount += 1;
+ },
+ });
+
+ expect(mergeCount).toBe(1);
+ });
+
// Case 2: Same styles AND same name -> reuse existing token
test("token with same styles and same name reuses existing token", () => {
const breakpoints = toMap<Breakpoint>([{ id: "base", label: "base" }]);
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 85493241b2..4cee4e494e 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -38,7 +38,7 @@ import {
} from "@webstudio-is/sdk";
import { detectTokenConflicts } from "./style-source-utils";
import { type ConflictResolution } from "./token-conflict-dialog";
-import { buildMergedBreakpointIds } from "./breakpoints-utils";
+import { buildMergedBreakpointIds, maxBreakpoints } from "./breakpoints-utils";
import {
$registeredComponentMetas,
$registeredTemplates,
@@ -338,7 +338,8 @@ export const insertWebstudioElementAt = (insertable?: Insertable) => {
export const insertWebstudioFragmentAt = (
fragment: WebstudioFragment,
insertable?: Insertable,
- conflictResolution?: ConflictResolution
+ conflictResolution?: ConflictResolution,
+ options?: { onBreakpointLimitMerge?: () => void }
): boolean => {
const hasChildren = fragment.children.length > 0;
const hasTokens = fragment.styleSources.length > 0;
@@ -358,6 +359,7 @@ export const insertWebstudioFragmentAt = (
availableVariables: [],
projectId: project.id,
conflictResolution,
+ onBreakpointLimitMerge: options?.onBreakpointLimitMerge,
});
});
return true;
@@ -385,6 +387,7 @@ export const insertWebstudioFragmentAt = (
}),
projectId: project.id,
conflictResolution,
+ onBreakpointLimitMerge: options?.onBreakpointLimitMerge,
});
const children: Instance["children"] = fragment.children.map((child) => {
if (child.type === "id") {
@@ -1348,12 +1351,14 @@ export const insertWebstudioFragmentCopy = ({
availableVariables,
projectId,
conflictResolution = "theirs",
+ onBreakpointLimitMerge,
}: {
data: Omit<WebstudioData, "pages">;
fragment: WebstudioFragment;
availableVariables: DataSource[];
projectId: Project["id"];
conflictResolution?: ConflictResolution;
+ onBreakpointLimitMerge?: () => void;
}) => {
const newInstanceIds = new Map<Instance["id"], Instance["id"]>();
const newDataSourceIds = new Map<DataSource["id"], DataSource["id"]>();
@@ -1411,10 +1416,20 @@ export const insertWebstudioFragmentCopy = ({
// merge breakpoints
+ let didMergeBreakpointsDueToLimit = false;
const mergedBreakpointIds = buildMergedBreakpointIds(
fragment.breakpoints,
- breakpoints
+ breakpoints,
+ {
+ maxBreakpointCount: maxBreakpoints,
+ onBreakpointMergedDueToLimit: () => {
+ didMergeBreakpointsDueToLimit = true;
+ },
+ }
);
+ if (didMergeBreakpointsDueToLimit) {
+ onBreakpointLimitMerge?.();
+ }
for (const newBreakpoint of fragment.breakpoints) {
if (mergedBreakpointIds.has(newBreakpoint.id) === false) {
breakpoints.set(newBreakpoint.id, newBreakpoint);
@@ -1869,7 +1884,8 @@ export const detectFragmentTokenConflicts = ({
const mergedBreakpointIds = buildMergedBreakpointIds(
fragment.breakpoints,
- data.breakpoints
+ data.breakpoints,
+ { maxBreakpointCount: maxBreakpoints }
);
return detectTokenConflicts({
@@ -1925,7 +1941,8 @@ export const detectPageTokenConflicts = ({
const mergedBreakpointIds = buildMergedBreakpointIds(
combinedBreakpoints,
- data.breakpoints
+ data.breakpoints,
+ { maxBreakpointCount: maxBreakpoints }
);
return detectTokenConflicts({