-
-
Notifications
You must be signed in to change notification settings - Fork 418
/
themes.tsx
305 lines (277 loc) · 8.08 KB
/
themes.tsx
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import {
MaskOptions,
addChildren,
applyMask,
createStrengthenMask,
createTheme,
createWeakenMask,
} from '@tamagui/create-theme'
import { colorTokens, darkColors, lightColors } from './tokens'
type ColorName = keyof typeof colorTokens.dark
const lightTransparent = 'rgba(255,255,255,0)'
const darkTransparent = 'rgba(10,10,10,0)'
// background => foreground
const palettes = {
dark: [
darkTransparent,
'#050505',
'#151515',
'#191919',
'#232323',
'#282828',
'#323232',
'#424242',
'#494949',
'#545454',
'#626262',
'#a5a5a5',
'#fff',
lightTransparent,
],
light: [
lightTransparent,
'#fff',
'#f9f9f9',
'hsl(0, 0%, 97.3%)',
'hsl(0, 0%, 95.1%)',
'hsl(0, 0%, 94.0%)',
'hsl(0, 0%, 92.0%)',
'hsl(0, 0%, 89.5%)',
'hsl(0, 0%, 81.0%)',
'hsl(0, 0%, 56.1%)',
'hsl(0, 0%, 50.3%)',
'hsl(0, 0%, 42.5%)',
'hsl(0, 0%, 9.0%)',
darkTransparent,
],
}
const templateColors = {
color1: 1,
color2: 2,
color3: 3,
color4: 4,
color5: 5,
color6: 6,
color7: 7,
color8: 8,
color9: 9,
color10: 10,
color11: 11,
color12: 12,
}
const templateShadows = {
shadowColor: 1,
shadowColorHover: 1,
shadowColorPress: 2,
shadowColorFocus: 2,
}
// we can use subset of our template as a "skip" so it doesn't get adjusted with masks
const skip = {
...templateColors,
...templateShadows,
}
// templates use the palette and specify index
// negative goes backwards from end so -1 is the last item
const template = {
...skip,
// the background, color, etc keys here work like generics - they make it so you
// can publish components for others to use without mandating a specific color scale
// the @tamagui/button Button component looks for `$background`, so you set the
// dark_red_Button theme to have a stronger background than the dark_red theme.
background: 2,
backgroundHover: 3,
backgroundPress: 1,
backgroundFocus: 2,
backgroundStrong: 1,
backgroundTransparent: 0,
color: -1,
colorHover: -2,
colorPress: -1,
colorFocus: -2,
colorTransparent: -0,
borderColor: 4,
borderColorHover: 5,
borderColorPress: 3,
borderColorFocus: 4,
placeholderColor: -4,
}
const lightShadowColor = 'rgba(0,0,0,0.02)'
const lightShadowColorStrong = 'rgba(0,0,0,0.066)'
const darkShadowColor = 'rgba(0,0,0,0.2)'
const darkShadowColorStrong = 'rgba(0,0,0,0.3)'
const lightShadows = {
shadowColor: lightShadowColorStrong,
shadowColorHover: lightShadowColorStrong,
shadowColorPress: lightShadowColor,
shadowColorFocus: lightShadowColor,
}
const darkShadows = {
shadowColor: darkShadowColorStrong,
shadowColorHover: darkShadowColorStrong,
shadowColorPress: darkShadowColor,
shadowColorFocus: darkShadowColor,
}
const lightTemplate = {
...template,
// our light color palette is... a bit unique
borderColor: 6,
borderColorHover: 7,
borderColorFocus: 5,
borderColorPress: 6,
...lightShadows,
}
const darkTemplate = { ...template, ...darkShadows }
const light = createTheme(palettes.light, lightTemplate)
const dark = createTheme(palettes.dark, darkTemplate)
type SubTheme = typeof light
const baseThemes: {
light: SubTheme
dark: SubTheme
} = {
light,
dark,
}
const masks = {
weaker: createWeakenMask(),
stronger: createStrengthenMask(),
}
// default mask options for most uses
const maskOptions: MaskOptions = {
skip,
// avoids the transparent ends
max: palettes.light.length - 2,
min: 1,
}
const allThemes = addChildren(baseThemes, (name, theme) => {
const isLight = name === 'light'
const inverseName = isLight ? 'dark' : 'light'
const inverseTheme = baseThemes[inverseName]
const transparent = (hsl: string, opacity = 0) =>
hsl.replace(`%)`, `%, ${opacity})`).replace(`hsl(`, `hsla(`)
// setup colorThemes and their inverses
const [colorThemes, inverseColorThemes] = [
colorTokens[name],
colorTokens[inverseName],
].map((colorSet) => {
return Object.fromEntries(
Object.keys(colorSet).map((color) => {
const colorPalette = Object.values(colorSet[color]) as string[]
// were re-ordering these
const [head, tail] = [
colorPalette.slice(0, 6),
colorPalette.slice(colorPalette.length - 5),
]
// add our transparent colors first/last
// and make sure the last (foreground) color is white/black rather than colorful
// this is mostly for consistency with the older theme-base
const palette = [
transparent(colorPalette[0]),
...head,
...tail,
theme.color,
transparent(colorPalette[colorPalette.length - 1]),
]
const colorTheme = createTheme(
palette,
isLight
? {
...lightTemplate,
// light color themes are a bit less sensitive
borderColor: 4,
borderColorHover: 5,
borderColorFocus: 4,
borderColorPress: 4,
}
: darkTemplate
)
return [color, colorTheme]
})
) as Record<ColorName, SubTheme>
})
const allColorThemes = addChildren(colorThemes, (colorName, colorTheme) => {
const inverse = inverseColorThemes[colorName]
return {
...getAltThemes(colorTheme, inverse),
...getComponentThemes(colorTheme, inverse),
}
})
const baseActiveTheme = applyMask(colorThemes.blue, masks.weaker, {
...maskOptions,
strength: 4,
})
const baseSubThemes = {
...getAltThemes(theme, inverseTheme, baseActiveTheme),
...getComponentThemes(theme, inverseTheme),
}
return {
...baseSubThemes,
...allColorThemes,
}
function getAltThemes(theme: SubTheme, inverse: SubTheme, activeTheme?: SubTheme) {
const maskOptionsAlt = {
...maskOptions,
skip: templateShadows,
}
const alt1 = applyMask(theme, masks.weaker, maskOptionsAlt)
const alt2 = applyMask(alt1, masks.weaker, maskOptionsAlt)
const active =
activeTheme ??
applyMask(theme, masks.weaker, {
...maskOptions,
strength: 4,
})
return addChildren({ alt1, alt2, active }, (_, subTheme) => {
return getComponentThemes(subTheme, subTheme === inverse ? theme : inverse)
})
}
function getComponentThemes(theme: SubTheme, inverse: SubTheme) {
const weaker1 = applyMask(theme, masks.weaker, maskOptions)
const weaker2 = applyMask(weaker1, masks.weaker, maskOptions)
const stronger1 = applyMask(theme, masks.stronger, maskOptions)
const inverse1 = applyMask(inverse, masks.weaker, maskOptions)
const inverse2 = applyMask(inverse1, masks.weaker, maskOptions)
const strongerBorderLighterBackground: SubTheme = isLight
? {
...stronger1,
borderColor: weaker1.borderColor,
borderColorHover: weaker1.borderColorHover,
borderColorPress: weaker1.borderColorPress,
borderColorFocus: weaker1.borderColorFocus,
}
: {
...theme,
borderColor: weaker1.borderColor,
borderColorHover: weaker1.borderColorHover,
borderColorPress: weaker1.borderColorPress,
borderColorFocus: weaker1.borderColorFocus,
}
return {
Card: weaker1,
Button: weaker2,
Checkbox: weaker2,
DrawerFrame: weaker1,
SliderTrack: stronger1,
SliderTrackActive: weaker2,
SliderThumb: inverse1,
Progress: weaker1,
ProgressIndicator: inverse,
Switch: weaker2,
SwitchThumb: inverse2,
TooltipArrow: weaker1,
TooltipContent: weaker2,
Input: strongerBorderLighterBackground,
TextArea: strongerBorderLighterBackground,
Tooltip: inverse1,
}
}
})
export const themes = {
...allThemes,
// bring back the full type, the rest use a subset to avoid clogging up ts,
// tamagui will be smart and use the top level themes as the type for useTheme() etc
light: createTheme(palettes.light, lightTemplate, { nonInheritedValues: lightColors }),
dark: createTheme(palettes.dark, darkTemplate, { nonInheritedValues: darkColors }),
}
// if (process.env.NODE_ENV === 'development') {
// console.log(JSON.stringify(themes).length)
// }