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.

experimental: migrate filter sections to style object model (#4097)

Bogdan Chadkin90c62d1a1135
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.tsx
index 9de87cd46a..86f786131a 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
@@ -1,12 +1,20 @@
-import type { SectionProps } from "../shared/section";
-import type { LayersValue, StyleProperty } from "@webstudio-is/css-engine";
+import {
+ toValue,
+ type StyleProperty,
+ type StyleValue,
+} from "@webstudio-is/css-engine";
import { Tooltip, Flex, Text } from "@webstudio-is/design-system";
-import { parseCssValue } from "@webstudio-is/css-data";
-import { RepeatedStyleSection } from "../../shared/style-section";
import { InfoCircleIcon } from "@webstudio-is/icons";
-import { addLayer } from "../../style-layer-utils";
-import { LayersList } from "../../style-layers-list";
+import { humanizeString } from "~/shared/string-utils";
+import { RepeatedStyleSection } from "../../shared/style-section";
import { FilterSectionContent } from "../../shared/filter-content";
+import { parseCssFragment } from "../../shared/parse-css-fragment";
+import {
+ addRepeatedStyleItem,
+ editRepeatedStyleItem,
+ RepeatedStyle,
+} from "../../shared/repeated-style";
+import { useComputedStyleDecl } from "../../shared/model";
export const properties = ["backdropFilter"] satisfies [
StyleProperty,
@@ -17,9 +25,16 @@ const property: StyleProperty = properties[0];
const label = "Backdrop Filters";
const initialBackdropFilter = "blur(0px)";
-export const Section = (props: SectionProps) => {
- const { currentStyle, deleteProperty } = props;
- const value = currentStyle[property]?.value;
+const getItemProps = (_index: number, value: StyleValue) => {
+ const label =
+ value.type === "function"
+ ? `${humanizeString(value.name)}: ${toValue(value.args)}`
+ : "Unknown filter";
+ return { label };
+};
+
+export const Section = () => {
+ const styleDecl = useComputedStyleDecl("filter");
return (
<RepeatedStyleSection
@@ -27,55 +42,52 @@ export const Section = (props: SectionProps) => {
description="Backdrop filters are similar to filters, but are applied to the area behind an element. This can be useful for creating frosted glass effects."
properties={properties}
onAdd={() => {
- addLayer(
- property,
- parseCssValue(property, initialBackdropFilter) as LayersValue,
- currentStyle,
- props.createBatchUpdate
+ addRepeatedStyleItem(
+ [styleDecl],
+ parseCssFragment(initialBackdropFilter, "backdropFilter")
);
}}
>
- {value?.type === "tuple" && value.value.length > 0 && (
- <LayersList
- {...props}
- property={property}
- value={value}
- label={label}
- deleteProperty={deleteProperty}
- renderContent={(layerProps) => {
- if (layerProps.layer.type !== "function") {
- return <></>;
- }
-
- return (
- <FilterSectionContent
- {...layerProps}
- property={property}
- layer={layerProps.layer}
- tooltip={
- <Tooltip
- variant="wrapped"
- content={
- <Flex gap="2" direction="column">
- <Text variant="regularBold">{label}</Text>
- <Text variant="monoBold">backdrop-filter</Text>
- <Text>
- Applies graphical effects like blur or color shift to
- the area behind an element
- <br /> <br />
- <Text variant="mono">{initialBackdropFilter}</Text>
- </Text>
- </Flex>
- }
- >
- <InfoCircleIcon />
- </Tooltip>
+ <RepeatedStyle
+ label={label}
+ styles={[styleDecl]}
+ getItemProps={getItemProps}
+ renderItemContent={(index, primaryValue) => (
+ <FilterSectionContent
+ index={index}
+ property={property}
+ propertyValue={toValue(primaryValue)}
+ layer={primaryValue}
+ onEditLayer={(index, value, options) => {
+ editRepeatedStyleItem(
+ [styleDecl],
+ index,
+ new Map([["backdropFilter", value]]),
+ options
+ );
+ }}
+ tooltip={
+ <Tooltip
+ variant="wrapped"
+ content={
+ <Flex gap="2" direction="column">
+ <Text variant="regularBold">{label}</Text>
+ <Text variant="monoBold">backdrop-filter</Text>
+ <Text>
+ Applies graphical effects like blur or color shift to the
+ area behind an element
+ <br /> <br />
+ <Text variant="mono">{initialBackdropFilter}</Text>
+ </Text>
+ </Flex>
}
- />
- );
- }}
- />
- )}
+ >
+ <InfoCircleIcon />
+ </Tooltip>
+ }
+ />
+ )}
+ />
</RepeatedStyleSection>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsx b/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsx
index f1c5d8e2e2..a1094a35a3 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
@@ -1,12 +1,20 @@
-import type { SectionProps } from "../shared/section";
import { Flex, Tooltip, Text } from "@webstudio-is/design-system";
-import { parseCssValue } from "@webstudio-is/css-data";
-import { RepeatedStyleSection } from "../../shared/style-section";
import { InfoCircleIcon } from "@webstudio-is/icons";
-import { LayersValue, type StyleProperty } from "@webstudio-is/css-engine";
-import { LayersList } from "../../style-layers-list";
-import { addLayer } from "../../style-layer-utils";
+import {
+ toValue,
+ type StyleProperty,
+ type StyleValue,
+} from "@webstudio-is/css-engine";
+import { RepeatedStyleSection } from "../../shared/style-section";
import { FilterSectionContent } from "../../shared/filter-content";
+import {
+ addRepeatedStyleItem,
+ editRepeatedStyleItem,
+ RepeatedStyle,
+} from "../../shared/repeated-style";
+import { parseCssFragment } from "../../shared/parse-css-fragment";
+import { useComputedStyleDecl } from "../../shared/model";
+import { humanizeString } from "~/shared/string-utils";
export const properties = ["filter"] satisfies [
StyleProperty,
@@ -17,9 +25,16 @@ const property: StyleProperty = properties[0];
const label = "Filters";
const initialFilter = "blur(0px)";
-export const Section = (props: SectionProps) => {
- const { currentStyle, deleteProperty } = props;
- const value = currentStyle[property]?.value;
+const getItemProps = (_index: number, value: StyleValue) => {
+ const label =
+ value.type === "function"
+ ? `${humanizeString(value.name)}: ${toValue(value.args)}`
+ : "Unknown filter";
+ return { label };
+};
+
+export const Section = () => {
+ const styleDecl = useComputedStyleDecl("filter");
return (
<RepeatedStyleSection
@@ -27,55 +42,52 @@ export const Section = (props: SectionProps) => {
description="Filter effects allow you to apply graphical effects like blurring, color shifting, and more to elements."
properties={properties}
onAdd={() => {
- addLayer(
- property,
- parseCssValue(property, initialFilter) as LayersValue,
- currentStyle,
- props.createBatchUpdate
+ addRepeatedStyleItem(
+ [styleDecl],
+ parseCssFragment(initialFilter, "filter")
);
}}
>
- {value?.type === "tuple" && value.value.length > 0 && (
- <LayersList
- {...props}
- property={property}
- value={value}
- label={label}
- deleteProperty={deleteProperty}
- renderContent={(layerProps) => {
- if (layerProps.layer.type !== "function") {
- return <></>;
- }
-
- return (
- <FilterSectionContent
- {...layerProps}
- property={property}
- layer={layerProps.layer}
- tooltip={
- <Tooltip
- variant="wrapped"
- content={
- <Flex gap="2" direction="column">
- <Text variant="regularBold">{label}</Text>
- <Text variant="monoBold">filter</Text>
- <Text>
- Applies graphical effects like blur or color shift to
- an element, for example:
- <br /> <br />
- <Text variant="mono">{initialFilter}</Text>
- </Text>
- </Flex>
- }
- >
- <InfoCircleIcon />
- </Tooltip>
+ <RepeatedStyle
+ label={label}
+ styles={[styleDecl]}
+ getItemProps={getItemProps}
+ renderItemContent={(index, primaryValue) => (
+ <FilterSectionContent
+ index={index}
+ property={property}
+ propertyValue={toValue(primaryValue)}
+ layer={primaryValue}
+ onEditLayer={(index, value, options) => {
+ editRepeatedStyleItem(
+ [styleDecl],
+ index,
+ new Map([["filter", value]]),
+ options
+ );
+ }}
+ tooltip={
+ <Tooltip
+ variant="wrapped"
+ content={
+ <Flex gap="2" direction="column">
+ <Text variant="regularBold">{label}</Text>
+ <Text variant="monoBold">filter</Text>
+ <Text>
+ Applies graphical effects like blur or color shift to an
+ element, for example:
+ <br /> <br />
+ <Text variant="mono">{initialFilter}</Text>
+ </Text>
+ </Flex>
}
- />
- );
- }}
- />
- )}
+ >
+ <InfoCircleIcon />
+ </Tooltip>
+ }
+ />
+ )}
+ />
</RepeatedStyleSection>
);
};
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.tsx
index f9f4c15ade..eca7a67ecd 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
@@ -2,7 +2,6 @@ import { camelCase } from "change-case";
import {
type InvalidValue,
type TupleValue,
- type FunctionValue,
toValue,
StyleValue,
} from "@webstudio-is/css-engine";
@@ -22,10 +21,7 @@ import {
type IntermediateStyleValue,
} from "../shared/css-value-input";
import { parseCssValue } from "@webstudio-is/css-data";
-import type {
- DeleteProperty,
- StyleUpdateOptions,
-} from "../shared/use-style-data";
+import type { StyleUpdateOptions } from "../shared/use-style-data";
import { ShadowContent } from "./shadow-content";
import { parseCssFragment } from "./parse-css-fragment";
@@ -80,7 +76,7 @@ const filterFunctions = {
type FilterContentProps = {
index: number;
property: "filter" | "backdropFilter";
- layer: FunctionValue;
+ layer: StyleValue;
propertyValue: string;
tooltip: JSX.Element;
onEditLayer: (
@@ -88,7 +84,6 @@ type FilterContentProps = {
layers: TupleValue,
options: StyleUpdateOptions
) => void;
- deleteProperty: DeleteProperty;
};
type FilterFunction = keyof typeof filterFunctions;
@@ -101,7 +96,6 @@ export const FilterSectionContent = ({
property,
propertyValue,
onEditLayer,
- deleteProperty,
tooltip,
layer,
}: FilterContentProps) => {
@@ -116,7 +110,11 @@ export const FilterSectionContent = ({
>(undefined);
useEffect(() => {
- if (isFilterFunction(layer.name) === false || layer.args.type !== "tuple") {
+ if (
+ layer.type !== "function" ||
+ isFilterFunction(layer.name) === false ||
+ layer.args.type !== "tuple"
+ ) {
return;
}
@@ -217,13 +215,15 @@ export const FilterSectionContent = ({
}
keywords={[]}
setValue={handleFilterFunctionValueChange}
- deleteProperty={deleteProperty}
+ deleteProperty={() => {}}
/>
</Grid>
) : undefined}
</Flex>
- {filterFunction === "drop-shadow" && layer.args.type === "tuple" ? (
+ {filterFunction === "drop-shadow" &&
+ layer.type === "function" &&
+ layer.args.type === "tuple" ? (
<ShadowContent
index={index}
property="dropShadow"
@@ -278,12 +278,6 @@ export const FilterSectionContent = ({
// We prevent the default behaviour
event.preventDefault();
}
-
- if (event.key === "Escape") {
- deleteProperty(property, { isEphemeral: true });
- setIntermediateValue(undefined);
- event.preventDefault();
- }
}}
/>
</Flex>
diff --git a/apps/builder/app/builder/features/style-panel/shared/repeated-style.test.ts b/apps/builder/app/builder/features/style-panel/shared/repeated-style.test.ts
new file mode 100644
index 0000000000..1f5ea58aee
--- /dev/null
+++ b/apps/builder/app/builder/features/style-panel/shared/repeated-style.test.ts
@@ -0,0 +1,344 @@
+import { beforeEach, expect, test } from "@jest/globals";
+import {
+ addRepeatedStyleItem,
+ deleteRepeatedStyleItem,
+ editRepeatedStyleItem,
+ swapRepeatedStyleItems,
+ toggleRepeatedStyleItem,
+} from "./repeated-style";
+import { createComputedStyleDeclStore } from "./model";
+import { parseCssFragment } from "./parse-css-fragment";
+import {
+ $breakpoints,
+ $selectedBreakpointId,
+ $selectedInstanceSelector,
+ $styles,
+} from "~/shared/nano-states";
+import { registerContainers } from "~/shared/sync";
+
+registerContainers();
+
+beforeEach(() => {
+ $breakpoints.set(new Map([["base", { id: "base", label: "" }]]));
+ $selectedBreakpointId.set("base");
+ $selectedInstanceSelector.set(["box"]);
+ $styles.set(new Map());
+});
+
+test("add layer to repeated style", () => {
+ const $transitionProperty =
+ createComputedStyleDeclStore("transitionProperty");
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("opacity", "transitionProperty")
+ );
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("transform", "transitionProperty")
+ );
+ expect($transitionProperty.get().cascadedValue).toEqual({
+ type: "layers",
+ value: [
+ { type: "unparsed", value: "opacity" },
+ { type: "unparsed", value: "transform" },
+ ],
+ });
+});
+
+test("add tuple to repeated style", () => {
+ const $filter = createComputedStyleDeclStore("filter");
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("blur(5px)", "filter")
+ );
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("brightness(0.5)", "filter")
+ );
+ expect($filter.get().cascadedValue).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "function",
+ name: "blur",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "px", value: 5 }],
+ },
+ },
+ {
+ type: "function",
+ name: "brightness",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "number", value: 0.5 }],
+ },
+ },
+ ],
+ });
+});
+
+test("edit layer in repeated style", () => {
+ const $transitionProperty =
+ createComputedStyleDeclStore("transitionProperty");
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("opacity", "transitionProperty")
+ );
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("transform", "transitionProperty")
+ );
+ editRepeatedStyleItem(
+ [$transitionProperty.get()],
+ 1,
+ parseCssFragment("width", "transitionProperty")
+ );
+ expect($transitionProperty.get().cascadedValue).toEqual({
+ type: "layers",
+ value: [
+ { type: "unparsed", value: "opacity" },
+ { type: "unparsed", value: "width" },
+ ],
+ });
+});
+
+test("edit tuple in repeated style", () => {
+ const $filter = createComputedStyleDeclStore("filter");
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("blur(5px)", "filter")
+ );
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("brightness(0.5)", "filter")
+ );
+ editRepeatedStyleItem(
+ [$filter.get()],
+ 1,
+ parseCssFragment("contrast(200%)", "filter")
+ );
+ expect($filter.get().cascadedValue).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "function",
+ name: "blur",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "px", value: 5 }],
+ },
+ },
+ {
+ type: "function",
+ name: "contrast",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "%", value: 200 }],
+ },
+ },
+ ],
+ });
+});
+
+test("delete layer from repeated style", () => {
+ const $transitionProperty =
+ createComputedStyleDeclStore("transitionProperty");
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("opacity", "transitionProperty")
+ );
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("transform", "transitionProperty")
+ );
+ deleteRepeatedStyleItem([$transitionProperty.get()], 0);
+ expect($transitionProperty.get().cascadedValue).toEqual({
+ type: "layers",
+ value: [{ type: "unparsed", value: "transform" }],
+ });
+});
+
+test("delete value without layers from repeated style", () => {
+ const $transitionProperty =
+ createComputedStyleDeclStore("transitionProperty");
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("opacity", "transitionProperty")
+ );
+ expect($transitionProperty.get().source.name).toEqual("local");
+ deleteRepeatedStyleItem([$transitionProperty.get()], 0);
+ expect($transitionProperty.get().source.name).toEqual("default");
+ expect($transitionProperty.get().cascadedValue).toEqual({
+ type: "keyword",
+ value: "all",
+ });
+});
+
+test("delete tuple from repeated style", () => {
+ const $filter = createComputedStyleDeclStore("filter");
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("blur(5px)", "filter")
+ );
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("brightness(0.5)", "filter")
+ );
+ deleteRepeatedStyleItem([$filter.get()], 0);
+ expect($filter.get().cascadedValue).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "function",
+ name: "brightness",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "number", value: 0.5 }],
+ },
+ },
+ ],
+ });
+});
+
+test("toggle layer in repeated style", () => {
+ const $transitionProperty =
+ createComputedStyleDeclStore("transitionProperty");
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("opacity", "transitionProperty")
+ );
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("transform", "transitionProperty")
+ );
+ toggleRepeatedStyleItem([$transitionProperty.get()], 0);
+ expect($transitionProperty.get().cascadedValue).toEqual({
+ type: "layers",
+ value: [
+ { type: "unparsed", value: "opacity", hidden: true },
+ { type: "unparsed", value: "transform" },
+ ],
+ });
+ toggleRepeatedStyleItem([$transitionProperty.get()], 0);
+ expect($transitionProperty.get().cascadedValue).toEqual({
+ type: "layers",
+ value: [
+ { type: "unparsed", value: "opacity", hidden: false },
+ { type: "unparsed", value: "transform" },
+ ],
+ });
+});
+
+test("toggle tuple in repeated style", () => {
+ const $filter = createComputedStyleDeclStore("filter");
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("blur(5px)", "filter")
+ );
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("brightness(0.5)", "filter")
+ );
+ toggleRepeatedStyleItem([$filter.get()], 0);
+ expect($filter.get().cascadedValue).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "function",
+ name: "blur",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "px", value: 5 }],
+ },
+ hidden: true,
+ },
+ {
+ type: "function",
+ name: "brightness",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "number", value: 0.5 }],
+ },
+ },
+ ],
+ });
+ toggleRepeatedStyleItem([$filter.get()], 0);
+ expect($filter.get().cascadedValue).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "function",
+ name: "blur",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "px", value: 5 }],
+ },
+ hidden: false,
+ },
+ {
+ type: "function",
+ name: "brightness",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "number", value: 0.5 }],
+ },
+ },
+ ],
+ });
+});
+
+test("swap layers in repeated style", () => {
+ const $transitionProperty =
+ createComputedStyleDeclStore("transitionProperty");
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("opacity", "transitionProperty")
+ );
+ addRepeatedStyleItem(
+ [$transitionProperty.get()],
+ parseCssFragment("transform", "transitionProperty")
+ );
+ swapRepeatedStyleItems([$transitionProperty.get()], 0, 1);
+ expect($transitionProperty.get().cascadedValue).toEqual({
+ type: "layers",
+ value: [
+ { type: "unparsed", value: "transform" },
+ { type: "unparsed", value: "opacity" },
+ ],
+ });
+});
+
+test("add tuple items in repeated style", () => {
+ const $filter = createComputedStyleDeclStore("filter");
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("blur(5px)", "filter")
+ );
+ addRepeatedStyleItem(
+ [$filter.get()],
+ parseCssFragment("brightness(0.5)", "filter")
+ );
+ swapRepeatedStyleItems([$filter.get()], 0, 1);
+ expect($filter.get().cascadedValue).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "function",
+ name: "brightness",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "number", value: 0.5 }],
+ },
+ },
+ {
+ type: "function",
+ name: "blur",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", unit: "px", value: 5 }],
+ },
+ },
+ ],
+ });
+});
diff --git a/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx b/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx
index 058bd3bf39..67e5b5fd4e 100644
--- a/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx
@@ -4,6 +4,8 @@ import type {
LayersValue,
StyleProperty,
StyleValue,
+ TupleValue,
+ UnparsedValue,
} from "@webstudio-is/css-engine";
import {
EyeconClosedIcon,
@@ -25,18 +27,20 @@ import type { ComputedStyleDecl } from "~/shared/style-object-model";
import { createBatchUpdate, type StyleUpdateOptions } from "./use-style-data";
import { ColorThumb } from "./color-thumb";
+type ItemType = "layers" | "tuple";
+
const normalizeStyleValue = (
value: undefined | StyleValue,
- primaryValue: StyleValue
-): LayersValue => {
- const primaryLayersCount =
- primaryValue.type === "layers" ? primaryValue.value.length : 0;
- const layers = value?.type === "layers" ? value.value : [];
- const normalizedLayers: LayersValue = {
- type: "layers",
- value: repeatUntil(layers, primaryLayersCount),
- };
- return normalizedLayers;
+ primaryValue: StyleValue,
+ itemType: ItemType = primaryValue.type === "tuple" ? "tuple" : "layers"
+) => {
+ const primaryItemsCount =
+ primaryValue.type === itemType ? primaryValue.value.length : 0;
+ const items = value?.type === itemType ? value.value : [];
+ return {
+ type: itemType,
+ value: repeatUntil(items, primaryItemsCount),
+ } as TupleValue | LayersValue;
};
export const addRepeatedStyleItem = (
@@ -48,14 +52,17 @@ export const addRepeatedStyleItem = (
styles.map((styleDecl) => [styleDecl.property, styleDecl.cascadedValue])
);
const primaryValue = styles[0].cascadedValue;
+ // infer type from new items
+ // because current values could be css wide keywords
+ const itemType: ItemType =
+ Array.from(newItems.values())[0]?.type === "tuple" ? "tuple" : "layers";
for (const [property, value] of newItems) {
- const currentValue = currentStyles.get(property);
- const normalizedValue = normalizeStyleValue(currentValue, primaryValue);
- const newLayers = value.type === "layers" ? value.value : [];
- batch.setProperty(property)({
- type: "layers",
- value: [...normalizedValue.value, ...newLayers],
- });
+ const oldValue = currentStyles.get(property);
+ const newValue = normalizeStyleValue(oldValue, primaryValue, itemType);
+ const newItems =
+ value.type === newValue.type ? (value.value as UnparsedValue[]) : [];
+ newValue.value.push(...newItems);
+ batch.setProperty(property)(newValue);
}
batch.publish();
};
@@ -72,66 +79,74 @@ export const editRepeatedStyleItem = (
);
const primaryValue = styles[0].cascadedValue;
for (const [property, value] of newItems) {
- const currentValue = currentStyles.get(property);
- const normalizedValue = normalizeStyleValue(currentValue, primaryValue);
- const newLayers = value.type === "layers" ? value.value : [];
- normalizedValue.value.splice(index, 1, ...newLayers);
- batch.setProperty(property)({
- type: "layers",
- value: normalizedValue.value,
- });
+ const oldValue = currentStyles.get(property);
+ const newValue = normalizeStyleValue(oldValue, primaryValue);
+ const newItems = value.type === newValue.type ? value.value : [];
+ newValue.value.splice(index, 1, ...newItems);
+ batch.setProperty(property)(newValue);
}
batch.publish(options);
};
-const createLayersTransformer =
- (styles: ComputedStyleDecl[]) =>
- (
- transform: (property: string, value: LayersValue) => Partial<LayersValue>
- ) => {
- const batch = createBatchUpdate();
- const primaryValue = styles[0].cascadedValue;
- for (const styleDecl of styles) {
- const value = styleDecl.cascadedValue;
- const normalizedValue = normalizeStyleValue(value, primaryValue);
- const newLayers: LayersValue = {
- ...normalizedValue,
- ...transform(styleDecl.property, normalizedValue),
- };
- // delete empty layers
- if (newLayers.value.length === 0) {
- batch.deleteProperty(styleDecl.property as StyleProperty);
- } else {
- batch.setProperty(styleDecl.property as StyleProperty)(newLayers);
- }
+export const deleteRepeatedStyleItem = (
+ styles: ComputedStyleDecl[],
+ index: number
+) => {
+ const batch = createBatchUpdate();
+ const primaryValue = styles[0].cascadedValue;
+ for (const styleDecl of styles) {
+ const oldValue = styleDecl.cascadedValue;
+ const newValue = normalizeStyleValue(oldValue, primaryValue);
+ newValue.value.splice(index, 1);
+ if (newValue.value.length > 0) {
+ batch.setProperty(styleDecl.property as StyleProperty)(newValue);
+ } else {
+ // delete empty layers or tuple
+ batch.deleteProperty(styleDecl.property as StyleProperty);
}
- batch.publish();
- };
-
-const hideLayer = (value: LayersValue, layerIndex: number) => ({
- value: value.value.map((item, index) =>
- index === layerIndex
- ? { ...item, hidden: false === (item.hidden ?? false) }
- : item
- ),
-});
+ }
+ batch.publish();
+};
-const deleteLayer = (value: LayersValue, layerIndex: number) => ({
- value: value.value.filter((_item, index) => index !== layerIndex),
-});
+export const toggleRepeatedStyleItem = (
+ styles: ComputedStyleDecl[],
+ index: number
+) => {
+ const batch = createBatchUpdate();
+ const primaryValue = styles[0].cascadedValue;
+ for (const styleDecl of styles) {
+ const oldValue = styleDecl.cascadedValue;
+ const newValue = normalizeStyleValue(oldValue, primaryValue);
+ newValue.value[index] = {
+ ...newValue.value[index],
+ hidden: false === (newValue.value[index].hidden ?? false),
+ };
+ batch.setProperty(styleDecl.property as StyleProperty)(newValue);
+ }
+ batch.publish();
+};
-const swapLayers = (value: LayersValue, oldIndex: number, newIndex: number) => {
- const newValue = Array.from(value.value);
- // You can swap only if there are at least two layers
- // As we are checking across multiple properties, we can't be sure
- // which property don't have two layers so we are checking here.
- if (value.value.length >= 2) {
- newValue.splice(oldIndex, 1);
- newValue.splice(newIndex, 0, value.value[oldIndex]);
+export const swapRepeatedStyleItems = (
+ styles: ComputedStyleDecl[],
+ oldIndex: number,
+ newIndex: number
+) => {
+ const batch = createBatchUpdate();
+ const primaryValue = styles[0].cascadedValue;
+ for (const styleDecl of styles) {
+ const oldValue = styleDecl.cascadedValue;
+ const newValue = normalizeStyleValue(oldValue, primaryValue);
+ // You can swap only if there are at least two layers
+ // As we are checking across multiple properties, we can't be sure
+ // which property don't have two layers so we are checking here.
+ if (newValue.value.length >= 2) {
+ const oldItem = newValue.value[oldIndex];
+ newValue.value.splice(oldIndex, 1);
+ newValue.value.splice(newIndex, 0, oldItem);
+ }
+ batch.setProperty(styleDecl.property as StyleProperty)(newValue);
}
- return {
- value: newValue,
- };
+ batch.publish();
};
export const RepeatedStyle = (props: {
@@ -143,37 +158,37 @@ export const RepeatedStyle = (props: {
) => { label: string; color?: RgbaColor };
renderItemContent: (index: number, primaryValue: StyleValue) => JSX.Element;
}) => {
- const transformLayers = createLayersTransformer(props.styles);
const { label, styles, getItemProps, renderItemContent } = props;
- // first property should describe the amount of layers
- const layers = styles[0].cascadedValue;
- const primaryValues = layers?.type === "layers" ? layers.value : [];
+ // first property should describe the amount of layers or tuple items
+ const primaryValue = styles[0].cascadedValue;
+ const primaryItems =
+ primaryValue.type === "layers" || primaryValue.type === "tuple"
+ ? primaryValue.value
+ : [];
const sortableItems = useMemo(
() =>
- Array.from(Array(primaryValues.length), (_, index) => ({
+ Array.from(Array(primaryItems.length), (_, index) => ({
id: String(index),
index,
})),
- [primaryValues.length]
+ [primaryItems.length]
);
const { dragItemId, placementIndicator, sortableRefCallback } = useSortable({
items: sortableItems,
onSort: (newIndex, oldIndex) =>
- transformLayers((_property, value) =>
- swapLayers(value, oldIndex, newIndex)
- ),
+ swapRepeatedStyleItems(styles, oldIndex, newIndex),
});
- if (primaryValues.length === 0) {
+ if (primaryItems.length === 0) {
return;
}
return (
<CssValueListArrowFocus dragItemId={dragItemId}>
<Flex direction="column" ref={sortableRefCallback}>
- {primaryValues.map((primaryValue, index) => {
+ {primaryItems.map((primaryValue, index) => {
const id = String(index);
const { label: itemLabel, color: itemColor } = getItemProps(
index,
@@ -201,9 +216,7 @@ export const RepeatedStyle = (props: {
disabled={false}
tabIndex={-1}
onPressedChange={() =>
- transformLayers((_property, value) =>
- hideLayer(value, index)
- )
+ toggleRepeatedStyleItem(styles, index)
}
icon={
primaryValue.hidden ? (
@@ -217,11 +230,7 @@ export const RepeatedStyle = (props: {
variant="destructive"
tabIndex={-1}
icon={<SubtractIcon />}
- onClick={() =>
- transformLayers((_property, value) =>
- deleteLayer(value, index)
- )
- }
+ onClick={() => deleteRepeatedStyleItem(styles, index)}
/>
</>
}
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
deleted file mode 100644
index 5376e7b948..0000000000
--- a/apps/builder/app/builder/features/style-panel/style-layer-utils.test.ts
+++ /dev/null
@@ -1,394 +0,0 @@
-import { afterEach, describe, expect, test } from "@jest/globals";
-import type {
- FunctionValue,
- LayersValue,
- StyleProperty,
- StyleValue,
- TupleValue,
-} from "@webstudio-is/css-engine";
-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 { parseCssValue } from "@webstudio-is/css-data";
-
-let styleInfo: StyleInfo = {};
-let published = false;
-const deletedProperties = new Set<string>();
-
-const property: StyleProperty = "boxShadow";
-
-afterEach(() => {
- styleInfo = {};
- published = false;
- deletedProperties.clear();
-});
-
-const createBatchUpdate: CreateBatchUpdate = () => ({
- setProperty: (propertyName: StyleProperty) => (newValue: StyleValue) => {
- styleInfo[propertyName] = { value: newValue, local: newValue };
- },
- deleteProperty: (propertyName: string) => {
- deletedProperties.add(propertyName);
- },
- publish: (_options?: unknown) => {
- published = true;
- },
-});
-
-describe("boxShadowUtils", () => {
- test("delets a layer using the index", () => {
- const layers: LayersValue = {
- type: "layers",
- value: [
- {
- type: "tuple",
- value: [
- {
- type: "unit",
- unit: "number",
- value: 0,
- },
- {
- type: "unit",
- unit: "px",
- value: 60,
- },
- {
- type: "unit",
- unit: "px",
- value: 80,
- },
- {
- alpha: 0.6,
- b: 0,
- g: 0,
- r: 0,
- type: "rgb",
- },
- ],
- },
- {
- type: "tuple",
- value: [
- {
- type: "unit",
- unit: "number",
- value: 0,
- },
- {
- type: "unit",
- unit: "px",
- value: 45,
- },
- {
- type: "unit",
- unit: "px",
- value: 26,
- },
- {
- alpha: 0.14,
- b: 0,
- g: 0,
- r: 0,
- type: "rgb",
- },
- ],
- },
- ],
- };
-
- deleteLayer(property, 1, layers, createBatchUpdate);
- const boxShadowValue = styleInfo["boxShadow"]?.value as LayersValue;
-
- expect(published).toBe(true);
- expect(boxShadowValue).toBeDefined();
- expect(deletedProperties.has("boxShadow")).toBe(false);
- expect(boxShadowValue.value[0].type).toBe("tuple");
- expect((boxShadowValue.value[0] as TupleValue).value)
- .toMatchInlineSnapshot(`
- [
- {
- "type": "unit",
- "unit": "number",
- "value": 0,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 60,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 80,
- },
- {
- "alpha": 0.6,
- "b": 0,
- "g": 0,
- "r": 0,
- "type": "rgb",
- },
- ]
- `);
- });
-
- test("delets the property when there is only one layer in it", () => {
- const layers: LayersValue = {
- type: "layers",
- value: [
- {
- type: "tuple",
- value: [
- {
- type: "unit",
- unit: "px",
- value: 60,
- },
- {
- type: "unit",
- unit: "px",
- value: -16,
- },
- {
- type: "keyword",
- value: "teal",
- },
- ],
- },
- ],
- };
-
- deleteLayer(property, 0, layers, createBatchUpdate);
- expect(published).toBe(true);
- expect(deletedProperties.has("boxShadow")).toBe(true);
- });
-
- test("hides the layer using index", () => {
- const layers: LayersValue = {
- type: "layers",
- value: [
- {
- type: "tuple",
- value: [
- {
- type: "unit",
- unit: "px",
- value: 60,
- },
- {
- type: "unit",
- unit: "px",
- value: -16,
- },
- {
- type: "keyword",
- value: "teal",
- },
- ],
- },
- ],
- };
-
- hideLayer(property, 0, layers, createBatchUpdate);
- const boxShadowValue = styleInfo["boxShadow"]?.value as LayersValue;
-
- expect(published).toBe(true);
- expect(boxShadowValue).toBeDefined();
- expect(deletedProperties.has("boxShadow")).toBe(false);
- expect(boxShadowValue.value[0].hidden).toBe(true);
- });
-
- test("adds layer to box-shadow proeprty", () => {
- addLayer(
- property,
- parseCssValue(
- "boxShadow",
- `10px 10px 10px 0px rgba(0, 0, 0, 0.75)`
- ) as LayersValue,
- styleInfo,
- createBatchUpdate
- );
-
- const boxShadow = styleInfo["boxShadow"]?.value as LayersValue;
-
- expect(boxShadow).toBeDefined();
- expect(published).toBe(true);
- expect(boxShadow.value).toHaveLength(1);
- expect(boxShadow.value).toMatchInlineSnapshot(`
- [
- {
- "type": "tuple",
- "value": [
- {
- "type": "unit",
- "unit": "px",
- "value": 10,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 10,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 10,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- {
- "alpha": 0.75,
- "b": 0,
- "g": 0,
- "r": 0,
- "type": "rgb",
- },
- ],
- },
- ]
- `);
- });
-
- test("updates the layer with new value using the index", () => {
- const newLayer: LayersValue = {
- type: "layers",
- value: [
- {
- type: "tuple",
- value: [
- {
- type: "unit",
- unit: "px",
- value: 50,
- },
- {
- type: "unit",
- unit: "px",
- value: 50,
- },
- {
- type: "keyword",
- value: "green",
- },
- ],
- },
- ],
- };
-
- const oldLayers: LayersValue = {
- type: "layers",
- value: [
- {
- type: "tuple",
- value: [
- {
- type: "unit",
- unit: "px",
- value: 60,
- },
- {
- type: "unit",
- unit: "px",
- value: -16,
- },
- {
- type: "keyword",
- value: "teal",
- },
- ],
- },
- ],
- };
-
- updateLayer(property, newLayer, oldLayers, 0, createBatchUpdate, {
- isEphemeral: false,
- });
- const boxShadow = styleInfo["boxShadow"]?.value as LayersValue;
- expect(boxShadow).toBeDefined();
- expect(boxShadow.value[0]).toBe(newLayer.value[0]);
- });
-
- test("swaps the layers using indexe's", () => {
- addLayer(
- property,
- parseCssValue(
- "boxShadow",
- `0 60px 80px rgba(0,0,0,0.60), 0 45px 26px rgba(0,0,0,0.14)`
- ) as LayersValue,
- styleInfo,
- createBatchUpdate
- );
-
- swapLayers(property, 0, 1, styleInfo, createBatchUpdate);
- const boxShadow = styleInfo["boxShadow"]?.value as LayersValue;
-
- expect(boxShadow.value[0].type).toBe("tuple");
- expect((boxShadow.value[0] as TupleValue).value).toMatchInlineSnapshot(`
- [
- {
- "type": "unit",
- "unit": "number",
- "value": 0,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 45,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 26,
- },
- {
- "alpha": 0.14,
- "b": 0,
- "g": 0,
- "r": 0,
- "type": "rgb",
- },
- ]
- `);
- });
-});
-
-test("Generates humane layer names for filter style layer", () => {
- expect(
- getHumanizedTextFromLayer(
- "filter",
- (parseCssValue("filter", "blur(50px)") as TupleValue)
- .value[0] as FunctionValue
- )
- ).toMatchInlineSnapshot(`
-{
- "color": undefined,
- "name": "Blur: 50px",
- "value": "blur(50px)",
-}
-`);
-
- expect(
- getHumanizedTextFromLayer(
- "filter",
- (parseCssValue("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)",
-}
-`);
-});
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.ts
deleted file mode 100644
index 6bbfe6ee19..0000000000
--- a/apps/builder/app/builder/features/style-panel/style-layer-utils.ts
+++ /dev/null
@@ -1,202 +0,0 @@
-import {
- StyleValue,
- toValue,
- 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";
-import type {
- CreateBatchUpdate,
- StyleUpdateOptions,
-} from "./shared/use-style-data";
-import { humanizeString } from "~/shared/string-utils";
-import type { LayerListProperty } from "./style-layers-list";
-
-export const deleteLayer = <Layers extends TupleValue | LayersValue>(
- property: StyleProperty,
- index: number,
- layers: Layers,
- createBatchUpdate: SectionProps["createBatchUpdate"]
-) => {
- const batch = createBatchUpdate();
- const newLayers = [...layers.value];
- newLayers.splice(index, 1);
-
- const propertyValue: LayersValue | TupleValue =
- layers.type === "tuple"
- ? {
- type: "tuple",
- value: newLayers as TupleValue["value"],
- }
- : {
- type: "layers",
- value: newLayers as LayersValue["value"],
- };
-
- if (newLayers.length === 0) {
- batch.deleteProperty(property);
- } else {
- batch.setProperty(property)(propertyValue);
- }
-
- batch.publish();
-};
-
-export const hideLayer = <Layers extends LayersValue | TupleValue>(
- property: StyleProperty,
- index: number,
- layers: Layers,
- createBatchUpdate: SectionProps["createBatchUpdate"]
-) => {
- if (layers.type !== "layers" && layers.type !== "tuple") {
- return;
- }
-
- const newLayersValue = layers.value.map((layer, layerIndex) => {
- if (layerIndex !== index) {
- return layer;
- }
-
- if (layer.type === "function" || layer.type === "tuple") {
- return {
- ...layer,
- hidden: layer.hidden ? false : true,
- } as Layers["value"][number];
- }
- });
-
- const newLayers: Layers = JSON.parse(JSON.stringify(layers));
- newLayers.value = newLayersValue as Layers["value"];
-
- const batch = createBatchUpdate();
- batch.setProperty(property)(newLayers);
- batch.publish();
-};
-
-export const addLayer = <Layers extends LayersValue | TupleValue>(
- property: StyleProperty,
- value: Layers | InvalidValue,
- style: StyleInfo,
- createBatchUpdate: SectionProps["createBatchUpdate"]
-) => {
- if (
- value.type === "invalid" ||
- (value.type !== "layers" && value.type !== "tuple")
- ) {
- return;
- }
-
- const existingValues = style[property]?.value;
- if (existingValues?.type === "layers") {
- value.value = [...value.value, ...existingValues.value] as Layers["value"];
- }
-
- // Transitions come's with a default property of tuple. Which needs to be overwritten
- // Because, we handle transitions, box-shadow and text-shadows etc using layers in the project.
- // So, we merge the values of the new layer with the existing layer is tuple and the property is filter or backdropFilter
- if (
- (property === "filter" || property === "backdropFilter") &&
- existingValues?.type === "tuple"
- ) {
- value.value = [
- ...value.value,
- ...(existingValues?.value || []),
- ] as Layers["value"];
- }
-
- const batch = createBatchUpdate();
- batch.setProperty(property)(value);
- batch.publish();
-};
-
-export const updateLayer = <Layers extends LayersValue | TupleValue>(
- property: StyleProperty,
- newValue: Layers,
- oldValue: Layers,
- index: number,
- createBatchUpdate: SectionProps["createBatchUpdate"],
- options: StyleUpdateOptions
-) => {
- const batch = createBatchUpdate();
- const newLayers = [...oldValue.value];
- newLayers.splice(index, 1, ...newValue.value);
-
- const newPropertyValue: TupleValue | LayersValue =
- oldValue.type === "tuple"
- ? {
- type: "tuple",
- value: newLayers as TupleValue["value"],
- }
- : {
- type: "layers",
- value: newLayers as LayersValue["value"],
- };
-
- batch.setProperty(property)(newPropertyValue);
- batch.publish(options);
-};
-
-export const getLayerCount = (property: StyleProperty, style: StyleInfo) => {
- const value = style[property]?.value;
- const existingValue =
- value?.type === "layers" || value?.type === "tuple" ? value : undefined;
- return existingValue?.value.length ?? 0;
-};
-
-export const getValue = (property: StyleProperty, style: StyleInfo) => {
- const value = style[property]?.value;
- return (value?.type === "layers" || value?.type === "tuple") &&
- value.value.length > 0
- ? value
- : undefined;
-};
-
-export const swapLayers = (
- property: StyleProperty,
- newIndex: number,
- oldIndex: number,
- style: StyleInfo,
- createBatchUpdate: CreateBatchUpdate
-) => {
- const batch = createBatchUpdate();
- const value = getValue(property, style);
-
- if (value === undefined) {
- return;
- }
-
- const newValue = [...value.value];
- newValue.splice(oldIndex, 1);
- newValue.splice(newIndex, 0, value.value[oldIndex]);
-
- batch.setProperty(property)(
- value.type === "tuple"
- ? {
- type: "tuple",
- value: newValue as TupleValue["value"],
- }
- : {
- type: "layers",
- value: newValue as LayersValue["value"],
- }
- );
- batch.publish();
-};
-
-export const getHumanizedTextFromLayer = (
- property: LayerListProperty,
- layer: StyleValue
-) => {
- switch (property) {
- 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.tsx
deleted file mode 100644
index f53736d7d0..0000000000
--- a/apps/builder/app/builder/features/style-panel/style-layers-list.tsx
+++ /dev/null
@@ -1,214 +0,0 @@
-import {
- type FunctionValue,
- type LayersValue,
- type StyleProperty,
- type TupleValue,
-} from "@webstudio-is/css-engine";
-import {
- CssValueListArrowFocus,
- CssValueListItem,
- Flex,
- Label,
- SmallIconButton,
- SmallToggleButton,
- useSortable,
-} from "@webstudio-is/design-system";
-import type { SectionProps } from "./sections";
-import {
- deleteLayer,
- getLayerCount,
- hideLayer,
- swapLayers,
- updateLayer,
- getHumanizedTextFromLayer,
-} from "./style-layer-utils";
-import { useMemo } from "react";
-import type {
- DeleteProperty,
- StyleUpdateOptions,
-} from "./shared/use-style-data";
-import { FloatingPanel } from "~/builder/shared/floating-panel";
-import {
- EyeconClosedIcon,
- EyeconOpenIcon,
- SubtractIcon,
-} from "@webstudio-is/icons";
-import { ColorThumb } from "./shared/color-thumb";
-
-export type LayerListProperty = Extract<
- StyleProperty,
- | "filter"
- | "backdropFilter"
- | "textShadow"
- | "boxShadow"
- | "transitionProperty"
->;
-
-type LayerListProps = SectionProps & {
- disabled?: boolean;
- label: string;
- property: LayerListProperty;
- value: TupleValue | LayersValue;
- deleteProperty: DeleteProperty;
- deleteLayer?: (index: number) => boolean | void;
- swapLayers?: (oldIndex: number, newIndex: number) => void;
- hideLayer?: (index: number) => void;
- renderContent: (props: {
- index: number;
- layer: TupleValue | FunctionValue;
- property: LayerListProperty;
- propertyValue: string;
- onDeleteLayer: (index: number) => void;
- onEditLayer: (
- index: number,
- layers: TupleValue | LayersValue,
- options: StyleUpdateOptions
- ) => void;
- deleteProperty: DeleteProperty;
- }) => JSX.Element;
-};
-
-export const LayersList = (props: LayerListProps) => {
- const {
- label,
- property,
- value,
- currentStyle,
- createBatchUpdate,
- renderContent,
- deleteProperty,
- } = props;
- const layersCount = getLayerCount(property, currentStyle);
-
- const sortableItems = useMemo(
- () =>
- Array.from(Array(layersCount), (_, index) => ({
- id: String(index),
- index,
- })),
- [layersCount]
- );
-
- const { dragItemId, placementIndicator, sortableRefCallback } = useSortable({
- items: sortableItems,
- onSort: (newIndex, oldIndex) =>
- props.swapLayers
- ? props.swapLayers(oldIndex, newIndex)
- : swapLayers(
- property,
- newIndex,
- oldIndex,
- currentStyle,
- createBatchUpdate
- ),
- });
-
- const handleDeleteLayer = (index: number) => {
- return props?.deleteLayer
- ? props.deleteLayer(index)
- : deleteLayer(property, index, value, createBatchUpdate);
- };
-
- const handleHideLayer = (index: number) => {
- return props?.hideLayer
- ? props.hideLayer(index)
- : hideLayer(property, index, value, createBatchUpdate);
- };
-
- const onEditLayer = (
- index: number,
- newLayers: TupleValue | LayersValue,
- options: StyleUpdateOptions
- ) => {
- return updateLayer(
- property,
- newLayers,
- value,
- index,
- createBatchUpdate,
- options
- );
- };
-
- return (
- <CssValueListArrowFocus dragItemId={dragItemId}>
- <Flex direction="column" ref={sortableRefCallback}>
- {value.value.map((layer, index) => {
- // Because we are using a tuple or function to represent the layers,
- // We use tuple for text-shadow and box-shadow properties
- // and function for filter and backdrop-filter property
-
- const isLayerATupleOrFunction =
- layer.type === "tuple" || layer.type === "function";
-
- if (isLayerATupleOrFunction === false) {
- return;
- }
-
- const id = String(index);
- const properties = getHumanizedTextFromLayer(property, layer);
- if (properties === undefined) {
- return;
- }
-
- return (
- <FloatingPanel
- key={index}
- title={label}
- content={renderContent({
- index,
- property,
- layer,
- onEditLayer,
- propertyValue: properties.value,
- onDeleteLayer: handleDeleteLayer,
- deleteProperty,
- })}
- >
- <CssValueListItem
- id={id}
- draggable={value.value.length > 1}
- active={dragItemId === id}
- index={index}
- label={<Label truncate>{properties.name}</Label>}
- hidden={layer.hidden}
- thumbnail={
- property === "textShadow" || property === "boxShadow" ? (
- <ColorThumb color={properties.color} />
- ) : undefined
- }
- buttons={
- <>
- {layer.type === "tuple" || layer.type === "function" ? (
- <SmallToggleButton
- variant="normal"
- pressed={layer.hidden}
- disabled={false}
- tabIndex={-1}
- onPressedChange={() => handleHideLayer(index)}
- icon={
- layer.hidden ? (
- <EyeconClosedIcon />
- ) : (
- <EyeconOpenIcon />
- )
- }
- />
- ) : undefined}
- <SmallIconButton
- variant="destructive"
- tabIndex={-1}
- icon={<SubtractIcon />}
- onClick={() => handleDeleteLayer(index)}
- />
- </>
- }
- />
- </FloatingPanel>
- );
- })}
- {placementIndicator}
- </Flex>
- </CssValueListArrowFocus>
- );
-};
diff --git a/fixtures/ssg-netlify-by-project-id/app/constants.mjs b/fixtures/ssg-netlify-by-project-id/app/constants.mjs
index a3e96aa259..e3a9ce885b 100644
--- a/fixtures/ssg-netlify-by-project-id/app/constants.mjs
+++ b/fixtures/ssg-netlify-by-project-id/app/constants.mjs
@@ -2,6 +2,8 @@
* We use mjs extension as constants in this file is shared with the build script
* and we use `node --eval` to extract the constants.
*/
+import { UrlCanParse } from "@webstudio-is/image";
+
export const assetBaseUrl = "/assets/";
export const imageBaseUrl = "/assets/";
@@ -9,7 +11,7 @@ export const imageBaseUrl = "/assets/";
* @type {import("@webstudio-is/image").ImageLoader}
*/
export const imageLoader = (props) => {
- if (URL.canParse(props.src)) {
+ if (UrlCanParse(props.src)) {
return props.src;
}
diff --git a/fixtures/ssg-netlify-by-project-id/public/favicon.ico b/fixtures/ssg-netlify-by-project-id/public/favicon.ico
new file mode 100644
index 0000000000..9cb8c815dc
Binary files /dev/null and b/fixtures/ssg-netlify-by-project-id/public/favicon.ico differ