-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicStyleSheet.ts
271 lines (256 loc) · 8.52 KB
/
DynamicStyleSheet.ts
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
import { useMemo } from 'react';
import {
ImageStyle,
StyleSheet,
TextStyle,
ViewStyle,
useWindowDimensions,
} from 'react-native';
type DeepRecord = Record<string, Record<string, any>>;
type FunctionType = (...args: any) => any;
type FunctionStyleParams = {
width: number;
height: number;
isExtraSmallAndUp: boolean;
isExtraSmall: boolean;
isSmallAndUp: boolean;
isSmall: boolean;
isSmallAndDown: boolean;
isMediumAndUp: boolean;
isMedium: boolean;
isMediumAndDown: boolean;
isLargeAndUp: boolean;
isLarge: boolean;
isLargeAndDown: boolean;
isExtraLargeAndUp: boolean;
isExtraLarge: boolean;
isExtraLargeAndDown: boolean;
isExtraExtraLarge: boolean;
isExtraExtraLargeAndDown: boolean;
};
type FunctionStyle<T> = (params: FunctionStyleParams) => T | undefined;
type BreakpointStyle<
XS = any,
S = any,
M = any,
L = any,
XL = any,
XXL = any,
> = { xs?: XS; s?: S; m?: M; l?: L; xl?: XL; xxl?: XXL };
type ContainNumber = number | string | undefined;
type DynamicNumberStyle<T> = T extends undefined
? undefined
: T extends number
? number | [number, number] | FunctionStyle<T>
: T | FunctionStyle<T>;
type DynamicStyle<T> = {
[P in keyof T]: T[P] extends ContainNumber
?
| DynamicNumberStyle<T[P]>
| BreakpointStyle<T[P], T[P], T[P], T[P], T[P], T[P]>
:
| T[P]
| BreakpointStyle<T[P], T[P], T[P], T[P], T[P], T[P]>
| FunctionStyle<T[P]>;
};
type DynamicNamedStyles<T> = {
[P in keyof T]:
| DynamicStyle<ViewStyle>
| BreakpointStyle<
ViewStyle,
ViewStyle,
ViewStyle,
ViewStyle,
ViewStyle,
ViewStyle
>
| DynamicStyle<TextStyle>
| BreakpointStyle<
TextStyle,
TextStyle,
TextStyle,
TextStyle,
TextStyle,
TextStyle
>
| DynamicStyle<ImageStyle>
| BreakpointStyle<
ImageStyle,
ImageStyle,
ImageStyle,
ImageStyle,
ImageStyle,
ImageStyle
>;
};
type NamedStyles<T> = { [P in keyof T]: ViewStyle | TextStyle | ImageStyle };
type StaticBreakpointStyle<T extends BreakpointStyle> =
| (T extends { xs?: any } ? T['xs'] : undefined)
| (T extends { s?: any } ? T['s'] : undefined)
| (T extends { m?: any } ? T['m'] : undefined)
| (T extends { l?: any } ? T['l'] : undefined)
| (T extends { xl?: any } ? T['xl'] : undefined)
| (T extends { xxl?: any } ? T['xxl'] : undefined);
type StaticNamedStyles<T> = {
[P in keyof T]: T[P] extends BreakpointStyle
?
| StaticBreakpointStyle<T[P]>
| Omit<T[P], 'xs' | 's' | 'm' | 'l' | 'xl' | 'xxl'>
: {
[S in keyof T[P]]: T[P][S] extends [number, number]
? number
: T[P][S] extends FunctionType
? ReturnType<T[P][S]>
: T[P][S] extends BreakpointStyle
? StaticBreakpointStyle<T[P][S]>
: T[P][S];
};
};
const DynamicStyleSheet = {
create: <T extends DynamicNamedStyles<T>>(
styles: T | DynamicNamedStyles<T>,
): T => {
return styles as T;
},
getRelativeValue: (
minValue: number,
minRelativeValue: number,
maxValue: number,
maxRelativeValue: number,
value: number,
) => {
if (value <= minValue) {
return minRelativeValue;
} else if (value >= maxValue) {
return maxRelativeValue;
} else {
if (minRelativeValue < maxRelativeValue) {
return (
minRelativeValue +
(value - minValue) *
((maxRelativeValue - minRelativeValue) /
(maxValue - minValue))
);
} else {
return (
minRelativeValue -
(value - minValue) *
((minRelativeValue - maxRelativeValue) /
(maxValue - minValue))
);
}
}
},
getBreakpointStyle: (
style: Record<string, any>,
breakpointsParams: FunctionStyleParams,
) => {
let breakpointStyle: any;
if ('xs' in style && breakpointsParams.isExtraSmallAndUp) {
breakpointStyle = style.xs;
}
if ('s' in style && breakpointsParams.isSmallAndUp) {
breakpointStyle = style.s;
}
if ('m' in style && breakpointsParams.isMediumAndUp) {
breakpointStyle = style.m;
}
if ('l' in style && breakpointsParams.isLargeAndUp) {
breakpointStyle = style.l;
}
if ('xl' in style && breakpointsParams.isExtraLargeAndUp) {
breakpointStyle = style.xl;
}
if ('xxl' in style && breakpointsParams.isExtraExtraLarge) {
breakpointStyle = style.xxl;
}
return breakpointStyle;
},
};
const useBreakpoints = (): FunctionStyleParams => {
const { width, height } = useWindowDimensions();
const isExtraSmallAndUp = width >= 0;
const isExtraSmall = width < 576;
const isSmallAndUp = width >= 576;
const isSmall = width >= 576 && width < 768;
const isSmallAndDown = width < 768;
const isMediumAndUp = width >= 768;
const isMedium = width >= 768 && width < 992;
const isMediumAndDown = width < 992;
const isLargeAndUp = width >= 992;
const isLarge = width >= 992 && width < 1200;
const isLargeAndDown = width < 1200;
const isExtraLargeAndUp = width >= 1200;
const isExtraLarge = width >= 1200 && width < 1400;
const isExtraLargeAndDown = width < 1400;
const isExtraExtraLarge = width >= 1400;
const isExtraExtraLargeAndDown = width >= 1400 || width < 1400;
return {
width,
height,
isExtraSmallAndUp,
isExtraSmall,
isSmallAndUp,
isSmall,
isSmallAndDown,
isMediumAndUp,
isMedium,
isMediumAndDown,
isLargeAndUp,
isLarge,
isLargeAndDown,
isExtraLargeAndUp,
isExtraLarge,
isExtraLargeAndDown,
isExtraExtraLarge,
isExtraExtraLargeAndDown,
};
};
const useDynamicStyles = <T extends DynamicNamedStyles<T>>(
dynamicStyles: T | DynamicNamedStyles<T>,
params?: { minWidth: number; maxWidth: number },
): StaticNamedStyles<T> => {
const breakpointsParams = useBreakpoints();
return useMemo(() => {
const styles: DeepRecord = {};
const dynamicStylesRecord = dynamicStyles as DeepRecord;
for (const styleKey of Object.keys(dynamicStyles)) {
const breakpointStyle = DynamicStyleSheet.getBreakpointStyle(
dynamicStylesRecord[styleKey],
breakpointsParams,
);
styles[styleKey] = { ...breakpointStyle };
for (const styleProperty of Object.keys(
dynamicStylesRecord[styleKey],
)) {
const styleValue = dynamicStylesRecord[styleKey][styleProperty];
if (Array.isArray(styleValue)) {
if (styleValue.length >= 2 && params) {
styles[styleKey][styleProperty] =
DynamicStyleSheet.getRelativeValue(
params.minWidth,
styleValue[0],
params.maxWidth,
styleValue[1],
breakpointsParams.width,
);
}
} else if (typeof styleValue === 'function') {
styles[styleKey][styleProperty] =
styleValue(breakpointsParams);
} else if (typeof styleValue === 'object') {
styles[styleKey][styleProperty] =
DynamicStyleSheet.getBreakpointStyle(
styleValue,
breakpointsParams,
);
} else {
styles[styleKey][styleProperty] = styleValue;
}
}
}
return StyleSheet.create(styles as NamedStyles<T>);
}, [dynamicStyles, params, breakpointsParams]);
};
export default DynamicStyleSheet;
export { useDynamicStyles, useBreakpoints };