1
1
import { Platform , processColor } from 'react-native' ;
2
2
import type { Spec , StylesIOS } from './NativeSimpleToast' ;
3
3
4
+ const unsupportedPlatform = 'RNSimpleToast: unsupported platform' ;
5
+
4
6
const RCTToast = Platform . select < ( ) => Spec > ( {
5
7
ios : ( ) => require ( './NativeSimpleToast' ) . default ,
6
8
android : ( ) => require ( 'react-native' ) . ToastAndroid ,
7
9
default : ( ) => {
8
- throw new Error ( 'RNSimpleToast: unsupported platform' ) ;
10
+ throw new Error ( unsupportedPlatform ) ;
9
11
} ,
10
12
} ) ( ) ;
11
13
@@ -21,7 +23,7 @@ const constantsSource = Platform.select<
21
23
ios : ( ) => require ( './NativeSimpleToast' ) . default . getConstants ( ) ,
22
24
android : ( ) => require ( 'react-native' ) . ToastAndroid ,
23
25
default : ( ) => {
24
- throw new Error ( 'RNSimpleToast: unsupported platform' ) ;
26
+ throw new Error ( unsupportedPlatform ) ;
25
27
} ,
26
28
} ) ( ) ;
27
29
@@ -33,23 +35,23 @@ export default {
33
35
BOTTOM : constantsSource . BOTTOM ,
34
36
CENTER : constantsSource . CENTER ,
35
37
36
- show ( message : string , durationSeconds : number , options : StylesIOS = { } ) {
38
+ show ( message : string , duration : number , options ? : StylesIOS ) {
37
39
RCTToast . show (
38
40
message ,
39
- durationSeconds ?? constantsSource . SHORT ,
41
+ duration ?? constantsSource . SHORT ,
40
42
processColors ( options ) ,
41
43
) ;
42
44
} ,
43
45
44
46
showWithGravity (
45
47
message : string ,
46
- durationSeconds : number ,
48
+ duration : number ,
47
49
gravity : number ,
48
- options : StylesIOS = { } ,
50
+ options ? : StylesIOS ,
49
51
) {
50
52
RCTToast . showWithGravity (
51
53
message ,
52
- durationSeconds ?? constantsSource . SHORT ,
54
+ duration ?? constantsSource . SHORT ,
53
55
gravity ,
54
56
processColors ( options ) ,
55
57
) ;
@@ -61,11 +63,11 @@ export default {
61
63
gravity : number ,
62
64
xOffset : number ,
63
65
yOffset : number ,
64
- options : StylesIOS = { } ,
66
+ options ? : StylesIOS ,
65
67
) {
66
68
RCTToast . showWithGravityAndOffset (
67
69
message ,
68
- duration ,
70
+ duration ?? constantsSource . SHORT ,
69
71
gravity ,
70
72
xOffset ,
71
73
yOffset ,
@@ -74,17 +76,16 @@ export default {
74
76
} ,
75
77
} ;
76
78
77
- function processColors ( options : StylesIOS ) {
78
- if ( Platform . OS === 'ios' ) {
79
- return {
80
- // the types are not 100% correct
81
- ...options ,
82
- messageColor : processColor ( options . textColor ) as number | undefined ,
83
- backgroundColor : processColor ( options . backgroundColor ) as
84
- | number
85
- | undefined ,
86
- } ;
79
+ function processColors ( options ?: StylesIOS ) {
80
+ if ( Platform . OS === 'android' || ! options ) {
81
+ return undefined ;
87
82
}
88
-
89
- return { } ;
83
+ return {
84
+ // the types are not 100% correct
85
+ ...options ,
86
+ messageColor : processColor ( options . textColor ) as number | undefined ,
87
+ backgroundColor : processColor ( options . backgroundColor ) as
88
+ | number
89
+ | undefined ,
90
+ } ;
90
91
}
0 commit comments