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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
experimental: support webflow local styles (#3695)
Bogdan Chadkin0e457fe0adf5
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.tsxindex 8086b65e27..38b57c99a0 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@@ -662,30 +662,42 @@ test("H Flex", async () => {expect(toCss(fragment)).toMatchInlineSnapshot(`""`);});-test("QuickStack", async () => {+test("QuickStack with instance styles", async () => {const fragment = await toWebstudioFragment({type: "@webflow/XscpData",payload: {nodes: [{- _id: "91782272-bf55-194d-ce85-9ddc69c51dee",+ _id: "aeb3895f-67c9-b5a0-69b4-7960b893ec04",type: "Layout",tag: "div",classes: [],children: [- "91782272-bf55-194d-ce85-9ddc69c51def",- "91782272-bf55-194d-ce85-9ddc69c51df0",+ "ca264069-ff87-c2e5-7dc0-a4c0ec0e9146",+ "ec30cdfd-1a60-eeb0-1bfa-1df6f1956ed5",],+ data: {+ style: {+ base: {+ main: {+ noPseudo: {+ gridTemplateColumns: "1fr 1fr",+ gridTemplateRows: "auto",+ },+ },+ },+ },+ },},{- _id: "91782272-bf55-194d-ce85-9ddc69c51def",+ _id: "ca264069-ff87-c2e5-7dc0-a4c0ec0e9146",type: "Cell",tag: "div",classes: [],children: [],},{- _id: "91782272-bf55-194d-ce85-9ddc69c51df0",+ _id: "ec30cdfd-1a60-eeb0-1bfa-1df6f1956ed5",type: "Cell",tag: "div",classes: [],@@ -712,6 +724,10 @@ test("QuickStack", async () => {justify-content: center;padding: 20px}+ Local {+ grid-template-columns: 1fr 1fr;+ grid-template-rows: auto+ }wf-layout-layout {display: grid}diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/schema.ts b/apps/builder/app/shared/copy-paste/plugin-webflow/schema.tsindex 41934f9234..28cea8a2bb 100644--- a/apps/builder/app/shared/copy-paste/plugin-webflow/schema.ts+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/schema.ts@@ -2,6 +2,16 @@ import { z } from "zod";const Attr = z.object({ id: z.string() }).partial();+const styleBase = z.string();++const styleBreakpoint = z.string();++const stylePseudo = z.string();++const styleProperty = z.string();++const styleValue = z.string();+const WfNodeData = z.object({attr: Attr.optional(),xattr: z.array(z.object({ name: z.string(), value: z.string() })).optional(),@@ -10,6 +20,15 @@ const WfNodeData = z.object({conditions: z.array(z.boolean()),}).optional(),+ style: z+ .record(+ styleBase,+ z.record(+ styleBreakpoint,+ z.record(stylePseudo, z.record(styleProperty, styleValue))+ )+ )+ .optional(),});const WfBaseNode = z.object({diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts b/apps/builder/app/shared/copy-paste/plugin-webflow/styles.tsindex 7b3c4b3a41..393814ba65 100644--- a/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/styles.ts@@ -13,7 +13,7 @@ import {type ParsedStyleDecl,} from "@webstudio-is/css-data";import { kebabCase } from "change-case";-import { equalMedia } from "@webstudio-is/css-engine";+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";@@ -191,6 +191,42 @@ const addBreakpoints = (};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}`);+ continue;+ }++ for (const style of styles) {+ fragment.styles.push({+ styleSourceId,+ breakpointId: breakpoint.id,+ property: style.property,+ value: style.value,+ state: style.state,+ });+ if (style.value.type === "invalid") {+ const error = `Invalid style value: Local "${kebabCase(style.property)}: ${style.value.value}"`;+ toast.error(error);+ console.error(error);+ }+ }+ }+};++const addNodeTokenStyles = ({styleSourceId,name,variants,@@ -205,8 +241,6 @@ const addNodeStyles = ({fragment: WebstudioFragment;breakpointsByName: BreakpointsByWfName;}) => {- const parsedVariants = toParsedVariants(variants);-fragment.styleSources.push({type: "token",id: styleSourceId,@@ -222,28 +256,68 @@ const addNodeStyles = ({}styleSourceSelection.values.push(styleSourceId);- for (const [breakpointName, styles] of parsedVariants) {- const breakpoint = breakpointsByName.get(breakpointName);- if (breakpoint === undefined) {- console.error(`No breakpoint found for ${breakpointName}`);- continue;- }+ addNodeStyles({+ styleSourceId,+ variants,+ fragment,+ breakpointsByName,+ });+};- for (const style of styles) {- fragment.styles.push({- styleSourceId,- breakpointId: breakpoint.id,- property: style.property,- value: style.value,- state: style.state,- });- if (style.value.type === "invalid") {- const error = `Invalid style value: ${name} "${kebabCase(style.property)}: ${style.value.value}"`;- toast.error(error);- console.error(error);+const addNodeLocalStyles = ({+ styleSourceId,+ 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>();+ // @todo we don't know what is base, maybe the opposite of cringe?+ for (const baseStyle of Object.values(style ?? {})) {+ for (const [breakpoint, breakpointStyle] of Object.entries(baseStyle)) {+ let css = ``;+ // @todo we don't know format for pseudo+ // because webflow does not set styles on pseudo+ // in quick stack instance+ for (const pseudoStyle of Object.values(breakpointStyle)) {+ for (const [property, value] of Object.entries(pseudoStyle)) {+ hasLocalStyles = true;+ css += `${hyphenateProperty(property)}: ${value};`;+ }}+ instanceDataVariants.set(breakpoint, css);}}+ if (hasLocalStyles === false) {+ return;+ }++ fragment.styleSources.push({+ type: "local",+ id: styleSourceId,+ });+ let styleSourceSelection = fragment.styleSourceSelections.find(+ (selection) => selection.instanceId === instanceId+ );+ if (styleSourceSelection === undefined) {+ styleSourceSelection = { instanceId, values: [] };+ fragment.styleSourceSelections.push(styleSourceSelection);+ }+ styleSourceSelection.values.push(styleSourceId);++ addNodeStyles({+ styleSourceId,+ variants: instanceDataVariants,+ fragment,+ breakpointsByName,+ });};const mapComponentAndPresetStyles = (@@ -369,7 +443,7 @@ export const addStyles = async (const breakpointsByName = addBreakpoints($breakpoints.get(), fragment);for (const name of mapComponentAndPresetStyles(wfNode, stylePresets)) {- addNodeStyles({+ addNodeTokenStyles({styleSourceId: await generateStyleSourceId(name),name,variants: new Map(@@ -388,6 +462,14 @@ export const addStyles = async (fragment,breakpointsByName,});++ addNodeLocalStyles({+ styleSourceId: await generateStyleSourceId(wfNode._id),+ style: wfNode.data?.style,+ instanceId,+ fragment,+ breakpointsByName,+ });}const instance = fragment.instances.find(@@ -423,7 +505,7 @@ export const addStyles = async (}});- addNodeStyles({+ addNodeTokenStyles({styleSourceId: await generateStyleSourceId(classId),name: style.name,variants,