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.

experimental: Only add a breakpoint when there are styles on that breakpoint (#3701)

Oleg Isonenfb9b6b3bd76e
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
index f264ab55b5..632cdecccc 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
@@ -2798,10 +2798,7 @@ describe("Styles", () => {
"background-color: hsla(150, 100.00%, 50.00%, 1.00);",
},
xl: {
- styleLess: "background-color: hsla(69, 100.00%, 50.00%, 1.00);",
- },
- xxl: {
- styleLess: "background-color: hsla(14, 100.00%, 50.00%, 1.00);",
+ styleLess: "",
},
medium: {
styleLess:
@@ -2822,6 +2819,14 @@ describe("Styles", () => {
},
});
+ expect(fragment.breakpoints).toEqual([
+ { id: expect.any(String), label: "base" },
+ { id: expect.any(String), label: "large", minWidth: 1280 },
+ { id: expect.any(String), label: "medium", maxWidth: 991 },
+ { id: expect.any(String), label: "small", maxWidth: 767 },
+ { id: expect.any(String), label: "tiny", maxWidth: 479 },
+ ]);
+
expect(toCss(fragment)).toMatchInlineSnapshot(`
"@media all {
Div Block 2 {
@@ -2847,16 +2852,6 @@ describe("Styles", () => {
Div Block 2 {
background-color: rgba(0, 255, 128, 1)
}
- }
- @media all and (min-width: 1440px) {
- Div Block 2 {
- background-color: rgba(217, 255, 0, 1)
- }
- }
- @media all and (min-width: 1920px) {
- Div Block 2 {
- background-color: rgba(255, 60, 0, 1)
- }
}"
`);
});
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts b/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
index e3d22d976b..52975b97d0 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts
@@ -1,12 +1,11 @@
import type {
Breakpoint,
- Breakpoints,
Instance,
WebstudioFragment,
} from "@webstudio-is/sdk";
import type { WfAsset, WfElementNode, WfNode, WfStyle } from "./schema";
import { nanoid } from "nanoid";
-import { $breakpoints, $styleSources } from "~/shared/nano-states";
+import { $styleSources } from "~/shared/nano-states";
import {
parseCss,
pseudoElements,
@@ -14,7 +13,6 @@ import {
} from "@webstudio-is/css-data";
import { kebabCase } from "change-case";
import { equalMedia, hyphenateProperty } from "@webstudio-is/css-engine";
-import { isBaseBreakpoint } from "~/shared/breakpoints";
import type { Styles as WfStylePresets } from "./__generated__/style-presets";
import { builderApi } from "~/shared/builder-api";
import { url } from "css-tree";
@@ -75,15 +73,6 @@ const parseVariantName = (variant: string) => {
};
};
-const findWsBreakpoint = (
- wfBreakpoint: WfBreakpoint,
- breakpoints: Breakpoints
-) => {
- return Array.from($breakpoints.get().values()).find((breakpoint) => {
- return equalMedia(breakpoint, wfBreakpoint);
- });
-};
-
// Apparently webflow supports this variable notation: `color: @var_relume-variable-color-neutral-1`
// Until actual variables are supported, we need to replace it with "unset",
// otherwise property will be skipped and that will result in an inherited value, which is more problematic.
@@ -139,75 +128,43 @@ const toParsedVariants = (variants: UnparsedVariants) => {
return parsedVariants;
};
-type BreakpointsByWfName = Map<WfBreakpointName, Breakpoint>;
-
-const addBreakpoints = (
- breakpoints: Breakpoints,
- fragment: WebstudioFragment
-) => {
- const add = (newBreakpoint: Breakpoint) => {
- const breakpoint = fragment.breakpoints.find((breakpoint) => {
- return equalMedia(breakpoint, newBreakpoint);
- });
- if (breakpoint === undefined) {
- fragment.breakpoints.push(newBreakpoint);
- }
- };
- // Creates a map of wf breakpoint name to ws breakpoint config:
- const wfBreakpointNameToId: BreakpointsByWfName = new Map();
- for (const [wfBreakpointName, wfBreakpoint] of wfBreakpoints) {
- if (wfBreakpointName === "base") {
- const baseBreakpoint = Array.from(breakpoints.values()).find(
- isBaseBreakpoint
- );
- if (baseBreakpoint) {
- wfBreakpointNameToId.set(wfBreakpointName, baseBreakpoint);
- add(baseBreakpoint);
- }
- continue;
- }
-
- const wsBreakpoint = findWsBreakpoint(wfBreakpoint, breakpoints);
- if (wsBreakpoint) {
- wfBreakpointNameToId.set(wfBreakpointName, wsBreakpoint);
- add(wsBreakpoint);
- continue;
- }
- const newBreakpoint: Breakpoint = {
- id: nanoid(),
- label: wfBreakpointName,
- ...(wfBreakpoint.minWidth !== undefined && {
- minWidth: wfBreakpoint.minWidth,
- }),
- ...(wfBreakpoint.maxWidth !== undefined && {
- maxWidth: wfBreakpoint.maxWidth,
- }),
- };
-
- add(newBreakpoint);
- wfBreakpointNameToId.set(wfBreakpointName, newBreakpoint);
- }
- return wfBreakpointNameToId;
-};
-
const addNodeStyles = ({
styleSourceId,
variants,
fragment,
- breakpointsByName,
}: {
styleSourceId: string;
variants: UnparsedVariants;
fragment: WebstudioFragment;
- breakpointsByName: BreakpointsByWfName;
}) => {
const parsedVariants = toParsedVariants(variants);
- for (const [breakpointName, styles] of parsedVariants) {
- const breakpoint = breakpointsByName.get(breakpointName);
- if (breakpoint === undefined) {
- console.error(`No breakpoint found for ${breakpointName}`);
+ for (const [wfBreakpointName, styles] of parsedVariants) {
+ if (styles.length === 0) {
+ // We don't want to add breakpoints if there are no styles defined on them
continue;
}
+ const wfBreakpoint = wfBreakpoints.get(wfBreakpointName);
+ if (wfBreakpoint === undefined) {
+ console.error(`Unknown breakpoint "${wfBreakpointName}"`);
+ continue;
+ }
+ let breakpoint = fragment.breakpoints.find((breakpoint) => {
+ return equalMedia(breakpoint, wfBreakpoint);
+ });
+
+ if (breakpoint === undefined) {
+ breakpoint = {
+ id: nanoid(),
+ label: wfBreakpointName,
+ ...(wfBreakpoint.minWidth !== undefined && {
+ minWidth: wfBreakpoint.minWidth,
+ }),
+ ...(wfBreakpoint.maxWidth !== undefined && {
+ maxWidth: wfBreakpoint.maxWidth,
+ }),
+ } satisfies Breakpoint;
+ fragment.breakpoints.push(breakpoint);
+ }
for (const style of styles) {
fragment.styles.push({
@@ -250,14 +207,12 @@ const addNodeTokenStyles = ({
variants,
instanceId,
fragment,
- breakpointsByName,
}: {
styleSourceId: string;
name: string;
variants: UnparsedVariants;
instanceId: Instance["id"];
fragment: WebstudioFragment;
- breakpointsByName: BreakpointsByWfName;
}) => {
fragment.styleSources.push({
type: "token",
@@ -271,7 +226,6 @@ const addNodeTokenStyles = ({
styleSourceId,
variants,
fragment,
- breakpointsByName,
});
};
@@ -280,13 +234,11 @@ const addNodeLocalStyles = ({
style,
instanceId,
fragment,
- breakpointsByName,
}: {
styleSourceId: string;
style: NonNullable<WfElementNode["data"]>["style"];
instanceId: Instance["id"];
fragment: WebstudioFragment;
- breakpointsByName: BreakpointsByWfName;
}) => {
let hasLocalStyles = false;
const instanceDataVariants = new Map<string, string>();
@@ -327,7 +279,6 @@ const addNodeLocalStyles = ({
styleSourceId,
variants: instanceDataVariants,
fragment,
- breakpointsByName,
});
};
@@ -487,8 +438,6 @@ export const addStyles = async ({
continue;
}
- const breakpointsByName = addBreakpoints($breakpoints.get(), fragment);
-
for (const name of mapComponentAndPresetStyles(wfNode, stylePresets)) {
addNodeTokenStyles({
styleSourceId: await generateStyleSourceId(name),
@@ -507,7 +456,6 @@ export const addStyles = async ({
),
instanceId,
fragment,
- breakpointsByName,
});
addNodeLocalStyles({
@@ -515,7 +463,6 @@ export const addStyles = async ({
style: wfNode.data?.style,
instanceId,
fragment,
- breakpointsByName,
});
}
@@ -569,7 +516,6 @@ export const addStyles = async ({
variants,
instanceId,
fragment,
- breakpointsByName,
});
}
}