@@ -3,39 +3,53 @@ import { create } from 'zustand';
33
44import { tokens } from './cunningham-tokens' ;
55
6- type Tokens = typeof tokens . themes . default ;
6+ type Tokens = typeof tokens . themes . default &
7+ Partial < ( typeof tokens . themes ) [ keyof typeof tokens . themes ] > ;
78type ColorsTokens = Tokens [ 'theme' ] [ 'colors' ] ;
89type FontSizesTokens = Tokens [ 'theme' ] [ 'font' ] [ 'sizes' ] ;
910type SpacingsTokens = Tokens [ 'theme' ] [ 'spacings' ] ;
1011type ComponentTokens = Tokens [ 'components' ] ;
1112export type Theme = keyof typeof tokens . themes ;
1213
13- interface AuthStore {
14- theme : string ;
14+ interface ThemeStore {
15+ theme : Theme ;
1516 setTheme : ( theme : Theme ) => void ;
16- themeTokens : ( ) => Partial < Tokens [ 'theme' ] > ;
17- colorsTokens : ( ) => Partial < ColorsTokens > ;
18- fontSizesTokens : ( ) => Partial < FontSizesTokens > ;
19- spacingsTokens : ( ) => Partial < SpacingsTokens > ;
20- componentTokens : ( ) => ComponentTokens ;
17+ themeTokens : Partial < Tokens [ 'theme' ] > ;
18+ colorsTokens : Partial < ColorsTokens > ;
19+ fontSizesTokens : Partial < FontSizesTokens > ;
20+ spacingsTokens : Partial < SpacingsTokens > ;
21+ componentTokens : ComponentTokens ;
2122}
2223
23- export const useCunninghamTheme = create < AuthStore > ( ( set , get ) => {
24- const currentTheme = ( ) =>
25- merge (
26- tokens . themes [ 'default' ] ,
27- tokens . themes [ get ( ) . theme as keyof typeof tokens . themes ] ,
28- ) as Tokens ;
29-
30- return {
31- theme : 'default' ,
32- themeTokens : ( ) => currentTheme ( ) . theme ,
33- colorsTokens : ( ) => currentTheme ( ) . theme . colors ,
34- componentTokens : ( ) => currentTheme ( ) . components ,
35- spacingsTokens : ( ) => currentTheme ( ) . theme . spacings ,
36- fontSizesTokens : ( ) => currentTheme ( ) . theme . font . sizes ,
37- setTheme : ( theme : Theme ) => {
38- set ( { theme } ) ;
39- } ,
40- } ;
41- } ) ;
24+ const getMergedTokens = ( theme : Theme ) => {
25+ return merge ( { } , tokens . themes [ 'default' ] , tokens . themes [ theme ] ) ;
26+ } ;
27+
28+ const DEFAULT_THEME : Theme = 'default' ;
29+ const defaultTokens = getMergedTokens ( DEFAULT_THEME ) ;
30+
31+ const initialState : ThemeStore = {
32+ theme : DEFAULT_THEME ,
33+ setTheme : ( ) => { } ,
34+ themeTokens : defaultTokens . theme ,
35+ colorsTokens : defaultTokens . theme . colors ,
36+ componentTokens : defaultTokens . components ,
37+ spacingsTokens : defaultTokens . theme . spacings ,
38+ fontSizesTokens : defaultTokens . theme . font . sizes ,
39+ } ;
40+
41+ export const useCunninghamTheme = create < ThemeStore > ( ( set ) => ( {
42+ ...initialState ,
43+ setTheme : ( theme : Theme ) => {
44+ const newTokens = getMergedTokens ( theme ) ;
45+
46+ set ( {
47+ theme,
48+ themeTokens : newTokens . theme ,
49+ colorsTokens : newTokens . theme . colors ,
50+ componentTokens : newTokens . components ,
51+ spacingsTokens : newTokens . theme . spacings ,
52+ fontSizesTokens : newTokens . theme . font . sizes ,
53+ } ) ;
54+ } ,
55+ } ) ) ;
0 commit comments