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.

Support minWidth and maxWith in media queries, both are optional (#1305)

Oleg Isonene87eeaa006ab
diff --git a/apps/builder/app/builder/features/breakpoints/trigger-button.tsx b/apps/builder/app/builder/features/breakpoints/trigger-button.tsx
index ac620b20a6..a080049c1d 100644
--- a/apps/builder/app/builder/features/breakpoints/trigger-button.tsx
+++ b/apps/builder/app/builder/features/breakpoints/trigger-button.tsx
@@ -2,11 +2,11 @@ import { forwardRef, type ComponentProps, type ElementRef } from "react";
import { useStore } from "@nanostores/react";
import { Button, Text } from "@webstudio-is/design-system";
import { useCanvasWidth } from "~/builder/shared/nano-states";
-import { willRender } from "~/builder/shared/breakpoints";
import {
selectedBreakpointStore,
zoomStore,
} from "~/shared/nano-states/breakpoints";
+import { matchMedia } from "@webstudio-is/css-engine";
type TriggerButtonProps = ComponentProps<typeof Button>;
@@ -20,7 +20,7 @@ export const TriggerButton = forwardRef<
if (breakpoint === undefined) {
return null;
}
- const variant = willRender(breakpoint, canvasWidth) ? "contrast" : "subtle";
+ const variant = matchMedia(breakpoint, canvasWidth) ? "contrast" : "subtle";
return (
<Button {...props} ref={ref} color="dark" aria-label="Show 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 dd968ccb79..40d61d25ca 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
@@ -36,7 +36,7 @@ export const useUpdateCanvasWidth = () => {
: maxWidthBelowNextBreakpoint;
const width = Math.max(
- selectedBreakpoint.minWidth,
+ selectedBreakpoint.minWidth ?? 0,
initialMaxAvailableWidth,
minWidth
);
diff --git a/apps/builder/app/builder/features/breakpoints/width-setting.tsx b/apps/builder/app/builder/features/breakpoints/width-setting.tsx
index 9a13448470..da29499324 100644
--- a/apps/builder/app/builder/features/breakpoints/width-setting.tsx
+++ b/apps/builder/app/builder/features/breakpoints/width-setting.tsx
@@ -27,13 +27,13 @@ export const WidthSetting = () => {
// We want to enable unconstrained resizing in a preview mode
const min = isPreviewMode
? minWidth
- : Math.max(minWidth, selectedBreakpoint.minWidth);
+ : Math.max(minWidth, selectedBreakpoint.minWidth ?? 0);
const max = isPreviewMode
? maxWidth
: Math.min(
maxWidth,
- nextBreakpoint ? nextBreakpoint.minWidth - 1 : maxWidth
+ nextBreakpoint ? (nextBreakpoint.minWidth ?? 1) - 1 : maxWidth
);
return (
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 f6cc30724e..758dbf4160 100644
--- a/apps/builder/app/builder/features/style-panel/style-panel.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-panel.tsx
@@ -8,11 +8,11 @@ import {
import type { Instance } from "@webstudio-is/project-build";
import type { Publish } from "~/shared/pubsub";
import { selectedBreakpointStore } from "~/shared/nano-states/breakpoints";
-import { willRender } from "~/builder/shared/breakpoints";
import { useStyleData } from "./shared/use-style-data";
import { StyleSettings } from "./style-settings";
import { useCanvasWidth } from "~/builder/shared/nano-states";
import { StyleSourcesSection } from "./style-source-section";
+import { matchMedia } from "@webstudio-is/css-engine";
type StylePanelProps = {
publish: Publish;
@@ -37,7 +37,7 @@ export const StylePanel = ({ selectedInstance, publish }: StylePanelProps) => {
return null;
}
- if (willRender(breakpoint, canvasWidth) === false) {
+ if (matchMedia(breakpoint, canvasWidth) === false) {
return (
<Box css={{ p: theme.spacing[5] }}>
<Card css={{ p: theme.spacing[9], mt: theme.spacing[9] }}>
diff --git a/apps/builder/app/builder/shared/breakpoints/index.ts b/apps/builder/app/builder/shared/breakpoints/index.ts
deleted file mode 100644
index 9227bc300f..0000000000
--- a/apps/builder/app/builder/shared/breakpoints/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./will-render";
diff --git a/apps/builder/app/builder/shared/breakpoints/will-render.ts b/apps/builder/app/builder/shared/breakpoints/will-render.ts
deleted file mode 100644
index 575b22ebc9..0000000000
--- a/apps/builder/app/builder/shared/breakpoints/will-render.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-import type { Breakpoint } from "@webstudio-is/project-build";
-
-export const willRender = (breakpoint: Breakpoint, canvasWidth: number) =>
- canvasWidth >= breakpoint.minWidth;
diff --git a/packages/css-engine/src/core/css-engine.test.ts b/packages/css-engine/src/core/css-engine.test.ts
index 1c600d01e4..296c5343b4 100644
--- a/packages/css-engine/src/core/css-engine.test.ts
+++ b/packages/css-engine/src/core/css-engine.test.ts
@@ -29,6 +29,45 @@ describe("CssEngine", () => {
beforeEach(reset);
+ test("minWidth media rule", () => {
+ engine.addMediaRule("0", { minWidth: 0 });
+ engine.addStyleRule(".c1", {
+ style: { color: { type: "keyword", value: "red" } },
+ breakpoint: "0",
+ });
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media all and (min-width: 0px) {
+ .c1 { color: red }
+ }"
+ `);
+ });
+
+ test("maxWidth media rule", () => {
+ engine.addMediaRule("0", { maxWidth: 1000 });
+ engine.addStyleRule(".c1", {
+ style: { color: { type: "keyword", value: "red" } },
+ breakpoint: "0",
+ });
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media all and (max-width: 1000px) {
+ .c1 { color: red }
+ }"
+ `);
+ });
+
+ test("maxWidth and maxWith media rule", () => {
+ engine.addMediaRule("0", { maxWidth: 1000, minWidth: 360 });
+ engine.addStyleRule(".c1", {
+ style: { color: { type: "keyword", value: "red" } },
+ breakpoint: "0",
+ });
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media all and (min-width: 360px) and (max-width: 1000px) {
+ .c1 { color: red }
+ }"
+ `);
+ });
+
test("use default media rule when there is no matching one registered", () => {
engine.addStyleRule(".c", {
style: style0,
diff --git a/packages/css-engine/src/core/index.ts b/packages/css-engine/src/core/index.ts
index a985ac4dc5..e8163937af 100644
--- a/packages/css-engine/src/core/index.ts
+++ b/packages/css-engine/src/core/index.ts
@@ -8,3 +8,4 @@ export type {
} from "./rules";
export * from "./create-css-engine";
export * from "./to-value";
+export * from "./match-media";
diff --git a/packages/css-engine/src/core/match-media.test.ts b/packages/css-engine/src/core/match-media.test.ts
new file mode 100644
index 0000000000..cfebb6c725
--- /dev/null
+++ b/packages/css-engine/src/core/match-media.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, test } from "@jest/globals";
+import { matchMedia } from "./match-media";
+
+describe("matchMedia", () => {
+ test("minWidth", () => {
+ expect(matchMedia({ minWidth: 100 }, 10)).toBe(false);
+ expect(matchMedia({ minWidth: 100 }, 100)).toBe(true);
+ expect(matchMedia({ minWidth: 100 }, 101)).toBe(true);
+ });
+
+ test("maxWidth", () => {
+ expect(matchMedia({ maxWidth: 100 }, 101)).toBe(false);
+ expect(matchMedia({ maxWidth: 100 }, 100)).toBe(true);
+ expect(matchMedia({ maxWidth: 100 }, 10)).toBe(true);
+ });
+
+ test("minWidth and maxWidth", () => {
+ expect(matchMedia({ maxWidth: 100, minWidth: 10 }, 9)).toBe(false);
+ expect(matchMedia({ maxWidth: 100, minWidth: 10 }, 101)).toBe(false);
+ expect(matchMedia({ maxWidth: 100, minWidth: 10 }, 100)).toBe(true);
+ expect(matchMedia({ maxWidth: 100, minWidth: 10 }, 10)).toBe(true);
+ expect(matchMedia({ maxWidth: 100, minWidth: 10 }, 11)).toBe(true);
+ });
+});
diff --git a/packages/css-engine/src/core/match-media.ts b/packages/css-engine/src/core/match-media.ts
new file mode 100644
index 0000000000..ae5d575be1
--- /dev/null
+++ b/packages/css-engine/src/core/match-media.ts
@@ -0,0 +1,7 @@
+import type { MediaRuleOptions } from "./rules";
+
+export const matchMedia = (options: MediaRuleOptions, width: number) => {
+ const minWidth = options.minWidth ?? Number.MIN_SAFE_INTEGER;
+ const maxWidth = options.maxWidth ?? Number.MAX_SAFE_INTEGER;
+ return width >= minWidth && width <= maxWidth;
+};
diff --git a/packages/css-engine/src/core/rules.ts b/packages/css-engine/src/core/rules.ts
index 29895d23e7..6359ed7b70 100644
--- a/packages/css-engine/src/core/rules.ts
+++ b/packages/css-engine/src/core/rules.ts
@@ -76,6 +76,7 @@ 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 (
@@ -106,15 +107,12 @@ export class MediaRule {
let conditionText = "";
const { minWidth, maxWidth } = this.options;
if (minWidth !== undefined) {
- conditionText = `min-width: ${minWidth}px`;
+ conditionText = ` and (min-width: ${minWidth}px)`;
}
if (maxWidth !== undefined) {
- conditionText = `max-width: ${maxWidth}px`;
+ conditionText += ` and (max-width: ${maxWidth}px)`;
}
- if (conditionText) {
- conditionText = `and (${conditionText}) `;
- }
- return `@media ${this.#mediaType} ${conditionText}{\n${rules.join(
+ return `@media ${this.#mediaType}${conditionText} {\n${rules.join(
"\n"
)}\n}`;
}
diff --git a/packages/project-build/src/schema/breakpoints.ts b/packages/project-build/src/schema/breakpoints.ts
index aab5cd8b1a..8e5be1b22a 100644
--- a/packages/project-build/src/schema/breakpoints.ts
+++ b/packages/project-build/src/schema/breakpoints.ts
@@ -5,7 +5,8 @@ const BreakpointId = z.string();
export const Breakpoint = z.object({
id: BreakpointId,
label: z.string(),
- minWidth: z.number(),
+ minWidth: z.number().optional(),
+ maxWidth: z.number().optional(),
});
export type Breakpoint = z.infer<typeof Breakpoint>;
diff --git a/packages/project/src/shared/breakpoints/sort.ts b/packages/project/src/shared/breakpoints/sort.ts
index 3a8ad64eb7..fa7469b73e 100644
--- a/packages/project/src/shared/breakpoints/sort.ts
+++ b/packages/project/src/shared/breakpoints/sort.ts
@@ -3,9 +3,10 @@ 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 - breakpointB.minWidth;
+ return (breakpointA.minWidth ?? 0) - (breakpointB.minWidth ?? 0);
});
};