Skip to content

Commit

Permalink
feat(css functions): support styles as function of theme
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Feb 9, 2021
1 parent d6c25e6 commit dd19d91
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export type Theme = {
export type Options = {}

export declare function hypostyle(theme?: Theme, options?: Options): {
css(props: Partial<HypostyleObject>): string;
css(
props: ((tokens: Theme['tokens']) => Partial<HypostyleObject>) |
Partial<HypostyleObject>
): string;
injectGlobal(props: Partial<HypostyleObject>): any;
keyframes: KeyframesAddon['keyframes'];
flush(): string;
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ function hypostyle (theme = {}, config = {}) {

return {
css (props) {
return Object.keys(props).length ? nano.rule(style(props, t)) : ''
const p = typeof props === 'function' ? props(t.tokens) : props
return Object.keys(p).length ? nano.rule(style(p, t)) : ''
},
injectGlobal (props) {
return nano.global(style(props, t))
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"husky": "^4.2.5",
"nodemon": "^2.0.4",
"prettier-standard": "^16.4.1",
"semantic-release": "^17.2.1"
"semantic-release": "^17.2.1",
"typescript": "^4.1.3"
},
"config": {
"commitizen": {
Expand Down
10 changes: 10 additions & 0 deletions test/hypostyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,14 @@ export default (test, assert) => {

assert(sheet.includes(animation))
})

test('css as a function', () => {
const { css, flush } = hypostyle(defaults)

css(tokens => ({ fs: tokens.fontSize[1] }))

const sheet = flush()

assert(sheet.includes('font-size:3rem'))
})
}

0 comments on commit dd19d91

Please sign in to comment.