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.

refactor: drop tailwind shorthands (#3693)

Bogdan Chadkin56f9bc0689f3
diff --git a/packages/css-data/package.json b/packages/css-data/package.json
index 6d059a1e23..5e05d33b2e 100644
--- a/packages/css-data/package.json
+++ b/packages/css-data/package.json
@@ -37,8 +37,8 @@
"private": true,
"sideEffects": false,
"dependencies": {
- "@unocss/core": "^0.61.0",
- "@unocss/preset-uno": "^0.61.0",
+ "@unocss/core": "^0.61.2",
+ "@unocss/preset-uno": "^0.61.2",
"@webstudio-is/css-engine": "workspace:*",
"change-case": "^5.4.4",
"colord": "^2.9.3",
diff --git a/packages/css-data/src/shorthands.test.ts b/packages/css-data/src/shorthands.test.ts
index 4cad0abc49..480a898465 100644
--- a/packages/css-data/src/shorthands.test.ts
+++ b/packages/css-data/src/shorthands.test.ts
@@ -603,13 +603,13 @@ test("expand font-variant with css-wide keywords", () => {
test("expand text-decoration", () => {
expect(expandShorthands([["text-decoration", `underline`]])).toEqual([
["text-decoration-line", "underline"],
- ["text-decoration-style", "initial"],
- ["text-decoration-color", "initial"],
+ ["text-decoration-style", "solid"],
+ ["text-decoration-color", "currentcolor"],
]);
expect(expandShorthands([["text-decoration", `underline dotted`]])).toEqual([
["text-decoration-line", "underline"],
["text-decoration-style", "dotted"],
- ["text-decoration-color", "initial"],
+ ["text-decoration-color", "currentcolor"],
]);
expect(
expandShorthands([["text-decoration", `green wavy underline`]])
diff --git a/packages/css-data/src/shorthands.ts b/packages/css-data/src/shorthands.ts
index df4cbeabe4..a0d44103e2 100644
--- a/packages/css-data/src/shorthands.ts
+++ b/packages/css-data/src/shorthands.ts
@@ -1026,9 +1026,15 @@ const expandShorthand = function* (property: string, value: CssNode) {
],
value
);
- yield ["text-decoration-line", line ?? createInitialNode()] as const;
- yield ["text-decoration-style", style ?? createInitialNode()] as const;
- yield ["text-decoration-color", color ?? createInitialNode()] as const;
+ yield ["text-decoration-line", line ?? createIdentifier("none")] as const;
+ yield [
+ "text-decoration-style",
+ style ?? createIdentifier("solid"),
+ ] as const;
+ yield [
+ "text-decoration-color",
+ color ?? createIdentifier("currentcolor"),
+ ] as const;
break;
}
diff --git a/packages/css-data/src/tailwind-parser/parse.test.ts b/packages/css-data/src/tailwind-parser/parse.test.ts
index 58679c6092..f3ca115273 100644
--- a/packages/css-data/src/tailwind-parser/parse.test.ts
+++ b/packages/css-data/src/tailwind-parser/parse.test.ts
@@ -38,60 +38,43 @@ describe("parseTailwindToWebstudio", () => {
test("no-underline", async () => {
const tailwindClasses = `no-underline`;
- expect(await parseTailwindToWebstudio(tailwindClasses))
- .toMatchInlineSnapshot(`
-[
- {
- "property": "textDecorationLine",
- "value": {
- "type": "keyword",
- "value": "none",
- },
- },
-]
-`);
+ expect(await parseTailwindToWebstudio(tailwindClasses)).toEqual([
+ {
+ property: "textDecorationLine",
+ value: { type: "keyword", value: "none" },
+ },
+ {
+ property: "textDecorationStyle",
+ value: { type: "keyword", value: "solid" },
+ },
+ {
+ property: "textDecorationColor",
+ value: { type: "keyword", value: "currentColor" },
+ },
+ ]);
});
test("expand margins", async () => {
const tailwindClasses = `m-4`;
- expect(await parseTailwindToWebstudio(tailwindClasses))
- .toMatchInlineSnapshot(`
-[
- {
- "property": "marginBottom",
- "value": {
- "type": "unit",
- "unit": "rem",
- "value": 1,
- },
- },
- {
- "property": "marginLeft",
- "value": {
- "type": "unit",
- "unit": "rem",
- "value": 1,
- },
- },
- {
- "property": "marginRight",
- "value": {
- "type": "unit",
- "unit": "rem",
- "value": 1,
- },
- },
- {
- "property": "marginTop",
- "value": {
- "type": "unit",
- "unit": "rem",
- "value": 1,
- },
- },
-]
-`);
+ expect(await parseTailwindToWebstudio(tailwindClasses)).toEqual([
+ {
+ property: "marginTop",
+ value: { type: "unit", unit: "rem", value: 1 },
+ },
+ {
+ property: "marginRight",
+ value: { type: "unit", unit: "rem", value: 1 },
+ },
+ {
+ property: "marginBottom",
+ value: { type: "unit", unit: "rem", value: 1 },
+ },
+ {
+ property: "marginLeft",
+ value: { type: "unit", unit: "rem", value: 1 },
+ },
+ ]);
});
test("substitute variables - gradient", async () => {
@@ -269,123 +252,60 @@ describe("parseTailwindToWebstudio", () => {
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",
- },
- },
-]
-`);
+ expect(await parseTailwindToWebstudio(tailwindClasses)).toEqual([
+ {
+ property: "borderTopWidth",
+ value: { type: "unit", unit: "px", value: 1 },
+ },
+ {
+ property: "borderRightWidth",
+ value: { type: "unit", unit: "px", value: 1 },
+ },
+ {
+ property: "borderBottomWidth",
+ value: { type: "unit", unit: "px", value: 1 },
+ },
+ {
+ property: "borderLeftWidth",
+ value: { type: "unit", unit: "px", value: 1 },
+ },
+ {
+ property: "borderTopColor",
+ 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: "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: "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" },
+ },
+ ]);
});
});
describe("parseTailwindToCss", () => {
- test("expand margins", async () => {
- const tailwindClasses = `m-4`;
-
- expect(await parseTailwindToCss(tailwindClasses)).toMatchInlineSnapshot(
- `".mb-4{margin-bottom:1rem}.ml-4{margin-left:1rem}.mr-4{margin-right:1rem}.mt-4{margin-top:1rem}"`
- );
- });
-
test("substitute variables - gradient", async () => {
const tailwindClasses = `bg-gradient-to-r from-indigo-500`;
diff --git a/packages/css-data/src/tailwind-parser/parse.ts b/packages/css-data/src/tailwind-parser/parse.ts
index 7f126ea59f..44dad9c42e 100644
--- a/packages/css-data/src/tailwind-parser/parse.ts
+++ b/packages/css-data/src/tailwind-parser/parse.ts
@@ -1,9 +1,8 @@
import { UnoGenerator, createGenerator } from "@unocss/core";
import { type Theme, presetUno } from "@unocss/preset-uno";
+import warnOnce from "warn-once";
import type { EmbedTemplateStyleDecl } from "@webstudio-is/react-sdk";
-import { expandTailwindShorthand } from "./shorthand";
import { substituteVariables } from "./substitute";
-import warnOnce from "warn-once";
import { parseCss } from "../parse-css";
let unoLazy: UnoGenerator<Theme> | undefined = undefined;
@@ -19,8 +18,7 @@ const uno = () => {
* Parses Tailwind classes to CSS by expanding shorthands and substituting variables.
*/
export const parseTailwindToCss = async (classes: string, warn = warnOnce) => {
- const expandedClasses = expandTailwindShorthand(classes);
- const generated = await uno().generate(expandedClasses, {
+ const generated = await uno().generate(classes, {
preflights: true,
});
diff --git a/packages/css-data/src/tailwind-parser/shorthand.test.ts b/packages/css-data/src/tailwind-parser/shorthand.test.ts
deleted file mode 100644
index 7d8782ab2d..0000000000
--- a/packages/css-data/src/tailwind-parser/shorthand.test.ts
+++ /dev/null
@@ -1,62 +0,0 @@
-import { describe, expect, test } from "@jest/globals";
-import { expandTailwindShorthand } from "./shorthand"; // Import the function
-
-describe("expandTailwindShorthand", () => {
- test("expand mx-4 to mr-4 ml-4", () => {
- expect(expandTailwindShorthand("mx-4")).toBe("mr-4 ml-4");
- });
-
- test("do not expand mb-4", () => {
- expect(expandTailwindShorthand("mb-4")).toBe("mb-4");
- });
-
- test("expand flex-initial to grow-0 shrink basis-auto", () => {
- expect(expandTailwindShorthand("flex-initial")).toBe(
- "grow-0 shrink basis-auto"
- );
- });
-
- test("return the original class if not a shorthand", () => {
- expect(expandTailwindShorthand("bg-red-500")).toBe("bg-red-500");
- });
-
- test("handle classes without a modifier", () => {
- expect(expandTailwindShorthand("flex-none")).toBe(
- "grow-0 shrink-0 basis-auto"
- );
- });
-
- test("handle dynamic values", () => {
- expect(expandTailwindShorthand("flex-1")).toBe("grow shrink basis-[0%]");
- });
-
- 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"
- );
- });
-
- test("expand rounded-lg", () => {
- expect(expandTailwindShorthand("rounded-lg")).toBe(
- "rounded-tl-lg rounded-tr-lg rounded-br-lg rounded-bl-lg"
- );
- });
-
- test("do not expand rounded-tl-lg", () => {
- expect(expandTailwindShorthand("rounded-tl-lg")).toBe("rounded-tl-lg");
- });
-
- test("do not expand border-r", () => {
- expect(expandTailwindShorthand("border-r")).toBe("border-r");
- });
-});
diff --git a/packages/css-data/src/tailwind-parser/shorthand.ts b/packages/css-data/src/tailwind-parser/shorthand.ts
deleted file mode 100644
index c1731428b8..0000000000
--- a/packages/css-data/src/tailwind-parser/shorthand.ts
+++ /dev/null
@@ -1,89 +0,0 @@
-/**
- * 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 = (classnames: string) => {
- return classnames
- .trim()
- .split(/\s+/)
- .map((classname) => {
- const groupKey = Object.keys(conflictingClassGroups).find(
- (key) => classname.startsWith(`${key}-`) || classname === key
- );
-
- if (groupKey === undefined) {
- return classname;
- }
-
- const group = conflictingClassGroups[groupKey];
- const modifier = classname.substring(groupKey.length);
- const expanded = group.map((cls) => `${cls}${modifier}`);
-
- return expanded.join(" ");
- })
- .join(" ");
-};
-
-const orderByKeysDesc = (obj: Record<string, string[]>) => {
- const ordered: Record<string, string[]> = {};
- Object.keys(obj)
- .sort()
- .reverse()
- .forEach((key) => {
- ordered[key] = obj[key];
- });
- return ordered;
-};
-
-/**
- * A little bit changed tailwind-merge
- * conflictingClassGroups https://github.com/dcastil/tailwind-merge/blob/fb1fc1f9d106f2184e24949e6e6d1a67904706fa/src/lib/default-config.ts#L1716
- *
- * Attributions
- * MIT License
- * Copyright (c) 2021 Dany Castillo
- **/
-const conflictingClassGroups: Record<string, string[]> = orderByKeysDesc({
- overflow: ["overflow-x", "overflow-y"],
- overscroll: ["overscroll-x", "overscroll-y"],
- inset: ["top", "right", "bottom", "left"],
- "inset-x": ["right", "left"],
- "inset-y": ["top", "bottom"],
-
- "flex-initial": ["grow-0", "shrink", "basis-auto"], // 0 1 auto
- "flex-auto": ["grow", "shrink", "basis-auto"], // 1 1 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"],
-
- p: ["pt", "pr", "pb", "pl"],
- px: ["pr", "pl"],
- py: ["pt", "pb"],
- m: ["mt", "mr", "mb", "ml"],
- mx: ["mr", "ml"],
- my: ["mt", "mb"],
-
- rounded: ["rounded-tl", "rounded-tr", "rounded-br", "rounded-bl"],
- "rounded-t": ["rounded-tl", "rounded-tr"],
- "rounded-r": ["rounded-tr", "rounded-br"],
- "rounded-b": ["rounded-br", "rounded-bl"],
- "rounded-l": ["rounded-tl", "rounded-bl"],
-
- "rounded-tl": ["rounded-tl"],
- "rounded-tr": ["rounded-tr"],
- "rounded-br": ["rounded-br"],
- "rounded-bl": ["rounded-bl"],
-
- "border-spacing": ["border-spacing-x", "border-spacing-y"],
-
- "border-x": ["border-r", "border-l"],
- "border-y": ["border-t", "border-b"],
- "border-r": ["border-r"],
- "border-l": ["border-l"],
- "border-b": ["border-b"],
- "border-t": ["border-t"],
-
- border: ["border-t", "border-r", "border-b", "border-l"],
- "no-underline": ["[text-decoration-line:none]"],
-});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 6d81dbd347..9e273eaced 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1068,11 +1068,11 @@ importers:
packages/css-data:
dependencies:
'@unocss/core':
- specifier: ^0.61.0
- version: 0.61.0
+ specifier: ^0.61.2
+ version: 0.61.2
'@unocss/preset-uno':
- specifier: ^0.61.0
- version: 0.61.0
+ specifier: ^0.61.2
+ version: 0.61.2
'@webstudio-is/css-engine':
specifier: workspace:*
version: link:../css-engine
@@ -5557,23 +5557,23 @@ packages:
resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==}
- '@unocss/[email protected]':
- resolution: {integrity: sha512-Y/Ly3LPIAzOBlWCdKBVzVzIaaWDsf+oWPIUZlaW7DL++WWypVBCghmxXIT5dyuMGXE560Hj92st4AkXfuVdxGQ==}
+ '@unocss/[email protected]':
+ resolution: {integrity: sha512-CbETiw3vPs9xlKumBhwjSA3I7fkQjx/nTEN2UjFZSxBrlh+7MrTOCKwbEdGB7AjM3Ef6vO30sredq4t1usWoNA==}
- '@unocss/[email protected]':
- resolution: {integrity: sha512-9ru/UR4kZ1+jGXpMawV9T8kpL54FrJBmWKMuFlDTEDIwtzDyyfLbt/buoXdzKDLmil9hOXH3IH8+dah/OiiDoA==}
+ '@unocss/[email protected]':
+ resolution: {integrity: sha512-M2cvrhd8qiiULCxtfcOyG1dcPV0J143n8ECYD4KzVSCuCTU2zRS2EXa9LQrilagpXXFyy12/6+K7uBduvGQ06A==}
- '@unocss/[email protected]':
- resolution: {integrity: sha512-P+DdMtPtzAQ2aQ1/WWPoO3X/qvky+Fqq4eKXIvbqXOQ9c2oem7/dnsPeT08zzLIqxVJnuykymPwRT85EumS0gg==}
+ '@unocss/[email protected]':
+ resolution: {integrity: sha512-69NJhqe77C8jeRyweIqeNWP83wNzio3dOPliCF1FOHkVU1vw9NQaHMAOBe5/eGLgR9YcegQCLSJkcx0k25B5qw==}
- '@unocss/[email protected]':
- resolution: {integrity: sha512-mkKOra3dQEc3uI7aPIqa3t8MJXlmpLSgGaPfEJK52xkFe991ex6CiUunYMMWbh6ZSzmdxkO31IwQIH9lcmj/Uw==}
+ '@unocss/[email protected]':
+ resolution: {integrity: sha512-iS/CEB7fiyrP/kIaP5whgaa5JpQLuknk26Uy4d8Ea4Q/nHuBtUlhfzTBJstXBW9kMTcLfO1O6QY+JI4M3CQzpg==}
- '@unocss/[email protected]':
- resolution: {integrity: sha512-PooyLVAF4wH9KvW4OKfDxYFuM4qmnlU+Ci6O6RGgVsKyQMq76crRqqK76lbnehg7jOoZJVxmWfQ6k5gT3aQeXQ==}
+ '@unocss/[email protected]':
+ resolution: {integrity: sha512-2BpU5RZfsG+TpjQ6UyhUsz8I/fUoTBXhTu1oaPUqUxGT6Swu2Y/FRQul3TUn8NPbtJ//Qm3etNp6S1PZBT0+aw==}
- '@unocss/[email protected]':
- resolution: {integrity: sha512-MCdmfhE6Q9HSWjWqi2sx5/nnKyOEhfhoo+pVumHIqkHQICQ/LuKioFf7Y7e5ycqjFE/7dC2hKGZJ8WTMGIOMwA==}
+ '@unocss/[email protected]':
+ resolution: {integrity: sha512-E0FbNzIk8TkVUF+kZGscDEmfYHps8/9qjvwJK6BjbbnxTaBPoSY9DQjsGWcCiD9pUKJt2tquNwADPiYzzkiKtA==}
engines: {node: '>=14'}
'@upstash/[email protected]':
@@ -14519,34 +14519,34 @@ snapshots:
'@ungap/[email protected]': {}
- '@unocss/[email protected]': {}
+ '@unocss/[email protected]': {}
- '@unocss/[email protected]':
+ '@unocss/[email protected]':
dependencies:
- '@unocss/core': 0.61.0
+ '@unocss/core': 0.61.2
- '@unocss/[email protected]':
+ '@unocss/[email protected]':
dependencies:
- '@unocss/core': 0.61.0
- '@unocss/extractor-arbitrary-variants': 0.61.0
- '@unocss/rule-utils': 0.61.0
+ '@unocss/core': 0.61.2
+ '@unocss/extractor-arbitrary-variants': 0.61.2
+ '@unocss/rule-utils': 0.61.2
- '@unocss/[email protected]':
+ '@unocss/[email protected]':
dependencies:
- '@unocss/core': 0.61.0
- '@unocss/preset-mini': 0.61.0
- '@unocss/preset-wind': 0.61.0
- '@unocss/rule-utils': 0.61.0
+ '@unocss/core': 0.61.2
+ '@unocss/preset-mini': 0.61.2
+ '@unocss/preset-wind': 0.61.2
+ '@unocss/rule-utils': 0.61.2
- '@unocss/[email protected]':
+ '@unocss/[email protected]':
dependencies:
- '@unocss/core': 0.61.0
- '@unocss/preset-mini': 0.61.0
- '@unocss/rule-utils': 0.61.0
+ '@unocss/core': 0.61.2
+ '@unocss/preset-mini': 0.61.2
+ '@unocss/rule-utils': 0.61.2
- '@unocss/[email protected]':
+ '@unocss/[email protected]':
dependencies:
- '@unocss/core': 0.61.0
+ '@unocss/core': 0.61.2
magic-string: 0.30.10
'@upstash/[email protected]':