@tamagui/theme-base 1.0.1-beta.99 → 1.0.1-rc.0

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,220 +1,24 @@
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, tokens } from './tokens'
3
+ import { colorTokens } from './tokens'
5
4
 
6
- const { color } = tokens
7
-
8
- // spaghetti ahead
9
-
10
- type ThemeCreatorProps = {
11
- shift?: number
12
- backgrounds: (string | Variable)[]
13
- backgroundStrong?: string | Variable
14
- borderColors?: (string | Variable)[]
15
- colors?: (string | Variable)[]
16
- offsets?: {
17
- color?: number[] | null
18
- background?: number[] | null
19
- borderColor?: number[] | null
20
- }
21
- isLight?: boolean
22
- isBase?: boolean
23
- }
24
-
25
- // helpers
26
-
27
- const alternates = [1, 2, 3, 4] as const
28
- const alts = [1, 2, 3] as const
29
- type AltKeys = 1 | 2 | 3
30
- type AltName<Name extends string, Keys extends string | number> = `${Name}_alt${Keys}`
31
- type ThemeCreator<A = any> = (str: number, props: ThemeCreatorProps) => A
32
-
33
- function createThemesFrom<Name extends string, GetTheme extends ThemeCreator = ThemeCreator>(
34
- name: Name,
35
- getTheme: GetTheme,
36
- props: ThemeCreatorProps
37
- ): {
38
- [key in `${Name}_alt${AltKeys}` | `${Name}` | `${Name}_darker`]: GetTheme extends ThemeCreator<
39
- infer Theme
40
- >
41
- ? Theme
42
- : never
43
- } {
44
- const { shift = 0 } = props
45
- const theme = getTheme(0 + shift, props)
46
- let themeEntries: any[] = [[name, theme]]
47
-
48
- // generate alternates (for use in other themes), but keep just alts
49
- const altThemes: any[] = alternates.map((alt) => [
50
- `${name}_alt${alt}`,
51
- getTheme(alt + (props.isLight ? 0 : 0) + shift, props),
52
- ])
53
- const altThemes2: any[] = alternates.map((alt) => [
54
- `${name}_alt${alt}`,
55
- getTheme(alt + shift, props),
56
- ])
57
- const altButtonThemes: any[] = alternates.map((_, i) => {
58
- const [bName, bTheme] = [altThemes2[i][0], (altThemes2[i + 1] || altThemes2[i])[1]]
59
- return [`${bName}_Button` as any, bTheme]
60
- })
61
- // add these after alts since we rely on positioning
62
- const darkerTheme = getTheme(Math.max(0, shift + (props.isLight ? 1 : -1)), props)
63
- const activeTheme = makeActiveTheme(theme)
64
-
65
- // TODO can be de-duped
66
- const inverted = altThemes.map(([_name, theme]) => {
67
- return {
68
- ...theme,
69
- background: theme.color,
70
- backgroundHover: theme.colorHover,
71
- backgroundFocus: theme.colorFocus,
72
- backgroundPress: theme.colorPress,
73
- color: theme.background,
74
- colorHover: theme.backgroundHover,
75
- colorFocus: theme.backgroundFocus,
76
- colorPress: theme.backgroundPress,
77
- }
78
- })
79
-
80
- themeEntries = [
81
- ...themeEntries,
82
- // keep just alts
83
- ...altThemes.slice(0, alts.length),
84
- ...altButtonThemes.slice(0, alts.length),
85
- [`${name}_Button`, altThemes2[1][1]],
86
- [`${name}_DrawerFrame`, altThemes2[1][1]],
87
- [`${name}_SliderTrack`, altThemes[0][1]],
88
- [`${name}_SliderTrackActive`, altThemes[3][1]],
89
- [`${name}_SliderThumb`, inverted[2]],
90
- [`${name}_Progress`, altThemes[2][1]],
91
- [`${name}_ProgressIndicator`, inverted[2]],
92
- [`${name}_Switch`, altThemes[0][1]],
93
- [`${name}_SwitchThumb`, inverted[2]],
94
- [`${name}_TooltipArrow`, altThemes[1][1]],
95
- [`${name}_TooltipContent`, altThemes[1][1]],
96
- [`${name}_darker`, darkerTheme],
97
- [`${name}_active`, activeTheme],
98
- ]
99
- const themes = Object.fromEntries(themeEntries)
100
- // if (props.isLight) {
101
- // console.log('themes', themes)
102
- // }
103
- return themes as any
104
- }
105
-
106
- const themeCreator = (
107
- str = 1,
108
- {
109
- backgrounds,
110
- isLight = true,
111
- isBase = false,
112
- colors = [...backgrounds].reverse(),
113
- borderColors = isLight ? colors : backgrounds,
114
- backgroundStrong,
115
- offsets: offsetsProp,
116
- }: ThemeCreatorProps
117
- ) => {
118
- const offsets = {
119
- borderColor: offsetsProp?.borderColor ?? offsetsProp?.background ?? [0, 0, 0, 0],
120
- background: offsetsProp?.background ?? [0, 0, 0, 0],
121
- color: offsetsProp?.color ?? [0, 0, 0, 0],
122
- }
123
- const darkColors = isLight ? colors : backgrounds
124
- const lighterDir = isLight ? -1 : 1
125
- const darkerDir = -lighterDir
126
- const strongerDir = isLight ? darkerDir : lighterDir
127
- const softerDir = -strongerDir
128
- const get = (arr: any[], index: number, name = 'background') => {
129
- return arr[Math.max(0, Math.min(index + (offsets[name][str] || 0), arr.length - 1))]
130
- }
131
-
132
- const colorTranslucent = setColorAlpha(getVariableValue(get(colors, 0 + str, 'color')), 0.5)
133
-
134
- let theme = {
135
- background: get(backgrounds, str),
136
- backgroundStrong: backgroundStrong || get(backgrounds, str + strongerDir * 2),
137
- backgroundSoft: get(backgrounds, str + softerDir * 2),
138
- backgroundHover: get(backgrounds, str + lighterDir),
139
- backgroundPress: get(backgrounds, str + darkerDir),
140
- backgroundFocus: get(backgrounds, str + darkerDir * 2),
141
- backgroundTransparent: color.grayA1Light,
142
- color: get(colors, 0 + str, 'color'),
143
- colorHover: get(colors, 1 + str, 'color'),
144
- colorPress: get(colors, 2 + str, 'color'),
145
- colorFocus: get(colors, 3 + str, 'color'),
146
- colorTranslucent,
147
- colorMid: (isLight ? colors : backgrounds)[Math.floor(colors.length / 2)],
148
- shadowColor: isLight ? color.grayA2Light : color.grayA8Light,
149
- shadowColorHover: darkColors[!isLight ? 1 : 8],
150
- shadowColorPress: darkColors[!isLight ? 1 : 8],
151
- shadowColorFocus: darkColors[!isLight ? 1 : 8],
152
- borderColor: null as any as string | Variable,
153
- borderColorHover: null as any as string | Variable,
154
- borderColorPress: null as any as string | Variable,
155
- borderColorFocus: null as any as string | Variable,
156
- }
157
-
158
- if (process.env.ENABLE_STEPS) {
159
- Object.assign(theme, {
160
- step1: backgrounds[0],
161
- step2: backgrounds[1],
162
- step3: backgrounds[2],
163
- step4: backgrounds[3],
164
- step5: backgrounds[4],
165
- step6: backgrounds[5],
166
- step7: backgrounds[6],
167
- step8: backgrounds[7],
168
- step9: backgrounds[8],
169
- step10: backgrounds[9],
170
- step11: backgrounds[10],
171
- step12: backgrounds[11],
172
- })
173
- }
174
-
175
- if (isBase) {
176
- Object.assign(theme, {
177
- borderColor: isLight
178
- ? get(colors, 8 - str, 'borderColor')
179
- : get(backgrounds, 2 + str, 'borderColor'),
180
- borderColorHover: isLight
181
- ? get(colors, 6 - str, 'borderColor')
182
- : get(backgrounds, 3 + str, 'borderColor'),
183
- borderColorPress: isLight
184
- ? get(colors, 5 - str, 'borderColor')
185
- : get(backgrounds, 1 + str, 'borderColor'),
186
- borderColorFocus: isLight
187
- ? get(colors, 4 - str, 'borderColor')
188
- : get(backgrounds, 3 + str, 'borderColor'),
189
- })
190
- } else {
191
- Object.assign(theme, {
192
- borderColor: get(borderColors, 1 + strongerDir, 'borderColor'),
193
- borderColorHover: get(borderColors, 2 + strongerDir, 'borderColor'),
194
- borderColorPress: get(borderColors, 3 + strongerDir, 'borderColor'),
195
- borderColorFocus: get(borderColors, 4 + strongerDir, 'borderColor'),
196
- })
197
- }
198
-
199
- return theme
200
- }
201
-
202
- const themeColors = {
5
+ export const themes = createThemes<keyof typeof colorTokens.light>({
6
+ activeColor: 'blue',
203
7
  light: [
204
8
  '#fff',
205
9
  '#f4f4f4',
206
- color.gray1Light,
207
- color.gray2Light,
208
- color.gray3Light,
209
- color.gray4Light,
210
- color.gray5Light,
211
- color.gray6Light,
212
- color.gray7Light,
213
- color.gray8Light,
214
- color.gray9Light,
215
- color.gray10Light,
216
- color.gray11Light,
217
- color.gray12Light,
10
+ 'hsl(0, 0%, 99.0%)',
11
+ 'hsl(0, 0%, 97.3%)',
12
+ 'hsl(0, 0%, 95.1%)',
13
+ 'hsl(0, 0%, 93.0%)',
14
+ 'hsl(0, 0%, 90.9%)',
15
+ 'hsl(0, 0%, 88.7%)',
16
+ 'hsl(0, 0%, 85.8%)',
17
+ 'hsl(0, 0%, 78.0%)',
18
+ 'hsl(0, 0%, 56.1%)',
19
+ 'hsl(0, 0%, 52.3%)',
20
+ 'hsl(0, 0%, 43.5%)',
21
+ 'hsl(0, 0%, 9.0%)',
218
22
  ],
219
23
  dark: [
220
24
  '#111111',
@@ -230,172 +34,6 @@ const themeColors = {
230
34
  '#626262',
231
35
  '#777777',
232
36
  ],
233
- }
234
-
235
- const lightThemes = createThemesFrom('light', themeCreator, {
236
- backgrounds: themeColors.light,
237
- // for now web non-ios style native is...
238
- backgroundStrong: isWeb ? '#fefefe' : '#f2f2f2',
239
- borderColors: themeColors.light.slice(2),
240
- isLight: true,
241
- offsets: {
242
- background: [2, 2, 2, 2, 2, 2],
243
- },
244
- })
245
-
246
- const darkThemes = createThemesFrom('dark', themeCreator, {
247
- backgrounds: themeColors.dark,
248
- colors: themeColors.light.slice(2),
249
- backgroundStrong: '#070707',
250
- isBase: true,
251
- isLight: false,
252
- shift: 1,
253
- offsets: {
254
- color: [0, 4, 5, 5, 5, 5],
255
- },
256
- })
257
-
258
- darkThemes.dark_darker.background = '#030303'
259
- darkThemes.dark_darker.backgroundStrong = '#000'
260
-
261
- const light = createTheme({
262
- ...lightColors,
263
- ...lightThemes.light,
37
+ colorsLight: colorTokens.light,
38
+ colorsDark: colorTokens.dark,
264
39
  })
265
-
266
- const dark = createTheme({
267
- ...darkColors,
268
- ...darkThemes.dark,
269
- })
270
-
271
- type ThemeBase = {
272
- [Key in keyof typeof dark]: string
273
- }
274
-
275
- const baseThemes = {
276
- // light
277
- ...lightThemes,
278
- light,
279
- light_active: makeActiveTheme(lightThemes.light),
280
- light_Card: lightThemes.light,
281
- light_SliderTrack: lightThemes.light_alt2,
282
- light_SliderTrackActive: lightThemes.light_alt3,
283
- light_Switch: lightThemes.light_alt1,
284
- light_SwitchThumb: darkThemes.dark,
285
- light_DrawerFrame: lightThemes.light_alt1,
286
-
287
- // dark
288
- ...darkThemes,
289
- dark,
290
- dark_active: makeActiveTheme(darkThemes.dark),
291
- dark_Card: darkThemes.dark,
292
- dark_DrawerFrame: darkThemes.dark_alt2,
293
- dark_SliderTrack: darkThemes.dark_darker,
294
- dark_SliderTrackActive: darkThemes.dark_alt3,
295
- dark_Switch: darkThemes.dark_darker,
296
- dark_SwitchThumb: lightThemes.light_alt3,
297
- dark_Button: darkThemes.dark_alt1,
298
- }
299
-
300
- function makeActiveTheme(theme: any) {
301
- const res = { ...theme }
302
- res.backgroundHover = res.background
303
- res.colorHover = res.color
304
- res.backgroundPress = res.background
305
- res.colorPress = res.color
306
- return res
307
- }
308
-
309
- const darkEntries = Object.entries(dark)
310
- const lightEntries = Object.entries(light)
311
- function findColors(prefix: string, dark = false): ThemeBase {
312
- return Object.fromEntries(
313
- (dark ? darkEntries : lightEntries).filter(([k]) => k.startsWith(prefix))
314
- ) as any
315
- }
316
-
317
- export const colorSchemes = [
318
- { name: 'blue', colors: findColors('blue'), darkColors: findColors('blue', true) },
319
- { name: 'gray', colors: findColors('gray'), darkColors: findColors('gray', true) },
320
- { name: 'green', colors: findColors('green'), darkColors: findColors('green', true) },
321
- { name: 'orange', colors: findColors('orange'), darkColors: findColors('orange', true) },
322
- { name: 'pink', colors: findColors('pink'), darkColors: findColors('pink', true) },
323
- { name: 'purple', colors: findColors('purple'), darkColors: findColors('purple', true) },
324
- { name: 'red', colors: findColors('red'), darkColors: findColors('red', true) },
325
- { name: 'yellow', colors: findColors('yellow'), darkColors: findColors('yellow', true) },
326
- { name: 'teal', colors: findColors('teal'), darkColors: findColors('teal', true) },
327
- ] as const
328
-
329
- export type ColorNames = typeof colorSchemes[number]['name']
330
-
331
- // nice and flat
332
- export const colorNames: ColorNames[] = [
333
- 'blue',
334
- 'gray',
335
- 'green',
336
- 'orange',
337
- 'pink',
338
- 'purple',
339
- 'red',
340
- 'yellow',
341
- 'teal',
342
- ]
343
-
344
- type ColorThemeNames =
345
- | ColorNames
346
- | AltName<`light_${ColorNames}`, AltKeys>
347
- | AltName<`dark_${ColorNames}`, AltKeys>
348
-
349
- const colorThemeEntries = colorSchemes.flatMap(({ name, colors, darkColors }) => {
350
- const [altLightThemes, altDarkThemes] = [
351
- { colors: darkColors, backgrounds: colors },
352
- { colors, backgrounds: darkColors },
353
- ].map((props, i) => {
354
- const isLight = i === 0
355
- const colorsBase = Object.values(props.colors).slice(2)
356
- const [backgrounds, colors] = [
357
- Object.values(props.backgrounds),
358
- isLight ? colorsBase : [colorsBase[0], '#ccc', ...colorsBase.slice(1)],
359
- ] as const
360
-
361
- const scheme = isLight ? 'light' : 'dark'
362
- const shift = isLight ? 0 : 1
363
-
364
- // if (!isLight) {
365
- // const [_, h, sp, lp] = backgrounds[0].match(/hsl\(([0-9\.]+), ([0-9\.]+)\%, ([0-9\.]+)\%\)/)!
366
- // backgrounds.unshift(`hsl(${h}, ${sp}%, ${parseFloat(lp) / 2}%)`)
367
- // }
368
-
369
- const themeWithAlts = createThemesFrom(name, themeCreator, {
370
- colors,
371
- backgrounds,
372
- borderColors: backgrounds,
373
- isLight,
374
- shift,
375
- isBase: false,
376
- offsets: {
377
- background: isLight ? [2, 2, 2, 2, 2, 2] : null,
378
- borderColor: isLight ? [2, 2, 2, 3, 2] : null,
379
- color: isLight ? [0, 0, -1, -2, -3, -3, -4] : [-1, -1, -1, -1, -1, -1],
380
- },
381
- })
382
-
383
- return Object.entries(themeWithAlts).map(([k, v]) => [`${scheme}_${k}`, v])
384
- })
385
- return [...altLightThemes, ...altDarkThemes]
386
- })
387
-
388
- type MyThemeBase = typeof baseThemes['light_alt1']
389
-
390
- const colorThemes: {
391
- [key in ColorThemeNames]: MyThemeBase
392
- } = Object.fromEntries(colorThemeEntries) as any
393
-
394
- const allThemes = {
395
- ...baseThemes,
396
- ...colorThemes,
397
- } as const
398
-
399
- export const themes: {
400
- [key in keyof typeof allThemes]: typeof baseThemes['light']
401
- } = allThemes as any
package/src/tokens.tsx CHANGED
@@ -2,9 +2,7 @@ import {
2
2
  blue,
3
3
  blueDark,
4
4
  gray,
5
- grayA,
6
5
  grayDark,
7
- grayDarkA,
8
6
  green,
9
7
  greenDark,
10
8
  orange,
@@ -15,10 +13,6 @@ import {
15
13
  purpleDark,
16
14
  red,
17
15
  redDark,
18
- teal,
19
- tealDark,
20
- violet,
21
- violetDark,
22
16
  yellow,
23
17
  yellowDark,
24
18
  } from '@tamagui/colors'
@@ -36,7 +30,7 @@ import { Variable, createTokens } from '@tamagui/core'
36
30
  // but also one more wrinkle...
37
31
  // space is used in conjunction with size
38
32
  // i'm setting space to generally just a fixed fraction of size (~1/3-2/3 still fine tuning)
39
- const size = {
33
+ export const size = {
40
34
  0: 0,
41
35
  0.25: 2,
42
36
  0.5: 4,
@@ -51,15 +45,10 @@ const size = {
51
45
  true: 44,
52
46
  4.5: 48,
53
47
  5: 52,
54
- 5.5: 59,
55
48
  6: 64,
56
- 6.5: 69,
57
49
  7: 74,
58
- 7.6: 79,
59
50
  8: 84,
60
- 8.5: 89,
61
51
  9: 94,
62
- 9.5: 99,
63
52
  10: 104,
64
53
  11: 124,
65
54
  12: 144,
@@ -83,14 +72,14 @@ const spaces = Object.entries(size).map(([k, v]) => [
83
72
 
84
73
  const spacesNegative = spaces.map(([k, v]) => [`-${k}`, -v])
85
74
 
86
- const space: {
75
+ export const space: {
87
76
  [Key in `-${SizeKeys}` | SizeKeys]: Key extends keyof Sizes ? Sizes[Key] : number
88
77
  } = {
89
78
  ...Object.fromEntries(spaces),
90
79
  ...Object.fromEntries(spacesNegative),
91
80
  } as any
92
81
 
93
- const zIndex = {
82
+ export const zIndex = {
94
83
  0: 0,
95
84
  1: 100,
96
85
  2: 200,
@@ -99,64 +88,52 @@ const zIndex = {
99
88
  5: 500,
100
89
  }
101
90
 
102
- const colorTokens = {
91
+ export const colorTokens = {
103
92
  light: {
104
93
  blue: blue,
105
94
  gray: gray,
106
- grayA: grayA,
107
95
  green: green,
108
96
  orange: orange,
109
97
  pink: pink,
110
98
  purple: purple,
111
99
  red: red,
112
- violet: violet,
113
100
  yellow: yellow,
114
- teal: teal,
115
101
  },
116
102
  dark: {
117
103
  blue: blueDark,
118
104
  gray: grayDark,
119
- grayA: grayDarkA,
120
105
  green: greenDark,
121
106
  orange: orangeDark,
122
107
  pink: pinkDark,
123
108
  purple: purpleDark,
124
109
  red: redDark,
125
- violet: violetDark,
126
110
  yellow: yellowDark,
127
- teal: tealDark,
128
111
  },
129
112
  }
130
113
 
131
114
  export const darkColors = {
132
115
  ...colorTokens.dark.blue,
133
116
  ...colorTokens.dark.gray,
134
- ...colorTokens.dark.grayA,
135
117
  ...colorTokens.dark.green,
136
118
  ...colorTokens.dark.orange,
137
119
  ...colorTokens.dark.pink,
138
120
  ...colorTokens.dark.purple,
139
121
  ...colorTokens.dark.red,
140
- ...colorTokens.dark.violet,
141
122
  ...colorTokens.dark.yellow,
142
- ...colorTokens.dark.teal,
143
123
  }
144
124
 
145
125
  export const lightColors = {
146
126
  ...colorTokens.light.blue,
147
127
  ...colorTokens.light.gray,
148
- ...colorTokens.light.grayA,
149
128
  ...colorTokens.light.green,
150
129
  ...colorTokens.light.orange,
151
130
  ...colorTokens.light.pink,
152
131
  ...colorTokens.light.purple,
153
132
  ...colorTokens.light.red,
154
- ...colorTokens.light.violet,
155
133
  ...colorTokens.light.yellow,
156
- ...colorTokens.light.teal,
157
134
  }
158
135
 
159
- const allColors = {
136
+ export const color = {
160
137
  ...postfixObjKeys(lightColors, 'Light'),
161
138
  ...postfixObjKeys(darkColors, 'Dark'),
162
139
  }
@@ -170,7 +147,7 @@ function postfixObjKeys<A extends { [key: string]: Variable<string> | string },
170
147
  return Object.fromEntries(Object.entries(obj).map(([k, v]) => [`${k}${postfix}`, v])) as any
171
148
  }
172
149
 
173
- const radius = {
150
+ export const radius = {
174
151
  0: 0,
175
152
  1: 3,
176
153
  2: 5,
@@ -187,7 +164,7 @@ const radius = {
187
164
  }
188
165
 
189
166
  export const tokens = createTokens({
190
- color: allColors,
167
+ color,
191
168
  radius,
192
169
  zIndex,
193
170
  space,
File without changes
package/types/index.d.ts CHANGED
File without changes