-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.ts
1721 lines (1537 loc) · 58.8 KB
/
index.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
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 {StyleSheet} from 'react-native';
import type {AnimatableNumericValue, Animated, ColorValue, ImageStyle, PressableStateCallbackType, StyleProp, TextStyle, ViewStyle} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import type {EdgeInsets} from 'react-native-safe-area-context';
import type {ValueOf} from 'type-fest';
import type ImageSVGProps from '@components/ImageSVG/types';
import * as Browser from '@libs/Browser';
import * as UserUtils from '@libs/UserUtils';
// eslint-disable-next-line no-restricted-imports
import {defaultTheme} from '@styles/theme';
import colors from '@styles/theme/colors';
import type {ThemeColors} from '@styles/theme/types';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type {Transaction} from '@src/types/onyx';
import {defaultStyles} from '..';
import type {ThemeStyles} from '..';
import shouldPreventScrollOnAutoCompleteSuggestion from './autoCompleteSuggestion';
import getCardStyles from './cardStyles';
import containerComposeStyles from './containerComposeStyles';
import createModalStyleUtils from './generators/ModalStyleUtils';
import createReportActionContextMenuStyleUtils from './generators/ReportActionContextMenuStyleUtils';
import createTooltipStyleUtils from './generators/TooltipStyleUtils';
import getContextMenuItemStyles from './getContextMenuItemStyles';
import getHighResolutionInfoWrapperStyle from './getHighResolutionInfoWrapperStyle';
import getNavigationModalCardStyle from './getNavigationModalCardStyles';
import getSignInBgStyles from './getSignInBgStyles';
import {compactContentContainerStyles} from './optionRowStyles';
import positioning from './positioning';
import type {
AllStyles,
AvatarSize,
AvatarSizeName,
AvatarSizeValue,
AvatarStyle,
ButtonSizeValue,
ButtonStateName,
EReceiptColorName,
EreceiptColorStyle,
ParsableStyle,
SVGAvatarColorStyle,
TextColorStyle,
} from './types';
const workspaceColorOptions: SVGAvatarColorStyle[] = [
{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 eReceiptColorStyles: Partial<Record<EReceiptColorName, EreceiptColorStyle>> = {
[CONST.ERECEIPT_COLORS.YELLOW]: {backgroundColor: colors.yellow600, color: colors.yellow100},
[CONST.ERECEIPT_COLORS.ICE]: {backgroundColor: colors.blue800, color: colors.ice400},
[CONST.ERECEIPT_COLORS.BLUE]: {backgroundColor: colors.blue400, color: colors.blue100},
[CONST.ERECEIPT_COLORS.GREEN]: {backgroundColor: colors.green800, color: colors.green400},
[CONST.ERECEIPT_COLORS.TANGERINE]: {backgroundColor: colors.tangerine800, color: colors.tangerine400},
[CONST.ERECEIPT_COLORS.PINK]: {backgroundColor: colors.pink800, color: colors.pink400},
};
const eReceiptColors: EReceiptColorName[] = [
CONST.ERECEIPT_COLORS.YELLOW,
CONST.ERECEIPT_COLORS.ICE,
CONST.ERECEIPT_COLORS.BLUE,
CONST.ERECEIPT_COLORS.GREEN,
CONST.ERECEIPT_COLORS.TANGERINE,
CONST.ERECEIPT_COLORS.PINK,
];
const avatarBorderSizes: Partial<Record<AvatarSizeName, number>> = {
[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.XLARGE]: variables.componentBorderRadiusLarge,
[CONST.AVATAR_SIZE.LARGE_BORDERED]: variables.componentBorderRadiusRounded,
[CONST.AVATAR_SIZE.SMALL_NORMAL]: variables.componentBorderRadiusMedium,
};
const avatarSizes: Record<AvatarSizeName, AvatarSizeValue> = {
[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.XLARGE]: variables.avatarSizeXLarge,
[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 avatarFontSizes: Partial<Record<AvatarSizeName, number>> = {
[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,
};
const avatarBorderWidths: Partial<Record<AvatarSizeName, number>> = {
[CONST.AVATAR_SIZE.DEFAULT]: 3,
[CONST.AVATAR_SIZE.SMALL_SUBSCRIPT]: 2,
[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.XLARGE]: 4,
[CONST.AVATAR_SIZE.MEDIUM]: 3,
[CONST.AVATAR_SIZE.LARGE_BORDERED]: 4,
};
/**
* Converts a color in hexadecimal notation into RGB notation.
*
* @param hexadecimal A color in hexadecimal notation.
* @returns `undefined` if the input color is not in hexadecimal notation. Otherwise, the RGB components of the input color.
*/
function hexadecimalToRGBArray(hexadecimal: string): number[] | undefined {
const components = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexadecimal);
if (components === null) {
return undefined;
}
return components.slice(1).map((component) => parseInt(component, 16));
}
/**
* Converts a color in RGBA notation to an equivalent color in RGB notation.
*
* @param foregroundRGB The three components of the foreground color in RGB notation.
* @param backgroundRGB The three components of the background color in RGB notation.
* @param opacity The desired opacity of the foreground color.
* @returns The RGB components of the RGBA color converted to RGB.
*/
function convertRGBAToRGB(foregroundRGB: number[], backgroundRGB: number[], opacity: number): number[] {
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 red A unit value representing the first component of a color in RGB notation.
* @param green A unit value representing the second component of a color in RGB notation.
* @param blue A unit value representing the third component of a color in RGB notation.
* @returns An array with the three components of a color in RGB notation.
*/
function convertUnitValuesToRGB(red: number, green: number, blue: number): number[] {
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 red The first component of a color in RGB notation.
* @param green The second component of a color in RGB notation.
* @param blue The third component of a color in RGB notation.
* @returns An array with three unit values representing the components of a color in RGB notation.
*/
function convertRGBToUnitValues(red: number, green: number, blue: number): number[] {
return [red / 255, green / 255, blue / 255];
}
/**
* Matches an RGBA or RGB color value and extracts the color components.
*
* @param color - The RGBA or RGB color value to match and extract components from.
* @returns 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: string): number[] | null {
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;
}
/**
* Return the style size from an avatar size constant
*/
function getAvatarSize(size: AvatarSizeName): number {
return avatarSizes[size];
}
/**
* Return the width style from an avatar size constant
*/
function getAvatarWidthStyle(size: AvatarSizeName): ViewStyle {
const avatarSize = getAvatarSize(size);
return {
width: avatarSize,
};
}
/**
* Get Font size of '+1' text on avatar overlay
*/
function getAvatarExtraFontSizeStyle(size: AvatarSizeName): TextStyle {
return {
fontSize: avatarFontSizes[size],
};
}
/**
* Get Bordersize of Avatar based on avatar size
*/
function getAvatarBorderWidth(size: AvatarSizeName): ViewStyle {
return {
borderWidth: avatarBorderWidths[size],
};
}
/**
* Return the border radius for an avatar
*/
function getAvatarBorderRadius(size: AvatarSizeName, type?: string): ViewStyle {
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
*/
function getAvatarBorderStyle(size: AvatarSizeName, type: string): ViewStyle {
return {
overflow: 'hidden',
...getAvatarBorderRadius(size, type),
};
}
/**
* Helper method to return workspace avatar color styles
*/
function getDefaultWorkspaceAvatarColor(text: string): ViewStyle {
const colorHash = UserUtils.hashText(text.trim(), workspaceColorOptions.length);
return workspaceColorOptions.at(colorHash) ?? {backgroundColor: colors.blue200, fill: colors.blue700};
}
/**
* Helper method to return formatted backgroundColor and fill styles
*/
function getBackgroundColorAndFill(backgroundColor: string, fill: string): SVGAvatarColorStyle {
return {backgroundColor, fill};
}
/**
* Helper method to return eReceipt color code
*/
function getEReceiptColorCode(transaction: OnyxEntry<Transaction>): EReceiptColorName {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const transactionID = transaction?.parentTransactionID || transaction?.transactionID || '';
const colorHash = UserUtils.hashText(transactionID.trim(), eReceiptColors.length);
return eReceiptColors.at(colorHash) ?? CONST.ERECEIPT_COLORS.YELLOW;
}
/**
* Helper method to return eReceipt color code for Receipt Thumbnails
*/
function getFileExtensionColorCode(fileExtension?: string): EReceiptColorName {
switch (fileExtension) {
case CONST.IOU.FILE_TYPES.DOC:
return CONST.ERECEIPT_COLORS.PINK;
case CONST.IOU.FILE_TYPES.HTML:
return CONST.ERECEIPT_COLORS.TANGERINE;
default:
return CONST.ERECEIPT_COLORS.GREEN;
}
}
/**
* Helper method to return eReceipt color styles
*/
function getEReceiptColorStyles(colorCode: EReceiptColorName): EreceiptColorStyle | undefined {
return eReceiptColorStyles[colorCode];
}
type SafeAreaPadding = {
paddingTop: number;
paddingBottom: number;
paddingLeft: number;
paddingRight: number;
};
/**
* Takes safe area insets and returns padding to use for a View
*/
function getSafeAreaPadding(insets?: EdgeInsets, insetsPercentage: number = variables.safeInsertPercentage): SafeAreaPadding {
return {
paddingTop: insets?.top ?? 0,
paddingBottom: (insets?.bottom ?? 0) * insetsPercentage,
paddingLeft: (insets?.left ?? 0) * insetsPercentage,
paddingRight: (insets?.right ?? 0) * insetsPercentage,
};
}
/**
* Takes safe area insets and returns margin to use for a View
*/
function getSafeAreaMargins(insets?: EdgeInsets): ViewStyle {
return {marginBottom: (insets?.bottom ?? 0) * variables.safeInsertPercentage};
}
function getZoomSizingStyle(
isZoomed: boolean,
imgWidth: number,
imgHeight: number,
zoomScale: number,
containerHeight: number,
containerWidth: number,
isLoading: boolean,
): ViewStyle | undefined {
// 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 a style with width set to the specified number
*/
function getWidthStyle(width: number): ViewStyle & ImageStyle {
return {
width,
};
}
/**
* Returns a style with border radius set to the specified number
*/
function getBorderRadiusStyle(borderRadius: number): ViewStyle & ImageStyle {
return {
borderRadius,
};
}
/**
* Returns a style with backgroundColor and borderColor set to the same color
*/
function getBackgroundAndBorderStyle(backgroundColor: ColorValue | undefined): ViewStyle {
return {
backgroundColor,
borderColor: backgroundColor,
};
}
/**
* Returns a style with the specified backgroundColor
*/
function getBackgroundColorStyle(backgroundColor: ColorValue): ViewStyle {
return {
backgroundColor,
};
}
/**
* Returns a style for text color
*/
function getTextColorStyle(color: string): TextColorStyle {
return {
color,
};
}
/**
* Returns a style with the specified borderColor
*/
function getBorderColorStyle(borderColor: string): ViewStyle {
return {
borderColor,
};
}
/**
* Returns the width style for the wordmark logo on the sign in page
*/
function getSignInWordmarkWidthStyle(isSmallScreenWidth: boolean, environment: ValueOf<typeof CONST.ENVIRONMENT>): ViewStyle {
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};
}
/**
* Returns a background color with opacity style
*/
function getBackgroundColorWithOpacityStyle(backgroundColor: string, opacity: number): ViewStyle {
const result = hexadecimalToRGBArray(backgroundColor);
if (result !== undefined) {
return {
backgroundColor: `rgba(${result.at(0)}, ${result.at(1)}, ${result.at(2)}, ${opacity})`,
};
}
return {};
}
function getWidthAndHeightStyle(width: number, height?: number): Pick<ViewStyle, 'height' | 'width'> {
return {
width,
height: height ?? width,
};
}
function getIconWidthAndHeightStyle(small: boolean, medium: boolean, large: boolean, width: number, height: number, isButtonIcon: boolean): Pick<ImageSVGProps, 'width' | 'height'> {
switch (true) {
case small:
return {width: isButtonIcon ? variables.iconSizeExtraSmall : variables.iconSizeSmall, height: isButtonIcon ? variables.iconSizeExtraSmall : variables?.iconSizeSmall};
case medium:
return {width: isButtonIcon ? variables.iconSizeSmall : variables.iconSizeNormal, height: isButtonIcon ? variables.iconSizeSmall : variables.iconSizeNormal};
case large:
return {width: isButtonIcon ? variables.iconSizeNormal : variables.iconSizeLarge, height: isButtonIcon ? variables.iconSizeNormal : variables.iconSizeLarge};
default: {
return {width, height};
}
}
}
function getButtonStyleWithIcon(
styles: ThemeStyles,
small: boolean,
medium: boolean,
large: boolean,
hasIcon?: boolean,
hasText?: boolean,
shouldShowRightIcon?: boolean,
): ViewStyle | undefined {
const useDefaultButtonStyles = !!(hasIcon && shouldShowRightIcon) || !!(!hasIcon && !shouldShowRightIcon);
switch (true) {
case small: {
const verticalStyle = hasIcon ? styles.pl2 : styles.pr2;
return useDefaultButtonStyles ? styles.buttonSmall : {...styles.buttonSmall, ...(hasText ? verticalStyle : styles.ph0)};
}
case medium: {
const verticalStyle = hasIcon ? styles.pl3 : styles.pr3;
return useDefaultButtonStyles ? styles.buttonMedium : {...styles.buttonMedium, ...(hasText ? verticalStyle : styles.ph0)};
}
case large: {
const verticalStyle = hasIcon ? styles.pl4 : styles.pr4;
return useDefaultButtonStyles ? styles.buttonLarge : {...styles.buttonLarge, ...(hasText ? verticalStyle : styles.ph0)};
}
default: {
if (hasIcon && !hasText) {
return {...styles.buttonMedium, ...styles.ph0};
}
return undefined;
}
}
}
type MarginPaddingValue = ViewStyle['marginTop' | 'marginBottom' | 'paddingTop' | 'paddingBottom'];
/**
* Combine margin/padding with safe area inset
*
* @param modalContainerValue - margin or padding value
* @param safeAreaValue - safe area inset
* @param shouldAddSafeAreaValue - indicator whether safe area inset should be applied
*/
function getCombinedSpacing(modalContainerValue: MarginPaddingValue, safeAreaValue: number, shouldAddSafeAreaValue: boolean): MarginPaddingValue {
// modalContainerValue can only be added to safe area inset if it's a number, otherwise it's returned as is
if (typeof modalContainerValue === 'number') {
return modalContainerValue + (shouldAddSafeAreaValue ? safeAreaValue : 0);
}
if (!modalContainerValue) {
return shouldAddSafeAreaValue ? safeAreaValue : 0;
}
return modalContainerValue;
}
type ModalPaddingStylesParams = {
shouldAddBottomSafeAreaMargin: boolean;
shouldAddTopSafeAreaMargin: boolean;
shouldAddBottomSafeAreaPadding: boolean;
shouldAddTopSafeAreaPadding: boolean;
safeAreaPaddingTop: number;
safeAreaPaddingBottom: number;
safeAreaPaddingLeft: number;
safeAreaPaddingRight: number;
modalContainerStyleMarginTop: MarginPaddingValue;
modalContainerStyleMarginBottom: MarginPaddingValue;
modalContainerStylePaddingTop: MarginPaddingValue;
modalContainerStylePaddingBottom: MarginPaddingValue;
insets: EdgeInsets;
};
function getModalPaddingStyles({
shouldAddBottomSafeAreaMargin,
shouldAddTopSafeAreaMargin,
shouldAddBottomSafeAreaPadding,
shouldAddTopSafeAreaPadding,
safeAreaPaddingTop,
safeAreaPaddingBottom,
safeAreaPaddingLeft,
safeAreaPaddingRight,
modalContainerStyleMarginTop,
modalContainerStyleMarginBottom,
modalContainerStylePaddingTop,
modalContainerStylePaddingBottom,
insets,
}: ModalPaddingStylesParams): ViewStyle {
// use fallback value for safeAreaPaddingBottom to keep padding bottom consistent with padding top.
// More info: issue #17376
const safeAreaPaddingBottomWithFallback = insets.bottom === 0 && typeof modalContainerStylePaddingTop === 'number' ? modalContainerStylePaddingTop ?? 0 : safeAreaPaddingBottom;
return {
marginTop: getCombinedSpacing(modalContainerStyleMarginTop, safeAreaPaddingTop, shouldAddTopSafeAreaMargin),
marginBottom: getCombinedSpacing(modalContainerStyleMarginBottom, safeAreaPaddingBottomWithFallback, shouldAddBottomSafeAreaMargin),
paddingTop: getCombinedSpacing(modalContainerStylePaddingTop, safeAreaPaddingTop, shouldAddTopSafeAreaPadding),
paddingBottom: getCombinedSpacing(modalContainerStylePaddingBottom, safeAreaPaddingBottomWithFallback, shouldAddBottomSafeAreaPadding),
paddingLeft: safeAreaPaddingLeft ?? 0,
paddingRight: safeAreaPaddingRight ?? 0,
};
}
/**
* Returns the font size for the HTML code tag renderer.
*/
function getCodeFontSize(isInsideH1: boolean) {
return isInsideH1 ? 15 : 13;
}
/**
* Gives the width for Emoji picker Widget
*/
function getEmojiPickerStyle(isSmallScreenWidth: boolean): ViewStyle {
if (isSmallScreenWidth) {
return {
width: CONST.SMALL_EMOJI_PICKER_SIZE.WIDTH,
};
}
return {
width: CONST.EMOJI_PICKER_SIZE.WIDTH,
height: CONST.EMOJI_PICKER_SIZE.HEIGHT,
};
}
function getPaymentMethodMenuWidth(isSmallScreenWidth: boolean): ViewStyle {
const margin = 20;
return {width: !isSmallScreenWidth ? variables.sideBarWidth - margin * 2 : undefined};
}
/**
* Parse styleParam and return Styles array
*/
function parseStyleAsArray<T extends AllStyles>(styleParam: T | T[]): T[] {
return Array.isArray(styleParam) ? styleParam : [styleParam];
}
/**
* Parse style function and return Styles object
*/
function parseStyleFromFunction(style: ParsableStyle, state: PressableStateCallbackType): StyleProp<ViewStyle> {
return typeof style === 'function' ? style(state) : style;
}
/**
* Receives any number of object or array style objects and returns them all as an array
*/
function combineStyles<T extends AllStyles>(...allStyles: Array<T | T[]>): T[] {
let finalStyles: T[] = [];
allStyles.forEach((style) => {
finalStyles = finalStyles.concat(parseStyleAsArray(style));
});
return finalStyles;
}
/**
* Get variable padding-left as style
*/
function getPaddingLeft(paddingLeft: number): ViewStyle {
return {
paddingLeft,
};
}
/**
* Get variable padding-right as style
*/
function getPaddingRight(paddingRight: number): ViewStyle {
return {
paddingRight,
};
}
/**
* Checks to see if the iOS device has safe areas or not
*/
function hasSafeAreas(windowWidth: number, windowHeight: number): boolean {
const heightsIphonesWithNotches = [812, 896, 844, 926];
return heightsIphonesWithNotches.includes(windowHeight) || heightsIphonesWithNotches.includes(windowWidth);
}
/**
* Get height as style
*/
function getHeight(height: number): ViewStyle {
return {
height,
};
}
/**
* Get minimum height as style
*/
function getMinimumHeight(minHeight: number): ViewStyle {
return {
minHeight,
};
}
/**
* Get minimum width as style
*/
function getMinimumWidth(minWidth: number): ViewStyle & ImageStyle {
return {
minWidth,
};
}
/**
* Get maximum height as style
*/
function getMaximumHeight(maxHeight: number): ViewStyle {
return {
maxHeight,
};
}
/**
* Get maximum width as style
*/
function getMaximumWidth(maxWidth: number): ViewStyle {
return {
maxWidth,
};
}
/**
* Return style for opacity animation.
*/
function fade(fadeAnimation: Animated.Value): Animated.WithAnimatedValue<ViewStyle> {
return {
opacity: fadeAnimation,
};
}
type AvatarBorderStyleParams = {
theme: ThemeColors;
isHovered: boolean;
isPressed: boolean;
isInReportAction: boolean;
shouldUseCardBackground: boolean;
};
function getHorizontalStackedAvatarBorderStyle({theme, isHovered, isPressed, isInReportAction = false, shouldUseCardBackground = false}: AvatarBorderStyleParams): ViewStyle {
let borderColor = shouldUseCardBackground ? theme.cardBG : theme.appBG;
if (isHovered) {
borderColor = isInReportAction ? theme.hoverComponentBG : theme.border;
}
if (isPressed) {
borderColor = isInReportAction ? theme.hoverComponentBG : theme.buttonPressedBG;
}
return {borderColor};
}
/**
* Get computed avatar styles based on position and border size
*/
function getHorizontalStackedAvatarStyle(index: number, overlapSize: number): ViewStyle {
return {
marginLeft: index > 0 ? -overlapSize : 0,
zIndex: index + 2,
};
}
/**
* Get computed avatar styles of '+1' overlay based on size
*/
function getHorizontalStackedOverlayAvatarStyle(oneAvatarSize: AvatarSize, oneAvatarBorderWidth: number): ViewStyle {
return {
borderWidth: oneAvatarBorderWidth,
borderRadius: oneAvatarSize.width,
marginLeft: -(oneAvatarSize.width + oneAvatarBorderWidth * 2),
zIndex: 6,
borderStyle: 'solid',
};
}
/**
* Gets the correct size for the empty state background image based on screen dimensions
*/
function getReportWelcomeBackgroundImageStyle(isSmallScreenWidth: boolean): ImageStyle {
if (isSmallScreenWidth) {
return {
position: 'absolute',
bottom: 0,
height: CONST.EMPTY_STATE_BACKGROUND.SMALL_SCREEN.IMAGE_HEIGHT,
width: '100%',
};
}
return {
position: 'absolute',
bottom: 0,
height: CONST.EMPTY_STATE_BACKGROUND.WIDE_SCREEN.IMAGE_HEIGHT,
width: '100%',
};
}
/**
* Gets the style for the container of the empty state background image that overlap the created report action
*/
function getReportWelcomeBackgroundContainerStyle(): ViewStyle {
return {
position: 'absolute',
top: CONST.EMPTY_STATE_BACKGROUND.OVERLAP,
width: '100%',
};
}
/**
* Returns fontSize style
*/
function getFontSizeStyle(fontSize: number): TextStyle {
return {
fontSize,
};
}
/**
* Returns lineHeight style
*/
function getLineHeightStyle(lineHeight: number): TextStyle {
return {
lineHeight,
};
}
type GetBaseAutoCompleteSuggestionContainerStyleParams = {
left: number;
bottom: number;
width: number;
};
/**
* Gets the correct position for the base auto complete suggestion container
*/
function getBaseAutoCompleteSuggestionContainerStyle({left, bottom, width}: GetBaseAutoCompleteSuggestionContainerStyleParams): ViewStyle {
return {
position: 'absolute',
bottom,
left,
width,
};
}
const shouldPreventScroll = shouldPreventScrollOnAutoCompleteSuggestion();
/**
* Gets the correct position for auto complete suggestion container
*/
function getAutoCompleteSuggestionContainerStyle(itemsHeight: number): ViewStyle {
'worklet';
const borderWidth = 2;
const height = itemsHeight + 2 * CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTER_INNER_PADDING + (shouldPreventScroll ? borderWidth : 0);
return {
height,
minHeight: CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTION_ROW_HEIGHT,
};
}
function getEmojiReactionBubbleTextStyle(isContextMenu = false): TextStyle {
if (isContextMenu) {
return {
fontSize: 17,
lineHeight: 24,
};
}
return {
fontSize: 15,
lineHeight: 22,
};
}
function getTransformScaleStyle(scaleValue: AnimatableNumericValue): ViewStyle {
return {
transform: [{scale: scaleValue}],
};
}
/**
* Returns a style object with a rotation transformation applied based on the provided direction prop.
*
* @param direction - The direction of the rotation (CONST.DIRECTION.LEFT or CONST.DIRECTION.RIGHT).
*/
function getDirectionStyle(direction: ValueOf<typeof CONST.DIRECTION>): ViewStyle {
if (direction === CONST.DIRECTION.LEFT) {
return {transform: 'rotate(180deg)'};
}
return {};
}
/**
* Returns a style object with display flex or none basing on the condition value.
*/
function displayIfTrue(condition: boolean): ViewStyle {
return {display: condition ? 'flex' : 'none'};
}
/**
* Gets the correct height for emoji picker list based on screen dimensions
*/
function getEmojiPickerListHeight(isRenderingShortcutRow: boolean, windowHeight: number): ViewStyle {
const style = {
height: isRenderingShortcutRow ? CONST.NON_NATIVE_EMOJI_PICKER_LIST_HEIGHT + CONST.CATEGORY_SHORTCUT_BAR_HEIGHT : CONST.NON_NATIVE_EMOJI_PICKER_LIST_HEIGHT,
};
if (windowHeight) {
// dimensions of content above the emoji picker list
const dimensions = isRenderingShortcutRow ? CONST.EMOJI_PICKER_TEXT_INPUT_SIZES + CONST.CATEGORY_SHORTCUT_BAR_HEIGHT : CONST.EMOJI_PICKER_TEXT_INPUT_SIZES;
return {
...style,
maxHeight: windowHeight - dimensions,
};
}
return style;
}
/**
* Returns padding vertical based on number of lines
*/
function getComposeTextAreaPadding(isComposerFullSize: boolean): TextStyle {
let paddingValue = 5;
// Issue #26222: If isComposerFullSize paddingValue will always be 5 to prevent padding jumps when adding multiple lines.
if (!isComposerFullSize) {
paddingValue = 8;
}
return {
paddingTop: paddingValue,
paddingBottom: paddingValue,
};
}
/**
* Returns style object for the mobile on WEB
*/
function getOuterModalStyle(windowHeight: number, viewportOffsetTop: number): ViewStyle {
return Browser.isMobile() ? {maxHeight: windowHeight, marginTop: viewportOffsetTop} : {};
}
/**
* Returns style object for flexWrap depending on the screen size
*/
function getWrappingStyle(isExtraSmallScreenWidth: boolean): ViewStyle {
return {
flexWrap: isExtraSmallScreenWidth ? 'wrap' : 'nowrap',
};
}
/**
* Returns the text container styles for menu items depending on if the menu item container a small avatar
*/
function getMenuItemTextContainerStyle(isSmallAvatarSubscriptMenu: boolean): ViewStyle {
return {
minHeight: isSmallAvatarSubscriptMenu ? variables.avatarSizeSubscript : variables.componentSizeNormal,
};
}
/**
* Returns color style
*/
function getColorStyle(color: string): TextColorStyle {
return {color};
}
/**
* Returns the checkbox pressable style
*/
function getCheckboxPressableStyle(borderRadius = 6): ViewStyle {
return {
justifyContent: 'center',
alignItems: 'center',
borderRadius,
};
}
/**
* Returns style object for the dropbutton height
*/
function getDropDownButtonHeight(buttonSize: ButtonSizeValue): ViewStyle {
if (buttonSize === CONST.DROPDOWN_BUTTON_SIZE.LARGE) {
return {
height: variables.componentSizeLarge,
};
}
if (buttonSize === CONST.DROPDOWN_BUTTON_SIZE.SMALL) {
return {
height: variables.componentSizeSmall,
};
}