@tamagui/theme-base 1.0.1-beta.198 → 1.0.1-beta.199

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/themes.tsx CHANGED
@@ -1,241 +1,9 @@
1
- import { Variable, createTheme, getVariableValue, isWeb } from '@tamagui/core'
1
+ import { createThemes } from '@tamagui/create-themes'
2
2
 
3
- import { setColorAlpha } from './colorUtils'
4
- import { darkColors, lightColors } from './tokens'
3
+ import { colorTokens } from './tokens'
5
4
 
6
- // spaghetti ahead
7
-
8
- type ThemeCreatorProps = {
9
- shift?: number
10
- backgrounds: (string | Variable)[]
11
- backgroundStrong?: string | Variable
12
- borderColors?: (string | Variable)[]
13
- colors?: (string | Variable)[]
14
- activeTheme?: any
15
- offsets?: {
16
- color?: number[] | null
17
- background?: number[] | null
18
- borderColor?: number[] | null
19
- }
20
- isLight?: boolean
21
- isBase?: boolean
22
- }
23
-
24
- // helpers
25
-
26
- const alternates = [1, 2, 3] as const
27
- const alts = [1, 2] as const
28
- type AltKeys = 1 | 2
29
- type AltName<Name extends string, Keys extends string | number> = `${Name}_alt${Keys}`
30
- type ThemeCreator<A = any> = (str: number, props: ThemeCreatorProps) => A
31
-
32
- interface SubTheme {
33
- background: Variable<any> | string
34
- backgroundStrong: Variable<any> | string
35
- backgroundSoft: Variable<any> | string
36
- backgroundHover: Variable<any> | string
37
- backgroundPress: Variable<any> | string
38
- backgroundFocus: Variable<any> | string
39
- backgroundTransparent: Variable<any> | string
40
- color: Variable<any> | string
41
- colorHover: Variable<any> | string
42
- colorPress: Variable<any> | string
43
- colorFocus: Variable<any> | string
44
- colorTranslucent: Variable<any> | string
45
- colorMid: Variable<any> | string
46
- shadowColor: Variable<any> | string
47
- shadowColorHover: Variable<any> | string
48
- shadowColorPress: Variable<any> | string
49
- shadowColorFocus: Variable<any> | string
50
- borderColor: Variable<any> | string
51
- borderColorHover: Variable<any> | string
52
- borderColorPress: Variable<any> | string
53
- borderColorFocus: Variable<any> | string
54
- color1: Variable<any> | string
55
- color2: Variable<any> | string
56
- color3: Variable<any> | string
57
- color4: Variable<any> | string
58
- color5: Variable<any> | string
59
- color6: Variable<any> | string
60
- color7: Variable<any> | string
61
- color8: Variable<any> | string
62
- color9: Variable<any> | string
63
- color10: Variable<any> | string
64
- color11: Variable<any> | string
65
- color12: Variable<any> | string
66
- }
67
-
68
- function createThemesFrom<Name extends string, GetTheme extends ThemeCreator = ThemeCreator>(
69
- name: Name,
70
- getTheme: GetTheme,
71
- props: ThemeCreatorProps
72
- ): {
73
- [key in `${Name}_alt${AltKeys}` | `${Name}` | `${Name}_darker`]: GetTheme extends ThemeCreator<
74
- infer Theme
75
- >
76
- ? Theme
77
- : never
78
- } {
79
- const { shift = 0 } = props
80
- const theme = getTheme(0 + shift, props)
81
- let themeEntries: any[] = [[name, theme]]
82
-
83
- // generate alternates (for use in other themes), but keep just alts
84
- const altThemes: any[] = alternates.map((alt) => [
85
- `${name}_alt${alt}`,
86
- getTheme(alt * 1 + shift, props),
87
- ])
88
- const altThemes2: any[] = alternates.map((alt) => [
89
- `${name}_alt${alt}`,
90
- getTheme(alt + shift, props),
91
- ])
92
- const altButtonThemes: any[] = alternates.map((_, i) => {
93
- const [bName, bTheme] = [altThemes2[i][0], (altThemes2[i + 1] || altThemes2[i])[1]]
94
- return [`${bName}_Button` as any, bTheme]
95
- })
96
- // add these after alts since we rely on positioning
97
- const darkerTheme = getTheme(Math.max(0, shift + (props.isLight ? 1 : -1)), props)
98
- const activeTheme = props.activeTheme || {
99
- ...theme,
100
- borderColor: props.backgrounds[7],
101
- background: props.backgrounds[6],
102
- backgroundHover: props.backgrounds[6],
103
- backgroundPress: props.backgrounds[6],
104
- }
105
-
106
- // TODO can be de-duped
107
- const inverted = altThemes.map(([_name, theme]) => {
108
- return {
109
- ...theme,
110
- background: theme.color,
111
- backgroundHover: theme.colorHover,
112
- backgroundFocus: theme.colorFocus,
113
- backgroundPress: theme.colorPress,
114
- color: theme.background,
115
- colorHover: theme.backgroundHover,
116
- colorFocus: theme.backgroundFocus,
117
- colorPress: theme.backgroundPress,
118
- }
119
- })
120
-
121
- themeEntries = [
122
- ...themeEntries,
123
- // keep just alts
124
- ...altThemes.slice(0, alts.length),
125
- ...altButtonThemes.slice(0, alts.length),
126
- [`${name}_Button`, altThemes2[1][1]],
127
- [`${name}_DrawerFrame`, altThemes2[1][1]],
128
- [`${name}_SliderTrack`, altThemes[0][1]],
129
- [`${name}_SliderTrackActive`, altThemes[2][1]],
130
- [`${name}_SliderThumb`, inverted[2]],
131
- [`${name}_Progress`, altThemes[2][1]],
132
- [`${name}_ProgressIndicator`, inverted[2]],
133
- [`${name}_Switch`, altThemes[0][1]],
134
- [`${name}_SwitchThumb`, inverted[2]],
135
- [`${name}_TooltipArrow`, altThemes[1][1]],
136
- [`${name}_TooltipContent`, altThemes[1][1]],
137
- [`${name}_darker`, darkerTheme],
138
- [`${name}_active`, activeTheme],
139
- ]
140
- const themes = Object.fromEntries(themeEntries)
141
- // if (props.isLight) {
142
- // console.log('themes', themes)
143
- // }
144
- return themes as any
145
- }
146
-
147
- const themeCreator = (
148
- str = 1,
149
- {
150
- backgrounds,
151
- isLight = true,
152
- isBase = false,
153
- colors = [...backgrounds].reverse(),
154
- borderColors = isLight ? colors : backgrounds,
155
- backgroundStrong,
156
- offsets: offsetsProp,
157
- }: ThemeCreatorProps
158
- ) => {
159
- const offsets = {
160
- borderColor: offsetsProp?.borderColor ?? offsetsProp?.background ?? [0, 0, 0, 0],
161
- background: offsetsProp?.background ?? [0, 0, 0, 0],
162
- color: offsetsProp?.color ?? [0, 0, 0, 0],
163
- }
164
- const darkColors = isLight ? colors : backgrounds
165
- const lighterDir = isLight ? -1 : 1
166
- const darkerDir = -lighterDir
167
- const strongerDir = isLight ? darkerDir : lighterDir
168
- const softerDir = -strongerDir
169
- const get = (arr: any[], index: number, name = 'background') => {
170
- return arr[Math.max(0, Math.min(index + (offsets[name][str] || 0), arr.length - 1))]
171
- }
172
-
173
- const colorTranslucent = setColorAlpha(getVariableValue(get(colors, 0 + str, 'color')), 0.5)
174
-
175
- const theme: SubTheme = {
176
- background: get(backgrounds, str),
177
- backgroundStrong: backgroundStrong || get(backgrounds, str + strongerDir * 2),
178
- backgroundSoft: get(backgrounds, str + softerDir * 2),
179
- backgroundHover: get(backgrounds, str + lighterDir),
180
- backgroundPress: get(backgrounds, str + darkerDir),
181
- backgroundFocus: get(backgrounds, str + darkerDir * 2),
182
- backgroundTransparent: 'hsla(0, 0%, 0%, 0.012)',
183
- color: get(colors, 0 + str, 'color'),
184
- colorHover: get(colors, 1 + str, 'color'),
185
- colorPress: get(colors, 2 + str, 'color'),
186
- colorFocus: get(colors, 3 + str, 'color'),
187
- colorTranslucent,
188
- colorMid: (isLight ? colors : backgrounds)[Math.floor(colors.length / 2)],
189
- shadowColor: isLight ? 'hsla(0, 0%, 0%, 0.03)' : 'hsla(0, 0%, 0%, 0.2)',
190
- shadowColorHover: darkColors[!isLight ? 1 : 8],
191
- shadowColorPress: darkColors[!isLight ? 1 : 8],
192
- shadowColorFocus: darkColors[!isLight ? 1 : 8],
193
- borderColor: null as any as string | Variable,
194
- borderColorHover: null as any as string | Variable,
195
- borderColorPress: null as any as string | Variable,
196
- borderColorFocus: null as any as string | Variable,
197
- color1: backgrounds[0],
198
- color2: backgrounds[1],
199
- color3: backgrounds[2],
200
- color4: backgrounds[3],
201
- color5: backgrounds[4],
202
- color6: backgrounds[5],
203
- color7: backgrounds[6],
204
- color8: backgrounds[7],
205
- color9: backgrounds[8],
206
- color10: backgrounds[9],
207
- color11: backgrounds[10],
208
- color12: backgrounds[11],
209
- }
210
-
211
- if (isBase) {
212
- Object.assign(theme, {
213
- borderColor: isLight
214
- ? get(colors, 8 - str, 'borderColor')
215
- : get(backgrounds, 2 + str, 'borderColor'),
216
- borderColorHover: isLight
217
- ? get(colors, 6 - str, 'borderColor')
218
- : get(backgrounds, 3 + str, 'borderColor'),
219
- borderColorPress: isLight
220
- ? get(colors, 5 - str, 'borderColor')
221
- : get(backgrounds, 1 + str, 'borderColor'),
222
- borderColorFocus: isLight
223
- ? get(colors, 4 - str, 'borderColor')
224
- : get(backgrounds, 3 + str, 'borderColor'),
225
- })
226
- } else {
227
- Object.assign(theme, {
228
- borderColor: get(borderColors, 1 + strongerDir, 'borderColor'),
229
- borderColorHover: get(borderColors, 2 + strongerDir, 'borderColor'),
230
- borderColorPress: get(borderColors, 3 + strongerDir, 'borderColor'),
231
- borderColorFocus: get(borderColors, 4 + strongerDir, 'borderColor'),
232
- })
233
- }
234
-
235
- return theme
236
- }
237
-
238
- const themeColors = {
5
+ export const themes = createThemes<keyof typeof colorTokens.light>({
6
+ activeColor: 'blue',
239
7
  light: [
240
8
  '#fff',
241
9
  '#f4f4f4',
@@ -266,184 +34,6 @@ const themeColors = {
266
34
  '#626262',
267
35
  '#777777',
268
36
  ],
269
- }
270
-
271
- const lightThemes = createThemesFrom('light', themeCreator, {
272
- backgrounds: themeColors.light,
273
- // for now web non-ios style native is...
274
- backgroundStrong: isWeb ? '#fefefe' : '#f2f2f2',
275
- borderColors: themeColors.light.slice(2),
276
- isLight: true,
277
- offsets: {
278
- background: [2, 2, 2, 2, 2, 2],
279
- },
280
- })
281
-
282
- const darkThemes = createThemesFrom('dark', themeCreator, {
283
- backgrounds: themeColors.dark,
284
- colors: themeColors.light.slice(2),
285
- backgroundStrong: '#070707',
286
- isBase: true,
287
- isLight: false,
288
- shift: 1,
289
- offsets: {
290
- color: [0, 4, 5, 5, 5, 5],
291
- },
292
- })
293
-
294
- darkThemes.dark_darker.background = '#030303'
295
- darkThemes.dark_darker.backgroundStrong = '#000'
296
-
297
- type BaseTheme = {
298
- [key in keyof typeof lightColors | keyof SubTheme]: Variable<string>
299
- }
300
-
301
- const light = createTheme({
302
- ...lightColors,
303
- ...lightThemes.light,
304
- }) as BaseTheme
305
-
306
- const dark = createTheme({
307
- ...darkColors,
308
- ...darkThemes.dark,
309
- }) as BaseTheme
310
-
311
- const baseThemes = {
312
- // light
313
- ...lightThemes,
314
- light,
315
- // reserving in types, updating later
316
- light_active: lightThemes.light,
317
- light_Card: lightThemes.light,
318
- light_SliderTrack: lightThemes.light_alt1,
319
- light_SliderTrackActive: lightThemes.light_alt2,
320
- light_Switch: lightThemes.light_alt2,
321
- light_SwitchThumb: lightThemes.light,
322
- light_DrawerFrame: lightThemes.light_alt1,
323
-
324
- // dark
325
- ...darkThemes,
326
- dark,
327
- // reserving in types, updating later
328
- dark_active: darkThemes.dark,
329
- dark_Card: darkThemes.dark,
330
- dark_DrawerFrame: darkThemes.dark_alt1,
331
- dark_SliderTrack: darkThemes.dark_darker,
332
- dark_SliderTrackActive: darkThemes.dark_alt2,
333
- dark_Switch: darkThemes.dark_alt2,
334
- dark_SwitchThumb: darkThemes.dark_darker,
335
- dark_Button: darkThemes.dark_alt1,
336
- }
337
-
338
- const darkEntries = Object.entries(dark)
339
- const lightEntries = Object.entries(light)
340
- function findColors(prefix: string, dark = false): BaseTheme {
341
- return Object.fromEntries(
342
- (dark ? darkEntries : lightEntries).filter(([k]) => k.startsWith(prefix))
343
- ) as any
344
- }
345
-
346
- export const colorSchemes = [
347
- { name: 'blue', colors: findColors('blue'), darkColors: findColors('blue', true) },
348
- { name: 'gray', colors: findColors('gray'), darkColors: findColors('gray', true) },
349
- { name: 'green', colors: findColors('green'), darkColors: findColors('green', true) },
350
- { name: 'orange', colors: findColors('orange'), darkColors: findColors('orange', true) },
351
- { name: 'pink', colors: findColors('pink'), darkColors: findColors('pink', true) },
352
- { name: 'purple', colors: findColors('purple'), darkColors: findColors('purple', true) },
353
- { name: 'red', colors: findColors('red'), darkColors: findColors('red', true) },
354
- { name: 'yellow', colors: findColors('yellow'), darkColors: findColors('yellow', true) },
355
- ] as const
356
-
357
- export type ColorNames = typeof colorSchemes[number]['name']
358
-
359
- // nice and flat
360
- export const colorNames: ColorNames[] = [
361
- 'blue',
362
- 'green',
363
- 'orange',
364
- 'pink',
365
- 'purple',
366
- 'red',
367
- 'yellow',
368
- 'gray',
369
- ]
370
-
371
- type ColorThemeNames =
372
- | ColorNames
373
- | AltName<`light_${ColorNames}`, AltKeys>
374
- | AltName<`dark_${ColorNames}`, AltKeys>
375
-
376
- const colorThemeEntries = colorSchemes.flatMap(({ name, colors, darkColors }) => {
377
- const [altLightThemes, altDarkThemes] = [
378
- { colors: darkColors, backgrounds: colors },
379
- { colors, backgrounds: darkColors },
380
- ].map((props, i) => {
381
- const isLight = i === 0
382
- const colorsBase = Object.values(props.colors).slice(2)
383
- const [backgrounds, colors] = [
384
- Object.values(props.backgrounds),
385
- isLight ? colorsBase : [colorsBase[0], '#ccc', ...colorsBase.slice(1)],
386
- ] as const
387
-
388
- const scheme = isLight ? 'light' : 'dark'
389
- const shift = isLight ? 0 : 1
390
-
391
- // if (!isLight) {
392
- // const [_, h, sp, lp] = backgrounds[0].match(/hsl\(([0-9\.]+), ([0-9\.]+)\%, ([0-9\.]+)\%\)/)!
393
- // backgrounds.unshift(`hsl(${h}, ${sp}%, ${parseFloat(lp) / 2}%)`)
394
- // }
395
-
396
- const themeWithAlts = createThemesFrom(name, themeCreator, {
397
- // @ts-ignore
398
- colors,
399
- // @ts-ignore
400
- backgrounds,
401
- // @ts-ignore
402
- borderColors: backgrounds,
403
- isLight,
404
- shift,
405
- isBase: false,
406
- offsets: {
407
- background: isLight ? [2, 2, 2, 2, 2, 2] : null,
408
- borderColor: isLight ? [2, 2, 2, 3, 2] : null,
409
- color: isLight ? [0, 0, -1, -2, -3, -3, -4] : [-1, -1, -1, -1, -1, -1],
410
- },
411
- })
412
-
413
- return Object.entries(themeWithAlts).map(([k, v]) => [`${scheme}_${k}`, v])
414
- })
415
- return [...altLightThemes, ...altDarkThemes]
37
+ colorsLight: colorTokens.light,
38
+ colorsDark: colorTokens.dark,
416
39
  })
417
-
418
- type TypedColorTheme = typeof lightThemes.light_alt1
419
- type TypedTheme = typeof light
420
- type TypedThemes = {
421
- [key in keyof typeof allThemes]: TypedTheme
422
- }
423
-
424
- const colorThemes: {
425
- [key in ColorThemeNames]: TypedColorTheme
426
- } = Object.fromEntries(colorThemeEntries) as any
427
-
428
- // add in base _active themes
429
- baseThemes.dark_active = {
430
- ...colorThemes.dark_blue_alt2,
431
- background: darkColors.blue10,
432
- backgroundHover: darkColors.blue11,
433
- backgroundPress: darkColors.blue9,
434
- backgroundFocus: darkColors.blue9,
435
- color: darkColors.blue12,
436
- colorHover: darkColors.blue12,
437
- colorPress: darkColors.blue12,
438
- colorFocus: darkColors.blue12,
439
- }
440
-
441
- // light active uses light foregound, ios style
442
- baseThemes.light_active = baseThemes.dark_active
443
-
444
- const allThemes = {
445
- ...baseThemes,
446
- ...colorThemes,
447
- } as const
448
-
449
- export const themes: TypedThemes = allThemes as any
package/src/tokens.tsx CHANGED
@@ -88,7 +88,7 @@ export const zIndex = {
88
88
  5: 500,
89
89
  }
90
90
 
91
- const colorTokens = {
91
+ export const colorTokens = {
92
92
  light: {
93
93
  blue: blue,
94
94
  gray: gray,