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.

Webflow breakpoints (#1340)

Oleg Isonen172a6da63512
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx b/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx
index 8faf9d0481..03cad66f40 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx
+++ b/apps/builder/app/builder/features/breakpoints/breakpoints-editor.tsx
@@ -73,7 +73,7 @@ const BreakpointEditorItem = ({
<TextField
css={{ textAlign: "right", width: 50 }}
variant="ghost"
- defaultValue={breakpoint.minWidth}
+ defaultValue={breakpoint.minWidth ?? breakpoint.maxWidth ?? 0}
type="number"
name="minWidth"
min={0}
diff --git a/apps/builder/app/builder/features/breakpoints/breakpoints.tsx b/apps/builder/app/builder/features/breakpoints/breakpoints.tsx
index 8114d9324e..25b4a26df8 100644
--- a/apps/builder/app/builder/features/breakpoints/breakpoints.tsx
+++ b/apps/builder/app/builder/features/breakpoints/breakpoints.tsx
@@ -2,7 +2,6 @@ import { useEffect, useState } from "react";
import { useStore } from "@nanostores/react";
import store from "immerhin";
import type { Breakpoint } from "@webstudio-is/project-build";
-import { utils } from "@webstudio-is/project";
import {
theme,
DropdownMenu,
@@ -30,6 +29,8 @@ import {
selectedBreakpointIdStore,
selectedBreakpointStore,
} from "~/shared/nano-states/breakpoints";
+import { compareMedia } from "@webstudio-is/css-engine";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
export const Breakpoints = () => {
const [view, setView] = useState<
@@ -133,28 +134,31 @@ export const Breakpoints = () => {
)}
{view === "selector" && (
<>
- {utils.breakpoints.sort(breakpoints).map((breakpoint) => {
- return (
- <DropdownMenuCheckboxItem
- checked={breakpoint === selectedBreakpoint}
- key={breakpoint.id}
- onMouseEnter={() => {
- setBreakpointPreview(breakpoint);
- }}
- onMouseLeave={() => {
- setBreakpointPreview(selectedBreakpoint);
- }}
- onSelect={() => {
- selectedBreakpointIdStore.set(breakpoint.id);
- }}
- >
- {breakpoint.label}
- <DropdownMenuItemRightSlot>
- {breakpoint.minWidth}
- </DropdownMenuItemRightSlot>
- </DropdownMenuCheckboxItem>
- );
- })}
+ {Array.from(breakpoints.values())
+ .sort(compareMedia)
+ .reverse()
+ .map((breakpoint) => {
+ return (
+ <DropdownMenuCheckboxItem
+ checked={breakpoint === selectedBreakpoint}
+ key={breakpoint.id}
+ onMouseEnter={() => {
+ setBreakpointPreview(breakpoint);
+ }}
+ onMouseLeave={() => {
+ setBreakpointPreview(selectedBreakpoint);
+ }}
+ onSelect={() => {
+ selectedBreakpointIdStore.set(breakpoint.id);
+ }}
+ >
+ {breakpoint.label}
+ <DropdownMenuItemRightSlot>
+ {breakpoint.minWidth ?? breakpoint.maxWidth ?? "any"}
+ </DropdownMenuItemRightSlot>
+ </DropdownMenuCheckboxItem>
+ );
+ })}
<DropdownMenuSeparator />
<form>
<ZoomSetting />
@@ -162,16 +166,20 @@ export const Breakpoints = () => {
</form>
<DropdownMenuSeparator />
<Preview breakpoint={breakpointPreview} />
- <DropdownMenuSeparator />
- <DropdownMenuItem
- css={{ justifyContent: "center" }}
- onSelect={(event) => {
- event.preventDefault();
- setView("editor");
- }}
- >
- Edit breakpoints
- </DropdownMenuItem>
+ {isFeatureEnabled("breakpointsEditor") && (
+ <>
+ <DropdownMenuSeparator />
+ <DropdownMenuItem
+ css={{ justifyContent: "center" }}
+ onSelect={(event) => {
+ event.preventDefault();
+ setView("editor");
+ }}
+ >
+ Edit breakpoints
+ </DropdownMenuItem>
+ </>
+ )}
</>
)}
</DropdownMenuContent>
diff --git a/apps/builder/app/builder/features/breakpoints/preview.tsx b/apps/builder/app/builder/features/breakpoints/preview.tsx
index 10275e5136..899ec6dbba 100644
--- a/apps/builder/app/builder/features/breakpoints/preview.tsx
+++ b/apps/builder/app/builder/features/breakpoints/preview.tsx
@@ -28,7 +28,11 @@ export const Preview = ({ breakpoint }: PreviewProps) => {
<DeprecatedParagraph color="hint">
{breakpoint === undefined
? "No breakpoint selected"
- : `@media (min-width: ${breakpoint.minWidth}px)`}
+ : "minWidth" in breakpoint
+ ? `@media (min-width: ${breakpoint.minWidth}px)`
+ : "maxWidth" in breakpoint
+ ? `@media (max-width: ${breakpoint.maxWidth}px)`
+ : "@media all"}
</DeprecatedParagraph>
</Flex>
);
diff --git a/apps/builder/app/builder/features/breakpoints/use-next-breakpoint.ts b/apps/builder/app/builder/features/breakpoints/use-next-breakpoint.ts
index 2c72f94124..4ed8c98e74 100644
--- a/apps/builder/app/builder/features/breakpoints/use-next-breakpoint.ts
+++ b/apps/builder/app/builder/features/breakpoints/use-next-breakpoint.ts
@@ -1,8 +1,8 @@
import { useMemo } from "react";
import { useBreakpoints } from "~/shared/nano-states";
-import { utils } from "@webstudio-is/project";
import { useStore } from "@nanostores/react";
import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";
+import { compareMedia } from "@webstudio-is/css-engine";
/**
* Return the next breakpoint from the currently selected one, sorted by the `sort()`
@@ -12,7 +12,7 @@ export const useNextBreakpoint = () => {
const [breakpoints] = useBreakpoints();
const sortedBreakpoints = useMemo(
- () => utils.breakpoints.sort(breakpoints),
+ () => Array.from(breakpoints.values()).sort(compareMedia),
[breakpoints]
);
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 40d61d25ca..b6a7f7751c 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
@@ -27,7 +27,7 @@ export const useUpdateCanvasWidth = () => {
if (isPreviewMode || selectedBreakpoint === undefined) {
return;
}
- const maxWidthBelowNextBreakpoint = (nextBreakpoint?.minWidth ?? 0) - 1;
+ const maxWidthBelowNextBreakpoint = (nextBreakpoint?.minWidth ?? 1) - 1;
const initialMaxAvailableWidth = initialWorkspaceRect.current
? Math.min(
maxWidthBelowNextBreakpoint,
diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.ts
index 309c364ad1..1b3c835281 100644
--- a/apps/builder/app/builder/features/style-panel/shared/style-info.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.ts
@@ -2,7 +2,6 @@ import { useMemo } from "react";
import { useStore } from "@nanostores/react";
import type { Style, StyleProperty, StyleValue } from "@webstudio-is/css-data";
import { properties } from "@webstudio-is/css-data";
-import { utils } from "@webstudio-is/project";
import type {
Breakpoints,
Instance,
@@ -23,6 +22,7 @@ import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";
import type { InstanceSelector } from "~/shared/tree-utils";
import { getComponentMeta } from "@webstudio-is/react-sdk";
import type { htmlTags as HtmlTags } from "html-tags";
+import { compareMedia } from "@webstudio-is/css-engine";
type CascadedValueInfo = {
breakpointId: string;
@@ -112,7 +112,7 @@ export const getCascadedBreakpointIds = (
breakpoints: Breakpoints,
selectedBreakpointId?: string
) => {
- const sortedBreakpoints = utils.breakpoints.sort(breakpoints);
+ const sortedBreakpoints = Array.from(breakpoints.values()).sort(compareMedia);
const cascadedBreakpointIds: string[] = [];
for (const breakpoint of sortedBreakpoints) {
if (breakpoint.id === selectedBreakpointId) {
diff --git a/apps/builder/app/builder/features/style-panel/style-panel.tsx b/apps/builder/app/builder/features/style-panel/style-panel.tsx
index 92b68734f0..a8a443115c 100644
--- a/apps/builder/app/builder/features/style-panel/style-panel.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-panel.tsx
@@ -45,7 +45,11 @@ export const StylePanel = ({ selectedInstance, publish }: StylePanelProps) => {
{`Please increase the canvas width.`}
</DeprecatedParagraph>
<DeprecatedParagraph>
- {`"${breakpoint.label}" breakpoint minimum width is ${breakpoint.minWidth}px.`}
+ {"minWidth" in breakpoint
+ ? `"${breakpoint.label}" breakpoint minimum width is ${breakpoint.minWidth}px.`
+ : "maxWidth" in breakpoint
+ ? `"${breakpoint.label}" breakpoint maximum width is ${breakpoint.maxWidth}px.`
+ : ""}
</DeprecatedParagraph>
</Card>
</Box>
diff --git a/apps/builder/app/shared/nano-states/breakpoints.ts b/apps/builder/app/shared/nano-states/breakpoints.ts
index daa1ef1796..7070de52ef 100644
--- a/apps/builder/app/shared/nano-states/breakpoints.ts
+++ b/apps/builder/app/shared/nano-states/breakpoints.ts
@@ -1,7 +1,7 @@
import { atom, computed } from "nanostores";
import type { Breakpoint } from "@webstudio-is/project-build";
-import { utils } from "@webstudio-is/project";
import { breakpointsContainer } from "./nano-states";
+import { compareMedia } from "@webstudio-is/css-engine";
export const minZoom = 10;
const maxZoom = 100;
@@ -29,7 +29,9 @@ export const selectedBreakpointStore = computed(
? undefined
: breakpoints.get(selectedBreakpointId);
// initially set first breakpoint as selected breakpoint
- const fallbackBreakpoint = utils.breakpoints.sort(breakpoints).at(0);
+ const fallbackBreakpoint = Array.from(breakpoints.values())
+ .sort(compareMedia)
+ .at(0);
return matchedBreakpoint ?? fallbackBreakpoint;
}
);
@@ -40,7 +42,9 @@ export const selectedBreakpointStore = computed(
export const selectBreakpointByOrderNumber = (orderNumber: number) => {
const breakpoints = breakpointsContainer.get();
const index = orderNumber - 1;
- const breakpoint = utils.breakpoints.sort(breakpoints).at(index);
+ const breakpoint = Array.from(breakpoints.values())
+ .sort(compareMedia)
+ .at(index);
if (breakpoint) {
selectedBreakpointIdStore.set(breakpoint.id);
}
diff --git a/packages/css-engine/src/core/compare-media.test.ts b/packages/css-engine/src/core/compare-media.test.ts
new file mode 100644
index 0000000000..58d01b6a4c
--- /dev/null
+++ b/packages/css-engine/src/core/compare-media.test.ts
@@ -0,0 +1,40 @@
+import { describe, test, expect } from "@jest/globals";
+import { compareMedia } from "./compare-media";
+
+describe("Compare media", () => {
+ test("mobile first", () => {
+ const initial = [
+ { minWidth: 0 },
+ { minWidth: 768 },
+ { minWidth: 1024 },
+ { minWidth: 1280 },
+ ];
+ expect(initial.sort(compareMedia)).toStrictEqual(initial);
+ });
+
+ test("random minWidth", () => {
+ const initial = [{ minWidth: 0 }, { minWidth: 3 }, { minWidth: 2 }];
+ const sorted = [{ minWidth: 0 }, { minWidth: 2 }, { minWidth: 3 }];
+ expect(initial.sort(compareMedia)).toStrictEqual(sorted);
+ });
+
+ test("webflow", () => {
+ const initial = [
+ { maxWidth: 991 },
+ { maxWidth: 767 },
+ { maxWidth: 479 },
+ { minWidth: 1280 },
+ { minWidth: 1440 },
+ { minWidth: 1920 },
+ ];
+ const sorted = [
+ { maxWidth: 991 },
+ { maxWidth: 767 },
+ { maxWidth: 479 },
+ { minWidth: 1280 },
+ { minWidth: 1440 },
+ { minWidth: 1920 },
+ ];
+ expect(initial.sort(compareMedia)).toStrictEqual(sorted);
+ });
+});
diff --git a/packages/css-engine/src/core/compare-media.ts b/packages/css-engine/src/core/compare-media.ts
new file mode 100644
index 0000000000..922459c551
--- /dev/null
+++ b/packages/css-engine/src/core/compare-media.ts
@@ -0,0 +1,18 @@
+import type { MediaRuleOptions } from "./rules";
+
+/**
+ * Sort by minWidth descending or maxWidth ascending
+ * We want media querries with bigger minWidth to override the smaller once, but the smaller maxWidth to override the bigger once.
+ */
+export const compareMedia = (
+ optionA: MediaRuleOptions,
+ optionB: MediaRuleOptions
+) => {
+ if (optionA?.minWidth !== undefined && optionB?.minWidth !== undefined) {
+ return optionA.minWidth - optionB.minWidth;
+ }
+ if (optionA?.maxWidth !== undefined && optionB?.maxWidth !== undefined) {
+ return optionB.maxWidth - optionA.maxWidth;
+ }
+ return "minWidth" in optionA ? 1 : -1;
+};
diff --git a/packages/css-engine/src/core/css-engine.ts b/packages/css-engine/src/core/css-engine.ts
index 774ff4a724..671800230d 100644
--- a/packages/css-engine/src/core/css-engine.ts
+++ b/packages/css-engine/src/core/css-engine.ts
@@ -7,6 +7,7 @@ import {
type FontFaceOptions,
type MediaRuleOptions,
} from "./rules";
+import { compareMedia } from "./compare-media";
import { StyleElement } from "./style-element";
import { StyleSheet } from "./style-sheet";
@@ -90,7 +91,11 @@ export class CssEngine {
for (const plaintextRule of this.#plainRules.values()) {
css.push(plaintextRule.cssText);
}
- for (const mediaRule of MediaRule.sort(this.#mediaRules.values())) {
+
+ const sortedMediaRules = Array.from(this.#mediaRules.values()).sort(
+ (ruleA, ruleB) => compareMedia(ruleA.options, ruleB.options)
+ );
+ for (const mediaRule of sortedMediaRules) {
const { cssText } = mediaRule;
if (cssText !== "") {
css.push(cssText);
diff --git a/packages/css-engine/src/core/index.ts b/packages/css-engine/src/core/index.ts
index 1a7f4a2858..77823b58f1 100644
--- a/packages/css-engine/src/core/index.ts
+++ b/packages/css-engine/src/core/index.ts
@@ -10,3 +10,4 @@ export * from "./create-css-engine";
export * from "./to-value";
export * from "./match-media";
export * from "./equal-media";
+export * from "./compare-media";
diff --git a/packages/css-engine/src/core/rules.ts b/packages/css-engine/src/core/rules.ts
index 6359ed7b70..52831a71ff 100644
--- a/packages/css-engine/src/core/rules.ts
+++ b/packages/css-engine/src/core/rules.ts
@@ -73,18 +73,6 @@ export type MediaRuleOptions = {
};
export class MediaRule {
- // Sort media rules by minWidth.
- // Needed to ensure that more specific media rules are inserted after less specific ones.
- // So that they get a higher specificity.
- // @todo remove this once we have ability to control order from the UI
- static sort(mediaRules: Iterable<MediaRule>) {
- return Array.from(mediaRules).sort((ruleA, ruleB) => {
- return (
- (ruleA.options.minWidth ?? -Number.MAX_SAFE_INTEGER) -
- (ruleB.options.minWidth ?? -Number.MAX_SAFE_INTEGER)
- );
- });
- }
options: MediaRuleOptions;
rules: Array<StyleRule | PlaintextRule> = [];
#mediaType;
diff --git a/packages/feature-flags/src/flags.ts b/packages/feature-flags/src/flags.ts
index ea5c5e0ad9..855c4b69b5 100644
--- a/packages/feature-flags/src/flags.ts
+++ b/packages/feature-flags/src/flags.ts
@@ -2,3 +2,4 @@ export const example = false;
export const dark = false;
export const unsupportedBrowsers = false;
export const displayContents = false;
+export const breakpointsEditor = false;
diff --git a/packages/project-build/src/db/build.ts b/packages/project-build/src/db/build.ts
index a19dffc088..08ade6410c 100644
--- a/packages/project-build/src/db/build.ts
+++ b/packages/project-build/src/db/build.ts
@@ -188,7 +188,6 @@ export async function createBuild(
newInstances = props.sourceBuild.instances;
newPages = props.sourceBuild.pages;
}
-
await client.build.create({
data: {
projectId: props.projectId,
diff --git a/packages/project-build/src/schema/breakpoints.ts b/packages/project-build/src/schema/breakpoints.ts
index 8e5be1b22a..21836a113b 100644
--- a/packages/project-build/src/schema/breakpoints.ts
+++ b/packages/project-build/src/schema/breakpoints.ts
@@ -19,11 +19,10 @@ export const Breakpoints = z.map(BreakpointId, Breakpoint);
export type Breakpoints = z.infer<typeof Breakpoints>;
-type BaseBreakpoint = Pick<Breakpoint, "label" | "minWidth">;
-
-export const initialBreakpoints: BaseBreakpoint[] = [
- { label: "Base", minWidth: 0 },
- { label: "Tablet", minWidth: 768 },
- { label: "Laptop", minWidth: 1024 },
- { label: "Desktop", minWidth: 1280 },
+export const initialBreakpoints: Array<Breakpoint> = [
+ { id: "placeholder", label: "Base" },
+ { id: "placeholder", label: "Tablet", maxWidth: 991 },
+ { id: "placeholder", label: "Mobile landscape", maxWidth: 767 },
+ { id: "placeholder", label: "Mobile portrait", maxWidth: 479 },
+ { id: "placeholder", label: "Large", minWidth: 1280 },
];
diff --git a/packages/project/src/index.ts b/packages/project/src/index.ts
index e7e07d6038..fe71648d4b 100644
--- a/packages/project/src/index.ts
+++ b/packages/project/src/index.ts
@@ -1,9 +1,3 @@
-import * as breakpointsUtils from "./shared/breakpoints";
-
-export const utils = {
- breakpoints: breakpointsUtils,
-} as const;
-
export * from "./shared/styles";
export * from "./shared/schema";
export * from "./shared/canvas-components";
diff --git a/packages/project/src/shared/breakpoints/index.ts b/packages/project/src/shared/breakpoints/index.ts
deleted file mode 100644
index c477172d8f..0000000000
--- a/packages/project/src/shared/breakpoints/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./sort";
diff --git a/packages/project/src/shared/breakpoints/sort.test.ts b/packages/project/src/shared/breakpoints/sort.test.ts
deleted file mode 100644
index 94eaaded16..0000000000
--- a/packages/project/src/shared/breakpoints/sort.test.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import { describe, test, expect } from "@jest/globals";
-import {
- type Breakpoints,
- initialBreakpoints,
-} from "@webstudio-is/project-build";
-import { nanoid } from "nanoid";
-import { sort } from "./sort";
-
-describe("Breakpoints sorting for visual rendering", () => {
- test("sort initial breakpoints", () => {
- const breakpoints: Breakpoints = new Map();
- for (const breakpoint of initialBreakpoints) {
- const id = nanoid();
- breakpoints.set(id, { ...breakpoint, id });
- }
-
- expect(sort(breakpoints)).toStrictEqual(Array.from(breakpoints.values()));
- });
-
- test("sort custom breakpoints", () => {
- const breakpoints = new Map([
- ["1", { id: "1", label: "0", minWidth: 0 }],
- ["2", { id: "2", label: "3", minWidth: 3 }],
- ["3", { id: "3", label: "2", minWidth: 2 }],
- ]);
- const sortedBreakpoints = [
- { id: "1", label: "0", minWidth: 0 },
- { id: "3", label: "2", minWidth: 2 },
- { id: "2", label: "3", minWidth: 3 },
- ];
- expect(sort(breakpoints)).toStrictEqual(sortedBreakpoints);
- });
-});
diff --git a/packages/project/src/shared/breakpoints/sort.ts b/packages/project/src/shared/breakpoints/sort.ts
deleted file mode 100644
index fa7469b73e..0000000000
--- a/packages/project/src/shared/breakpoints/sort.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import type { Breakpoints } from "@webstudio-is/project-build";
-
-/**
- * Sort by minWidth descending.
- * We want media querries with bigger minWidth to override the smaller once.
- * @todo remove this once we have ability to control order from the UI
- */
-export const sort = (breakpoints: Breakpoints) => {
- return Array.from(breakpoints.values()).sort((breakpointA, breakpointB) => {
- return (breakpointA.minWidth ?? 0) - (breakpointB.minWidth ?? 0);
- });
-};