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: show custom layer names in style-panel for redability (#3633)
Jaya Krishna Namburuc6a9a246d8b6
diff --git a/apps/builder/app/builder/features/style-panel/sections/backdrop-filter/backdrop-filter.tsx b/apps/builder/app/builder/features/style-panel/sections/backdrop-filter/backdrop-filter.tsxindex a800a7da45..7a7042605f 100644--- a/apps/builder/app/builder/features/style-panel/sections/backdrop-filter/backdrop-filter.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/backdrop-filter/backdrop-filter.tsx@@ -50,7 +50,7 @@ export const Section = (props: SectionProps) => {onClick={() => {addLayer(property,- parseFilter(initialBackdropFilter),+ parseFilter(property, initialBackdropFilter),currentStyle,props.createBatchUpdate);@@ -90,6 +90,7 @@ export const Section = (props: SectionProps) => {return (<FilterSectionContent{...layerProps}+ property={property}layer={layerProps.layer}tooltip={<Tooltipdiff --git a/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsx b/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsxindex 2d79f63d1c..fde1b607dc 100644--- a/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsx@@ -50,7 +50,7 @@ export const Section = (props: SectionProps) => {onClick={() => {addLayer(property,- parseFilter(initialFilter),+ parseFilter(property, initialFilter),currentStyle,props.createBatchUpdate);@@ -90,6 +90,7 @@ export const Section = (props: SectionProps) => {return (<FilterSectionContent{...layerProps}+ property={property}layer={layerProps.layer}tooltip={<Tooltipdiff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsxindex c4c912aeda..66e95afb16 100644--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsx@@ -100,7 +100,7 @@ export const Section = (props: SectionProps) => {{layers.value.length > 0 && (<LayersList{...props}- property={properties[0]}+ property={transitionLongHandProperties[0]}value={layers}label={label}deleteProperty={() =>diff --git a/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx b/apps/builder/app/builder/features/style-panel/shared/filter-content.tsxindex 5f504dcdc5..fe3ac5b5e6 100644--- a/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx@@ -1,6 +1,5 @@import {type InvalidValue,- type StyleProperty,type TupleValue,type FunctionValue,toValue,@@ -78,7 +77,7 @@ const filterFunctions = {type FilterContentProps = {index: number;- property: StyleProperty;+ property: "filter" | "backdropFilter";layer: FunctionValue;propertyValue: string;tooltip: JSX.Element;@@ -151,7 +150,7 @@ export const FilterSectionContent = ({value: string,options: StyleUpdateOptions = { isEphemeral: false }) => {- const layers = parseFilter(value);+ const layers = parseFilter(property, value);setIntermediateValue({type: layers.type === "invalid" ? "invalid" : "intermediate",value,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.tsindex f9c63f8c1f..05476cf924 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@@ -1,5 +1,6 @@import { afterEach, describe, expect, test } from "@jest/globals";import {+ FunctionValue,type LayersValue,type StyleProperty,type StyleValue,@@ -9,12 +10,13 @@ import type { CreateBatchUpdate } from "./shared/use-style-data";import {addLayer,deleteLayer,+ getHumanizedTextFromLayer,hideLayer,swapLayers,updateLayer,} from "./style-layer-utils";import type { StyleInfo } from "./shared/style-info";-import { parseShadow } from "@webstudio-is/css-data";+import { parseFilter, parseShadow } from "@webstudio-is/css-data";let styleInfo: StyleInfo = {};let published = false;@@ -360,3 +362,180 @@ describe("boxShadowUtils", () => {`);});});++test("Generates humane layer names for shadow style layer", () => {+ expect(+ getHumanizedTextFromLayer(+ "boxShadow",+ (+ parseShadow(+ "boxShadow",+ "inset 2px 2px 5px rgba(0, 0, 0, 0.5)"+ ) as LayersValue+ ).value[0] as TupleValue+ )+ ).toMatchInlineSnapshot(`+{+ "color": {+ "a": 0.5,+ "b": 0,+ "g": 0,+ "r": 0,+ },+ "name": "Inner Shadow: 2px 2px 5px",+ "value": "inset 2px 2px 5px rgba(0, 0, 0, 0.5)",+}+`);++ expect(+ getHumanizedTextFromLayer(+ "boxShadow",+ (+ parseShadow(+ "boxShadow",+ "3px 3px 10px rgba(255, 0, 0, 0.7)"+ ) as LayersValue+ ).value[0] as TupleValue+ )+ ).toMatchInlineSnapshot(`+{+ "color": {+ "a": 0.7,+ "b": 0,+ "g": 0,+ "r": 255,+ },+ "name": "Outer Shadow: 3px 3px 10px",+ "value": "3px 3px 10px rgba(255, 0, 0, 0.7)",+}+`);++ // colors is returning isValid as false in node env for 'red'+ expect(+ getHumanizedTextFromLayer(+ "textShadow",+ (parseShadow("textShadow", "text-shadow: 1px 1px 2px red") as LayersValue)+ .value[0] as TupleValue+ )+ ).toMatchInlineSnapshot(`+{+ "color": undefined,+ "name": "Text Shadow: 1px 1px 2px red",+ "value": "1px 1px 2px red",+}+`);+});++test("Generates humane layer names for filter style layer", () => {+ expect(+ getHumanizedTextFromLayer(+ "filter",+ (parseFilter("filter", "blur(50px)") as TupleValue)+ .value[0] as FunctionValue+ )+ ).toMatchInlineSnapshot(`+{+ "color": undefined,+ "name": "Blur: 50px",+ "value": "blur(50px)",+}+`);++ expect(+ getHumanizedTextFromLayer(+ "filter",+ (+ parseFilter(+ "filter",+ "filter: drop-shadow(4px 4px 10px blue);"+ ) as TupleValue+ ).value[0] as FunctionValue+ )+ ).toMatchInlineSnapshot(`+{+ "color": undefined,+ "name": "Drop Shadow: 4px 4px 10px blue",+ "value": "drop-shadow(4px 4px 10px blue)",+}+`);+});++test("Generates humane layer names for transition style layer", () => {+ const transition: TupleValue = {+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "width",+ },+ {+ type: "unit",+ value: 0,+ unit: "ms",+ },+ {+ type: "keyword",+ value: "ease-in-out",+ },+ {+ type: "unit",+ value: 0,+ unit: "ms",+ },+ ],+ hidden: false,+ };++ expect(getHumanizedTextFromLayer("transitionProperty", transition))+ .toMatchInlineSnapshot(`+{+ "color": undefined,+ "name": "Width: 0ms 0ms ease-in-out",+ "value": "width 0ms ease-in-out 0ms",+}+`);++ const transitionWithCustomTiming: TupleValue = {+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "all",+ },+ {+ type: "unit",+ value: 50,+ unit: "ms",+ },+ {+ type: "unit",+ value: 100,+ unit: "ms",+ },+ {+ type: "function",+ name: "cubic-bezier",+ args: {+ type: "layers",+ value: [+ { type: "unit", value: 0.12, unit: "number" },+ { type: "unit", value: 0, unit: "number" },+ { type: "unit", value: 0.39, unit: "number" },+ { type: "unit", value: 0, unit: "number" },+ ],+ },+ },+ ],+ hidden: false,+ };++ expect(+ getHumanizedTextFromLayer("transitionProperty", transitionWithCustomTiming)+ ).toMatchInlineSnapshot(`+{+ "color": undefined,+ "name": "All: 50ms 100ms ease-in-sine",+ "value": "all 50ms 100ms cubic-bezier(0.12, 0, 0.39, 0)",+}+`);+});diff --git a/apps/builder/app/builder/features/style-panel/style-layer-utils.ts b/apps/builder/app/builder/features/style-panel/style-layer-utils.tsindex 3b9244ad31..459eaeb54f 100644--- a/apps/builder/app/builder/features/style-panel/style-layer-utils.ts+++ b/apps/builder/app/builder/features/style-panel/style-layer-utils.ts@@ -1,8 +1,12 @@-import type {- InvalidValue,- LayersValue,- StyleProperty,- TupleValue,+import {+ KeywordValue,+ StyleValue,+ toValue,+ type FunctionValue,+ type InvalidValue,+ type LayersValue,+ type StyleProperty,+ type TupleValue,} from "@webstudio-is/css-engine";import type { SectionProps } from "./sections";import type { StyleInfo } from "./shared/style-info";@@ -10,6 +14,11 @@ import type {CreateBatchUpdate,StyleUpdateOptions,} from "./shared/use-style-data";+import { colord, type RgbaColor } from "colord";+import { humanizeString } from "~/shared/string-utils";+import { isAnimatableProperty } from "@webstudio-is/css-data";+import { findTimingFunctionFromValue } from "./sections/transitions/transition-utils";+import type { LayerListProperty } from "./style-layers-list";export const deleteLayer = <Layers extends TupleValue | LayersValue>(property: StyleProperty,@@ -181,3 +190,107 @@ export const swapLayers = ();batch.publish();};++export const getHumanizedTextFromLayer = (+ property: LayerListProperty,+ layer: StyleValue+) => {+ switch (property) {+ case "transitionProperty":+ if (layer.type === "tuple") {+ const properties = [...layer.value];+ const transitionProperty = properties.find(+ (item): item is KeywordValue =>+ item.type === "keyword" && isAnimatableProperty(item.value) === true+ );++ const transitionTimingFunction = properties.find(+ (item): item is KeywordValue | FunctionValue =>+ (item.type === "keyword" || item.type === "function") &&+ isAnimatableProperty(toValue(item)) === false+ );++ if (transitionProperty === undefined) {+ throw `Transition property is missing from the layer ${JSON.stringify(layer)}`;+ }++ properties.splice(properties.indexOf(transitionProperty), 1);+ if (transitionTimingFunction !== undefined) {+ properties.splice(properties.indexOf(transitionTimingFunction), 1);+ }++ const customTimingFunction = findTimingFunctionFromValue(+ toValue(transitionTimingFunction)+ );++ return {+ name: `${humanizeString(transitionProperty.value)}: ${toValue({ type: "tuple", value: properties })} ${customTimingFunction ?? toValue(transitionTimingFunction)}`,+ value: toValue(layer),+ color: undefined,+ };+ }+ break;++ case "textShadow":+ case "boxShadow":+ if (layer.type === "tuple") {+ const name = [];+ let color: RgbaColor | undefined;+ const properties = [...layer.value];++ if (property === "boxShadow") {+ const insetKeyword = layer.value.find(+ (item) => item.type === "keyword" && item.value === "inset"+ );+ if (insetKeyword !== undefined) {+ name.push("Inner Shadow: ");+ properties.splice(properties.indexOf(insetKeyword), 1);+ } else {+ name.push("Outer Shadow: ");+ }+ }++ if (property === "textShadow") {+ name.push("Text Shadow: ");+ }++ for (const item of properties) {+ if (item.type === "unit") {+ const value = toValue(item);+ name.push(value);+ }++ if (item.type === "rgb") {+ color = colord(toValue(item)).toRgb();+ }++ if (item.type === "keyword") {+ if (colord(item.value).isValid() === false) {+ name.push(item.value);+ } else {+ color = colord(item.value).toRgb();+ }+ }++ if (item.type === "unparsed") {+ name.push(item.value);+ }++ if (item.type === "function") {+ const value = `${item.name}(${toValue(item.args)})`;+ name.push(value);+ }+ }++ return { name: name.join(" "), value: toValue(layer), color };+ }+ break;+ case "filter":+ case "backdropFilter":+ if (layer.type === "function") {+ const name = `${humanizeString(layer.name)}: ${toValue(layer.args)}`;+ const value = `${layer.name}(${toValue(layer.args)})`;+ return { name, value, color: undefined };+ }+ }+};diff --git a/apps/builder/app/builder/features/style-panel/style-layers-list.tsx b/apps/builder/app/builder/features/style-panel/style-layers-list.tsxindex 0a2b3156ad..4350af6b68 100644--- a/apps/builder/app/builder/features/style-panel/style-layers-list.tsx+++ b/apps/builder/app/builder/features/style-panel/style-layers-list.tsx@@ -1,5 +1,4 @@import {- toValue,type FunctionValue,type LayersValue,type StyleProperty,@@ -21,6 +20,7 @@ import {hideLayer,swapLayers,updateLayer,+ getHumanizedTextFromLayer,} from "./style-layer-utils";import { useMemo } from "react";import type {@@ -34,12 +34,20 @@ import {SubtractIcon,} from "@webstudio-is/icons";import { ColorThumb } from "./shared/color-thumb";-import { colord, type RgbaColor } from "colord";++export type LayerListProperty = Extract<+ StyleProperty,+ | "filter"+ | "backdropFilter"+ | "textShadow"+ | "boxShadow"+ | "transitionProperty"+>;type LayerListProps = SectionProps & {disabled?: boolean;label: string;- property: StyleProperty;+ property: LayerListProperty;value: TupleValue | LayersValue;deleteProperty: DeleteProperty;deleteLayer?: (index: number) => boolean | void;@@ -48,7 +56,7 @@ type LayerListProps = SectionProps & {renderContent: (props: {index: number;layer: TupleValue | FunctionValue;- property: StyleProperty;+ property: LayerListProperty;propertyValue: string;onDeleteLayer: (index: number) => void;onEditLayer: (@@ -60,51 +68,6 @@ type LayerListProps = SectionProps & {}) => JSX.Element;};-const extractPropertiesFromLayer = (layer: TupleValue | FunctionValue) => {- if (layer.type === "function") {- const value = `${layer.name}(${toValue(layer.args)})`;- return { name: value, value, color: undefined };- }-- const name = [];- const shadow = [];- let color: RgbaColor | undefined;- for (const item of Object.values(layer.value)) {- if (item.type === "unit") {- const value = toValue(item);- name.push(value);- shadow.push(value);- }-- if (item.type === "rgb") {- color = colord(toValue(item)).toRgb();- shadow.push(toValue(item));- }-- if (item.type === "keyword") {- if (colord(item.value).isValid() === false) {- name.push(item.value);- } else {- color = colord(item.value).toRgb();- }- shadow.push(item.value);- }-- if (item.type === "unparsed") {- name.push(item.value);- shadow.push(item.value);- }-- if (item.type === "function") {- const value = `${item.name}(${toValue(item.args)})`;- name.push(value);- shadow.push(value);- }- }-- return { name: name.join(" "), value: shadow.join(" "), color };-};-export const LayersList = (props: LayerListProps) => {const {label,@@ -183,7 +146,10 @@ export const LayersList = (props: LayerListProps) => {}const id = String(index);- const properties = extractPropertiesFromLayer(layer);+ const properties = getHumanizedTextFromLayer(property, layer);+ if (properties === undefined) {+ return;+ }return (<FloatingPaneldiff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.tsindex 6d2b2666ce..4854e80a7e 100644--- a/packages/css-data/src/parse-css-value.ts+++ b/packages/css-data/src/parse-css-value.ts@@ -157,7 +157,7 @@ export const parseCssValue = (}if (property === "filter" || property === "backdropFilter") {- return parseFilter(input);+ return parseFilter(property, input);}if (property === "boxShadow" || property === "textShadow") {diff --git a/packages/css-data/src/property-parsers/filter.test.ts b/packages/css-data/src/property-parsers/filter.test.tsindex 4b5b5a3a95..0dc33d9eaf 100644--- a/packages/css-data/src/property-parsers/filter.test.ts+++ b/packages/css-data/src/property-parsers/filter.test.ts@@ -3,7 +3,7 @@ import { parseFilter } from "./filter";describe("parse filter", () => {test("parse values and returns the valid style property values", () => {- expect(parseFilter("blur(4px)")).toMatchInlineSnapshot(`+ expect(parseFilter("filter", "blur(4px)")).toMatchInlineSnapshot(`{"type": "tuple","value": [@@ -25,8 +25,9 @@ describe("parse filter", () => {}`);- expect(parseFilter("drop-shadow(10px 10px 25px rgba(0, 0, 255, 1))"))- .toMatchInlineSnapshot(`+ expect(+ parseFilter("filter", "drop-shadow(10px 10px 25px rgba(0, 0, 255, 1))")+ ).toMatchInlineSnapshot(`{"type": "tuple","value": [@@ -65,7 +66,7 @@ describe("parse filter", () => {}`);- expect(parseFilter("drop-shadow(10px 10px 25px #0000FF)"))+ expect(parseFilter("filter", "drop-shadow(10px 10px 25px #0000FF)")).toMatchInlineSnapshot(`{"type": "tuple",@@ -108,7 +109,10 @@ describe("parse filter", () => {test("Multiple valid function values", () => {expect(- parseFilter("blur(4px) drop-shadow(16px 16px 20px blue) opacity(25%)")+ parseFilter(+ "filter",+ "blur(4px) drop-shadow(16px 16px 20px blue) opacity(25%)"+ )).toMatchInlineSnapshot(`{"type": "tuple",@@ -177,7 +181,8 @@ describe("parse filter", () => {// parsers are used to use copied value. At the moment, we don't have support// for complex functions in the UI like the one below like calc(4px + 16em)test("Using complex functions inside filter function", () => {- expect(parseFilter("blur(calc(4px + 16em))")).toMatchInlineSnapshot(`+ expect(parseFilter("filter", "blur(calc(4px + 16em))"))+ .toMatchInlineSnapshot(`{"type": "tuple","value": [diff --git a/packages/css-data/src/property-parsers/filter.ts b/packages/css-data/src/property-parsers/filter.tsindex c36f1ab616..f080a3ac2e 100644--- a/packages/css-data/src/property-parsers/filter.ts+++ b/packages/css-data/src/property-parsers/filter.ts@@ -17,13 +17,23 @@ import { colord } from "colord";Which uses browser CSSStyleValue.parse to validate.*/-export const parseFilter = (input: string): TupleValue | InvalidValue => {+const FilterProperties = {+ filter: { cleanupKeywords: ["filter:"], propertyName: "filter" },+ backdropFilter: {+ cleanupKeywords: ["backdrop-filter:"],+ propertyName: "backdrop-filter",+ },+};++export const parseFilter = (+ property: keyof typeof FilterProperties,+ input: string+): TupleValue | InvalidValue => {let tokenStream = input.trim();tokenStream = tokenStream.endsWith(";")? tokenStream.slice(0, -1): tokenStream;-- const cleanupKeywords = ["filter:"];+ const { cleanupKeywords, propertyName } = FilterProperties[property];for (const cleanupKeyword of cleanupKeywords) {tokenStream = tokenStream.startsWith(cleanupKeyword)@@ -39,7 +49,10 @@ export const parseFilter = (input: string): TupleValue | InvalidValue => {};}- const isValidFilterDecleration = isValidDeclaration("filter", tokenStream);+ const isValidFilterDecleration = isValidDeclaration(+ propertyName,+ tokenStream+ );if (isValidFilterDecleration === false) {return {type: "invalid",