forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StyleUtils.js
400 lines (371 loc) · 10.9 KB
/
StyleUtils.js
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import _ from 'underscore';
import CONST from '../CONST';
import fontFamily from './fontFamily';
import themeColors from './themes/default';
import variables from './variables';
import colors from './colors';
import positioning from './utilities/positioning';
import styles from './styles';
/**
* Takes safe area insets and returns padding to use for a View
*
* @param {Object} insets
* @returns {Object}
*/
function getSafeAreaPadding(insets) {
return {
paddingTop: insets.top,
paddingBottom: insets.bottom * variables.safeInsertPercentage,
paddingLeft: insets.left * variables.safeInsertPercentage,
paddingRight: insets.right * variables.safeInsertPercentage,
};
}
/**
* Takes safe area insets and returns margin to use for a View
*
* @param {Object} insets
* @returns {Object}
*/
function getSafeAreaMargins(insets) {
return {marginBottom: insets.bottom * variables.safeInsertPercentage};
}
/**
* Return navigation menu styles.
*
* @param {Boolean} isSmallScreenWidth
* @returns {Object}
*/
function getNavigationDrawerStyle(isSmallScreenWidth) {
return isSmallScreenWidth
? {
width: '100%',
height: '100%',
borderColor: themeColors.border,
}
: {
height: '100%',
width: variables.sideBarWidth,
borderRightColor: themeColors.border,
};
}
function getNavigationDrawerType(isSmallScreenWidth) {
return isSmallScreenWidth ? 'slide' : 'permanent';
}
function getNavigationModalCardStyle(isSmallScreenWidth) {
return {
position: 'absolute',
top: 0,
right: 0,
width: isSmallScreenWidth ? '100%' : variables.sideBarWidth,
backgroundColor: 'transparent',
height: '100%',
};
}
/**
* @param {Boolean} isZoomed
* @param {Boolean} isDragging
* @return {Object}
*/
function getZoomCursorStyle(isZoomed, isDragging) {
if (!isZoomed) {
return {cursor: 'zoom-in'};
}
return {
cursor: isDragging ? 'grabbing' : 'zoom-out',
};
}
/**
* @param {Boolean} isZoomed
* @param {Number} imgWidth
* @param {Number} imgHeight
* @param {Number} zoomScale
* @param {Number} containerHeight
* @param {Number} containerWidth
* @return {Object}
*/
function getZoomSizingStyle(isZoomed, imgWidth, imgHeight, zoomScale, containerHeight, containerWidth) {
if (imgWidth === 0 || imgHeight === 0) {
return {
height: isZoomed ? '250%' : '100%',
width: isZoomed ? '250%' : '100%',
};
}
const top = `${Math.max((containerHeight - imgHeight) / 2, 0)}px`;
const left = `${Math.max((containerWidth - imgWidth) / 2, 0)}px`;
// Return different size and offset style based on zoomScale and isZoom.
if (isZoomed) {
// When both width and height are smaller than container(modal) size, set the height by multiplying zoomScale if it is zoomed in.
if (zoomScale > 1) {
return {
height: `${imgHeight * zoomScale}px`,
width: `${imgWidth * zoomScale}px`,
};
}
// If image height and width are bigger than container size, display image with original size because original size is bigger and position absolute.
return {
height: `${imgHeight}px`,
width: `${imgWidth}px`,
top,
left,
};
}
// If image is not zoomed in and image size is smaller than container size, display with original size based on offset and position absolute.
if (zoomScale > 1) {
return {
height: `${imgHeight}px`,
width: `${imgWidth}px`,
top,
left,
};
}
// If image is bigger than container size, display full image in the screen with scaled size (fit by container size) and position absolute.
// top, left offset should be different when displaying long or wide image.
const scaledTop = `${Math.max((containerHeight - (imgHeight * zoomScale)) / 2, 0)}px`;
const scaledLeft = `${Math.max((containerWidth - (imgWidth * zoomScale)) / 2, 0)}px`;
return {
height: `${imgHeight * zoomScale}px`,
width: `${imgWidth * zoomScale}px`,
top: scaledTop,
left: scaledLeft,
};
}
/**
* Returns auto grow text input style
*
* @param {Number} width
* @return {Object}
*/
function getAutoGrowTextInputStyle(width) {
return {
minWidth: 5,
width,
};
}
/**
* Returns a style with backgroundColor and borderColor set to the same color
*
* @param {String} backgroundColor
* @returns {Object}
*/
function getBackgroundAndBorderStyle(backgroundColor) {
return {
backgroundColor,
borderColor: backgroundColor,
};
}
/**
* Returns a style with the specified backgroundColor
*
* @param {String} backgroundColor
* @returns {Object}
*/
function getBackgroundColorStyle(backgroundColor) {
return {
backgroundColor,
};
}
/**
* Generate a style for the background color of the Badge
*
* @param {Boolean} success
* @param {Boolean} error
* @param {boolean} [isPressed=false]
* @return {Object}
*/
function getBadgeColorStyle(success, error, isPressed = false) {
if (success) {
return isPressed ? styles.badgeSuccessPressed : styles.badgeSuccess;
}
if (error) {
return isPressed ? styles.badgeDangerPressed : styles.badgeDanger;
}
return {};
}
/**
* Generate a style for the background color of the button, based on its current state.
*
* @param {String} [buttonState] - One of {'default', 'hovered', 'pressed'}
* @returns {Object}
*/
function getButtonBackgroundColorStyle(buttonState = CONST.BUTTON_STATES.DEFAULT) {
switch (buttonState) {
case CONST.BUTTON_STATES.ACTIVE:
return {backgroundColor: themeColors.buttonHoveredBG};
case CONST.BUTTON_STATES.PRESSED:
return {backgroundColor: themeColors.buttonPressedBG};
case CONST.BUTTON_STATES.DISABLED:
case CONST.BUTTON_STATES.DEFAULT:
default:
return {};
}
}
/**
* Generate fill color of an icon based on its state.
*
* @param {String} [buttonState] - One of {'default', 'hovered', 'pressed'}
* @returns {Object}
*/
function getIconFillColor(buttonState = CONST.BUTTON_STATES.DEFAULT) {
switch (buttonState) {
case CONST.BUTTON_STATES.ACTIVE:
return themeColors.text;
case CONST.BUTTON_STATES.PRESSED:
return themeColors.heading;
case CONST.BUTTON_STATES.COMPLETE:
return themeColors.iconSuccessFill;
case CONST.BUTTON_STATES.DEFAULT:
case CONST.BUTTON_STATES.DISABLED:
default:
return themeColors.icon;
}
}
/**
* @param {Animated.Value} rotate
* @param {Animated.Value} backgroundColor
* @returns {Object}
*/
function getAnimatedFABStyle(rotate, backgroundColor) {
return {
transform: [{rotate}],
backgroundColor,
};
}
/**
* @param {Number} width
* @param {Number} height
* @returns {Object}
*/
function getWidthAndHeightStyle(width, height) {
return {
width,
height,
};
}
/**
* @param {Object} params
* @returns {Object}
*/
function getModalPaddingStyles({
shouldAddBottomSafeAreaPadding,
shouldAddTopSafeAreaPadding,
safeAreaPaddingTop,
safeAreaPaddingBottom,
safeAreaPaddingLeft,
safeAreaPaddingRight,
modalContainerStylePaddingTop,
modalContainerStylePaddingBottom,
}) {
return {
paddingTop: shouldAddTopSafeAreaPadding
? (modalContainerStylePaddingTop || 0) + safeAreaPaddingTop
: modalContainerStylePaddingTop || 0,
paddingBottom: shouldAddBottomSafeAreaPadding
? (modalContainerStylePaddingBottom || 0) + safeAreaPaddingBottom
: modalContainerStylePaddingBottom || 0,
paddingLeft: safeAreaPaddingLeft || 0,
paddingRight: safeAreaPaddingRight || 0,
};
}
/**
* Takes fontStyle and fontWeight and returns the correct fontFamily
*
* @param {Object} params
* @returns {String}
*/
function getFontFamilyMonospace({fontStyle, fontWeight}) {
const italic = fontStyle === 'italic' && fontFamily.MONOSPACE_ITALIC;
const bold = fontWeight === 'bold' && fontFamily.MONOSPACE_BOLD;
const italicBold = italic && bold && fontFamily.MONOSPACE_BOLD_ITALIC;
return italicBold || bold || italic || fontFamily.MONOSPACE;
}
/**
* Gives the width for Emoji picker Widget
*
* @param {Boolean} isSmallScreenWidth
* @returns {String}
*/
function getEmojiPickerStyle(isSmallScreenWidth) {
return {
width: isSmallScreenWidth ? '100%' : CONST.EMOJI_PICKER_SIZE,
};
}
/**
* Get the random promo color and image for Login page
*
* @return {Object}
*/
function getLoginPagePromoStyle() {
const promos = [
{
backgroundColor: colors.green,
backgroundImageUri: `${CONST.CLOUDFRONT_URL}/images/homepage/brand-stories/freeplan_green.svg`,
},
{
backgroundColor: colors.orange,
backgroundImageUri: `${CONST.CLOUDFRONT_URL}/images/homepage/brand-stories/freeplan_orange.svg`,
},
{
backgroundColor: colors.pink,
backgroundImageUri: `${CONST.CLOUDFRONT_URL}/images/homepage/brand-stories/freeplan_pink.svg`,
},
{
backgroundColor: colors.blue,
backgroundImageUri: `${CONST.CLOUDFRONT_URL}/images/homepage/brand-stories/freeplan_blue.svg`,
},
];
return promos[_.random(0, 3)];
}
/**
* Generate the styles for the ReportActionItem wrapper view.
*
* @param {Boolean} [isHovered]
* @returns {Object}
*/
function getReportActionItemStyle(isHovered = false) {
return {
display: 'flex',
justifyContent: 'space-between',
backgroundColor: isHovered
? themeColors.hoverComponentBG
// Warning: Setting this to a non-transparent color will cause unread indicator to break on Android
: colors.transparent,
cursor: 'default',
};
}
/**
* Generate the wrapper styles for the mini ReportActionContextMenu.
*
* @param {Boolean} isReportActionItemGrouped
* @returns {Object}
*/
function getMiniReportActionContextMenuWrapperStyle(isReportActionItemGrouped) {
return {
...(isReportActionItemGrouped ? positioning.tn8 : positioning.tn4),
...positioning.r4,
position: 'absolute',
zIndex: 1,
};
}
export {
getSafeAreaPadding,
getSafeAreaMargins,
getNavigationDrawerStyle,
getNavigationDrawerType,
getNavigationModalCardStyle,
getZoomCursorStyle,
getZoomSizingStyle,
getAutoGrowTextInputStyle,
getBackgroundAndBorderStyle,
getBackgroundColorStyle,
getBadgeColorStyle,
getButtonBackgroundColorStyle,
getIconFillColor,
getAnimatedFABStyle,
getWidthAndHeightStyle,
getModalPaddingStyles,
getFontFamilyMonospace,
getEmojiPickerStyle,
getLoginPagePromoStyle,
getReportActionItemStyle,
getMiniReportActionContextMenuWrapperStyle,
};