forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.tsx
168 lines (159 loc) · 5.08 KB
/
shared.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
import {themed as globalColors} from './colors'
import {isMobile, isIOS, isAndroid, isTablet, isPhone, isElectron} from '@/constants/platform'
import type {_StylesCrossPlatform, _StylesMobile, _StylesDesktop} from './css'
import type {Background} from '@/common-adapters/text'
/* eslint-disable sort-keys */
export const globalMargins = {
xxtiny: 2,
xtiny: 4,
tiny: 8,
xsmall: 12,
small: 16,
medium: 24,
mediumLarge: 32,
large: 40,
xlarge: 64,
} as const
/* eslint-enable sort-keys */
export const backgroundModeToColor = {
get Announcements() {
return globalColors.blue
},
get Documentation() {
return globalColors.blueDarker
},
get HighRisk() {
return globalColors.red
},
get Information() {
return globalColors.yellow
},
get Normal() {
return globalColors.white
},
get Success() {
return globalColors.green
},
get Terminal() {
return globalColors.blueDarker2
},
}
export const backgroundModeToTextColor = (backgroundMode: Background) => {
switch (backgroundMode) {
case 'Information':
return globalColors.brown_75
case 'Normal':
return globalColors.black
case 'Terminal':
return globalColors.blueLighter
default:
return globalColors.white
}
}
const flexCommon = isMobile ? {} : ({display: 'flex'} as const)
export const util = {
fillAbsolute: {bottom: 0, left: 0, position: 'absolute', right: 0, top: 0},
flexBoxCenter: {...flexCommon, alignItems: 'center', justifyContent: 'center'},
flexBoxColumn: {...flexCommon, flexDirection: 'column'},
flexBoxColumnReverse: {...flexCommon, flexDirection: 'column-reverse'},
flexBoxRow: {...flexCommon, flexDirection: 'row'},
flexBoxRowReverse: {...flexCommon, flexDirection: 'row-reverse'},
flexGrow: {flexGrow: 1},
flexOne: {flex: 1},
flexWrap: {flexWrap: 'wrap'},
fullHeight: {height: '100%'},
fullWidth: {width: '100%'},
opacity0: {opacity: 0},
positionRelative: {position: 'relative'},
rounded: {borderRadius: 3},
} as const
// Take common styles and make them work on both. Deals with special cases in lineHeight and etc
type Unified<T> = {
[P in keyof T]: P extends 'lineHeight' ? _StylesCrossPlatform[P] : T[P]
}
function unifyStyles<T extends {}>(s_: T): Unified<T> {
const s: {[key: string]: unknown} = s_
return {
...s,
...(Object.hasOwn(s, 'lineHeight') && typeof s['lineHeight'] === 'number'
? ({
lineHeight: isMobile ? s['lineHeight'] : s['lineHeight'] === 0 ? '0' : `${s['lineHeight']}px`,
} as const)
: {}),
} as Unified<T>
}
// This is a better literal to literal inferrer but is too slow
// type Both<X, Y> = {
// [P in keyof X | keyof Y]: P extends keyof X
// ? P extends keyof Y
// ? X[P] | Y[P] // both
// : X[P]
// : P extends keyof Y
// ? Y[P]
// : undefined
// }
// type AsObj<T> = T extends object ? T : never
// export function platformStyles<
// T extends {
// common: any
// isMobile?: any
// isPhone?: any
// isTablet?: any
// isIOS?: any
// isAndroid?: any
// isElectron?: any
// },
// C = T extends {common: infer J} ? J : never,
// M = T extends {isMobile: infer J} ? J : never,
// P = T extends {isPhone: infer J} ? J : never,
// Tab = T extends {isTablet: infer J} ? J : never,
// A = T extends {isAndroid: infer J} ? J : never,
// I = T extends {isIOS: infer J} ? J : never,
// E = T extends {isElectron: infer J} ? J : never,
// Elec = Util.Assign<AsObj<C>, AsObj<E>>,
// Mob = Util.Assign<
// Util.Assign<Util.Assign<Util.Assign<Util.Assign<AsObj<C>, AsObj<I>>, AsObj<M>>, AsObj<P>>, AsObj<Tab>>,
// AsObj<A>
// >,
// OUT = Both<Elec, Mob>
// >(o: T): OUT {
// return {
// ...(o.common ? unifyStyles(o.common) : {}),
// ...(isMobile && o.isMobile ? o.isMobile : {}),
// ...(isIOS && o.isIOS ? o.isIOS : {}),
// ...(isAndroid && o.isAndroid ? o.isAndroid : {}),
// ...(isPhone && o.isPhone ? o.isPhone : {}),
// ...(isTablet && o.isTablet ? o.isTablet : {}),
// ...(isElectron && o.isElectron ? unifyStyles(o.isElectron) : {}),
// } as OUT
// }
export function platformStyles<
T extends {
common?: _StylesCrossPlatform
isMobile?: _StylesMobile
isPhone?: _StylesMobile
isTablet?: _StylesMobile
isIOS?: _StylesMobile
isAndroid?: _StylesMobile
isElectron?: _StylesDesktop
},
OUT = _StylesCrossPlatform,
>(o: T): OUT {
return {
...(o.common ? unifyStyles(o.common) : {}),
...(isMobile && o.isMobile ? o.isMobile : {}),
...(isIOS && o.isIOS ? o.isIOS : {}),
...(isAndroid && o.isAndroid ? o.isAndroid : {}),
...(isPhone && o.isPhone ? o.isPhone : {}),
...(isTablet && o.isTablet ? o.isTablet : {}),
...(isElectron && o.isElectron ? unifyStyles(o.isElectron) : {}),
} as OUT
}
/* eslint-disable sort-keys */
export const padding = (top: number, right?: number, bottom?: number, left?: number) => ({
paddingTop: top,
paddingRight: right !== undefined ? right : top,
paddingBottom: bottom !== undefined ? bottom : top,
paddingLeft: left !== undefined ? left : right !== undefined ? right : top,
})
/* eslint-enable sort-keys */