generated from nl-design-system/example
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add <StyleDictionaryStyle> component
- Loading branch information
Showing
6 changed files
with
116 additions
and
17 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@nl-design-system-unstable/theme-toolkit": minor | ||
--- | ||
|
||
Add `<StyleDictionaryStyle>` component. |
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
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,36 @@ | ||
/* eslint-env jest */ | ||
|
||
import { describe, expect, it } from '@jest/globals'; | ||
import { StyleDictionaryStyle } from './StyleDictionaryStyle'; | ||
import { render } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
|
||
describe('StyleDictionaryStyle', () => { | ||
const simpleTree = { | ||
example: { | ||
button: { | ||
color: { | ||
value: 'red', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
it('renders a HTML style element', () => { | ||
const { container } = render(<StyleDictionaryStyle tokens={simpleTree}></StyleDictionaryStyle>); | ||
|
||
const style = container.querySelector('style'); | ||
|
||
expect(style).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders a class name selector', () => { | ||
const { container } = render( | ||
<StyleDictionaryStyle tokens={simpleTree} selector=".my-theme"></StyleDictionaryStyle>, | ||
); | ||
|
||
const style = container.querySelector('style'); | ||
|
||
expect(style).toContain('.my-theme'); | ||
}); | ||
}); |
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,60 @@ | ||
import StyleDictionary from 'style-dictionary'; | ||
import memfs from '@bundled-es-modules/memfs'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { useState, useEffect } from 'react'; | ||
|
||
const styleDictionaryConversion = async (tokens: any, selector?: string): Promise<string> => { | ||
const { Volume } = memfs; | ||
const vol = new Volume(); | ||
const sd = new StyleDictionary( | ||
{ | ||
tokens, | ||
platforms: { | ||
css: { | ||
transforms: ['name/kebab'], | ||
transformGroup: 'css', | ||
files: [ | ||
{ | ||
destination: 'variables.css', | ||
format: 'css/variables', | ||
options: { | ||
selector, | ||
outputReferences: true, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
// typeof process !== 'undefined' ? { volume: vol } : {}, | ||
); | ||
|
||
let css = ''; | ||
|
||
await sd.buildAllPlatforms(); | ||
|
||
try { | ||
css = vol.readFileSync('/variables.css').toString('utf-8'); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
return css; | ||
}; | ||
|
||
export interface StyleDictionaryStyleProps { | ||
tokens: any; | ||
selector?: string; | ||
} | ||
|
||
export const StyleDictionaryStyle = ({ tokens, selector = ':root' }: PropsWithChildren<StyleDictionaryStyleProps>) => { | ||
const [css, setCss] = useState(''); | ||
|
||
useEffect(() => { | ||
styleDictionaryConversion(tokens, selector).then((css) => { | ||
setCss(css); | ||
}, console.error); | ||
}, [tokens]); | ||
|
||
return <style dangerouslySetInnerHTML={{ __html: css }}></style>; | ||
}; |
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,5 @@ | ||
// TODO: Make `"moduleResolution": "Bundler"` work in `tsconfig.json` | ||
declare module 'style-dictionary' { | ||
declare const StyleDictionary: any; | ||
export default StyleDictionary; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.