|
1 | | -import { DefaultTheme } from "../index"; |
| 1 | +import { DeepDictionary, DeepPartial } from "../utils"; |
2 | 2 |
|
3 | | -// https://stackoverflow.com/questions/60795256/typescript-type-merging |
4 | | -// https://dev.to/svehla/typescript-how-to-deep-merge-170c |
| 3 | +import defaultTheme from "./defaultTheme"; |
5 | 4 |
|
6 | | -/** |
7 | | - * Take two objects T and U and create the new one with uniq keys for T a U objectI |
8 | | - * helper generic for `DeepMergeTwoTypes` |
9 | | - */ |
10 | | -type GetObjDifferentKeys<T, U> = Omit<T, keyof U> & Omit<U, keyof T>; |
| 5 | +export type DefaultTheme = typeof defaultTheme; |
11 | 6 |
|
12 | | -/** |
13 | | - * Take two objects T and U and create the new one with the same objects keys |
14 | | - * helper generic for `DeepMergeTwoTypes` |
15 | | - */ |
16 | | -type GetObjSameKeys<T, U> = Omit<T | U, keyof GetObjDifferentKeys<T, U>>; |
| 7 | +export type ThemeKeys = keyof DefaultTheme; |
17 | 8 |
|
18 | | -type Merge<T, U> = |
19 | | - // non shared keys are optional |
20 | | - Partial<GetObjDifferentKeys<T, U>> & { |
21 | | - // shared keys are recursively resolved by `DeepMergeTwoTypes<...>` |
22 | | - [K in keyof GetObjSameKeys<T, U>]: DeepMerge<T[K], U[K]>; |
23 | | - }; |
| 9 | +export type ThemeContextType = DeepDictionary<DefaultTheme>; |
24 | 10 |
|
25 | | -// it merge 2 static types and try to avoid of unnecessary options (`'`) |
26 | | -/** |
27 | | - * @template T source object |
28 | | - * @template U target object |
29 | | - * |
30 | | - * @description Deep merge two theme objects |
31 | | - */ |
32 | | -export type DeepMerge<T, U> = |
33 | | - // check if generic types are arrays and unwrap it and do the recursion |
34 | | - [T, U] extends [(infer TItem)[], (infer UItem)[]] |
35 | | - ? DeepMerge<TItem, UItem>[] |
36 | | - : // check if generic types are objects |
37 | | - [T, U] extends [{ [key: string]: unknown }, { [key: string]: unknown }] |
38 | | - ? Merge<T, U> |
39 | | - : [T, U] extends [ |
40 | | - { [key: string]: unknown } | undefined, |
41 | | - { [key: string]: unknown } | undefined, |
42 | | - ] |
43 | | - ? Merge<NonNullable<T>, NonNullable<U>> | undefined |
44 | | - : T | U; |
| 11 | +export type PartialDefaultTheme = DeepPartial<DefaultTheme>; |
45 | 12 |
|
46 | | -interface _ComponentDefaultTheme { |
47 | | - components: DefaultTheme; |
48 | | -} |
49 | | - |
50 | | -declare const _brand: unique symbol; |
51 | | - |
52 | | -declare global { |
53 | | - namespace Renderlesskit { |
54 | | - export interface Theme extends _ComponentDefaultTheme {} |
55 | | - /** |
56 | | - * @template T default theme |
57 | | - * @template U user theme |
58 | | - * |
59 | | - * @description Safely Deep merges default theme with user theme |
60 | | - */ |
61 | | - export type MergeTheme<T, U> = DeepMerge< |
62 | | - T, |
63 | | - U extends { [x: string]: any } ? U : {} |
64 | | - >; |
65 | | - |
66 | | - type Brand<Type, Name = "ThemeKey"> = Type & { [_brand]: Name }; |
67 | | - |
68 | | - type Comps = Renderlesskit.Theme["components"]; |
69 | | - |
70 | | - /** |
71 | | - * @template C component name |
72 | | - * @template K theme key |
73 | | - * @template L theme key |
74 | | - * @template M theme key |
75 | | - * @template N theme key |
76 | | - */ |
77 | | - export type GetThemeValue< |
78 | | - C extends keyof Comps, |
79 | | - K extends keyof Comps[C] = Brand<keyof Comps[C]>, |
80 | | - L extends keyof Comps[C][K] = Brand<keyof Comps[C][K]>, |
81 | | - M extends keyof Comps[C][K][L] = Brand<keyof Comps[C][K][L]>, |
82 | | - N extends keyof Comps[C][K][L][M] = Brand<keyof Comps[C][K][L][M]>, |
83 | | - > = [C, K, L, M, N] extends [string, Brand<K>, Brand<L>, Brand<M>, Brand<N>] |
84 | | - ? Comps[C] |
85 | | - : [C, K, L, M, N] extends [string, string, Brand<L>, Brand<M>, Brand<N>] |
86 | | - ? Comps[C][K] |
87 | | - : [C, K, L, M, N] extends [string, string, string, Brand<M>, Brand<N>] |
88 | | - ? Comps[C][K][L] |
89 | | - : [C, K, L, M, N] extends [string, string, string, string, Brand<N>] |
90 | | - ? Comps[C][K][L][M] |
91 | | - : [C, K, L, M, N] extends [string, string, string, string, string] |
92 | | - ? Comps[C][K][L][M][N] |
93 | | - : never; |
94 | | - } |
95 | | -} |
| 13 | +export type ExtendableDefaultTheme = PartialDefaultTheme & { |
| 14 | + extend?: PartialDefaultTheme; |
| 15 | +}; |
0 commit comments