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: support gradient variables (#4239)

Bogdan Chadkin5f8d228810a3
diff --git a/apps/builder/app/builder/features/style-panel/controls/image/image-control.tsx b/apps/builder/app/builder/features/style-panel/controls/image/image-control.tsx
index 3c3052bc84..b51d69fbff 100644
--- a/apps/builder/app/builder/features/style-panel/controls/image/image-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/image/image-control.tsx
@@ -33,7 +33,7 @@ export const ImageControl = ({
}) => {
const assets = useStore($assets);
const styleDecl = useComputedStyleDecl(property);
- const styleValue = getRepeatedStyleItem(styleDecl.cascadedValue, index);
+ const styleValue = getRepeatedStyleItem(styleDecl, index);
const [remoteImageURL, setRemoteImageURL] = useState<
IntermediateValue | InvalidValue | undefined
>(undefined);
diff --git a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
index d9f817adcc..d53465e754 100644
--- a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
@@ -31,7 +31,7 @@ export const SelectControl = ({
const value =
index === undefined
? styleDecl.cascadedValue
- : getRepeatedStyleItem(styleDecl.cascadedValue, index);
+ : getRepeatedStyleItem(styleDecl, index);
const setValue = (value: StyleValue, options?: StyleUpdateOptions) => {
if (index === undefined) {
setProperty(property)(value, options);
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
index d2d2b301d0..b6fdbc4e2b 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
@@ -29,11 +29,13 @@ import { BackgroundImage } from "./background-image";
import { BackgroundPosition } from "./background-position";
import { PropertyInlineLabel } from "../../property-label";
import { ToggleGroupTooltip } from "../../controls/toggle-group/toggle-group-control";
-import { useComputedStyleDecl } from "../../shared/model";
+import { $availableVariables, useComputedStyleDecl } from "../../shared/model";
import {
getRepeatedStyleItem,
setRepeatedStyleItem,
} from "../../shared/repeated-style";
+import { CssValueInputContainer } from "../../shared/css-value-input";
+import { setProperty } from "../../shared/use-style-data";
const detectImageOrGradientToggle = (styleValue?: StyleValue) => {
if (styleValue?.type === "image") {
@@ -61,9 +63,39 @@ const Spacer = styled("div", {
height: theme.spacing[5],
});
+const GradientControl = ({ index }: { index: number }) => {
+ const styleDecl = useComputedStyleDecl("backgroundImage");
+ const value =
+ styleDecl.cascadedValue.type === "var"
+ ? styleDecl.cascadedValue
+ : getRepeatedStyleItem(styleDecl, index);
+ return (
+ <CssValueInputContainer
+ property="backgroundImage"
+ styleSource="default"
+ getOptions={() => $availableVariables.get()}
+ value={value}
+ setValue={(newValue, options) => {
+ if (newValue.type === "var") {
+ setProperty("backgroundImage")(newValue, options);
+ } else {
+ setRepeatedStyleItem(styleDecl, index, newValue, options);
+ }
+ }}
+ deleteProperty={() => {
+ if (styleDecl.cascadedValue.type === "var") {
+ setProperty("backgroundImage")(styleDecl.cascadedValue);
+ } else if (value) {
+ setRepeatedStyleItem(styleDecl, index, value);
+ }
+ }}
+ />
+ );
+};
+
const BackgroundRepeat = ({ index }: { index: number }) => {
const styleDecl = useComputedStyleDecl("backgroundRepeat");
- const value = getRepeatedStyleItem(styleDecl.cascadedValue, index);
+ const value = getRepeatedStyleItem(styleDecl, index);
const items = [
{
child: <CrossSmallIcon />,
@@ -136,7 +168,7 @@ const BackgroundRepeat = ({ index }: { index: number }) => {
const BackgroundAttachment = ({ index }: { index: number }) => {
const styleDecl = useComputedStyleDecl("backgroundAttachment");
- const value = getRepeatedStyleItem(styleDecl.cascadedValue, index);
+ const value = getRepeatedStyleItem(styleDecl, index);
return (
<ToggleGroup
type="single"
@@ -162,9 +194,7 @@ export const BackgroundContent = ({ index }: { index: number }) => {
const [imageGradientToggle, setImageGradientToggle] = useState<
"image" | "gradient"
>(() =>
- detectImageOrGradientToggle(
- getRepeatedStyleItem(backgroundImage.cascadedValue, index)
- )
+ detectImageOrGradientToggle(getRepeatedStyleItem(backgroundImage, index))
);
return (
@@ -217,6 +247,16 @@ export const BackgroundContent = ({ index }: { index: number }) => {
</FloatingPanelProvider>
</>
)}
+ {imageGradientToggle === "gradient" && (
+ <>
+ <PropertyInlineLabel
+ label="Gradient"
+ description={propertyDescriptions.backgroundImage}
+ properties={["backgroundImage"]}
+ />
+ <GradientControl index={index} />
+ </>
+ )}
<PropertyInlineLabel
label="Clip"
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx
index d65136c978..a24865591e 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx
@@ -36,7 +36,7 @@ export const BackgroundGradient = ({ index }: { index: number }) => {
const textAreaRef = useRef<HTMLTextAreaElement>(null);
const property = "backgroundImage";
const styleDecl = useComputedStyleDecl(property);
- const styleValue = getRepeatedStyleItem(styleDecl.cascadedValue, index);
+ const styleValue = getRepeatedStyleItem(styleDecl, index);
const [intermediateValue, setIntermediateValue] = useState<
IntermediateValue | InvalidValue | undefined
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx
index ca141a2124..e72a160db0 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx
@@ -39,7 +39,7 @@ export const BackgroundImage = ({ index }: { index: number }) => {
const textAreaRef = useRef<HTMLTextAreaElement>(null);
const assets = useStore($assets);
const styleDecl = useComputedStyleDecl("backgroundImage");
- const styleValue = getRepeatedStyleItem(styleDecl.cascadedValue, index);
+ const styleValue = getRepeatedStyleItem(styleDecl, index);
const [errors, setErrors] = useState<string[]>([]);
const [intermediateValue, setIntermediateValue] = useState<
IntermediateValue | InvalidValue | undefined
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx
index 0a955ae21e..ea654591fd 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-position.tsx
@@ -32,8 +32,8 @@ export const BackgroundPosition = ({ index }: { index: number }) => {
"backgroundPositionX",
"backgroundPositionY",
]);
- const xValue = getRepeatedStyleItem(backgroundPositionX.cascadedValue, index);
- const yValue = getRepeatedStyleItem(backgroundPositionY.cascadedValue, index);
+ const xValue = getRepeatedStyleItem(backgroundPositionX, index);
+ const yValue = getRepeatedStyleItem(backgroundPositionY, index);
const x = calculateBackgroundPosition(xValue);
const y = calculateBackgroundPosition(yValue);
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx
index a179548a38..d2fc185d1c 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx
@@ -39,7 +39,7 @@ const toTuple = (
export const BackgroundSize = ({ index }: { index: number }) => {
const property = "backgroundSize";
const styleDecl = useComputedStyleDecl(property);
- const styleValue = getRepeatedStyleItem(styleDecl.cascadedValue, index);
+ const styleValue = getRepeatedStyleItem(styleDecl, index);
const { items: defaultItems } = styleConfigByName(property);
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-thumbnail.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-thumbnail.tsx
index 70f332cac6..4688d2106a 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-thumbnail.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-thumbnail.tsx
@@ -113,10 +113,7 @@ export const BackgroundThumbnail = ({ index }: { index: number }) => {
const imageLoader = useStore($imageLoader);
const styles = useComputedStyles(repeatedProperties);
const [backgroundImage] = styles;
- const backgroundImageValue = getRepeatedStyleItem(
- backgroundImage.cascadedValue,
- index
- );
+ const backgroundImageValue = getRepeatedStyleItem(backgroundImage, index);
if (
backgroundImageValue?.type === "image" &&
@@ -154,9 +151,9 @@ export const BackgroundThumbnail = ({ index }: { index: number }) => {
if (backgroundImageValue?.type === "unparsed") {
const cssStyle: { [property in RepeatedProperty]?: string } = {};
- for (const { property, cascadedValue } of styles) {
- const value = getRepeatedStyleItem(cascadedValue, index);
- cssStyle[property as RepeatedProperty] = toValue(value);
+ for (const styleDecl of styles) {
+ const itemValue = getRepeatedStyleItem(styleDecl, index);
+ cssStyle[styleDecl.property as RepeatedProperty] = toValue(itemValue);
}
return <Thumbnail css={cssStyle} />;
}
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
index 8b06039d8d..c4dbedd823 100644
--- 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
@@ -19,6 +19,7 @@ import {
} from "~/shared/nano-states";
import { registerContainers } from "~/shared/sync";
import { setProperty } from "./use-style-data";
+import type { ComputedStyleDecl } from "~/shared/style-object-model";
registerContainers();
@@ -30,7 +31,7 @@ beforeEach(() => {
});
test("get repeated style item by index", () => {
- const styleValue: StyleValue = {
+ const cascadedValue: StyleValue = {
type: "layers",
value: [
{ type: "keyword", value: "red" },
@@ -38,28 +39,37 @@ test("get repeated style item by index", () => {
{ type: "keyword", value: "blue" },
],
};
- expect(getRepeatedStyleItem(styleValue, 0)).toEqual({
+ const styleDecl: ComputedStyleDecl = {
+ property: "color",
+ source: {
+ name: "default",
+ },
+ cascadedValue,
+ computedValue: cascadedValue,
+ usedValue: cascadedValue,
+ };
+ expect(getRepeatedStyleItem(styleDecl, 0)).toEqual({
type: "keyword",
value: "red",
});
- expect(getRepeatedStyleItem(styleValue, 1)).toEqual({
+ expect(getRepeatedStyleItem(styleDecl, 1)).toEqual({
type: "keyword",
value: "green",
});
- expect(getRepeatedStyleItem(styleValue, 2)).toEqual({
+ expect(getRepeatedStyleItem(styleDecl, 2)).toEqual({
type: "keyword",
value: "blue",
});
// repeat values
- expect(getRepeatedStyleItem(styleValue, 3)).toEqual({
+ expect(getRepeatedStyleItem(styleDecl, 3)).toEqual({
type: "keyword",
value: "red",
});
- expect(getRepeatedStyleItem(styleValue, 4)).toEqual({
+ expect(getRepeatedStyleItem(styleDecl, 4)).toEqual({
type: "keyword",
value: "green",
});
- expect(getRepeatedStyleItem(styleValue, 5)).toEqual({
+ expect(getRepeatedStyleItem(styleDecl, 5)).toEqual({
type: "keyword",
value: "blue",
});
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 fb0a1be93e..d87e325ca5 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
@@ -1,11 +1,12 @@
import { useMemo } from "react";
import type { RgbaColor } from "colord";
-import type {
- LayersValue,
- StyleProperty,
- StyleValue,
- TupleValue,
- UnparsedValue,
+import {
+ toValue,
+ type LayersValue,
+ type StyleProperty,
+ type StyleValue,
+ type TupleValue,
+ type UnparsedValue,
} from "@webstudio-is/css-engine";
import {
EyeconClosedIcon,
@@ -26,9 +27,28 @@ import { repeatUntil } from "~/shared/array-utils";
import type { ComputedStyleDecl } from "~/shared/style-object-model";
import { createBatchUpdate, type StyleUpdateOptions } from "./use-style-data";
import { ColorThumb } from "./color-thumb";
-import { properties } from "@webstudio-is/css-data";
+import { parseCssValue, properties } from "@webstudio-is/css-data";
+
+/**
+ * shows cascaded value
+ * or resolves css variable and provide reparsed version
+ * so for example gradients in variable are unparsed
+ * but this utility converts them into layers
+ */
+const getComputedValue = (styleDecl: ComputedStyleDecl) => {
+ if (styleDecl.cascadedValue.type === "var") {
+ const property = styleDecl.property as StyleProperty;
+ const serialized = toValue(styleDecl.computedValue);
+ return parseCssValue(property, serialized);
+ }
+ return styleDecl.cascadedValue;
+};
-export const getRepeatedStyleItem = (styleValue: StyleValue, index: number) => {
+export const getRepeatedStyleItem = (
+ styleDecl: ComputedStyleDecl,
+ index: number
+) => {
+ const styleValue = getComputedValue(styleDecl);
if (styleValue.type === "layers" || styleValue.type === "tuple") {
return styleValue.value[index % styleValue.value.length];
}
@@ -209,7 +229,7 @@ export const RepeatedStyle = (props: {
const { label, styles, getItemProps, renderThumbnail, renderItemContent } =
props;
// first property should describe the amount of layers or tuple items
- const primaryValue = styles[0].cascadedValue;
+ const primaryValue = getComputedValue(styles[0]);
const primaryItems =
primaryValue.type === "layers" || primaryValue.type === "tuple"
? primaryValue.value