Skip to content

Commit

Permalink
fix(meta-css): fix template unit alias handling
Browse files Browse the repository at this point in the history
- update TEMPLATE_UNITS aliases
- update __templateValue() helper
- add tests
  • Loading branch information
postspectacular committed Mar 18, 2024
1 parent 0c021bf commit d7f11c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/meta-css/src/generate.ts
Expand Up @@ -66,10 +66,10 @@ export const UNITS: Record<string, Fn<any, string>> = {
"%": percent,
};

export const TEMPLATE_UNITS: Record<string, Fn<any, string>> = {
percent: (x) => `${x}%`,
second: (x) => `${x}s`,
url: (x) => `url(${x})`,
export const TEMPLATE_UNITS: IObjectOf<string> = {
percent: "{0}%",
second: "{0}s",
url: "url({0})",
};

export const VARIATIONS: IObjectOf<string[]> = {
Expand Down Expand Up @@ -326,7 +326,7 @@ const __value = (x: NumOrString, unitID?: string) => {

/** @internal */
const __templateValue = (unitID?: string) =>
"{0}" + (unitID ? TEMPLATE_UNITS[unitID] || unitID : "");
unitID ? TEMPLATE_UNITS[unitID] || "{0}" + unitID : "{0}";

/** @internal */
const __withVariations = (
Expand Down
27 changes: 27 additions & 0 deletions packages/meta-css/test/generate.test.ts
Expand Up @@ -84,3 +84,30 @@ test("template multiple params", () => {
)
).toEqual({ test: { "--a": "{0}rem", "--b": "{1}s", __arity: 2 } });
});

test("template unit alias", () => {
expect(
expandTemplateSpec(
{},
{
name: "anim-delay",
props: "animation-delay",
unit: "second",
},
{},
NULL_LOGGER
)
).toEqual({ "anim-delay": { __arity: 1, "animation-delay": "{0}s" } });
expect(
expandTemplateSpec(
{},
{
name: "bg",
props: "background-image",
unit: "url",
},
{},
NULL_LOGGER
)
).toEqual({ bg: { __arity: 1, "background-image": "url({0})" } });
});

0 comments on commit d7f11c0

Please sign in to comment.