-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
StyleUtils.js
1365 lines (1249 loc) · 40.2 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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';
import spacing from './utilities/spacing';
import * as UserUtils from '../libs/UserUtils';
import * as Browser from '../libs/Browser';
import cursor from './utilities/cursor';
const workspaceColorOptions = [
{backgroundColor: colors.blue200, fill: colors.blue700},
{backgroundColor: colors.blue400, fill: colors.blue800},
{backgroundColor: colors.blue700, fill: colors.blue200},
{backgroundColor: colors.green200, fill: colors.green700},
{backgroundColor: colors.green400, fill: colors.green800},
{backgroundColor: colors.green700, fill: colors.green200},
{backgroundColor: colors.yellow200, fill: colors.yellow700},
{backgroundColor: colors.yellow400, fill: colors.yellow800},
{backgroundColor: colors.yellow700, fill: colors.yellow200},
{backgroundColor: colors.tangerine200, fill: colors.tangerine700},
{backgroundColor: colors.tangerine400, fill: colors.tangerine800},
{backgroundColor: colors.tangerine700, fill: colors.tangerine400},
{backgroundColor: colors.pink200, fill: colors.pink700},
{backgroundColor: colors.pink400, fill: colors.pink800},
{backgroundColor: colors.pink700, fill: colors.pink200},
{backgroundColor: colors.ice200, fill: colors.ice700},
{backgroundColor: colors.ice400, fill: colors.ice800},
{backgroundColor: colors.ice700, fill: colors.ice200},
];
const avatarBorderSizes = {
[CONST.AVATAR_SIZE.SMALL_SUBSCRIPT]: variables.componentBorderRadiusSmall,
[CONST.AVATAR_SIZE.MID_SUBSCRIPT]: variables.componentBorderRadiusSmall,
[CONST.AVATAR_SIZE.SUBSCRIPT]: variables.componentBorderRadiusMedium,
[CONST.AVATAR_SIZE.SMALLER]: variables.componentBorderRadiusMedium,
[CONST.AVATAR_SIZE.SMALL]: variables.componentBorderRadiusMedium,
[CONST.AVATAR_SIZE.HEADER]: variables.componentBorderRadiusMedium,
[CONST.AVATAR_SIZE.DEFAULT]: variables.componentBorderRadiusNormal,
[CONST.AVATAR_SIZE.MEDIUM]: variables.componentBorderRadiusLarge,
[CONST.AVATAR_SIZE.LARGE]: variables.componentBorderRadiusLarge,
[CONST.AVATAR_SIZE.LARGE_BORDERED]: variables.componentBorderRadiusRounded,
[CONST.AVATAR_SIZE.SMALL_NORMAL]: variables.componentBorderRadiusMedium,
};
const avatarSizes = {
[CONST.AVATAR_SIZE.DEFAULT]: variables.avatarSizeNormal,
[CONST.AVATAR_SIZE.SMALL_SUBSCRIPT]: variables.avatarSizeSmallSubscript,
[CONST.AVATAR_SIZE.MID_SUBSCRIPT]: variables.avatarSizeMidSubscript,
[CONST.AVATAR_SIZE.SUBSCRIPT]: variables.avatarSizeSubscript,
[CONST.AVATAR_SIZE.SMALL]: variables.avatarSizeSmall,
[CONST.AVATAR_SIZE.SMALLER]: variables.avatarSizeSmaller,
[CONST.AVATAR_SIZE.LARGE]: variables.avatarSizeLarge,
[CONST.AVATAR_SIZE.MEDIUM]: variables.avatarSizeMedium,
[CONST.AVATAR_SIZE.LARGE_BORDERED]: variables.avatarSizeLargeBordered,
[CONST.AVATAR_SIZE.HEADER]: variables.avatarSizeHeader,
[CONST.AVATAR_SIZE.MENTION_ICON]: variables.avatarSizeMentionIcon,
[CONST.AVATAR_SIZE.SMALL_NORMAL]: variables.avatarSizeSmallNormal,
};
const emptyAvatarStyles = {
[CONST.AVATAR_SIZE.SMALL]: styles.emptyAvatarSmall,
[CONST.AVATAR_SIZE.MEDIUM]: styles.emptyAvatarMedium,
[CONST.AVATAR_SIZE.LARGE]: styles.emptyAvatarLarge,
};
/**
* Return the style size from an avatar size constant
*
* @param {String} size
* @returns {Number}
*/
function getAvatarSize(size) {
return avatarSizes[size];
}
/**
* Return the height of magic code input container
*
* @returns {Object}
*/
function getHeightOfMagicCodeInput() {
return {height: styles.magicCodeInputContainer.minHeight - styles.textInputContainer.borderBottomWidth};
}
/**
* Return the style from an empty avatar size constant
*
* @param {String} size
* @returns {Object}
*/
function getEmptyAvatarStyle(size) {
return emptyAvatarStyles[size];
}
/**
* Return the width style from an avatar size constant
*
* @param {String} size
* @returns {Object}
*/
function getAvatarWidthStyle(size) {
const avatarSize = getAvatarSize(size);
return {
width: avatarSize,
};
}
/**
* Return the style from an avatar size constant
*
* @param {String} size
* @returns {Object}
*/
function getAvatarStyle(size) {
const avatarSize = getAvatarSize(size);
return {
height: avatarSize,
width: avatarSize,
borderRadius: avatarSize,
backgroundColor: themeColors.offline,
};
}
/**
* Get Font size of '+1' text on avatar overlay
* @param {String} size
* @returns {Object}
*/
function getAvatarExtraFontSizeStyle(size) {
const AVATAR_SIZES = {
[CONST.AVATAR_SIZE.DEFAULT]: variables.fontSizeNormal,
[CONST.AVATAR_SIZE.SMALL_SUBSCRIPT]: variables.fontSizeExtraSmall,
[CONST.AVATAR_SIZE.MID_SUBSCRIPT]: variables.fontSizeExtraSmall,
[CONST.AVATAR_SIZE.SUBSCRIPT]: variables.fontSizeExtraSmall,
[CONST.AVATAR_SIZE.SMALL]: variables.fontSizeSmall,
[CONST.AVATAR_SIZE.SMALLER]: variables.fontSizeExtraSmall,
[CONST.AVATAR_SIZE.LARGE]: variables.fontSizeXLarge,
[CONST.AVATAR_SIZE.MEDIUM]: variables.fontSizeMedium,
[CONST.AVATAR_SIZE.LARGE_BORDERED]: variables.fontSizeXLarge,
};
return {
fontSize: AVATAR_SIZES[size],
};
}
/**
* Get Bordersize of Avatar based on avatar size
* @param {String} size
* @returns {Object}
*/
function getAvatarBorderWidth(size) {
const AVATAR_SIZES = {
[CONST.AVATAR_SIZE.DEFAULT]: 3,
[CONST.AVATAR_SIZE.SMALL_SUBSCRIPT]: 1,
[CONST.AVATAR_SIZE.MID_SUBSCRIPT]: 2,
[CONST.AVATAR_SIZE.SUBSCRIPT]: 2,
[CONST.AVATAR_SIZE.SMALL]: 2,
[CONST.AVATAR_SIZE.SMALLER]: 2,
[CONST.AVATAR_SIZE.LARGE]: 4,
[CONST.AVATAR_SIZE.MEDIUM]: 3,
[CONST.AVATAR_SIZE.LARGE_BORDERED]: 4,
};
return {
borderWidth: AVATAR_SIZES[size],
};
}
/**
* Return the border radius for an avatar
*
* @param {String} size
* @param {String} type
* @returns {Object}
*/
function getAvatarBorderRadius(size, type) {
if (type === CONST.ICON_TYPE_WORKSPACE) {
return {borderRadius: avatarBorderSizes[size]};
}
// Default to rounded border
return {borderRadius: variables.buttonBorderRadius};
}
/**
* Return the border style for an avatar
*
* @param {String} size
* @param {String} type
* @returns {Object}
*/
function getAvatarBorderStyle(size, type) {
return {
overflow: 'hidden',
...getAvatarBorderRadius(size, type),
};
}
/**
* Helper method to return old dot default avatar associated with login
*
* @param {String} [workspaceName]
* @returns {Object}
*/
function getDefaultWorkspaceAvatarColor(workspaceName) {
const colorHash = UserUtils.hashText(workspaceName.trim(), workspaceColorOptions.length);
return workspaceColorOptions[colorHash];
}
/**
* Takes safe area insets and returns padding to use for a View
*
* @param {Object} insets
* @param {Number} [insetsPercentage] - Percentage of the insets to use for sides and bottom padding
* @returns {Object}
*/
function getSafeAreaPadding(insets, insetsPercentage = variables.safeInsertPercentage) {
return {
paddingTop: insets.top,
paddingBottom: insets.bottom * insetsPercentage,
paddingLeft: insets.left * insetsPercentage,
paddingRight: insets.right * insetsPercentage,
};
}
/**
* 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};
}
/**
* @param {Boolean} isZoomed
* @param {Boolean} isDragging
* @return {Object}
*/
function getZoomCursorStyle(isZoomed, isDragging) {
if (!isZoomed) {
return styles.cursorZoomIn;
}
return isDragging ? styles.cursorGrabbing : styles.cursorZoomOut;
}
/**
* @param {Boolean} isZoomed
* @param {Number} imgWidth
* @param {Number} imgHeight
* @param {Number} zoomScale
* @param {Number} containerHeight
* @param {Number} containerWidth
* @param {Boolean} isLoading
* @returns {Object | undefined}
*/
function getZoomSizingStyle(isZoomed, imgWidth, imgHeight, zoomScale, containerHeight, containerWidth, isLoading) {
// Hide image until finished loading to prevent showing preview with wrong dimensions
if (isLoading || imgWidth === 0 || imgHeight === 0) {
return undefined;
}
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 getWidthStyle(width) {
return {
width,
};
}
/**
* Returns auto grow height text input style
*
* @param {Number} textInputHeight
* @param {Number} maxHeight
* @returns {Object}
*/
function getAutoGrowHeightInputStyle(textInputHeight, maxHeight) {
if (textInputHeight > maxHeight) {
return {
...styles.pr0,
...styles.overflowAuto,
};
}
return {
...styles.pr0,
...styles.overflowHidden,
// maxHeight is not of the input only but the of the whole input container
// which also includes the top padding and bottom border
height: maxHeight - styles.textInputMultilineContainer.paddingTop - styles.textInputContainer.borderBottomWidth,
};
}
/**
* 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,
};
}
/**
* Returns a style for text color
*
* @param {String} color
* @returns {Object}
*/
function getTextColorStyle(color) {
return {
color,
};
}
/**
* Returns a style with the specified borderColor
*
* @param {String} borderColor
* @returns {Object}
*/
function getBorderColorStyle(borderColor) {
return {
borderColor,
};
}
/**
* Returns the width style for the wordmark logo on the sign in page
*
* @param {String} environment
* @param {Boolean} isSmallScreenWidth
* @returns {Object}
*/
function getSignInWordmarkWidthStyle(environment, isSmallScreenWidth) {
if (environment === CONST.ENVIRONMENT.DEV) {
return isSmallScreenWidth ? {width: variables.signInLogoWidthPill} : {width: variables.signInLogoWidthLargeScreenPill};
}
if (environment === CONST.ENVIRONMENT.STAGING) {
return isSmallScreenWidth ? {width: variables.signInLogoWidthPill} : {width: variables.signInLogoWidthLargeScreenPill};
}
if (environment === CONST.ENVIRONMENT.PRODUCTION) {
return isSmallScreenWidth ? {width: variables.signInLogoWidth} : {width: variables.signInLogoWidthLargeScreen};
}
return isSmallScreenWidth ? {width: variables.signInLogoWidthPill} : {width: variables.signInLogoWidthLargeScreenPill};
}
/**
* Converts a color in hexadecimal notation into RGB notation.
*
* @param {String} hexadecimal A color in hexadecimal notation.
* @returns {Array} `undefined` if the input color is not in hexadecimal notation. Otherwise, the RGB components of the input color.
*/
function hexadecimalToRGBArray(hexadecimal) {
const components = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexadecimal);
if (components === null) {
return undefined;
}
return _.map(components.slice(1), (component) => parseInt(component, 16));
}
/**
* Returns a background color with opacity style
*
* @param {String} backgroundColor
* @param {number} opacity
* @returns {Object}
*/
function getBackgroundColorWithOpacityStyle(backgroundColor, opacity) {
const result = hexadecimalToRGBArray(backgroundColor);
if (result !== undefined) {
return {
backgroundColor: `rgba(${result[0]}, ${result[1]}, ${result[2]}, ${opacity})`,
};
}
return {};
}
/**
* Generate a style for the background color of the Badge
*
* @param {Boolean} success
* @param {Boolean} error
* @param {boolean} [isPressed=false]
* @param {boolean} [isAdHoc=false]
* @return {Object}
*/
function getBadgeColorStyle(success, error, isPressed = false, isAdHoc = false) {
if (success) {
if (isAdHoc) {
return isPressed ? styles.badgeAdHocSuccessPressed : styles.badgeAdHocSuccess;
}
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'}
* @param {Boolean} isMenuItem - whether this button is apart of a list
* @returns {Object}
*/
function getButtonBackgroundColorStyle(buttonState = CONST.BUTTON_STATES.DEFAULT, isMenuItem = false) {
switch (buttonState) {
case CONST.BUTTON_STATES.PRESSED:
return {backgroundColor: themeColors.buttonPressedBG};
case CONST.BUTTON_STATES.ACTIVE:
return isMenuItem ? {backgroundColor: themeColors.border} : {backgroundColor: themeColors.buttonHoveredBG};
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'}
* @param {Boolean} isMenuIcon - whether this icon is apart of a list
* @returns {Object}
*/
function getIconFillColor(buttonState = CONST.BUTTON_STATES.DEFAULT, isMenuIcon = false) {
switch (buttonState) {
case CONST.BUTTON_STATES.ACTIVE:
case CONST.BUTTON_STATES.PRESSED:
return themeColors.iconHovered;
case CONST.BUTTON_STATES.COMPLETE:
return themeColors.iconSuccessFill;
case CONST.BUTTON_STATES.DEFAULT:
case CONST.BUTTON_STATES.DISABLED:
default:
if (isMenuIcon) {
return themeColors.iconMenu;
}
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 | null} height
* @returns {Object}
*/
function getWidthAndHeightStyle(width, height = null) {
return {
width,
height: height != null ? height : width,
};
}
/**
* @param {Object} params
* @returns {Object}
*/
function getModalPaddingStyles({
shouldAddBottomSafeAreaMargin,
shouldAddTopSafeAreaMargin,
shouldAddBottomSafeAreaPadding,
shouldAddTopSafeAreaPadding,
safeAreaPaddingTop,
safeAreaPaddingBottom,
safeAreaPaddingLeft,
safeAreaPaddingRight,
modalContainerStyleMarginTop,
modalContainerStyleMarginBottom,
modalContainerStylePaddingTop,
modalContainerStylePaddingBottom,
insets,
}) {
// use fallback value for safeAreaPaddingBottom to keep padding bottom consistent with padding top.
// More info: issue #17376
const safeAreaPaddingBottomWithFallback = insets.bottom === 0 ? modalContainerStylePaddingTop || 0 : safeAreaPaddingBottom;
return {
marginTop: (modalContainerStyleMarginTop || 0) + (shouldAddTopSafeAreaMargin ? safeAreaPaddingTop : 0),
marginBottom: (modalContainerStyleMarginBottom || 0) + (shouldAddBottomSafeAreaMargin ? safeAreaPaddingBottomWithFallback : 0),
paddingTop: shouldAddTopSafeAreaPadding ? (modalContainerStylePaddingTop || 0) + safeAreaPaddingTop : modalContainerStylePaddingTop || 0,
paddingBottom: shouldAddBottomSafeAreaPadding ? (modalContainerStylePaddingBottom || 0) + safeAreaPaddingBottomWithFallback : 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) {
if (isSmallScreenWidth) {
return {
width: CONST.SMALL_EMOJI_PICKER_SIZE.WIDTH,
};
}
return {
width: CONST.EMOJI_PICKER_SIZE.WIDTH,
height: CONST.EMOJI_PICKER_SIZE.HEIGHT,
};
}
/**
* 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`,
},
{
backgroundColor: colors.floralwhite,
backgroundImageUri: `${CONST.CLOUDFRONT_URL}/images/homepage/brand-stories/cpa-card.svg`,
redirectUri: `${CONST.USE_EXPENSIFY_URL}/accountants`,
},
];
return promos[_.random(0, 4)];
}
/**
* Generate the styles for the ReportActionItem wrapper view.
*
* @param {Boolean} [isHovered]
* @param {Boolean} [isLoading]
* @returns {Object}
*/
function getReportActionItemStyle(isHovered = false, isLoading = 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,
opacity: isLoading ? 0.5 : 1,
...styles.cursorInitial,
};
}
/**
* 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,
};
}
/**
* @param {Boolean} isSmallScreenWidth
* @returns {Object}
*/
function getPaymentMethodMenuWidth(isSmallScreenWidth) {
const margin = 20;
return {width: !isSmallScreenWidth ? variables.sideBarWidth - margin * 2 : undefined};
}
/**
* Converts a color in RGBA notation to an equivalent color in RGB notation.
*
* @param {Array} foregroundRGB The three components of the foreground color in RGB notation.
* @param {Array} backgroundRGB The three components of the background color in RGB notation.
* @param {number} opacity The desired opacity of the foreground color.
* @returns {Array} The RGB components of the RGBA color converted to RGB.
*/
function convertRGBAToRGB(foregroundRGB, backgroundRGB, opacity) {
const [foregroundRed, foregroundGreen, foregroundBlue] = foregroundRGB;
const [backgroundRed, backgroundGreen, backgroundBlue] = backgroundRGB;
return [(1 - opacity) * backgroundRed + opacity * foregroundRed, (1 - opacity) * backgroundGreen + opacity * foregroundGreen, (1 - opacity) * backgroundBlue + opacity * foregroundBlue];
}
/**
* Converts three unit values to the three components of a color in RGB notation.
*
* @param {number} red A unit value representing the first component of a color in RGB notation.
* @param {number} green A unit value representing the second component of a color in RGB notation.
* @param {number} blue A unit value representing the third component of a color in RGB notation.
* @returns {Array} An array with the three components of a color in RGB notation.
*/
function convertUnitValuesToRGB(red, green, blue) {
return [Math.floor(red * 255), Math.floor(green * 255), Math.floor(blue * 255)];
}
/**
* Converts the three components of a color in RGB notation to three unit values.
*
* @param {number} red The first component of a color in RGB notation.
* @param {number} green The second component of a color in RGB notation.
* @param {number} blue The third component of a color in RGB notation.
* @returns {Array} An array with three unit values representing the components of a color in RGB notation.
*/
function convertRGBToUnitValues(red, green, blue) {
return [red / 255, green / 255, blue / 255];
}
/**
* Matches an RGBA or RGB color value and extracts the color components.
*
* @param {string} color - The RGBA or RGB color value to match and extract components from.
* @returns {Array} An array containing the extracted color components [red, green, blue, alpha].
* Returns null if the input string does not match the pattern.
*/
function extractValuesFromRGB(color) {
const rgbaPattern = /rgba?\((?<r>[.\d]+)[, ]+(?<g>[.\d]+)[, ]+(?<b>[.\d]+)(?:\s?[,/]\s?(?<a>[.\d]+%?))?\)$/i;
const matchRGBA = color.match(rgbaPattern);
if (matchRGBA) {
const [, red, green, blue, alpha] = matchRGBA;
return [parseInt(red, 10), parseInt(green, 10), parseInt(blue, 10), alpha ? parseFloat(alpha) : 1];
}
return null;
}
/**
* Determines the theme color for a modal based on the app's background color,
* the modal's backdrop, and the backdrop's opacity.
*
* @param {String} bgColor - theme background color
* @returns {String} The theme color as an RGB value.
*/
function getThemeBackgroundColor(bgColor = themeColors.appBG) {
const backdropOpacity = variables.modalFullscreenBackdropOpacity;
const [backgroundRed, backgroundGreen, backgroundBlue] = extractValuesFromRGB(bgColor) || hexadecimalToRGBArray(bgColor);
const [backdropRed, backdropGreen, backdropBlue] = hexadecimalToRGBArray(themeColors.modalBackdrop);
const normalizedBackdropRGB = convertRGBToUnitValues(backdropRed, backdropGreen, backdropBlue);
const normalizedBackgroundRGB = convertRGBToUnitValues(backgroundRed, backgroundGreen, backgroundBlue);
const themeRGBNormalized = convertRGBAToRGB(normalizedBackdropRGB, normalizedBackgroundRGB, backdropOpacity);
const themeRGB = convertUnitValuesToRGB(...themeRGBNormalized);
return `rgb(${themeRGB.join(', ')})`;
}
/**
* Parse styleParam and return Styles array
* @param {Object|Object[]} styleParam
* @returns {Object[]}
*/
function parseStyleAsArray(styleParam) {
return _.isArray(styleParam) ? styleParam : [styleParam];
}
/**
* Parse style function and return Styles object
* @param {Object|Object[]|Function} style
* @param {Object} state
* @returns {Object[]}
*/
function parseStyleFromFunction(style, state) {
const functionAppliedStyle = _.isFunction(style) ? style(state) : style;
return parseStyleAsArray(functionAppliedStyle);
}
/**
* Receives any number of object or array style objects and returns them all as an array
* @param {Object|Object[]} allStyles
* @return {Object[]}
*/
function combineStyles(...allStyles) {
let finalStyles = [];
_.each(allStyles, (style) => {
finalStyles = finalStyles.concat(parseStyleAsArray(style));
});
return finalStyles;
}
/**
* Get variable padding-left as style
* @param {Number} paddingLeft
* @returns {Object}
*/
function getPaddingLeft(paddingLeft) {
return {
paddingLeft,
};
}
/**
* Checks to see if the iOS device has safe areas or not
*
* @param {Number} windowWidth
* @param {Number} windowHeight
* @returns {Boolean}
*/
function hasSafeAreas(windowWidth, windowHeight) {
const heightsIphonesWithNotches = [812, 896, 844, 926];
return _.contains(heightsIphonesWithNotches, windowHeight) || _.contains(heightsIphonesWithNotches, windowWidth);
}
/**
* Get height as style
* @param {Number} height
* @returns {Object}
*/
function getHeight(height) {
return {
height,
};
}
/**
* Get minimum height as style
* @param {Number} minHeight
* @returns {Object}
*/
function getMinimumHeight(minHeight) {
return {
minHeight,
};
}
/**
* Get maximum width as style
* @param {Number} maxWidth
* @returns {Object}
*/
function getMaximumWidth(maxWidth) {
return {
maxWidth,
};
}
/**
* Return style for opacity animation.
*
* @param {Animated.Value} fadeAnimation
* @returns {Object}
*/
function fade(fadeAnimation) {
return {
opacity: fadeAnimation,
};
}
/**
* Return width for keyboard shortcuts modal.
*
* @param {Boolean} isSmallScreenWidth
* @returns {Object}
*/
function getKeyboardShortcutsModalWidth(isSmallScreenWidth) {
if (isSmallScreenWidth) {
return {maxWidth: '100%'};
}
return {maxWidth: 600};
}
/**
* @param {Object} params
* @param {Boolean} params.isHovered
* @param {Boolean} params.isPressed
* @param {Boolean} params.isInReportAction
* @param {Boolean} params.shouldUseCardBackground
* @returns {Object}
*/
function getHorizontalStackedAvatarBorderStyle({isHovered, isPressed, isInReportAction = false, shouldUseCardBackground = false}) {
let backgroundColor = shouldUseCardBackground ? themeColors.cardBG : themeColors.appBG;
if (isHovered) {
backgroundColor = isInReportAction ? themeColors.highlightBG : themeColors.border;
}
if (isPressed) {
backgroundColor = isInReportAction ? themeColors.highlightBG : themeColors.buttonPressedBG;
}
return {
backgroundColor,
borderColor: backgroundColor,
};
}
/**
* Get computed avatar styles based on position and border size
* @param {Number} index
* @param {Number} overlapSize
* @param {Number} borderWidth
* @param {Number} borderRadius
* @returns {Object}
*/
function getHorizontalStackedAvatarStyle(index, overlapSize, borderWidth, borderRadius) {
return {
left: -(overlapSize * index),
borderRadius,
borderWidth,
zIndex: index + 2,
};
}
/**
* Get computed avatar styles of '+1' overlay based on size
* @param {Object} oneAvatarSize
* @param {Numer} oneAvatarBorderWidth
* @returns {Object}
*/
function getHorizontalStackedOverlayAvatarStyle(oneAvatarSize, oneAvatarBorderWidth) {
return {
borderWidth: oneAvatarBorderWidth,
borderRadius: oneAvatarSize.width,
left: -(oneAvatarSize.width * 2 + oneAvatarBorderWidth * 2),
zIndex: 6,
borderStyle: 'solid',
};
}
/**
* @param {Number} safeAreaPaddingBottom
* @returns {Object}
*/
function getErrorPageContainerStyle(safeAreaPaddingBottom = 0) {
return {
backgroundColor: themeColors.componentBG,
paddingBottom: 40 + safeAreaPaddingBottom,
};
}
/**
* Gets the correct size for the empty state background image based on screen dimensions
*
* @param {Boolean} isSmallScreenWidth
* @returns {Object}
*/
function getReportWelcomeBackgroundImageStyle(isSmallScreenWidth) {
if (isSmallScreenWidth) {
return {
height: CONST.EMPTY_STATE_BACKGROUND.SMALL_SCREEN.IMAGE_HEIGHT,
width: '100%',
position: 'absolute',
};
}
return {
height: CONST.EMPTY_STATE_BACKGROUND.WIDE_SCREEN.IMAGE_HEIGHT,
width: '100%',
position: 'absolute',
};
}
/**
* Gets the correct top margin size for the chat welcome message based on screen dimensions
*
* @param {Boolean} isSmallScreenWidth
* @returns {Object}
*/
function getReportWelcomeTopMarginStyle(isSmallScreenWidth) {
if (isSmallScreenWidth) {
return {
marginTop: CONST.EMPTY_STATE_BACKGROUND.SMALL_SCREEN.VIEW_HEIGHT,
};
}
return {
marginTop: CONST.EMPTY_STATE_BACKGROUND.WIDE_SCREEN.VIEW_HEIGHT,
};
}
/**
* Returns fontSize style
*
* @param {Number} fontSize
* @returns {Object}
*/
function getFontSizeStyle(fontSize) {
return {
fontSize,
};
}
/**
* Returns lineHeight style
*
* @param {Number} lineHeight
* @returns {Object}
*/
function getLineHeightStyle(lineHeight) {
return {
lineHeight,