-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
export tetra tokens as CSS Variables
- Loading branch information
1 parent
2a50875
commit 32b9909
Showing
4 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,5 +109,6 @@ | |
}, | ||
"resolutions": { | ||
"jackspeak": "2.1.1" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.140036830124618d624a2187b50d04289d5a087f326c9edfc0ccd733d76c4f52c3a313d4fc148794a2a9d81553016004e6742e8cf850670268a7387fc220c903" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Meta, Source } from '@storybook/blocks'; | ||
import cssVariables from './css-variables'; | ||
|
||
<Meta title="Tokens/CSS Variables" /> | ||
|
||
# Token CSS Variables | ||
|
||
The tokens are also available as CSS Variables. To use them: | ||
|
||
```jsx | ||
import cssVariables from '@chromatic-com/tetra/css-variables'; | ||
import { css, Global } from '@storybook/theming'; | ||
|
||
const tetraCSSVariables = css` | ||
${cssVariables} | ||
`; | ||
|
||
const App = () => ( | ||
<> | ||
<Global styles={tetraCSSVariables} /> | ||
{/* Other app components */} | ||
</> | ||
); | ||
``` | ||
|
||
<Source code={cssVariables} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
color, | ||
fontFamily, | ||
fontWeight, | ||
fontSize, | ||
lineHeight, | ||
breakpoint, | ||
spacing, | ||
} from './_tokens/tokens'; | ||
|
||
const prefix = 'tetra'; | ||
|
||
function jsonToCssVariables(tokenType: string, json: Record<string, any>) { | ||
const keys = Object.keys(json); | ||
|
||
const vars = keys.map((key) => { | ||
const varValue = json[key]; | ||
const varName = `--${prefix}-${tokenType}-${key}`; | ||
|
||
return ` ${varName}: ${varValue};`; | ||
}, {}); | ||
|
||
return vars.join('\n'); | ||
} | ||
|
||
function generateCSSVariables() { | ||
const cssVariables = `:root { | ||
${jsonToCssVariables('color', color)} | ||
${jsonToCssVariables('fontFamily', fontFamily)} | ||
${jsonToCssVariables('fontWeight', fontWeight)} | ||
${jsonToCssVariables('fontSize', fontSize)} | ||
${jsonToCssVariables('lineHeight', lineHeight)} | ||
${jsonToCssVariables('breakpoint', breakpoint)} | ||
${jsonToCssVariables('spacing', spacing)} | ||
}`; | ||
|
||
return cssVariables; | ||
} | ||
|
||
const cssVariables = generateCSSVariables(); | ||
|
||
export default cssVariables; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters