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: support css variables in transitions (#4317)

Bogdan Chadkin325eff146974
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx
index b7fdcfb572..8956e8c760 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx
@@ -18,15 +18,16 @@ import {
} from "@webstudio-is/design-system";
import { InfoCircleIcon } from "@webstudio-is/icons";
import { properties, propertyDescriptions } from "@webstudio-is/css-data";
-import type { StyleUpdateOptions } from "../../shared/use-style-data";
import { type IntermediateStyleValue } from "../../shared/css-value-input";
import { CssValueInputContainer } from "../../shared/css-value-input";
import { parseCssFragment } from "../../shared/css-fragment";
import { PropertyInlineLabel } from "../../property-label";
import { TransitionProperty } from "./transition-property";
-import { TransitionTiming } from "./transition-timing";
-import { useComputedStyles } from "../../shared/model";
-import { editRepeatedStyleItem } from "../../shared/repeated-style";
+import { $availableVariables, useComputedStyles } from "../../shared/model";
+import {
+ editRepeatedStyleItem,
+ setRepeatedStyleItem,
+} from "../../shared/repeated-style";
const getLayer = (value: undefined | StyleValue, index: number) =>
value?.type === "layers" ? value.value[index] : undefined;
@@ -86,15 +87,12 @@ export const TransitionContent = ({ index }: { index: number }) => {
);
};
- const handlePropertyUpdate = (
- params: {
- property?: StyleValue;
- timing?: StyleValue;
- delay?: StyleValue;
- duration?: StyleValue;
- },
- options: StyleUpdateOptions = { isEphemeral: false }
- ) => {
+ const updateIntermediateValue = (params: {
+ property?: StyleValue;
+ timing?: StyleValue;
+ delay?: StyleValue;
+ duration?: StyleValue;
+ }) => {
const shorthand = toValue({
type: "tuple",
value: [
@@ -108,12 +106,6 @@ export const TransitionContent = ({ index }: { index: number }) => {
type: "intermediate",
value: shorthand,
});
- editRepeatedStyleItem(
- styles,
- index,
- parseCssFragment(shorthand, ["transition"]),
- options
- );
};
return (
@@ -134,7 +126,10 @@ export const TransitionContent = ({ index }: { index: number }) => {
/>
<TransitionProperty
value={property ?? properties.transitionProperty.initial}
- onChange={(property) => handlePropertyUpdate({ property })}
+ onChange={(value) => {
+ updateIntermediateValue({ duration: value });
+ setRepeatedStyleItem(transitionProperty, index, value);
+ }}
/>
<PropertyInlineLabel
@@ -143,23 +138,21 @@ export const TransitionContent = ({ index }: { index: number }) => {
properties={["transitionDuration"]}
/>
<CssValueInputContainer
- key={"transitionDuration"}
- property={"transitionDuration"}
+ property="transitionDuration"
styleSource="local"
+ getOptions={() => $availableVariables.get()}
value={duration ?? properties.transitionDuration.initial}
deleteProperty={() => {}}
setValue={(value, options) => {
if (value === undefined) {
return;
}
-
- if (value.type === "unit") {
- handlePropertyUpdate({ duration: value }, options);
- return;
+ if (value.type === "layers") {
+ [value] = value.value;
}
-
- if (value.type === "layers" && value.value[0].type === "unit") {
- handlePropertyUpdate({ duration: value.value[0] }, options);
+ if (value.type === "unit" || value.type === "var") {
+ updateIntermediateValue({ duration: value });
+ setRepeatedStyleItem(transitionDuration, index, value, options);
}
}}
/>
@@ -170,32 +163,65 @@ export const TransitionContent = ({ index }: { index: number }) => {
properties={["transitionDelay"]}
/>
<CssValueInputContainer
- property={"transitionDelay"}
- key={"transitionDelay"}
+ property="transitionDelay"
styleSource="local"
+ getOptions={() => $availableVariables.get()}
value={delay ?? properties.transitionDelay.initial}
deleteProperty={() => {}}
setValue={(value, options) => {
if (value === undefined) {
return;
}
-
- if (value.type === "unit") {
- handlePropertyUpdate({ delay: value }, options);
- return;
+ if (value.type === "layers") {
+ [value] = value.value;
}
-
- if (value.type === "layers" && value.value[0].type === "unit") {
- handlePropertyUpdate({ delay: value.value[0] }, options);
+ if (value.type === "unit" || value.type === "var") {
+ updateIntermediateValue({ delay: value });
+ setRepeatedStyleItem(transitionDelay, index, value, options);
}
}}
/>
- <TransitionTiming
- timing={timingFunction ?? properties.transitionTimingFunction.initial}
- onTimingSelection={handlePropertyUpdate}
+ <PropertyInlineLabel
+ label="Easing"
+ description={propertyDescriptions.transitionTimingFunction}
+ properties={["transitionTimingFunction"]}
+ />
+ <CssValueInputContainer
+ property="transitionTimingFunction"
+ styleSource="local"
+ getOptions={() => [
+ { type: "keyword", value: "linear" },
+ { type: "keyword", value: "ease" },
+ { type: "keyword", value: "ease-in" },
+ { type: "keyword", value: "ease-out" },
+ { type: "keyword", value: "ease-in-out" },
+ { type: "keyword", value: "step-start" },
+ { type: "keyword", value: "step-end" },
+ ...$availableVariables.get(),
+ ]}
+ value={timingFunction ?? properties.transitionTimingFunction.initial}
+ deleteProperty={() => {}}
+ setValue={(value, options) => {
+ if (value === undefined) {
+ return;
+ }
+ if (value.type === "layers") {
+ [value] = value.value;
+ }
+ if (value.type === "keyword" || value.type === "var") {
+ updateIntermediateValue({ timing: value });
+ setRepeatedStyleItem(
+ transitionTimingFunction,
+ index,
+ value,
+ options
+ );
+ }
+ }}
/>
</Grid>
+
<Separator css={{ gridColumn: "span 2" }} />
<Flex
direction="column"
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx
deleted file mode 100644
index 831bb32d18..0000000000
--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx
+++ /dev/null
@@ -1,130 +0,0 @@
-import { useState, useEffect } from "react";
-import {
- StyleValue,
- toValue,
- type FunctionValue,
- type KeywordValue,
-} from "@webstudio-is/css-engine";
-import { parseCssValue, propertyDescriptions } from "@webstudio-is/css-data";
-import {
- Select,
- SelectGroup,
- SelectLabel,
- SelectItem,
-} from "@webstudio-is/design-system";
-import {
- defaultFunctions,
- easeInFunctions,
- easeOutFunctions,
- easeInOutFunctions,
- timingFunctions,
- type TimingFunctions,
- findTimingFunctionFromValue,
-} from "./transition-utils";
-import { PropertyInlineLabel } from "../../property-label";
-
-type TransitionTimingProps = {
- timing: StyleValue;
- onTimingSelection: (params: { timing: KeywordValue | FunctionValue }) => void;
-};
-
-const options: TimingFunctions[] = [
- ...Object.keys(timingFunctions),
- "custom",
-] as TimingFunctions[] & "custom";
-
-export const TransitionTiming = ({
- timing,
- onTimingSelection,
-}: TransitionTimingProps) => {
- const [value, setValue] = useState<TimingFunctions | "custom">(
- findTimingFunctionFromValue(toValue(timing)) ?? "custom"
- );
-
- useEffect(
- () => setValue(findTimingFunctionFromValue(toValue(timing)) ?? "custom"),
- [timing]
- );
-
- const handleTimingChange = (value: TimingFunctions | "custom") => {
- setValue(value);
-
- if (value === "custom") {
- onTimingSelection({ timing: { type: "keyword", value: "" } });
- return;
- }
-
- const selectedTiming = timingFunctions[value];
- const parsedEasing = parseCssValue(
- "transitionTimingFunction",
- selectedTiming
- );
-
- if (parsedEasing.type === "invalid") {
- return;
- }
- const easingValue =
- parsedEasing.type === "layers" ? parsedEasing.value[0] : parsedEasing;
-
- if (easingValue.type === "keyword" || easingValue.type === "function") {
- onTimingSelection({ timing: easingValue });
- }
- };
-
- return (
- <>
- <PropertyInlineLabel
- label="Easing"
- description={propertyDescriptions.transitionTimingFunction}
- properties={["transitionTimingFunction"]}
- />
- <Select options={options} value={value} onChange={handleTimingChange}>
- <SelectGroup>
- <SelectLabel>Default</SelectLabel>
- {Object.keys(defaultFunctions).map((defaultFunc) => {
- return (
- <SelectItem key={defaultFunc} value={defaultFunc} text="sentence">
- {defaultFunc}
- </SelectItem>
- );
- })}
- {value === "custom" && (
- <SelectItem key="custom" value="custom">
- Custom
- </SelectItem>
- )}
- </SelectGroup>
- <SelectGroup>
- <SelectLabel>Ease In</SelectLabel>
- {Object.keys(easeInFunctions).map((easeIn) => {
- return (
- <SelectItem key={easeIn} value={easeIn} text="sentence">
- {easeIn}
- </SelectItem>
- );
- })}
- </SelectGroup>
- <SelectGroup>
- <SelectLabel>Ease Out</SelectLabel>
- {Object.keys(easeOutFunctions).map((easeOut) => {
- return (
- <SelectItem key={easeOut} value={easeOut} text="sentence">
- {easeOut}
- </SelectItem>
- );
- })}
- </SelectGroup>
- <SelectGroup>
- <SelectLabel>Ease In Out</SelectLabel>
- {Object.keys(easeInOutFunctions).map((easeInOut) => {
- return (
- <SelectItem key={easeInOut} value={easeInOut} text="sentence">
- {easeInOut}
- </SelectItem>
- );
- })}
- </SelectGroup>
- </Select>
- </>
- );
-};
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts
deleted file mode 100644
index b7ca0c9e89..0000000000
--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts
+++ /dev/null
@@ -1,71 +0,0 @@
-import { transitionLongHandProperties } from "@webstudio-is/css-data";
-
-export type TransitionProperty = (typeof transitionLongHandProperties)[number];
-
-export const defaultFunctions = {
- linear: "linear",
- ease: "ease",
- "ease in": "ease-in",
- "ease out": "ease-out",
- "ease-in-out": "ease-in-out",
-} as const;
-
-export const easeInFunctions = {
- "ease-in-sine": "cubic-bezier(0.12,0,0.39,0)",
- "ease-in-quad": "cubic-bezier(0.11,0,0.5,0)",
- "ease-in-cubic": "cubic-bezier(0.32,0,0.67,0)",
- "ease-in-quart": "cubic-bezier(0.5,0,0.75,0)",
- "ease-in-quint": "cubic-bezier(0.64,0,0.78,0)",
- "ease-in-expo": "cubic-bezier(0.7,0,0.84,0)",
- "ease-in-circ": "cubic-bezier(0.55,0,1,0.45)",
- "ease-in-back": "cubic-bezier(0.36,0,0.66,-0.56)",
-} as const;
-
-export const easeOutFunctions = {
- "ease-out-sine": "cubic-bezier(0.61,1,0.88,1)",
- "ease-out-quad": "cubic-bezier(0.5,1,0.89,1)",
- "ease-out-cubic": "cubic-bezier(0.33,1,0.68,1)",
- "ease-out-quart": "cubic-bezier(0.25,1,0.5,1)",
- "ease-out-quint": "cubic-bezier(0.22,1,0.36,1)",
- "ease-out-expo": "cubic-bezier(0.16,1,0.3,1)",
- "ease-out-circ": "cubic-bezier(0,0.55,0.45,1)",
- "ease-out-back": "cubic-bezier(0.34,1.56,0.64,1)",
-} as const;
-
-export const easeInOutFunctions = {
- "ease-in-out-sine": "cubic-bezier(0.37,0,0.63,1)",
- "ease-in-out-quad": "cubic-bezier(0.45,0,0.55,1)",
- "ease-in-out-cubic": "cubic-bezier(0.65,0,0.35,1)",
- "ease-in-out-quart": "cubic-bezier(0.76,0,0.24,1)",
- "ease-in-out-quint": "cubic-bezier(0.83,0,0.17,1)",
- "ease-in-out-expo": "cubic-bezier(0.87,0,0.13,1)",
- "ease-in-out-circ": "cubic-bezier(0.85,0,0.15,1)",
- "ease-in-out-back": "cubic-bezier(0.68,-0.6,0.32,1.6)",
-} as const;
-
-export const timingFunctions = {
- ...defaultFunctions,
- ...easeInFunctions,
- ...easeOutFunctions,
- ...easeInOutFunctions,
-} as const;
-
-export type DefaultFunction = keyof typeof defaultFunctions;
-export type EaseInFunction = keyof typeof easeInFunctions;
-export type EaseOutFunction = keyof typeof easeOutFunctions;
-export type EaseInOutFunction = keyof typeof easeInOutFunctions;
-export type TimingFunctions =
- | DefaultFunction
- | EaseInFunction
- | EaseOutFunction
- | EaseInOutFunction;
-
-export const findTimingFunctionFromValue = (
- timingFunction: string
-): TimingFunctions | undefined => {
- const cleanedFuncValue = timingFunction.replace(/\s/g, "");
-
- return (Object.keys(timingFunctions) as TimingFunctions[]).find(
- (key: TimingFunctions) => timingFunctions[key] === cleanedFuncValue
- );
-};
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsx
index 6f13b8e512..7e0ebf824c 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
@@ -23,10 +23,6 @@ import {
} from "../../shared/repeated-style";
import { PropertySectionLabel } from "../../property-label";
import { useComputedStyles } from "../../shared/model";
-import {
- findTimingFunctionFromValue,
- type TransitionProperty,
-} from "./transition-utils";
import { TransitionContent } from "./transition-content";
import { parseCssFragment } from "../../shared/css-fragment";
@@ -36,7 +32,7 @@ const label = "Transitions";
const getTransitionLayers = (
styles: ComputedStyleDecl[],
- property: TransitionProperty
+ property: (typeof transitionLongHandProperties)[number]
) => {
const transitionPropertyValue = styles[0].cascadedValue;
const currentPropertyValue = styles.find(
@@ -73,11 +69,8 @@ const getLayerLabel = ({
"transitionTimingFunction"
)[index];
const timingFunction = toValue({ ...timingFunctionLayer, hidden: false });
- const humanizedTimingFunction =
- findTimingFunctionFromValue(timingFunction) ?? timingFunction;
const delay = toValue(getTransitionLayers(styles, "transitionDelay")[index]);
-
- return `${property}: ${duration} ${humanizedTimingFunction} ${delay}`;
+ return `${property}: ${duration} ${timingFunction} ${delay}`;
};
export const Section = () => {
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
index 84115dae24..8dafaf9fb1 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
@@ -609,11 +609,13 @@ export const CssValueInput = ({
const valueForDescription =
highlightedValue?.type === "keyword"
? highlightedValue
- : props.value?.type === "keyword"
- ? props.value
- : items[0]?.type === "keyword"
- ? items[0]
- : undefined;
+ : highlightedValue?.type === "var"
+ ? undefined
+ : props.value?.type === "keyword"
+ ? props.value
+ : items[0]?.type === "keyword"
+ ? items[0]
+ : undefined;
if (valueForDescription) {
const key = `${property}:${toValue(
diff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsx
index 4af2f52e9e..ae5b3aff3e 100644
--- a/packages/design-system/src/components/combobox.tsx
+++ b/packages/design-system/src/components/combobox.tsx
@@ -313,16 +313,11 @@ export const useCombobox = <Item,>({
if (isOpen) {
itemsCache.current = getItems();
- const matchedItems = match(
- inputValue ?? "",
- itemsCache.current,
- itemToString
- );
// Don't set isOpen to true if there are no items to show
// because otherwise first ESC press will try to close it and only next ESC
// will reset the value. When list is empty, first ESC should reset the value.
- setMatchedItems(matchedItems);
- setIsOpen(matchedItems.length > 0);
+ setMatchedItems(itemsCache.current);
+ setIsOpen(itemsCache.current.length > 0);
} else {
setMatchedItems([]);
setIsOpen(false);