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 animation shorthand (#3565)

Bogdan Chadkin3cc8e536e9e6
diff --git a/packages/css-data/src/shorthands.test.ts b/packages/css-data/src/shorthands.test.ts
index a86821d8da..0186503693 100644
--- a/packages/css-data/src/shorthands.test.ts
+++ b/packages/css-data/src/shorthands.test.ts
@@ -527,7 +527,43 @@ test("expand list-style", () => {
]);
});
-test.todo("animation");
+test("expand animation", () => {
+ expect(
+ expandShorthands([
+ ["animation", `3s ease-in 1s 2 reverse both paused slidein`],
+ ])
+ ).toEqual([
+ ["animation-duration", "3s"],
+ ["animation-timing-function", "ease-in"],
+ ["animation-delay", "1s"],
+ ["animation-iteration-count", "2"],
+ ["animation-direction", "reverse"],
+ ["animation-fill-mode", "both"],
+ ["animation-play-state", "paused"],
+ ["animation-name", "slidein"],
+ ["animation-timeline", "auto"],
+ ["animation-range-start", "normal"],
+ ["animation-range-end", "normal"],
+ ]);
+ expect(
+ expandShorthands([
+ ["animation", `3s linear slidein, 3s ease-out 5s slideout`],
+ ])
+ ).toEqual([
+ ["animation-duration", "3s,3s"],
+ ["animation-timing-function", "linear,ease-out"],
+ ["animation-delay", "0s,5s"],
+ ["animation-iteration-count", "1,1"],
+ ["animation-direction", "normal,normal"],
+ ["animation-fill-mode", "none,none"],
+ ["animation-play-state", "running,running"],
+ ["animation-name", "slidein,slideout"],
+ ["animation-timeline", "auto"],
+ ["animation-range-start", "normal"],
+ ["animation-range-end", "normal"],
+ ]);
+});
+
test.todo("container");
test.todo("contain-intrinsic-size");
test.todo("grid");
diff --git a/packages/css-data/src/shorthands.ts b/packages/css-data/src/shorthands.ts
index a487d6eeda..69ca7d4626 100644
--- a/packages/css-data/src/shorthands.ts
+++ b/packages/css-data/src/shorthands.ts
@@ -12,23 +12,42 @@ const createValueNode = (data?: CssNode[]): Value => ({
children: new List<CssNode>().fromArray(data ?? []),
});
-const createInitialNode = (): Value => ({
+const createNumber = (value: string): Value => ({
+ type: "Value",
+ children: new List<CssNode>().appendData({
+ type: "Number",
+ value,
+ }),
+});
+
+const createDimension = (value: string, unit: string): Value => ({
+ type: "Value",
+ children: new List<CssNode>().appendData({
+ type: "Dimension",
+ value,
+ unit,
+ }),
+});
+
+const createIdentifier = (name: string): Value => ({
type: "Value",
children: new List<CssNode>().appendData({
type: "Identifier",
- name: "initial",
+ name,
}),
});
+const createInitialNode = () => createIdentifier("initial");
+
const getValueList = (value: CssNode): CssNode[] => {
const children = "children" in value ? value.children?.toArray() : undefined;
return children ?? [value];
};
-const splitBySlash = (list: List<CssNode> | CssNode[]) => {
+const splitByOperator = (list: List<CssNode> | CssNode[], operator: string) => {
const lists: Array<undefined | CssNode[]> = [[]];
for (const node of list) {
- if (node.type === "Operator" && node.value === "/") {
+ if (node.type === "Operator" && node.value === operator) {
lists.push([]);
} else {
lists.at(-1)?.push(node);
@@ -37,6 +56,17 @@ const splitBySlash = (list: List<CssNode> | CssNode[]) => {
return lists;
};
+const joinByOperator = (list: List<CssNode> | CssNode[], operator: string) => {
+ const joined: CssNode[] = [];
+ for (const node of list) {
+ if (joined.length > 0) {
+ joined.push({ type: "Operator", value: operator });
+ }
+ joined.push(node);
+ }
+ return joined;
+};
+
/**
* Match the list of specified syntaxes with nodes
* Matches can be placed in different order than the list
@@ -181,11 +211,7 @@ const expandEdges = function* (property: string, value: CssNode) {
* border-radius = <length-percentage [0,∞]>{1,4} [ / <length-percentage [0,∞]>{1,4} ]?
*
*/
-const expandBorderRadius = function* (property: string, value: CssNode) {
- if (property !== "border-radius") {
- yield [property, value] as const;
- return;
- }
+const expandBorderRadius = function* (value: CssNode) {
const firstRadius = [];
const secondRadius = [];
let hasSecondRadius = false;
@@ -241,11 +267,7 @@ const expandBorderRadius = function* (property: string, value: CssNode) {
* <border-image-repeat> = [ stretch | repeat | round | space ]{1,2}
*
*/
-const expandBorderImage = function* (property: string, value: CssNode) {
- if (property !== "border-image") {
- yield [property, value] as const;
- return;
- }
+const expandBorderImage = function* (value: CssNode) {
const [source, config, repeat] = parseUnordered(
[
"<'border-image-source'>",
@@ -258,7 +280,10 @@ const expandBorderImage = function* (property: string, value: CssNode) {
let width = createInitialNode();
let outset = createInitialNode();
if (config) {
- const [sliceNodes, widthNodes, outsetNodes] = splitBySlash(config.children);
+ const [sliceNodes, widthNodes, outsetNodes] = splitByOperator(
+ config.children,
+ "/"
+ );
if (sliceNodes && sliceNodes.length > 0) {
slice = createValueNode(sliceNodes);
}
@@ -276,26 +301,6 @@ const expandBorderImage = function* (property: string, value: CssNode) {
yield ["border-image-repeat", repeat ?? createInitialNode()] as const;
};
-const expandGap = function* (property: string, value: CssNode) {
- switch (property) {
- case "gap":
- case "grid-gap": {
- const [rowGap, columnGap] = getValueList(value);
- yield ["row-gap", rowGap] as const;
- yield ["column-gap", columnGap ?? rowGap] as const;
- break;
- }
- case "grid-row-gap":
- yield ["row-gap", value] as const;
- break;
- case "grid-column-gap":
- yield ["column-gap", value] as const;
- break;
- default:
- yield [property, value] as const;
- }
-};
-
/**
*
* font =
@@ -346,9 +351,9 @@ const expandFont = function* (value: CssNode) {
};
const expandFlex = function* (value: CssNode) {
- const zero = createValueNode([{ type: "Number", value: "0" }]);
- const one = createValueNode([{ type: "Number", value: "1" }]);
- const auto = createValueNode([{ type: "Identifier", name: "auto" }]);
+ const zero = createNumber("0");
+ const one = createNumber("1");
+ const auto = createIdentifier("auto");
let grow: undefined | Value;
let shrink: undefined | Value;
let basis: undefined | Value;
@@ -369,7 +374,101 @@ const expandFlex = function* (value: CssNode) {
yield ["flex-basis", basis ?? zero] as const;
};
-const expandShorthandsAst = function* (property: string, value: CssNode) {
+/**
+ *
+ * animation = <single-animation>#
+ *
+ * <single-animation> =
+ * <time [0s,∞]> ||
+ * <easing-function> ||
+ * <time> ||
+ * <single-animation-iteration-count> ||
+ * <single-animation-direction> ||
+ * <single-animation-fill-mode> ||
+ * <single-animation-play-state> ||
+ * [ none | <keyframes-name> ]
+ *
+ */
+const expandAnimation = function* (value: CssNode) {
+ const durations: CssNode[] = [];
+ const timings: CssNode[] = [];
+ const delays: CssNode[] = [];
+ const iterationCounts: CssNode[] = [];
+ const directions: CssNode[] = [];
+ const fillModes: CssNode[] = [];
+ const playStates: CssNode[] = [];
+ const names: CssNode[] = [];
+ for (const animationNodes of splitByOperator(getValueList(value), ",")) {
+ const [
+ duration,
+ timing,
+ delay,
+ iterationCount,
+ direction,
+ fillMode,
+ playState,
+ name,
+ ] = parseUnordered(
+ [
+ "<time [0s,∞]>",
+ "<easing-function>",
+ "<time>",
+ "<single-animation-iteration-count>",
+ "<single-animation-direction>",
+ "<single-animation-fill-mode>",
+ "<single-animation-play-state>",
+ "[ none | <keyframes-name> ]",
+ ],
+ createValueNode(animationNodes)
+ );
+ durations.push(duration ?? createDimension("0", "s"));
+ timings.push(timing ?? createIdentifier("ease"));
+ delays.push(delay ?? createDimension("0", "s"));
+ iterationCounts.push(iterationCount ?? createNumber("1"));
+ directions.push(direction ?? createIdentifier("normal"));
+ fillModes.push(fillMode ?? createIdentifier("none"));
+ playStates.push(playState ?? createIdentifier("running"));
+ names.push(name ?? createIdentifier("none"));
+ }
+ yield [
+ "animation-duration",
+ createValueNode(joinByOperator(durations, ",")),
+ ] as const;
+ yield [
+ "animation-timing-function",
+ createValueNode(joinByOperator(timings, ",")),
+ ] as const;
+ yield [
+ "animation-delay",
+ createValueNode(joinByOperator(delays, ",")),
+ ] as const;
+ yield [
+ "animation-iteration-count",
+ createValueNode(joinByOperator(iterationCounts, ",")),
+ ] as const;
+ yield [
+ "animation-direction",
+ createValueNode(joinByOperator(directions, ",")),
+ ] as const;
+ yield [
+ "animation-fill-mode",
+ createValueNode(joinByOperator(fillModes, ",")),
+ ] as const;
+ yield [
+ "animation-play-state",
+ createValueNode(joinByOperator(playStates, ",")),
+ ] as const;
+ yield [
+ "animation-name",
+ createValueNode(joinByOperator(names, ",")),
+ ] as const;
+ // reset with animation shorthand but cannot be set with it
+ yield ["animation-timeline", createIdentifier("auto")] as const;
+ yield ["animation-range-start", createIdentifier("normal")] as const;
+ yield ["animation-range-end", createIdentifier("normal")] as const;
+};
+
+const expandShorthand = function* (property: string, value: CssNode) {
switch (property) {
case "font":
yield* expandFont(value);
@@ -400,6 +499,30 @@ const expandShorthandsAst = function* (property: string, value: CssNode) {
break;
}
+ case "border-radius":
+ yield* expandBorderRadius(value);
+ break;
+
+ case "border-image":
+ yield* expandBorderImage(value);
+ break;
+
+ case "gap":
+ case "grid-gap": {
+ const [rowGap, columnGap] = getValueList(value);
+ yield ["row-gap", rowGap] as const;
+ yield ["column-gap", columnGap ?? rowGap] as const;
+ break;
+ }
+
+ case "grid-row-gap":
+ yield ["row-gap", value] as const;
+ break;
+
+ case "grid-column-gap":
+ yield ["column-gap", value] as const;
+ break;
+
case "flex":
yield* expandFlex(value);
break;
@@ -475,6 +598,10 @@ const expandShorthandsAst = function* (property: string, value: CssNode) {
break;
}
+ case "animation":
+ yield* expandAnimation(value);
+ break;
+
default:
yield [property, value] as const;
}
@@ -503,22 +630,10 @@ export const expandShorthands = (
const generator = expandEdges(property, value);
for (const [property, value] of generator) {
- const generator = expandBorderRadius(property, value);
+ const generator = expandShorthand(property, value);
for (const [property, value] of generator) {
- const generator = expandBorderImage(property, value);
-
- for (const [property, value] of generator) {
- const generator = expandGap(property, value);
-
- for (const [property, value] of generator) {
- const generator = expandShorthandsAst(property, value);
-
- for (const [property, value] of generator) {
- longhands.push([property, generate(value)]);
- }
- }
- }
+ longhands.push([property, generate(value)]);
}
}
}