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: Box shadow property extractor (#2587)
Jaya Krishna Namburufbafebfdc9d2
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.tsxindex 219f1c6806..fefedbd08c 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@@ -1,4 +1,8 @@-import type { InvalidValue, LayersValue } from "@webstudio-is/css-engine";+import type {+ InvalidValue,+ LayersValue,+ TupleValue,+} from "@webstudio-is/css-engine";import { parseBoxShadow } from "@webstudio-is/css-data";import {Flex,@@ -11,14 +15,13 @@ import {} from "@webstudio-is/design-system";import { InformationIcon } from "@webstudio-is/icons";import { useState } from "react";-import type { RenderCategoryProps } from "../../style-sections";import type { IntermediateStyleValue } from "../../shared/css-value-input";type BoxShadowContentProps = {index: number;+ layer: TupleValue;value: string;onEditLayer: (index: number, layers: LayersValue) => void;- createBatchUpdate: RenderCategoryProps["createBatchUpdate"];};export const BoxShadowContent = (props: BoxShadowContentProps) => {diff --git a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadow-layer.tsx b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadow-layer.tsxindex 6c26432ebe..d4bd4a5d0f 100644--- a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadow-layer.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadow-layer.tsx@@ -60,8 +60,8 @@ export const Layer = (props: LayerProps) => {<BoxShadowContentindex={index}value={shadow}+ layer={layer}onEditLayer={props.onEditLayer}- createBatchUpdate={props.createBatchUpdate}/>}>diff --git a/packages/css-data/src/property-parsers/box-shadow-property-extractor.test.ts b/packages/css-data/src/property-parsers/box-shadow-property-extractor.test.tsnew file mode 100644index 0000000000..bf98fa63ed--- /dev/null+++ b/packages/css-data/src/property-parsers/box-shadow-property-extractor.test.ts@@ -0,0 +1,151 @@+import { describe, expect, test } from "@jest/globals";+import { extractBoxShadowProperties } from "./box-shadow-property-extractor";+import { parseBoxShadow } from ".";+import type { TupleValue } from "@webstudio-is/css-engine";++describe("extractBoxShadowProperties", () => {+ test("tokenize and extracts values from 5em 5em 2em 2em #0000", () => {+ const { inset, blur, spread, color } = extractBoxShadowProperties({+ type: "tuple",+ value: [+ {+ type: "unit",+ value: 5,+ unit: "em",+ },+ {+ type: "unit",+ value: 5,+ unit: "em",+ },+ {+ type: "unit",+ value: 2,+ unit: "em",+ },+ {+ type: "unit",+ value: 2,+ unit: "em",+ },+ {+ type: "rgb",+ alpha: 0,+ r: 0,+ g: 0,+ b: 0,+ },+ ],+ });++ expect(inset).toBeNull();+ 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({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "inset",+ },+ {+ type: "unit",+ value: 5,+ unit: "em",+ },+ {+ type: "unit",+ value: 5,+ unit: "em",+ },+ {+ type: "keyword",+ value: "red",+ },+ ],+ });++ expect(inset).toBeDefined();+ expect(spread).toBeNull();+ expect(offsetX?.value).toBe(5);+ expect(color?.type).toBe("keyword");+ });++ test("tokenize and extracts values from 5em 5em #fff", () => {+ const { offsetX, offsetY, color } = extractBoxShadowProperties({+ type: "tuple",+ value: [+ {+ type: "unit",+ value: 5,+ unit: "em",+ },+ {+ type: "unit",+ value: 5,+ unit: "em",+ },+ {+ type: "rgb",+ alpha: 1,+ b: 255,+ g: 255,+ r: 255,+ },+ ],+ });++ expect(offsetX?.value).toBe(5);+ expect(offsetY?.value).toBe(5);+ expect(color).toBeDefined();+ });++ test("tokenize and extracts values from 5em 5em", () => {+ const { offsetX, offsetY } = extractBoxShadowProperties({+ type: "tuple",+ value: [+ {+ type: "unit",+ value: 5,+ unit: "em",+ },+ {+ type: "unit",+ value: 5,+ unit: "em",+ },+ ],+ });++ expect(offsetX?.value).toBe(5);+ expect(offsetY?.value).toBe(5);+ });++ test("tokenize and extract values from 5em 5em 0em 5em inset #ffff", () => {+ const layers = parseBoxShadow("5em 5em 0em 5em inset #ffff");+ if (layers.type === "invalid") {+ throw new Error(`Failed in parsing box-shadow string`);+ }++ const layer = layers.value.find(+ (layer): layer is TupleValue => layer.type === "tuple"+ );+ if (!layer) {+ throw new Error(`Parsing box-shadow layer failed`);+ }++ const { blur, spread, color, offsetX, inset } =+ extractBoxShadowProperties(layer);++ expect(blur).toBeDefined();+ expect(blur?.value).toBe(0);+ expect(spread?.value).toBe(5);+ expect(offsetX?.value).toBe(5);+ expect(color).toBeDefined();+ expect(inset).toBeDefined();+ });+});diff --git a/packages/css-data/src/property-parsers/box-shadow-property-extractor.ts b/packages/css-data/src/property-parsers/box-shadow-property-extractor.tsnew file mode 100644index 0000000000..51ff024501--- /dev/null+++ b/packages/css-data/src/property-parsers/box-shadow-property-extractor.ts@@ -0,0 +1,62 @@+import type {+ KeywordValue,+ RgbValue,+ TupleValue,+ TupleValueItem,+ UnitValue,+} from "@webstudio-is/css-engine";++/*+ extractBoxShadowProperties accepts a TupleValue+ and just tokenizes the valuess of box-shadow items.++ To validate the box-shadow string value, should be used+ in combination with parseBoxShadow++ Eg:+ extractBoxShadowProperties(parseBoxShadow("5em 5em 5em 5em #ffff"))+*/++export const extractBoxShadowProperties = (+ shadow: TupleValue+): {+ inset: KeywordValue | null;+ color: RgbValue | KeywordValue | null;+ offsetX: UnitValue | null;+ offsetY: UnitValue | null;+ blur: UnitValue | null;+ spread: UnitValue | null;+} => {+ let properties = [...shadow.value];+ let inset: KeywordValue | null = null;+ let color: KeywordValue | RgbValue | null = null;++ properties = properties.filter(+ (prop: TupleValueItem): prop is KeywordValue => {+ if (prop.type === "keyword" && prop.value === "inset") {+ inset = prop;+ return false;+ }++ if (prop.type === "keyword" || prop.type === "rgb") {+ color = prop;+ return false;+ }++ return true;+ }+ );++ const [offsetX, offsetY, blur, spread] = properties.filter(+ (item): item is UnitValue => item.type === "unit"+ );++ return {+ color,+ inset,+ offsetX: offsetX ?? null,+ offsetY: offsetY ?? null,+ blur: blur ?? null,+ spread: spread ?? null,+ };+};diff --git a/packages/css-data/src/property-parsers/box-shadow.ts b/packages/css-data/src/property-parsers/box-shadow.tsindex e80a4b8da3..1b7737eb7b 100644--- a/packages/css-data/src/property-parsers/box-shadow.ts+++ b/packages/css-data/src/property-parsers/box-shadow.ts@@ -68,7 +68,7 @@ export const parseBoxShadow = (/*** https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow#syntax* `inset` and color can be at the start or end and their sequence can be anywhere.- * The rest need to foolow the sequence all the time+ * The rest need to follow the sequence all the time*/for (let index = 0; index < layer.length; index++) {diff --git a/packages/css-data/src/property-parsers/index.ts b/packages/css-data/src/property-parsers/index.tsindex 8bd15b24e0..d38f85e9e1 100644--- a/packages/css-data/src/property-parsers/index.ts+++ b/packages/css-data/src/property-parsers/index.ts@@ -2,3 +2,4 @@ export * from "./background";export * from "./box-shadow";export * from "./transition";export * from "./transition-property-extractor";+export * from "./box-shadow-property-extractor";