-
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 730607b
Showing
3 changed files
with
75 additions
and
1 deletion.
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 |
---|---|---|
@@ -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