Skip to content

Commit

Permalink
fix: fix types for withTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Apr 25, 2020
1 parent 5fa03f0 commit f32c56e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
20 changes: 16 additions & 4 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Platforms } from './platforms'

export type Shape<T> = { [key: string]: T }
export type Shape<T> = {
[key: string]: T
}

export type Primitive = string | number

export type Token = {
value: string | number
value: Primitive
type: 'color' | 'size' | 'unknown'
comment?: string
}
Expand All @@ -12,8 +16,16 @@ export type FlattenToken = Token & {
name: string
}

export type Meta = {
meta?: {
css?: string
}
}

export type TokensMap = {
[key: string]: TokensMap | Token | string
[key: string]: TokensMap | Token | Primitive
}

export type ThemeTokens = { [key in Platforms]?: TokensMap } & { meta?: { css?: string } }
export type ThemeTokens = {
[key in Platforms]?: Meta & TokensMap
}
20 changes: 11 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import deepmerge from 'deepmerge'

import { Shape, ThemeTokens } from './core/types'
import { Shape, Primitive, ThemeTokens } from './core/types'
import { esModuleInterop } from './core/import-module'

type Primitives = Shape<string | number>
type ThemeFn = (primitives: Primitives) => ThemeTokens
type Primitives = Shape<Primitive>
type ThemeFn = ThemeFn1 | ThemeFn2
type ThemeFn1 = (primitives: Primitives) => ThemeTokens
type ThemeFn2 = (primitives: Primitives) => ThemeFn1

// TODO: theme args must be fn or plain shape.
export function withTokens(theme1: ThemeFn, theme2?: ThemeFn) {
export function withTokens(themeFn1: ThemeFn, themeFn2?: ThemeFn) {
return (primitives1: Primitives = {}) => (primitives2: Primitives = {}) => {
const composedPrimitives = deepmerge(primitives1, primitives2)
const resolvedTheme1 = esModuleInterop(theme1)
const resolvedTheme2 = esModuleInterop(theme2)
if (resolvedTheme2 === undefined) {
return resolvedTheme1(composedPrimitives)
const theme1 = esModuleInterop<ThemeFn1>(themeFn1 as any)
const theme2 = esModuleInterop<ThemeFn1>(themeFn2 as any)
if (theme2 === undefined) {
return theme1(composedPrimitives)
}
return deepmerge(resolvedTheme1(composedPrimitives), resolvedTheme2(composedPrimitives))
return deepmerge(theme1(composedPrimitives), theme2(composedPrimitives))
}
}

0 comments on commit f32c56e

Please sign in to comment.