Skip to content

Commit

Permalink
feat: impl di for theme tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Apr 21, 2020
1 parent 2153a39 commit a756465
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/core/theme-layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export async function getThemeLayers(
// @ts-ignore
const files = await fg('**/*.{js,ts}', { cwd: source, ignore: options.exclude })
for (const fileName of files) {
const data = await importModule<ThemeTokens>(resolve(source, fileName))
const fn = await importModule<ThemeTokens>(resolve(source, fileName))
const maybeFn = fn()
const data = typeof maybeFn === 'function' ? maybeFn() : maybeFn
const { name: layer } = parse(fileName)
for (const [platform, levels] of platforms) {
if (options !== undefined && !options.platforms.includes(platform)) {
Expand Down
22 changes: 12 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import Color from 'color'
import deepmerge from 'deepmerge'

import { ThemeTokens } from './core/token.h'
import { Shape, ThemeTokens } from './core/token.h'

export function theme(shape1: ThemeTokens, shape2: ThemeTokens = {}): ThemeTokens {
return deepmerge(shape1, shape2)
}
type Primitives = Shape<string | number>
type ThemeFn = (primitives: Primitives) => ThemeTokens

export function color(inputColor: string, options: { h?: number; s?: number; l?: number }): string {
return Color(inputColor)
// @ts-ignore
.hsl(options.h, options.s, options.l)
.hex()
// TODO: theme args must be fn or plain shape.
export function withTokens(theme1: ThemeFn, theme2?: ThemeFn) {
return (primitives1: Primitives = {}) => (primitives2: Primitives = {}) => {
const composedPrimitives = deepmerge(primitives1, primitives2)
if (theme2 === undefined) {
return theme1(composedPrimitives)
}
return deepmerge(theme1(composedPrimitives), theme2(composedPrimitives))
}
}

0 comments on commit a756465

Please sign in to comment.