Skip to content

experimental: support linear() easing #4957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/css-data/src/parse-css-value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ test("parse transition-timing-function property", () => {
type: "invalid",
value: "ease, testing",
});
expect(
parseCssValue("transitionTimingFunction", "linear(0 0%, 1 100%)")
).toEqual({
type: "layers",
value: [{ type: "unparsed", value: "linear(0 0%,1 100%)" }],
});
});

test("parse transition-behavior property as layers", () => {
Expand Down
25 changes: 17 additions & 8 deletions packages/css-data/src/parse-css-value.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { colord } from "colord";
import * as csstree from "css-tree";
import { type CssNode, generate, lexer, List } from "css-tree";
import { type CssNode, generate, lexer, List, parse } from "css-tree";
import warnOnce from "warn-once";
import {
cssWideKeywords,
Expand All @@ -24,7 +23,7 @@ import { units } from "./__generated__/units";

export const cssTryParseValue = (input: string): undefined | CssNode => {
try {
const ast = csstree.parse(input, { context: "value" });
const ast = parse(input, { context: "value" });
return ast;
} catch {
return;
Expand Down Expand Up @@ -95,7 +94,6 @@ export const isValidDeclaration = (
if (ast == null) {
return false;
}

// scale css-proeprty accepts both number and percentage.
// The syntax from MDN is incorrect and should be updated.
// Here is a PR that fixes the same, but it is not merged yet.
Expand All @@ -105,7 +103,18 @@ export const isValidDeclaration = (
return lexer.match(syntax, ast).matched !== null;
}

const matchResult = csstree.lexer.matchProperty(cssPropertyName, ast);
if (
cssPropertyName === "transition-timing-function" ||
cssPropertyName === "animation-timing-function"
) {
if (
lexer.match("linear( [ <number> && <percentage>{0,2} ]# )", ast).matched
) {
return true;
}
}

const matchResult = lexer.matchProperty(cssPropertyName, ast);

// allow to parse unknown properties as unparsed
if (matchResult.error?.message.includes("Unknown property")) {
Expand Down Expand Up @@ -277,9 +286,9 @@ const parseLiteral = (
node.name === "rotateZ" ||
node.name === "perspective" ||
// <easing-function>
node.name === "linear" ||
node.name === "cubic-bezier" ||
node.name === "steps"
// treat linear function as unparsed
) {
const args: LayersValue = { type: "layers", value: [] };
for (const arg of node.children) {
Expand Down Expand Up @@ -397,9 +406,9 @@ export const parseCssValue = (
const layersValue: StyleValue = {
type: "layers",
value: splitRepeated(nodes).map((nodes) => {
const value = csstree.generate({
const value = generate({
type: "Value",
children: new csstree.List<CssNode>().fromArray(nodes),
children: new List<CssNode>().fromArray(nodes),
});
const parsed = parseCssValue(property, value, false) as LayerValueItem;
if (parsed.type === "invalid") {
Expand Down