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.

feat: Refactor existing `parseBoxShadow` to parse both box and text shadows (#3307)

Jaya Krishna Namburu966f3ea7272c
diff --git a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadow-content.tsx b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadow-content.tsx
index 1272cbc907..5af08575e2 100644
--- a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadow-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadow-content.tsx
@@ -8,9 +8,9 @@ import {
type UnitValue,
} from "@webstudio-is/css-engine";
import {
- extractBoxShadowProperties,
- parseBoxShadow,
- type ExtractedBoxShadowProperties,
+ extractShadowProperties,
+ parseShadow,
+ type ExtractedShadowProperties,
} from "@webstudio-is/css-data";
import {
Flex,
@@ -70,7 +70,7 @@ type BoxShadowContentProps = {
};
const convertValuesToTupple = (
- values: Record<keyof ExtractedBoxShadowProperties, StyleValue | null>
+ values: Partial<Record<keyof ExtractedShadowProperties, StyleValue>>
): TupleValue => {
return {
type: "tuple",
@@ -96,9 +96,9 @@ export const BoxShadowContent = ({
const [intermediateValue, setIntermediateValue] = useState<
IntermediateStyleValue | InvalidValue | undefined
>();
- const layerValues = useMemo<ExtractedBoxShadowProperties>(() => {
+ const layerValues = useMemo<ExtractedShadowProperties>(() => {
setIntermediateValue({ type: "intermediate", value: shadow });
- return extractBoxShadowProperties(layer);
+ return extractShadowProperties(layer);
}, [layer, shadow]);
const { offsetX, offsetY, blur, spread, color, inset } = layerValues;
const colorControlProp = color ?? {
@@ -120,7 +120,7 @@ export const BoxShadowContent = ({
if (intermediateValue === undefined) {
return;
}
- const layers = parseBoxShadow(intermediateValue.value);
+ const layers = parseShadow("boxShadow", intermediateValue.value);
if (layers.type === "invalid") {
setIntermediateValue({
type: "invalid",
@@ -133,9 +133,9 @@ export const BoxShadowContent = ({
};
const handlePropertyChange = (
- params: Partial<Record<keyof ExtractedBoxShadowProperties, StyleValue>>
+ params: Partial<Record<keyof ExtractedShadowProperties, StyleValue>>
) => {
- const newLayer = convertValuesToTupple(Object.assign(layerValues, params));
+ const newLayer = convertValuesToTupple({ ...layerValues, ...params });
setIntermediateValue({
type: "intermediate",
value: toValue(newLayer),
diff --git a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx
index 12c44ffba9..cd16ac1606 100644
--- a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx
@@ -18,7 +18,7 @@ import type { SectionProps } from "../shared/section";
import { LayersList } from "../../style-layers-list";
import { BoxShadowLayer } from "./box-shadow-layer";
import { addLayer } from "../../style-layer-utils";
-import { parseBoxShadow } from "@webstudio-is/css-data";
+import { parseShadow } from "@webstudio-is/css-data";
export const properties = ["boxShadow"] satisfies Array<StyleProperty>;
@@ -47,7 +47,7 @@ export const Section = (props: SectionProps) => {
onClick={() => {
addLayer(
property,
- parseBoxShadow(INITIAL_BOX_SHADOW),
+ parseShadow("boxShadow", INITIAL_BOX_SHADOW),
currentStyle,
props.createBatchUpdate
);
diff --git a/apps/builder/app/builder/features/style-panel/style-layer-utils.test.ts b/apps/builder/app/builder/features/style-panel/style-layer-utils.test.ts
index 4707884ff5..f552ae672c 100644
--- a/apps/builder/app/builder/features/style-panel/style-layer-utils.test.ts
+++ b/apps/builder/app/builder/features/style-panel/style-layer-utils.test.ts
@@ -14,7 +14,7 @@ import {
updateLayer,
} from "./style-layer-utils";
import type { StyleInfo } from "./shared/style-info";
-import { parseBoxShadow } from "@webstudio-is/css-data";
+import { parseShadow } from "@webstudio-is/css-data";
let styleInfo: StyleInfo = {};
let published = false;
@@ -207,7 +207,10 @@ describe("boxShadowUtils", () => {
test("adds layer to box-shadow proeprty", () => {
addLayer(
property,
- parseBoxShadow(`box-shadow: 10px 10px 10px 0px rgba(0, 0, 0, 0.75)`),
+ parseShadow(
+ "boxShadow",
+ `box-shadow: 10px 10px 10px 0px rgba(0, 0, 0, 0.75)`
+ ),
styleInfo,
createBatchUpdate
);
@@ -315,7 +318,8 @@ describe("boxShadowUtils", () => {
test("swaps the layers using indexe's", () => {
addLayer(
property,
- parseBoxShadow(
+ parseShadow(
+ "boxShadow",
`box-shadow: 0 60px 80px rgba(0,0,0,0.60), 0 45px 26px rgba(0,0,0,0.14);`
),
styleInfo,
diff --git a/packages/css-data/src/property-parsers/index.ts b/packages/css-data/src/property-parsers/index.ts
index 4101778e3d..1d411f05a4 100644
--- a/packages/css-data/src/property-parsers/index.ts
+++ b/packages/css-data/src/property-parsers/index.ts
@@ -1,6 +1,6 @@
export * from "./background";
-export * from "./box-shadow";
+export * from "./shadows";
export * from "./transition";
export * from "./transition-property-extractor";
-export * from "./box-shadow-property-extractor";
+export * from "./shadow-properties-extractor";
export * from "./filter";
diff --git a/packages/css-data/src/property-parsers/parsers.ts b/packages/css-data/src/property-parsers/parsers.ts
index a6ffdf0fee..bd9705d247 100644
--- a/packages/css-data/src/property-parsers/parsers.ts
+++ b/packages/css-data/src/property-parsers/parsers.ts
@@ -2,4 +2,4 @@
Longhand properties parsers. Each named export should match a property name.
*/
export { parseBackgroundImage as backgroundImage } from "./background";
-export { parseBoxShadow as boxShadow } from "./box-shadow";
+export { parseShadow } from "./shadows";
diff --git a/packages/css-data/src/property-parsers/box-shadow-property-extractor.test.ts b/packages/css-data/src/property-parsers/shadow-properties-extractor.test.ts
similarity index 61%
rename from packages/css-data/src/property-parsers/box-shadow-property-extractor.test.ts
rename to packages/css-data/src/property-parsers/shadow-properties-extractor.test.ts
index bf98fa63ed..f860ab978d 100644
--- a/packages/css-data/src/property-parsers/box-shadow-property-extractor.test.ts
+++ b/packages/css-data/src/property-parsers/shadow-properties-extractor.test.ts
@@ -1,11 +1,11 @@
import { describe, expect, test } from "@jest/globals";
-import { extractBoxShadowProperties } from "./box-shadow-property-extractor";
-import { parseBoxShadow } from ".";
+import { extractShadowProperties } from "./shadow-properties-extractor";
+import { parseShadow } from "./shadows";
import type { TupleValue } from "@webstudio-is/css-engine";
-describe("extractBoxShadowProperties", () => {
+describe("Extract Box-Shadow Properties", () => {
test("tokenize and extracts values from 5em 5em 2em 2em #0000", () => {
- const { inset, blur, spread, color } = extractBoxShadowProperties({
+ const { inset, blur, spread, color } = extractShadowProperties({
type: "tuple",
value: [
{
@@ -38,14 +38,14 @@ describe("extractBoxShadowProperties", () => {
],
});
- expect(inset).toBeNull();
+ expect(inset).not.toBeDefined();
expect(blur).toBeDefined();
expect(spread?.value).toBe(2);
expect(color?.type).toBe("rgb");
});
test("tokenize and extracts values from inset 5em 5em red", () => {
- const { inset, spread, color, offsetX } = extractBoxShadowProperties({
+ const { inset, spread, color, offsetX } = extractShadowProperties({
type: "tuple",
value: [
{
@@ -70,13 +70,13 @@ describe("extractBoxShadowProperties", () => {
});
expect(inset).toBeDefined();
- expect(spread).toBeNull();
+ expect(spread).not.toBeDefined();
expect(offsetX?.value).toBe(5);
expect(color?.type).toBe("keyword");
});
test("tokenize and extracts values from 5em 5em #fff", () => {
- const { offsetX, offsetY, color } = extractBoxShadowProperties({
+ const { offsetX, offsetY, color } = extractShadowProperties({
type: "tuple",
value: [
{
@@ -105,7 +105,7 @@ describe("extractBoxShadowProperties", () => {
});
test("tokenize and extracts values from 5em 5em", () => {
- const { offsetX, offsetY } = extractBoxShadowProperties({
+ const { offsetX, offsetY } = extractShadowProperties({
type: "tuple",
value: [
{
@@ -126,7 +126,7 @@ describe("extractBoxShadowProperties", () => {
});
test("tokenize and extract values from 5em 5em 0em 5em inset #ffff", () => {
- const layers = parseBoxShadow("5em 5em 0em 5em inset #ffff");
+ const layers = parseShadow("boxShadow", "5em 5em 0em 5em inset #ffff");
if (layers.type === "invalid") {
throw new Error(`Failed in parsing box-shadow string`);
}
@@ -139,7 +139,7 @@ describe("extractBoxShadowProperties", () => {
}
const { blur, spread, color, offsetX, inset } =
- extractBoxShadowProperties(layer);
+ extractShadowProperties(layer);
expect(blur).toBeDefined();
expect(blur?.value).toBe(0);
@@ -149,3 +149,40 @@ describe("extractBoxShadowProperties", () => {
expect(inset).toBeDefined();
});
});
+
+describe("Extract Text-Shadow Properties", () => {
+ test("tokenize and extracts values from 5em 10em 2em #0000", () => {
+ const { offsetX, offsetY, blur, color } = extractShadowProperties(
+ parseShadow("textShadow", "5em 10em 2em #0000")?.value[0] as TupleValue
+ );
+
+ expect(offsetY).toBeDefined();
+ expect(offsetX?.value).toBe(5);
+ expect(color?.type).toBe("rgb");
+ expect(blur?.value).toBe(2);
+ });
+
+ test("tokenize and extracts values from 5em 10em", () => {
+ const { offsetX, offsetY, blur, color } = extractShadowProperties(
+ parseShadow("textShadow", "5em 10em")?.value[0] as TupleValue
+ );
+
+ expect(offsetY?.value).toBe(10);
+ expect(offsetX?.value).toBe(5);
+
+ expect(color).not.toBeDefined();
+ expect(blur).not.toBeDefined();
+ });
+
+ test("tokenize and extracts values from box-shadow: red 60px -16px;", () => {
+ const { offsetX, offsetY, blur, color } = extractShadowProperties(
+ parseShadow("textShadow", "text-shadow: red 60px -16px;")
+ ?.value[0] as TupleValue
+ );
+
+ expect(offsetX?.value).toBe(60);
+ expect(offsetY?.value).toBe(-16);
+ expect(color?.type).toBe("keyword");
+ expect(blur).not.toBeDefined();
+ });
+});
diff --git a/packages/css-data/src/property-parsers/box-shadow-property-extractor.ts b/packages/css-data/src/property-parsers/shadow-properties-extractor.ts
similarity index 54%
rename from packages/css-data/src/property-parsers/box-shadow-property-extractor.ts
rename to packages/css-data/src/property-parsers/shadow-properties-extractor.ts
index d54fd7050b..fa57b8b482 100644
--- a/packages/css-data/src/property-parsers/box-shadow-property-extractor.ts
+++ b/packages/css-data/src/property-parsers/shadow-properties-extractor.ts
@@ -9,28 +9,30 @@ import type {
/*
extractBoxShadowProperties accepts a TupleValue
and just tokenizes the valuess of box-shadow items.
+ https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow#syntax
To validate the box-shadow string value, should be used
- in combination with parseBoxShadow
+ in combination with parseShadow function.
Eg:
- extractBoxShadowProperties(parseBoxShadow("5em 5em 5em 5em #ffff"))
+ extractBoxShadowProperties(parseShadow('boxShadow', "5em 5em 5em 5em #ffff"))
*/
-export type ExtractedBoxShadowProperties = {
- inset: KeywordValue | null;
- color: RgbValue | KeywordValue | null;
- offsetX: UnitValue | null;
- offsetY: UnitValue | null;
- blur: UnitValue | null;
- spread: UnitValue | null;
+export type ExtractedShadowProperties = {
+ inset?: KeywordValue | undefined;
+ color?: RgbValue | KeywordValue | undefined;
+ offsetX: UnitValue;
+ offsetY: UnitValue;
+ blur: UnitValue;
+ spread: UnitValue;
};
-export const extractBoxShadowProperties = (
+
+export const extractShadowProperties = (
shadow: TupleValue
-): ExtractedBoxShadowProperties => {
+): ExtractedShadowProperties => {
let properties = [...shadow.value];
- let inset: KeywordValue | null = null;
- let color: KeywordValue | RgbValue | null = null;
+ let inset: KeywordValue | undefined = undefined;
+ let color: KeywordValue | RgbValue | undefined = undefined;
properties = properties.filter(
(prop: TupleValueItem): prop is KeywordValue => {
@@ -53,11 +55,11 @@ export const extractBoxShadowProperties = (
);
return {
- color,
- inset,
- offsetX: offsetX ?? null,
- offsetY: offsetY ?? null,
- blur: blur ?? null,
- spread: spread ?? null,
+ ...(color !== undefined && { color }),
+ ...(inset !== undefined && { inset }),
+ offsetX,
+ offsetY,
+ blur,
+ spread,
};
};
diff --git a/packages/css-data/src/property-parsers/box-shadow.test.ts b/packages/css-data/src/property-parsers/shadows.test.ts
similarity index 79%
rename from packages/css-data/src/property-parsers/box-shadow.test.ts
rename to packages/css-data/src/property-parsers/shadows.test.ts
index 4dd55cdd79..e366b0134b 100644
--- a/packages/css-data/src/property-parsers/box-shadow.test.ts
+++ b/packages/css-data/src/property-parsers/shadows.test.ts
@@ -1,10 +1,21 @@
import { describe, expect, test } from "@jest/globals";
-import { parseBoxShadow } from "./box-shadow";
+import { parseShadow } from "./shadows";
-describe("parseBoxShadow", () => {
- test("parses value and returns invalid when used a invalid color", () => {
- expect(parseBoxShadow(`10px 10px 5px foo`)).toMatchInlineSnapshot(`
+describe("parse shadows", () => {
+ test("parses value and returns invalid when used a invalid boxShadow is passed", () => {
+ expect(parseShadow("boxShadow", `10px 10px 5px foo`))
+ .toMatchInlineSnapshot(`
+ {
+ "type": "invalid",
+ "value": "10px 10px 5px foo",
+ }
+ `);
+ });
+
+ test("parses value and returns invalid when a invalid textShadow is passed", () => {
+ expect(parseShadow("textShadow", `10px 10px 5px foo`))
+ .toMatchInlineSnapshot(`
{
"type": "invalid",
"value": "10px 10px 5px foo",
@@ -13,7 +24,7 @@ describe("parseBoxShadow", () => {
});
test("throws error when passed a value without a unit", () => {
- expect(parseBoxShadow(`10 10px 5px red`)).toMatchInlineSnapshot(`
+ expect(parseShadow("boxShadow", `10 10px 5px red`)).toMatchInlineSnapshot(`
{
"type": "invalid",
"value": "10 10px 5px red",
@@ -21,8 +32,44 @@ describe("parseBoxShadow", () => {
`);
});
+ test("parses values and returns a layer when a valid textShadow is passes", () => {
+ expect(parseShadow("textShadow", "text-shadow: 1px 1px 2px black;"))
+ .toMatchInlineSnapshot(`
+{
+ "type": "layers",
+ "value": [
+ {
+ "type": "tuple",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 1,
+ },
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 1,
+ },
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 2,
+ },
+ {
+ "type": "keyword",
+ "value": "black",
+ },
+ ],
+ },
+ ],
+}
+`);
+ });
+
test("inset and color values can be interchanged", () => {
- expect(parseBoxShadow(`inset 10px 10px 5px black;`)).toMatchInlineSnapshot(`
+ expect(parseShadow("boxShadow", `inset 10px 10px 5px black;`))
+ .toMatchInlineSnapshot(`
{
"type": "layers",
"value": [
@@ -60,7 +107,7 @@ describe("parseBoxShadow", () => {
});
test("parses value when inset is used but missing blur-radius", () => {
- expect(parseBoxShadow(`box-shadow: inset 5em 1em gold`))
+ expect(parseShadow("boxShadow", `box-shadow: inset 5em 1em gold`))
.toMatchInlineSnapshot(`
{
"type": "layers",
@@ -94,7 +141,7 @@ describe("parseBoxShadow", () => {
});
test("parses value when offsetX and offsetY are used", () => {
- expect(parseBoxShadow(`box-shadow: 60px -16px teal;`))
+ expect(parseShadow("boxShadow", `box-shadow: 60px -16px teal;`))
.toMatchInlineSnapshot(`
{
"type": "layers",
@@ -125,7 +172,8 @@ describe("parseBoxShadow", () => {
test("parses value from figma", () => {
expect(
- parseBoxShadow(
+ parseShadow(
+ "boxShadow",
"box-shadow: 0 60px 80px rgba(0,0,0,0.60), 0 45px 26px rgba(0,0,0,0.14);"
)
).toMatchInlineSnapshot(`
@@ -193,11 +241,14 @@ describe("parseBoxShadow", () => {
test(`parses multiple layers of box-shadow property`, () => {
expect(
- parseBoxShadow(`box-shadow:
+ parseShadow(
+ "boxShadow",
+ `box-shadow:
0 0 5px rgba(0, 0, 0, 0.2),
inset 0 0 10px rgba(0, 0, 0, 0.3),
0 0 15px rgba(0, 0, 0, 0.4);
- `)
+ `
+ )
).toMatchInlineSnapshot(`
{
"type": "layers",
diff --git a/packages/css-data/src/property-parsers/box-shadow.ts b/packages/css-data/src/property-parsers/shadows.ts
similarity index 84%
rename from packages/css-data/src/property-parsers/box-shadow.ts
rename to packages/css-data/src/property-parsers/shadows.ts
index f5d9ab87b4..8fbf051cf2 100644
--- a/packages/css-data/src/property-parsers/box-shadow.ts
+++ b/packages/css-data/src/property-parsers/shadows.ts
@@ -9,15 +9,23 @@ import type {
} from "@webstudio-is/css-engine";
import { cssTryParseValue } from "../parse-css-value";
-export const parseBoxShadow = (
- boxShadow: string
+const ShadowProperties = {
+ boxShadow: { cleanupKeywords: ["box-shadow:"], propertyName: "box-shadow" },
+ textShadow: {
+ cleanupKeywords: ["text-shadow:"],
+ propertyName: "text-shadow",
+ },
+};
+
+export const parseShadow = (
+ property: keyof typeof ShadowProperties,
+ shadow: string
): LayersValue | InvalidValue => {
- let tokenStream = boxShadow.trim();
+ let tokenStream = shadow.trim();
tokenStream = tokenStream.endsWith(";")
? tokenStream.slice(0, -1)
: tokenStream;
-
- const cleanupKeywords = ["box-shadow:"];
+ const { cleanupKeywords, propertyName } = ShadowProperties[property];
for (const cleanupKeyword of cleanupKeywords) {
tokenStream = tokenStream.startsWith(cleanupKeyword)
@@ -29,15 +37,15 @@ export const parseBoxShadow = (
if (cssAst === undefined) {
return {
type: "invalid",
- value: boxShadow,
+ value: shadow,
};
}
- const parsed = csstree.lexer.matchProperty("box-shadow", cssAst);
+ const parsed = csstree.lexer.matchProperty(propertyName, cssAst);
if (parsed.error) {
return {
type: "invalid",
- value: boxShadow,
+ value: shadow,
};
}
@@ -126,5 +134,5 @@ export const parseBoxShadow = (
type: "layers",
value: layers,
}
- : { type: "invalid", value: boxShadow };
+ : { type: "invalid", value: shadow };
};
diff --git a/packages/css-data/src/tailwind-parser/parse.ts b/packages/css-data/src/tailwind-parser/parse.ts
index 9aa59a68b8..84b2937f73 100644
--- a/packages/css-data/src/tailwind-parser/parse.ts
+++ b/packages/css-data/src/tailwind-parser/parse.ts
@@ -8,7 +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";
+import { parseShadow } from "../property-parsers/shadows";
let unoLazy: UnoGenerator<Theme> | undefined = undefined;
@@ -142,7 +142,7 @@ const postprocessBorder = (styles: EmbedTemplateStyleDecl[]) => {
const postprocessBoxShadows = (styles: EmbedTemplateStyleDecl[]) => {
return styles.map((style) => {
if (style.property === "boxShadow" && style.value.type === "unparsed") {
- const shadowStyle = parseBoxShadow(style.value.value);
+ const shadowStyle = parseShadow("boxShadow", style.value.value);
return {
property: style.property,
value: shadowStyle,
diff --git a/packages/sdk-components-react-radix/scripts/generate-tailwind-theme.ts b/packages/sdk-components-react-radix/scripts/generate-tailwind-theme.ts
index f06a8806f6..60ff965032 100644
--- a/packages/sdk-components-react-radix/scripts/generate-tailwind-theme.ts
+++ b/packages/sdk-components-react-radix/scripts/generate-tailwind-theme.ts
@@ -3,7 +3,7 @@ import { writeFile, mkdir } from "node:fs/promises";
// eslint-disable-next-line import/no-internal-modules
import defaultTheme from "tailwindcss/defaultTheme";
import type { StyleValue, StyleProperty } from "@webstudio-is/css-engine";
-import { parseCssValue, parseBoxShadow } from "@webstudio-is/css-data";
+import { parseCssValue, parseShadow } from "@webstudio-is/css-data";
import type { ResolvableTo } from "tailwindcss/types/config";
import { colors as colorOverrides } from "../src/theme/tailwind-colors";
@@ -159,7 +159,7 @@ const generatedThemeData: GeneratedThemeItem[] = [
values: Object.fromEntries(
Object.entries(theme.boxShadow).map(([name, value]) => [
name,
- parseBoxShadow(value),
+ parseShadow("boxShadow", value),
])
),
},