-
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.
- Loading branch information
Showing
2 changed files
with
27 additions
and
13 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
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 |
---|---|---|
@@ -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)) | ||
} | ||
} |