Skip to content

Commit

Permalink
feat(tokens): use CSS @property for primitive tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Niznikr committed Nov 13, 2024
1 parent 9e2a802 commit abb2401
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-sloths-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@launchpad-ui/tokens": patch
---

Use CSS `@property` for primitive tokens
40 changes: 38 additions & 2 deletions packages/tokens/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const sd = new StyleDictionary({
css: {
prefix: 'lp',
transformGroup: 'css',
transforms: ['name/kebab', 'time/seconds', 'size/rem', 'color/rgb', 'attribute/font'],
transforms: ['name/kebab', 'time/seconds', 'size/remToPx', 'color/rgb', 'attribute/font'],
buildPath: 'dist/',
options: {
outputReferences: true,
Expand All @@ -19,7 +19,7 @@ const sd = new StyleDictionary({
files: [
{
destination: 'index.css',
format: 'css/variables',
format: 'css/property',
filter: (token) => token.filePath !== 'src/color-aliases.json',
},
{
Expand Down Expand Up @@ -214,6 +214,42 @@ sd.registerFormat({
},
});

sd.registerFormat({
name: 'css/property',
format: async ({ dictionary, file }) => {
const header = await fileHeader({ file });
return (
header +
dictionary.allTokens
.map((token) => {
let syntax = '*';
switch (token.$type) {
case 'color':
syntax = token.name.includes('gradient') ? '*' : '<color>';
break;
case 'duration':
syntax = '<time>';
break;
case 'dimension':
syntax = '<length>';
break;
case 'number':
syntax = '<integer>';
break;
case 'fontWeight':
syntax = '<integer>';
break;
default:
break;
}

return `@property --${token.name} {\n\tsyntax: "${syntax}";\n\tinherits: false;\n\tinitial-value: ${token.$value};\n}\n`;
})
.join('\n')
);
},
});

sd.registerTransform({
name: 'custom/value/name',
type: 'attribute',
Expand Down
2 changes: 1 addition & 1 deletion packages/tokens/src/line-height.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"lineHeight": {
"$type": "number",
"$type": "dimension",
"100": {
"$value": "{size.16}"
},
Expand Down

0 comments on commit abb2401

Please sign in to comment.