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
feat: Add Webstudio borders and shadows support to Tailwind conversion (#2390)
Ivan Starkovc3be5eac2a27
diff --git a/packages/css-data/src/tailwind-parser/parse.test.ts b/packages/css-data/src/tailwind-parser/parse.test.tsindex 67d767f4b8..b722822657 100644--- a/packages/css-data/src/tailwind-parser/parse.test.ts+++ b/packages/css-data/src/tailwind-parser/parse.test.ts@@ -117,6 +117,242 @@ describe("parseTailwindToWebstudio", () => {},},]+`);+ });++ test("shadow", async () => {+ const tailwindClasses = `shadow-md`;++ expect(await parseTailwindToWebstudio(tailwindClasses))+ .toMatchInlineSnapshot(`+[+ {+ "property": "boxShadow",+ "value": {+ "type": "layers",+ "value": [+ {+ "type": "tuple",+ "value": [+ {+ "type": "unit",+ "unit": "number",+ "value": 0,+ },+ {+ "type": "unit",+ "unit": "number",+ "value": 0,+ },+ {+ "alpha": 0,+ "b": 0,+ "g": 0,+ "r": 0,+ "type": "rgb",+ },+ ],+ },+ {+ "type": "tuple",+ "value": [+ {+ "type": "unit",+ "unit": "number",+ "value": 0,+ },+ {+ "type": "unit",+ "unit": "number",+ "value": 0,+ },+ {+ "alpha": 0,+ "b": 0,+ "g": 0,+ "r": 0,+ "type": "rgb",+ },+ ],+ },+ {+ "type": "tuple",+ "value": [+ {+ "type": "unit",+ "unit": "number",+ "value": 0,+ },+ {+ "type": "unit",+ "unit": "px",+ "value": 4,+ },+ {+ "type": "unit",+ "unit": "px",+ "value": 6,+ },+ {+ "type": "unit",+ "unit": "px",+ "value": -1,+ },+ {+ "alpha": 0.1,+ "b": 0,+ "g": 0,+ "r": 0,+ "type": "rgb",+ },+ ],+ },+ {+ "type": "tuple",+ "value": [+ {+ "type": "unit",+ "unit": "number",+ "value": 0,+ },+ {+ "type": "unit",+ "unit": "px",+ "value": 2,+ },+ {+ "type": "unit",+ "unit": "px",+ "value": 4,+ },+ {+ "type": "unit",+ "unit": "px",+ "value": -2,+ },+ {+ "alpha": 0.1,+ "b": 0,+ "g": 0,+ "r": 0,+ "type": "rgb",+ },+ ],+ },+ ],+ },+ },+]+`);+ });++ test("border", async () => {+ const tailwindClasses = `border border-sky-500`;++ expect(await parseTailwindToWebstudio(tailwindClasses))+ .toMatchInlineSnapshot(`+[+ {+ "property": "borderBottomWidth",+ "value": {+ "type": "unit",+ "unit": "px",+ "value": 1,+ },+ },+ {+ "property": "borderLeftWidth",+ "value": {+ "type": "unit",+ "unit": "px",+ "value": 1,+ },+ },+ {+ "property": "borderRightWidth",+ "value": {+ "type": "unit",+ "unit": "px",+ "value": 1,+ },+ },+ {+ "property": "borderTopWidth",+ "value": {+ "type": "unit",+ "unit": "px",+ "value": 1,+ },+ },+ {+ "property": "borderBottomColor",+ "value": {+ "alpha": 1,+ "b": 233,+ "g": 165,+ "r": 14,+ "type": "rgb",+ },+ },+ {+ "property": "borderLeftColor",+ "value": {+ "alpha": 1,+ "b": 233,+ "g": 165,+ "r": 14,+ "type": "rgb",+ },+ },+ {+ "property": "borderRightColor",+ "value": {+ "alpha": 1,+ "b": 233,+ "g": 165,+ "r": 14,+ "type": "rgb",+ },+ },+ {+ "property": "borderTopColor",+ "value": {+ "alpha": 1,+ "b": 233,+ "g": 165,+ "r": 14,+ "type": "rgb",+ },+ },+ {+ "property": "borderTopStyle",+ "value": {+ "type": "keyword",+ "value": "solid",+ },+ },+ {+ "property": "borderRightStyle",+ "value": {+ "type": "keyword",+ "value": "solid",+ },+ },+ {+ "property": "borderBottomStyle",+ "value": {+ "type": "keyword",+ "value": "solid",+ },+ },+ {+ "property": "borderLeftStyle",+ "value": {+ "type": "keyword",+ "value": "solid",+ },+ },+]`);});});diff --git a/packages/css-data/src/tailwind-parser/parse.ts b/packages/css-data/src/tailwind-parser/parse.tsindex f459091d88..c69a853d0e 100644--- a/packages/css-data/src/tailwind-parser/parse.ts+++ b/packages/css-data/src/tailwind-parser/parse.ts@@ -8,6 +8,7 @@ import { substituteVariables } from "./substitute";import warnOnce from "warn-once";import { parseCssValue } from "../parse-css-value";import { LayersValue, type StyleProperty } from "@webstudio-is/css-engine";+import { parseBoxShadow } from "../property-parsers/box-shadow";let unoLazy: UnoGenerator<Theme> | undefined = undefined;@@ -97,6 +98,56 @@ const postprocessBackgrounds = (});};+/**+ * Tailwind by default has border-style: solid, but WebStudio doesn't.+ * Provide boder-style: solid if border-width is provided.+ **/+const postprocessBorder = (styles: EmbedTemplateStyleDecl[]) => {+ const borderPairs = [+ ["borderTopWidth", "borderTopStyle"],+ ["borderRightWidth", "borderRightStyle"],+ ["borderBottomWidth", "borderBottomStyle"],+ ["borderLeftWidth", "borderLeftStyle"],+ ] as const;++ const resultStyles = [...styles];++ for (const [borderWidthProperty, borderStyleProperty] of borderPairs) {+ const hasWidth = styles.some(+ (style) => style.property === borderWidthProperty+ );+ const hasStyle = styles.some(+ (style) => style.property === borderStyleProperty+ );+ if (hasWidth && hasStyle === false) {+ resultStyles.push({+ property: borderStyleProperty,+ value: {+ type: "keyword",+ value: "solid",+ },+ });+ }+ }+ return resultStyles;+};++/**+ * In WebStudio, box-shadow property is managed using a specialized "layer" type.+ **/+const postprocessBoxShadows = (styles: EmbedTemplateStyleDecl[]) => {+ return styles.map((style) => {+ if (style.property === "boxShadow" && style.value.type === "unparsed") {+ const shadowStyle = parseBoxShadow(style.value.value);+ return {+ property: style.property,+ value: shadowStyle,+ };+ }+ return style;+ });+};+/*** Parses Tailwind classes to webstudio template format.*/@@ -108,6 +159,8 @@ export const parseTailwindToWebstudio = async (let styles = parseCssToWebstudio(css);// postprocessingstyles = postprocessBackgrounds(styles, warn);+ styles = postprocessBorder(styles);+ styles = postprocessBoxShadows(styles);return styles;};diff --git a/packages/css-data/src/tailwind-parser/shorthand.test.ts b/packages/css-data/src/tailwind-parser/shorthand.test.tsindex 6aa611e24c..afd1fca85d 100644--- a/packages/css-data/src/tailwind-parser/shorthand.test.ts+++ b/packages/css-data/src/tailwind-parser/shorthand.test.ts@@ -17,7 +17,9 @@ describe("expandTailwindShorthand", () => {});test("handle classes without a modifier", () => {- expect(expandTailwindShorthand("none")).toBe("grow-0 shrink-0 basis-auto");+ expect(expandTailwindShorthand("flex-none")).toBe(+ "grow-0 shrink-0 basis-auto"+ );});test("handle dynamic values", () => {@@ -27,4 +29,16 @@ describe("expandTailwindShorthand", () => {test("expand inset-x-4 to right-4 left-4", () => {expect(expandTailwindShorthand("inset-x-4")).toBe("right-4 left-4");});++ test("do not expand border color", () => {+ expect(expandTailwindShorthand("border-sky-500")).toBe(+ "border-t-sky-500 border-r-sky-500 border-b-sky-500 border-l-sky-500"+ );+ });++ test("expand border width", () => {+ expect(expandTailwindShorthand("border border-sky-500")).toBe(+ "border-t border-r border-b border-l border-t-sky-500 border-r-sky-500 border-b-sky-500 border-l-sky-500"+ );+ });});diff --git a/packages/css-data/src/tailwind-parser/shorthand.ts b/packages/css-data/src/tailwind-parser/shorthand.tsindex 0475c8d518..7a1bef1980 100644--- a/packages/css-data/src/tailwind-parser/shorthand.ts+++ b/packages/css-data/src/tailwind-parser/shorthand.ts@@ -4,24 +4,36 @@ export const expandShorthand = (property: string) => {};* Expands shorthand CSS properties into their long-form equivalents.* For example, 'mx' is expanded to 'mr' (margin-right) and 'ml' (margin-left).**/-export const expandTailwindShorthand = (classname: string) => {- let expanded = conflictingClassGroups[classname];+export const expandTailwindShorthand = (classnames: string) => {+ return classnames+ .trim()+ .split(/\s+/)+ .map((classname) => {+ const groupKey = Object.keys(conflictingClassGroups).find((key) =>+ classname.startsWith(key)+ );+ if (groupKey === undefined) {+ return classname;+ }- if (expanded) {- return expanded.join(" ");- }+ const group = conflictingClassGroups[groupKey];+ const modifier = classname.substring(groupKey.length);+ const expanded = group.map((cls) => `${cls}${modifier}`);- const parts = classname.split("-");- const modifier = parts.pop();- const base = parts.join("-");-- expanded = conflictingClassGroups[base];-- if (expanded) {- return expanded.map((cls) => `${cls}-${modifier}`).join(" ");- }+ return expanded.join(" ");+ })+ .join(" ");+};- return classname;+const orderByKeysDesc = (obj: Record<string, string[]>) => {+ const ordered: Record<string, string[]> = {};+ Object.keys(obj)+ .sort()+ .reverse()+ .forEach((key) => {+ ordered[key] = obj[key];+ });+ return ordered;};/**@@ -32,7 +44,7 @@ export const expandTailwindShorthand = (classname: string) => {* MIT License* Copyright (c) 2021 Dany Castillo**/-const conflictingClassGroups: Record<string, string[]> = {+const conflictingClassGroups: Record<string, string[]> = orderByKeysDesc({overflow: ["overflow-x", "overflow-y"],overscroll: ["overscroll-x", "overscroll-y"],inset: ["top", "right", "bottom", "left"],@@ -41,7 +53,7 @@ const conflictingClassGroups: Record<string, string[]> = {"flex-initial": ["grow-0", "shrink", "basis-auto"], // 0 1 auto"flex-auto": ["grow", "shrink", "basis-auto"], // 1 1 auto- none: ["grow-0", "shrink-0", "basis-auto"], // 0 0 auto,+ "flex-none": ["grow-0", "shrink-0", "basis-auto"], // 0 0 auto,"flex-1": ["grow", "shrink", "basis-[0%]"], // 1 1 0%,gap: ["gap-x", "gap-y"],@@ -61,18 +73,8 @@ const conflictingClassGroups: Record<string, string[]> = {"border-spacing": ["border-spacing-x", "border-spacing-y"],- "border-w": ["border-w-t", "border-w-r", "border-w-b", "border-w-l"],- "border-w-x": ["border-w-r", "border-w-l"],- "border-w-y": ["border-w-t", "border-w-b"],- /*- We are supporting shorthand for border-color- "border-color": [- "border-color-t",- "border-color-r",- "border-color-b",- "border-color-l",- ],- "border-color-x": ["border-color-r", "border-color-l"],- "border-color-y": ["border-color-t", "border-color-b"],- */-};+ "border-x": ["border-r", "border-l"],+ "border-y": ["border-t", "border-b"],++ border: ["border-t", "border-r", "border-b", "border-l"],+});