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.

refactor: drop parseTransition shorthand and add migration for transition shorthand (#3607)

Jaya Krishna Namburu931e57108165
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 ccd6333133..9224536d5f 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
@@ -4,8 +4,8 @@ import {
type InvalidValue,
type LayersValue,
type TupleValue,
- KeywordValue,
- UnitValue,
+ type KeywordValue,
+ type UnitValue,
type StyleProperty,
} from "@webstudio-is/css-engine";
import {
@@ -21,7 +21,7 @@ import {
} from "@webstudio-is/design-system";
import {
extractTransitionProperties,
- parseTransition,
+ parseCssValue,
type ExtractedTransitionProperties,
} from "@webstudio-is/css-data";
import type {
@@ -39,6 +39,7 @@ import {
defaultTransitionTimingFunction,
defaultTransitionDelay,
deleteTransitionLayer,
+ parseTransitionShorthandToLayers,
} from "./transition-utils";
import type { StyleInfo } from "../../shared/style-info";
@@ -58,6 +59,11 @@ type TransitionContentProps = {
createBatchUpdate: CreateBatchUpdate;
};
+// We are allowing users to add/edit layers as shorthand from the style-panel
+// So, we need to use the shorthand property to validate the layer too.
+// We removed transition from properties list to drop support from advanced tab and so the typecasting.
+const shortHandTransitionProperty = "transition" as StyleProperty;
+
export const TransitionContent = ({
layer,
index,
@@ -89,8 +95,11 @@ export const TransitionContent = ({
return;
}
- const layers = parseTransition(intermediateValue.value);
- if (layers.type === "invalid") {
+ const layerValue = parseCssValue(
+ shortHandTransitionProperty,
+ intermediateValue.value
+ );
+ if (layerValue.type === "invalid") {
setIntermediateValue({
type: "invalid",
value: intermediateValue.value,
@@ -98,6 +107,8 @@ export const TransitionContent = ({
return;
}
+ const layers = parseTransitionShorthandToLayers(intermediateValue.value);
+
onEditLayer(index, layers, options);
};
@@ -111,21 +122,26 @@ export const TransitionContent = ({
}).filter<UnitValue | KeywordValue>(
(item): item is UnitValue | KeywordValue => item != null
);
- const newLayer: TupleValue = { type: "tuple", value };
- const layers = parseTransition(toValue(newLayer));
- if (layers.type === "invalid") {
+ const layerTuple: TupleValue = { type: "tuple", value };
+ const layerValue = parseCssValue(
+ shortHandTransitionProperty,
+ toValue(layerTuple)
+ );
+
+ if (layerValue.type === "invalid") {
setIntermediateValue({
type: "invalid",
- value: toValue(newLayer),
+ value: toValue(layerTuple),
});
return;
}
setIntermediateValue({
type: "intermediate",
- value: toValue(newLayer),
+ value: toValue(layerTuple),
});
- onEditLayer(index, layers, options);
+
+ onEditLayer(index, { type: "layers", value: [layerTuple] }, options);
};
return (
@@ -173,10 +189,15 @@ export const TransitionContent = ({
handlePropertyUpdate({ duration });
}}
setValue={(value, options) => {
- if (value === undefined || value.type !== "unit") {
+ if (
+ value === undefined ||
+ value.type !== "layers" ||
+ value.value[0].type !== "unit"
+ ) {
return;
}
- handlePropertyUpdate({ duration: value }, options);
+
+ handlePropertyUpdate({ duration: value.value[0] }, options);
}}
/>
@@ -206,10 +227,15 @@ export const TransitionContent = ({
keywords={[]}
deleteProperty={() => handlePropertyUpdate({ delay })}
setValue={(value, options) => {
- if (value === undefined || value.type !== "unit") {
+ if (
+ value === undefined ||
+ value.type !== "layers" ||
+ value.value[0].type !== "unit"
+ ) {
return;
}
- handlePropertyUpdate({ delay: value }, options);
+
+ handlePropertyUpdate({ delay: value.value[0] }, options);
}}
/>
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
index 7a10df0d18..d832b00ab2 100644
--- 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
@@ -1,5 +1,9 @@
import { useState, useEffect } from "react";
-import type { KeywordValue } from "@webstudio-is/css-engine";
+import {
+ toValue,
+ type FunctionValue,
+ type KeywordValue,
+} from "@webstudio-is/css-engine";
import {
Label,
Tooltip,
@@ -19,10 +23,11 @@ import {
type TimingFunctions,
findTimingFunctionFromValue,
} from "./transition-utils";
+import { parseTransitionLonghandProperty } from "@webstudio-is/css-data";
type TransitionTimingProps = {
- timing: KeywordValue;
- onTimingSelection: (params: { timing: KeywordValue }) => void;
+ timing: KeywordValue | FunctionValue;
+ onTimingSelection: (params: { timing: KeywordValue | FunctionValue }) => void;
};
const options: TimingFunctions[] = [
@@ -35,14 +40,43 @@ export const TransitionTiming = ({
onTimingSelection,
}: TransitionTimingProps) => {
const [value, setValue] = useState<TimingFunctions | "custom">(
- findTimingFunctionFromValue(timing.value) ?? "custom"
+ findTimingFunctionFromValue(toValue(timing)) ?? "custom"
);
useEffect(
- () => setValue(findTimingFunctionFromValue(timing.value) ?? "custom"),
- [timing.value]
+ () => 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 longhandValue = parseTransitionLonghandProperty(
+ "transitionTimingFunction",
+ selectedTiming
+ );
+
+ if (longhandValue.type === "invalid") {
+ return;
+ }
+
+ const timingValue = longhandValue.value[0];
+ const isValidTransitionTimingFunc =
+ timingValue.type === "keyword" || timingValue.type === "function";
+
+ if (isValidTransitionTimingFunc === true) {
+ onTimingSelection({
+ timing: timingValue,
+ });
+ }
+ };
+
return (
<>
<Flex align="center">
@@ -64,22 +98,7 @@ export const TransitionTiming = ({
<Label css={{ display: "inline" }}>Easing</Label>
</Tooltip>
</Flex>
- <Select
- options={options}
- value={value}
- onChange={(value) => {
- setValue(value);
-
- if (value === "custom") {
- onTimingSelection({ timing: { type: "keyword", value: "" } });
- return;
- }
-
- onTimingSelection({
- timing: { type: "keyword", value: timingFunctions[value] },
- });
- }}
- >
+ <Select options={options} value={value} onChange={handleTimingChange}>
<SelectGroup>
<SelectLabel>Default</SelectLabel>
{Object.keys(defaultFunctions).map((defaultFunc) => {
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.test.ts b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.test.ts
index ad0be47f8c..93dbef64f7 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.test.ts
+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.test.ts
@@ -92,7 +92,7 @@ describe("transition-utils", () => {
const layers = convertIndividualTransitionToLayers(properties);
expect(published).toBe(true);
- expect(toValue(layers)).toMatchInlineSnapshot(`"opacity 200ms ease 0s"`);
+ expect(toValue(layers)).toMatchInlineSnapshot(`"opacity 0ms ease 0ms"`);
});
test("update individual transition properties", () => {
@@ -134,7 +134,7 @@ describe("transition-utils", () => {
`"width 0ms ease 0ms"`
);
expect(toValue(layers)).toMatchInlineSnapshot(
- `"opacity 200ms ease 0s, width 0ms ease 0ms, opacity 200ms ease 0s"`
+ `"opacity 0ms ease 0ms, width 0ms ease 0ms, opacity 0ms ease 0ms"`
);
});
@@ -185,7 +185,7 @@ describe("transition-utils", () => {
expect(published).toBe(true);
expect(toValue(layers)).toMatchInlineSnapshot(
- `"opacity 200ms ease 0s, width 0ms ease-in-out 0ms"`
+ `"opacity 0ms ease 0ms, width 0ms ease-in-out 0ms"`
);
});
@@ -212,7 +212,7 @@ describe("transition-utils", () => {
expect(published).toBe(true);
expect(layers.value.length).toBe(2);
- expect(toValue(layers)).toMatchInlineSnapshot(`"opacity 200ms ease 0s"`);
+ expect(toValue(layers)).toMatchInlineSnapshot(`"opacity 0ms ease 0ms"`);
});
test("swap layer positions in transitions", () => {
@@ -250,7 +250,7 @@ describe("transition-utils", () => {
expect(published).toBe(true);
expect(layers.value.length).toBe(2);
expect(toValue(layers)).toMatchInlineSnapshot(
- `"width 0ms ease-in-out 0ms, opacity 200ms ease 0s"`
+ `"width 0ms ease-in-out 0ms, opacity 0ms ease 0ms"`
);
});
});
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
index 233f3dea97..5cdfc3081e 100644
--- 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
@@ -1,6 +1,8 @@
import {
+ FunctionValue,
type KeywordValue,
type LayersValue,
+ type StyleProperty,
type TupleValue,
type UnitValue,
} from "@webstudio-is/css-engine";
@@ -10,13 +12,15 @@ import type {
StyleUpdateOptions,
} from "../../shared/use-style-data";
import {
+ expandShorthands,
extractTransitionProperties,
+ isTransitionLongHandProperty,
isValidTransitionValue,
- parseTransition,
+ parseCssValue,
transitionLongHandProperties,
} from "@webstudio-is/css-data";
+import { camelCase } from "change-case";
-export const initialTransition = "opacity 200ms ease 0s";
export const defaultTransitionProperty: KeywordValue = {
type: "keyword",
value: "opacity",
@@ -97,8 +101,10 @@ export type TimingFunctions =
export const findTimingFunctionFromValue = (
timingFunction: string
): TimingFunctions | undefined => {
+ const cleanedFuncValue = timingFunction.replace(/\s/g, "");
+
return (Object.keys(timingFunctions) as TimingFunctions[]).find(
- (key: TimingFunctions) => timingFunctions[key] === timingFunction
+ (key: TimingFunctions) => timingFunctions[key] === cleanedFuncValue
);
};
@@ -198,9 +204,15 @@ export const addDefaultTransitionLayer = (props: {
}) => {
const { createBatchUpdate, currentStyle } = props;
const properties = getTransitionProperties(currentStyle);
- const { timing, property, delay, duration } = extractTransitionProperties(
- parseTransition(initialTransition).value[0] as TupleValue
- );
+ const { timing, property, delay, duration } = extractTransitionProperties({
+ type: "tuple",
+ value: [
+ defaultTransitionProperty,
+ defaultTransitionDuration,
+ defaultTransitionTimingFunction,
+ defaultTransitionDelay,
+ ],
+ });
const batch = createBatchUpdate();
if (property) {
@@ -269,18 +281,10 @@ export const editTransitionLayer = (props: {
const { index, layers, createBatchUpdate, options, currentStyle } = props;
const batch = createBatchUpdate();
- const properties = getTransitionProperties(currentStyle);
- const {
- transitionProperty,
- transitionDelay,
- transitionDuration,
- transitionTimingFunction,
- } = properties;
-
const newTransitionProperties: KeywordValue[] = [];
const newTransitionDurations: UnitValue[] = [];
const newTransitionDelays: UnitValue[] = [];
- const newTransitionTimingFunctions: KeywordValue[] = [];
+ const newTransitionTimingFunctions: Array<KeywordValue | FunctionValue> = [];
for (const layer of layers.value) {
if (layer.type !== "tuple") {
@@ -305,6 +309,14 @@ export const editTransitionLayer = (props: {
newTransitionTimingFunctions.push(timing);
}
+ const existingTransitionProperties = getTransitionProperties(currentStyle);
+ const {
+ transitionProperty,
+ transitionDelay,
+ transitionDuration,
+ transitionTimingFunction,
+ } = existingTransitionProperties;
+
const newProperty = [...transitionProperty.value];
newProperty.splice(index, 1, ...newTransitionProperties);
batch.setProperty("transitionProperty")({
@@ -349,6 +361,14 @@ export const swapTransitionLayers = (props: {
for (const property of transitionLongHandProperties) {
const layer = properties[property];
const layervalue = [...layer.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 (layervalue.length < 2) {
+ continue;
+ }
+
layervalue.splice(oldIndex, 1);
layervalue.splice(newIndex, 0, layer.value[oldIndex]);
batch.setProperty(property)({
@@ -387,3 +407,28 @@ export const hideTransitionLayer = (props: {
batch.publish();
};
+
+export const parseTransitionShorthandToLayers = (
+ transition: string
+): LayersValue => {
+ const longhands = expandShorthands([["transition", transition]]);
+
+ const layerTuple: TupleValue = { type: "tuple", value: [] };
+ for (const [hyphenedProperty, value] of longhands) {
+ const longhandProperty = camelCase(hyphenedProperty) as StyleProperty;
+ const longhandValue = parseCssValue(longhandProperty, value);
+
+ if (
+ longhandValue.type === "layers" &&
+ isTransitionLongHandProperty(longhandProperty) &&
+ isValidTransitionValue(longhandValue.value[0])
+ ) {
+ layerTuple.value.push(longhandValue.value[0]);
+ }
+ }
+
+ return {
+ type: "layers",
+ value: [layerTuple],
+ };
+};
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 32957f0148..345dded6dc 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
@@ -18,7 +18,6 @@ import { LayersList } from "../../style-layers-list";
import { $selectedOrLastStyleSourceSelector } from "~/shared/nano-states";
import { TransitionContent } from "./transition-content";
import {
- initialTransition,
getTransitionProperties,
deleteTransitionProperties,
addDefaultTransitionLayer,
@@ -31,6 +30,7 @@ import {
import type { StyleProperty } from "@webstudio-is/css-engine";
const label = "Transitions";
+const initialTransition = "opacity 200ms ease 0s";
export const properties =
transitionLongHandProperties as unknown as Array<StyleProperty>;
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
index d3224abb1b..cd772a6251 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
@@ -89,6 +89,12 @@ const extractPropertiesFromLayer = (layer: TupleValue | FunctionValue) => {
}
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 };
diff --git a/apps/builder/app/shared/webstudio-data-migrator.ts b/apps/builder/app/shared/webstudio-data-migrator.ts
index 087d2f92af..a9c34c000e 100644
--- a/apps/builder/app/shared/webstudio-data-migrator.ts
+++ b/apps/builder/app/shared/webstudio-data-migrator.ts
@@ -23,8 +23,9 @@ export const migrateWebstudioDataMutable = (data: WebstudioData) => {
for (const [styleDeclKey, styleDecl] of data.styles) {
const property = styleDecl.property as string;
- // expand overflow shorthand into overflow-x and overflow-y longhands
- if (property === "overflow") {
+ // expands overflow shorthand into overflow-x and overflow-y longhands
+ // expands transition shorthand into transition-property, transition-duration, transition-timing-function, transition-delay longhands
+ if (property === "overflow" || property === "transition") {
data.styles.delete(styleDeclKey);
const longhands = expandShorthands([
[property, toValue(styleDecl.value)],
diff --git a/packages/css-data/bin/mdn-data.ts b/packages/css-data/bin/mdn-data.ts
index a41d219795..43298ea126 100755
--- a/packages/css-data/bin/mdn-data.ts
+++ b/packages/css-data/bin/mdn-data.ts
@@ -255,36 +255,9 @@ const filteredProperties: FilteredProperties = (() => {
let property: Property;
const result = {} as FilteredProperties;
- /*
- A transition is a shorthand property that represents the combination of the other four properties.
- Typically, we exclude shorthand properties when using the expanded ones.
- However, in this case, the transition property in the designs allows users to set all transition values at once.
- Therefore, we need to make this property available from the generated list.
-
- The initial properties for transition is
- config.initial = [
- 'transition-delay',
- 'transition-duration',
- 'transition-property',
- 'transition-timing-function'
- ]
-
- We replace it with the defaults of the rest of the four properties
- "all 0s ease 0s"
- */
-
- const supportedComplexProperties: Record<string, string> = {
- // @todo remove support for transition shorthand
- transition: "all 0s ease 0s",
- };
-
for (property in properties) {
const config = properties[property];
- if (property in supportedComplexProperties) {
- config.initial = supportedComplexProperties[property];
- }
-
const isSupportedProperty =
// make sure the property standard and described in mdn
(config.status === "standard" && "mdn_url" in config) ||
diff --git a/packages/css-data/src/__generated__/keyword-values.ts b/packages/css-data/src/__generated__/keyword-values.ts
index bd4ac59115..5b16a44a24 100644
--- a/packages/css-data/src/__generated__/keyword-values.ts
+++ b/packages/css-data/src/__generated__/keyword-values.ts
@@ -4883,22 +4883,6 @@ export const keywordValues = {
"unset",
],
transformStyle: ["flat", "preserve-3d", "initial", "inherit", "unset"],
- transition: [
- "none",
- "all",
- "linear",
- "ease",
- "ease-in",
- "ease-out",
- "ease-in-out",
- "step-start",
- "step-end",
- "normal",
- "allow-discrete",
- "initial",
- "inherit",
- "unset",
- ],
transitionDelay: ["initial", "inherit", "unset"],
transitionDuration: ["initial", "inherit", "unset"],
transitionProperty: ["none", "all", "initial", "inherit", "unset"],
diff --git a/packages/css-data/src/__generated__/properties.ts b/packages/css-data/src/__generated__/properties.ts
index 1c90860aec..c8c936ed73 100644
--- a/packages/css-data/src/__generated__/properties.ts
+++ b/packages/css-data/src/__generated__/properties.ts
@@ -2889,34 +2889,6 @@ export const properties = {
},
types: [],
},
- transition: {
- unitGroups: ["time"],
- inherited: false,
- initial: {
- type: "tuple",
- value: [
- {
- type: "keyword",
- value: "all",
- },
- {
- type: "unit",
- unit: "s",
- value: 0,
- },
- {
- type: "keyword",
- value: "ease",
- },
- {
- type: "unit",
- unit: "s",
- value: 0,
- },
- ],
- },
- types: ["custom-ident", "time"],
- },
transitionDelay: {
unitGroups: ["time"],
inherited: false,
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index 5eedb7aaa5..08e26609ab 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -14,8 +14,7 @@ import {
isTransitionLongHandProperty,
parseFilter,
parseShadow,
- parseTransition,
- parseTransitionLonghands,
+ parseTransitionLonghandProperty,
} from "./property-parsers";
import { camelCase } from "change-case";
@@ -94,12 +93,8 @@ export const parseCssValue = (
return parseShadow(property, input);
}
- if (property === "transition") {
- return parseTransition(input);
- }
-
if (isTransitionLongHandProperty(property)) {
- return parseTransitionLonghands(property, input);
+ return parseTransitionLonghandProperty(property, input);
}
if (
diff --git a/packages/css-data/src/property-parsers/transition-property-extractor.test.ts b/packages/css-data/src/property-parsers/transition-property-extractor.test.ts
index f99e591871..361dfcee07 100644
--- a/packages/css-data/src/property-parsers/transition-property-extractor.test.ts
+++ b/packages/css-data/src/property-parsers/transition-property-extractor.test.ts
@@ -81,11 +81,11 @@ describe("extractTransitionProperties", () => {
property: { type: "keyword", value: "opacity" },
duration: { type: "unit", value: 200, unit: "ms" },
timing: { type: "keyword", value: "ease" },
- delay: null,
+ delay: undefined,
});
expect(result).toMatchInlineSnapshot(`
{
- "delay": null,
+ "delay": undefined,
"duration": {
"type": "unit",
"unit": "ms",
@@ -127,7 +127,7 @@ describe("extractTransitionProperties", () => {
expect(result).toEqual({
property: { type: "keyword", value: "opacity" },
duration: { type: "unit", value: 200, unit: "ms" },
- timing: null,
+ timing: undefined,
delay: { type: "unit", value: 10, unit: "ms" },
});
expect(result).toMatchInlineSnapshot(`
@@ -146,7 +146,7 @@ describe("extractTransitionProperties", () => {
"type": "keyword",
"value": "opacity",
},
- "timing": null,
+ "timing": undefined,
}
`);
});
@@ -170,12 +170,12 @@ describe("extractTransitionProperties", () => {
expect(result).toEqual({
property: { type: "keyword", value: "opacity" },
duration: { type: "unit", value: 200, unit: "ms" },
- timing: null,
- delay: null,
+ timing: undefined,
+ delay: undefined,
});
expect(result).toMatchInlineSnapshot(`
{
- "delay": null,
+ "delay": undefined,
"duration": {
"type": "unit",
"unit": "ms",
@@ -185,7 +185,7 @@ describe("extractTransitionProperties", () => {
"type": "keyword",
"value": "opacity",
},
- "timing": null,
+ "timing": undefined,
}
`);
});
@@ -206,10 +206,10 @@ describe("extractTransitionProperties", () => {
],
});
expect(result).toEqual({
- property: null,
+ property: undefined,
duration: { type: "unit", value: 10, unit: "ms" },
timing: { type: "keyword", value: "ease" },
- delay: null,
+ delay: undefined,
});
});
@@ -230,9 +230,9 @@ describe("extractTransitionProperties", () => {
expect(result).toEqual({
property: { type: "keyword", value: "opacity" },
- duration: null,
+ duration: undefined,
timing: { type: "keyword", value: "ease" },
- delay: null,
+ delay: undefined,
});
expect(result).toMatchInlineSnapshot;
});
@@ -261,11 +261,11 @@ describe("extractTransitionProperties", () => {
property: { type: "keyword", value: "opacity" },
duration: { type: "unit", value: 50, unit: "ms" },
timing: { type: "keyword", value: "ease" },
- delay: null,
+ delay: undefined,
});
expect(result).toMatchInlineSnapshot(`
{
- "delay": null,
+ "delay": undefined,
"duration": {
"type": "unit",
"unit": "ms",
@@ -307,11 +307,11 @@ describe("extractTransitionProperties", () => {
property: { type: "keyword", value: "transform" },
duration: { type: "unit", value: 0.6, unit: "s" },
timing: { type: "keyword", value: "cubic-bezier(0.36, 0, 0.66, -0.56)" },
- delay: null,
+ delay: undefined,
});
expect(result).toMatchInlineSnapshot(`
{
- "delay": null,
+ "delay": undefined,
"duration": {
"type": "unit",
"unit": "s",
@@ -329,7 +329,7 @@ describe("extractTransitionProperties", () => {
`);
});
- test("parses and extracts values from custom-property ease", () => {
+ test("parses and throws error for non-animatable 'custom-property ease'", () => {
const result = extractTransitionProperties({
type: "tuple",
value: [
@@ -345,20 +345,17 @@ describe("extractTransitionProperties", () => {
});
expect(result).toEqual({
- property: { type: "keyword", value: "custom-property" },
- duration: null,
+ property: undefined,
+ duration: undefined,
timing: { type: "keyword", value: "ease" },
- delay: null,
+ delay: undefined,
});
expect(result).toMatchInlineSnapshot(`
{
- "delay": null,
- "duration": null,
- "property": {
- "type": "keyword",
- "value": "custom-property",
- },
+ "delay": undefined,
+ "duration": undefined,
+ "property": undefined,
"timing": {
"type": "keyword",
"value": "ease",
@@ -379,17 +376,17 @@ describe("extractTransitionProperties", () => {
});
expect(result).toEqual({
- property: null,
- duration: null,
+ property: undefined,
+ duration: undefined,
timing: { type: "keyword", value: "ease" },
- delay: null,
+ delay: undefined,
});
expect(result).toMatchInlineSnapshot(`
{
- "delay": null,
- "duration": null,
- "property": null,
+ "delay": undefined,
+ "duration": undefined,
+ "property": undefined,
"timing": {
"type": "keyword",
"value": "ease",
diff --git a/packages/css-data/src/property-parsers/transition-property-extractor.ts b/packages/css-data/src/property-parsers/transition-property-extractor.ts
index ffbbba8f6e..a2a5287d9d 100644
--- a/packages/css-data/src/property-parsers/transition-property-extractor.ts
+++ b/packages/css-data/src/property-parsers/transition-property-extractor.ts
@@ -1,18 +1,21 @@
-import type {
- KeywordValue,
- TupleValue,
- UnitValue,
+import {
+ FunctionValue,
+ toValue,
+ type KeywordValue,
+ type TupleValue,
+ type UnitValue,
} from "@webstudio-is/css-engine";
+import { isAnimatableProperty } from "..";
export const isTimingFunction = (timing: string) => {
const regex =
- /^(ease(-in-out|-in|-out)?|linear|cubic-bezier\((-?\d+(\.\d+)?, ?){3}-?\d+(\.\d+)?\)|steps\(\d+(-?(start|end))?\))$/gm;
+ /^(ease(-in-out|-in|-out)?|linear|cubic-bezier\((-?\d+(\.\d+)?(, ?)?){3}-?\d+(\.\d+)?\)|steps\(\d+(,(start|end|jump-start|jump-end|jump-none|jump-both))?\))$/gm;
return regex.test(timing);
};
export type ExtractedTransitionProperties = {
property?: KeywordValue;
- timing?: KeywordValue;
+ timing?: KeywordValue | FunctionValue;
delay?: UnitValue;
duration?: UnitValue;
};
@@ -21,26 +24,29 @@ export const extractTransitionProperties = (
transition: TupleValue
): ExtractedTransitionProperties => {
let property: KeywordValue | undefined;
- let timing: KeywordValue | undefined;
+ let timing: KeywordValue | FunctionValue | undefined;
- const keywordValues: KeywordValue[] = [];
const unitValues: UnitValue[] = [];
- for (const property of transition.value) {
- if (property.type === "keyword") {
- keywordValues.push(property);
+ for (const item of transition.value) {
+ if (
+ item.type === "keyword" &&
+ isAnimatableProperty(toValue(item)) === true
+ ) {
+ property = item;
}
- if (property.type === "unit") {
- unitValues.push(property);
+ if (item.type === "keyword" && isAnimatableProperty(item.value) === false) {
+ timing = item;
+ }
+
+ if (item.type === "function") {
+ timing = item;
}
- }
- if (keywordValues.length && isTimingFunction(keywordValues[0].value)) {
- timing = keywordValues[0];
- } else {
- property = keywordValues[0];
- timing = keywordValues[1] ?? undefined;
+ if (item.type === "unit") {
+ unitValues.push(item);
+ }
}
const duration: UnitValue | undefined = unitValues[0] ?? undefined;
diff --git a/packages/css-data/src/property-parsers/transition.test.ts b/packages/css-data/src/property-parsers/transition.test.ts
index aefdba6a43..06633c5d0a 100644
--- a/packages/css-data/src/property-parsers/transition.test.ts
+++ b/packages/css-data/src/property-parsers/transition.test.ts
@@ -1,103 +1,151 @@
import { expect, test } from "@jest/globals";
-import { parseTransition } from "./transition";
+import { parseTransitionLonghandProperty } from "./transition";
+import { toValue } from "@webstudio-is/css-engine";
-test("parses value and returns invalid when used a invalid color", () => {
- expect(parseTransition(`10px 10px 5px foo`)).toMatchInlineSnapshot(`
- {
- "type": "invalid",
- "value": "10px 10px 5px foo",
- }
- `);
-});
-
-test("parse value and returns multiple valid layers", () => {
- expect(parseTransition("color .2s, text-shadow .2s")).toMatchInlineSnapshot(`
+test("parses a valid transitionDuration longhand property", () => {
+ expect(parseTransitionLonghandProperty("transitionDuration", `10ms, 10ms`))
+ .toMatchInlineSnapshot(`
{
"type": "layers",
"value": [
{
- "type": "tuple",
- "value": [
- {
- "type": "keyword",
- "value": "color",
- },
- {
- "type": "unit",
- "unit": "s",
- "value": 0.2,
- },
- ],
+ "type": "unit",
+ "unit": "ms",
+ "value": 10,
},
{
- "type": "tuple",
- "value": [
- {
- "type": "keyword",
- "value": "text-shadow",
- },
- {
- "type": "unit",
- "unit": "s",
- "value": 0.2,
- },
- ],
+ "type": "unit",
+ "unit": "ms",
+ "value": 10,
},
],
}
`);
});
-test("parse value and returns valid layer", () => {
- expect(parseTransition("opacity 200ms ease 0s")).toMatchInlineSnapshot(`
+test("parses an in-valid transitionDuration longhand property", () => {
+ expect(parseTransitionLonghandProperty("transitionDuration", `10ms, foo`))
+ .toMatchInlineSnapshot(`
+{
+ "type": "invalid",
+ "value": "10ms, foo",
+}
+`);
+});
+
+test("parses a vaild transitionProeprty longhand property", () => {
+ expect(
+ parseTransitionLonghandProperty("transitionProperty", "opacity, width, all")
+ ).toMatchInlineSnapshot(`
{
"type": "layers",
"value": [
{
- "type": "tuple",
- "value": [
- {
- "type": "keyword",
- "value": "opacity",
- },
- {
- "type": "unit",
- "unit": "ms",
- "value": 200,
- },
- {
- "type": "keyword",
- "value": "ease",
- },
- {
- "type": "unit",
- "unit": "s",
- "value": 0,
- },
- ],
+ "type": "keyword",
+ "value": "opacity",
+ },
+ {
+ "type": "keyword",
+ "value": "width",
+ },
+ {
+ "type": "keyword",
+ "value": "all",
},
],
}
`);
});
-test("throws error if any custom transition proeprty is used", () => {
- const transition = parseTransition("all 200ms ease 0s, --foo 200ms ease 0s");
- expect(transition).toMatchInlineSnapshot(`
+test("parses only valid transitionProperty longhand property", () => {
+ expect(
+ parseTransitionLonghandProperty("transitionProperty", "opacity, width, foo")
+ ).toMatchInlineSnapshot(`
{
"type": "invalid",
- "value": "all 200ms ease 0s, --foo 200ms ease 0s",
+ "value": "opacity, width, foo",
+}
+`);
+});
+
+test("parses a vaild transitionTimingFunction longhand property", () => {
+ const parsedValue = parseTransitionLonghandProperty(
+ "transitionTimingFunction",
+ "ease, ease-in, cubic-bezier(0.68,-0.6,0.32,1.6), steps(4, jump-start)"
+ );
+ expect(parsedValue).toMatchInlineSnapshot(`
+{
+ "type": "layers",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "ease",
+ },
+ {
+ "type": "keyword",
+ "value": "ease-in",
+ },
+ {
+ "args": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "0.68",
+ },
+ {
+ "type": "keyword",
+ "value": "-0.6",
+ },
+ {
+ "type": "keyword",
+ "value": "0.32",
+ },
+ {
+ "type": "keyword",
+ "value": "1.6",
+ },
+ ],
+ },
+ "name": "cubic-bezier",
+ "type": "function",
+ },
+ {
+ "args": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "4",
+ },
+ {
+ "type": "keyword",
+ "value": "jump-start",
+ },
+ ],
+ },
+ "name": "steps",
+ "type": "function",
+ },
+ ],
}
`);
+ expect(toValue(parsedValue)).toMatchInlineSnapshot(
+ `"ease, ease-in, cubic-bezier(0.68, -0.6, 0.32, 1.6), steps(4, jump-start)"`
+ );
});
-test("throws error if any custom transition timing function is used", () => {
- const transition = parseTransition("all 200ms custom-function 0s");
- expect(transition).toMatchInlineSnapshot(`
+test("parses any invalid transitionTimingFunction proeprty and returns invalud", () => {
+ expect(
+ parseTransitionLonghandProperty(
+ "transitionTimingFunction",
+ "ease, ease-in, cubic-bezier(0.68,-0.6,0.32,1.6), testing"
+ )
+ ).toMatchInlineSnapshot(`
{
"type": "invalid",
- "value": "all 200ms custom-function 0s",
+ "value": "ease, ease-in, cubic-bezier(0.68,-0.6,0.32,1.6), testing",
}
`);
});
diff --git a/packages/css-data/src/property-parsers/transition.ts b/packages/css-data/src/property-parsers/transition.ts
index 3da05efe90..ac3c09bcc7 100644
--- a/packages/css-data/src/property-parsers/transition.ts
+++ b/packages/css-data/src/property-parsers/transition.ts
@@ -4,11 +4,9 @@ import type {
KeywordValue,
LayerValueItem,
LayersValue,
- TupleValue,
- TupleValueItem,
Unit,
- UnparsedValue,
UnitValue,
+ FunctionValue,
} from "@webstudio-is/css-engine";
import { animatableProperties } from "../";
import { isTimingFunction } from "./transition-property-extractor";
@@ -56,151 +54,131 @@ export const isTransitionLongHandProperty = (
export const isValidTransitionValue = (
value: LayerValueItem
-): value is KeywordValue | UnitValue => {
- return value.type === "keyword" || value.type === "unit";
+): value is KeywordValue | UnitValue | FunctionValue => {
+ return (
+ value.type === "keyword" ||
+ value.type === "unit" ||
+ value.type === "function"
+ );
};
-export const parseTransition = (
- transition: string
+export const parseTransitionLonghandProperty = (
+ property: (typeof transitionLongHandProperties)[number],
+ input: string
): LayersValue | InvalidValue => {
- let tokenStream = transition.trim();
- tokenStream = tokenStream.endsWith(";")
- ? tokenStream.slice(0, -1)
- : tokenStream;
-
- const cleanupKeywords = ["transition:"];
-
- for (const cleanupKeyword of cleanupKeywords) {
- tokenStream = tokenStream.startsWith(cleanupKeyword)
- ? tokenStream.slice(cleanupKeyword.length).trim()
- : tokenStream;
- }
-
- const cssAst = cssTryParseValue(tokenStream);
+ const cssAst = cssTryParseValue(input);
if (cssAst === undefined) {
return {
type: "invalid",
- value: transition,
+ value: input,
};
}
- const parsed = csstree.lexer.matchProperty("transition", cssAst);
+ const parsed = csstree.lexer.matchProperty(kebabCase(property), cssAst);
if (parsed.error) {
return {
type: "invalid",
- value: transition,
+ value: input,
};
}
+ const layers: LayersValue = { type: "layers", value: [] };
try {
- const layers: TupleValue[] = [];
csstree.walk(cssAst, (node) => {
if (node.type === "Value") {
const children = node.children;
- let layer: csstree.CssNode[] = [];
for (const child of children) {
- layer.push(child);
+ switch (property) {
+ case "transitionProperty": {
+ if (child.type === "Identifier") {
+ if (isAnimatableProperty(child.name) === false) {
+ throw new Error(
+ `Invalid animatable property, received ${csstree.generate(child)}`
+ );
+ }
- if (child.type === "Operator" || children.last === child) {
- const transition: TupleValueItem[] = [];
+ layers.value.push({ type: "keyword", value: child.name });
+ }
+ break;
+ }
+
+ case "transitionTimingFunction": {
+ if (child.type === "Identifier") {
+ if (isTimingFunction(csstree.generate(child)) === false) {
+ throw new Error(
+ `Invalid timing function, received ${csstree.generate(child)}`
+ );
+ }
- for (let index = 0; index < layer.length; index++) {
- const item = layer[index];
- if (item.type === "Identifier") {
- const isAnimatable = isAnimatableProperty(item.name);
- const isTimingFunc = isTimingFunction(item.name);
+ layers.value.push({ type: "keyword", value: child.name });
+ }
- if (isTimingFunc === false && isAnimatable === false) {
- throw new Error(`Invalid transition property: ${item.name}`);
+ if (child.type === "Function") {
+ if (isTimingFunction(csstree.generate(child)) === false) {
+ throw new Error(
+ `Invalid timing function, received ${csstree.generate(child)}`
+ );
}
- transition.push({
- type: "keyword",
- value: item.name,
- });
+ // transition-timing function arguments are comma seperated values
+ const args: LayersValue = { type: "layers", value: [] };
+ const timingFunction: FunctionValue = {
+ type: "function",
+ args,
+ name: child.name,
+ };
+
+ for (const arg of child.children) {
+ if (arg.type === "Number") {
+ args.value.push({ type: "keyword", value: arg.value });
+ }
+
+ if (arg.type === "Identifier") {
+ args.value.push({ type: "keyword", value: arg.name });
+ }
+
+ if (arg.type === "Dimension") {
+ args.value.push({
+ type: "unit",
+ value: Number(arg.value),
+ unit: arg.unit as Unit,
+ });
+ }
+ }
+
+ layers.value.push(timingFunction);
}
- if (item.type === "Dimension") {
- transition.push({
+ break;
+ }
+
+ case "transitionDuration":
+ case "transitionDelay": {
+ if (child.type === "Dimension") {
+ layers.value.push({
type: "unit",
- value: Number(item.value),
- unit: item.unit as Unit,
+ value: Number(child.value),
+ unit: child.unit as Unit,
});
}
- if (item.type === "Function") {
- transition.push({
- type: "keyword",
- value: csstree.generate(item),
- });
- }
+ break;
}
- layers.push({
- type: "tuple",
- value: transition,
- });
- layer = [];
- continue;
+ default: {
+ throw new Error(`Received invalid property to parse ${property}`);
+ }
}
}
}
});
-
- return layers.length > 0
- ? {
- type: "layers",
- value: layers,
- }
- : { type: "invalid", value: transition };
} catch (error) {
- return {
- type: "invalid",
- value: transition,
- };
- }
-};
-
-export const parseTransitionLonghands = (
- property: (typeof transitionLongHandProperties)[number],
- input: string
-): LayersValue | InvalidValue | UnparsedValue => {
- const cssAst = cssTryParseValue(input);
- if (cssAst === undefined) {
- return {
- type: "invalid",
- value: input,
- };
- }
-
- const parsed = csstree.lexer.matchProperty(kebabCase(property), cssAst);
- if (parsed.error) {
return {
type: "invalid",
value: input,
};
}
- const layers: LayersValue = { type: "layers", value: [] };
- csstree.walk(cssAst, (node) => {
- if (node.type === "Identifier") {
- if (
- isAnimatableProperty(node.name) === true ||
- isTimingFunction(node.name) === true
- ) {
- layers.value.push({ type: "keyword", value: node.name });
- }
- }
-
- if (node.type === "Dimension") {
- layers.value.push({
- type: "unit",
- value: Number(node.value),
- unit: node.unit as Unit,
- });
- }
- });
-
return layers.value.length > 0 ? layers : { type: "invalid", value: input };
};
diff --git a/packages/css-engine/src/__generated__/types.ts b/packages/css-engine/src/__generated__/types.ts
index fd622c64c0..98df161460 100644
--- a/packages/css-engine/src/__generated__/types.ts
+++ b/packages/css-engine/src/__generated__/types.ts
@@ -306,7 +306,6 @@ export type Property =
| "transformBox"
| "transformOrigin"
| "transformStyle"
- | "transition"
| "transitionDelay"
| "transitionDuration"
| "transitionProperty"