-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
223 lines (216 loc) · 5.22 KB
/
utils.ts
File metadata and controls
223 lines (216 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* @fileoverview Theme utilities — color resolution and composition.
*/
import type { ColorValue } from '../colors'
import type { Palette } from '../effects/shimmer'
import type { ColorReference, Theme, ThemeColors } from './types'
import { ArrayIsArray } from '../primordials'
/**
* Rainbow gradient colors used for the `'rainbow'` color keyword.
* 10 hues — cycles through the full color spectrum with smooth transitions.
*/
export const RAINBOW_GRADIENT: Palette = [
// Red/pink.
[255, 100, 120],
// Orange.
[255, 140, 80],
// Yellow/gold.
[255, 180, 60],
// Yellow/green.
[220, 200, 80],
// Green.
[120, 200, 100],
// Cyan/turquoise.
[80, 200, 180],
// Blue.
[80, 160, 220],
// Purple/violet.
[140, 120, 220],
// Pink/magenta.
[200, 100, 200],
// Red/pink.
[255, 100, 140],
]
/**
* Create new theme from complete specification.
*
* @param config - Theme configuration
* @returns Theme object
*
* @example
* ```ts
* const theme = createTheme({
* name: 'custom',
* displayName: 'Custom',
* colors: {
* primary: [255, 100, 200],
* success: 'greenBright',
* error: 'redBright',
* warning: 'yellowBright',
* info: 'blueBright',
* step: 'cyanBright',
* text: 'white',
* textDim: 'gray',
* link: 'cyanBright',
* prompt: 'primary'
* }
* })
* ```
*/
export function createTheme(
config: Pick<Theme, 'name' | 'displayName' | 'colors'> &
Partial<Omit<Theme, 'name' | 'displayName' | 'colors'>>,
): Theme {
return {
__proto__: null,
name: config.name,
displayName: config.displayName,
colors: { __proto__: null, ...config.colors } as ThemeColors,
effects: config.effects
? { __proto__: null, ...config.effects }
: undefined,
meta: config.meta ? { __proto__: null, ...config.meta } : undefined,
} as Theme
}
/**
* Extend existing theme with custom overrides.
* Deep merge of colors and effects.
*
* @param base - Base theme
* @param overrides - Custom overrides
* @returns Extended theme
*
* @example
* ```ts
* const custom = extendTheme(SOCKET_THEME, {
* name: 'custom',
* colors: { primary: [255, 100, 200] }
* })
* ```
*/
export function extendTheme(
base: Theme,
overrides: Partial<Omit<Theme, 'colors'>> & {
colors?: Partial<ThemeColors> | undefined
},
): Theme {
return {
__proto__: null,
...base,
...overrides,
colors: {
__proto__: null,
...base.colors,
...overrides.colors,
} as ThemeColors,
effects: overrides.effects
? {
__proto__: null,
...base.effects,
...overrides.effects,
spinner:
overrides.effects.spinner !== undefined
? {
__proto__: null,
...base.effects?.spinner,
...overrides.effects.spinner,
}
: base.effects?.spinner,
shimmer:
overrides.effects.shimmer !== undefined
? {
__proto__: null,
...base.effects?.shimmer,
...overrides.effects.shimmer,
}
: base.effects?.shimmer,
pulse:
overrides.effects.pulse !== undefined
? {
__proto__: null,
...base.effects?.pulse,
...overrides.effects.pulse,
}
: base.effects?.pulse,
}
: base.effects,
meta: overrides.meta
? {
__proto__: null,
...base.meta,
...overrides.meta,
}
: base.meta,
} as Theme
}
/**
* Resolve color reference to concrete value.
* Handles semantic keywords: 'primary', 'secondary', 'rainbow', 'inherit'
*
* @param value - Color reference
* @param colors - Theme palette
* @returns Resolved color
*
* @example
* ```ts
* resolveColor('primary', theme.colors)
* resolveColor([255, 0, 0], theme.colors)
* ```
*/
export function resolveColor(
value: ColorReference | ColorValue,
colors: ThemeColors,
): ColorValue | 'inherit' | Palette {
if (typeof value === 'string') {
if (value === 'primary') {
return colors.primary
}
if (value === 'secondary') {
return colors.secondary ?? colors.primary
}
if (value === 'inherit') {
return 'inherit'
}
if (value === 'rainbow') {
return RAINBOW_GRADIENT
}
return value as ColorValue
}
return value as ColorValue
}
/**
* Resolve shimmer color with gradient support.
*
* @param value - Shimmer color
* @param theme - Theme context
* @returns Resolved color
*
* @example
* ```ts
* resolveShimmerColor('rainbow', theme)
* resolveShimmerColor('primary', theme)
* ```
*/
export function resolveShimmerColor(
value: ColorReference | ColorValue[] | undefined,
theme: Theme,
): ColorValue | Palette | 'inherit' {
if (!value) {
return 'inherit'
}
if (value === 'rainbow') {
return RAINBOW_GRADIENT
}
if (value === 'inherit') {
return 'inherit'
}
if (ArrayIsArray(value)) {
if (value.length > 0 && ArrayIsArray(value[0])) {
// Gradient
return value as Palette
}
// Single RGB
return value as unknown as ColorValue
}
return resolveColor(value as ColorReference, theme.colors)
}