forked from vonovak/react-native-simple-toast
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
93 lines (84 loc) · 2.15 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
import { Platform, processColor } from 'react-native';
import type { Spec, StylesIOS } from './NativeSimpleToast';
const unsupportedPlatform = 'RNSimpleToast: unsupported platform';
const RCTToast = Platform.select<() => Spec>({
ios: () => require('./NativeSimpleToast').default,
android: () => require('react-native').ToastAndroid,
harmony: () => require('react-native').ToastAndroid,
default: () => {
throw new Error(unsupportedPlatform);
},
})();
const constantsSource = Platform.select<
() => {
SHORT: number;
LONG: number;
TOP: number;
BOTTOM: number;
CENTER: number;
}
>({
ios: () => require('./NativeSimpleToast').default.getConstants(),
android: () => require('react-native').ToastAndroid,
harmony: () => require('react-native').ToastAndroid,
default: () => {
throw new Error(unsupportedPlatform);
},
})();
export default {
SHORT: constantsSource.SHORT,
LONG: constantsSource.LONG,
TOP: constantsSource.TOP,
BOTTOM: constantsSource.BOTTOM,
CENTER: constantsSource.CENTER,
show(message: string, duration: number, options?: StylesIOS) {
RCTToast.show(
message,
duration ?? constantsSource.SHORT,
processColors(options),
);
},
showWithGravity(
message: string,
duration: number,
gravity: number,
options?: StylesIOS,
) {
RCTToast.showWithGravity(
message,
duration ?? constantsSource.SHORT,
gravity,
processColors(options),
);
},
showWithGravityAndOffset(
message: string,
duration: number,
gravity: number,
xOffset: number,
yOffset: number,
options?: StylesIOS,
) {
RCTToast.showWithGravityAndOffset(
message,
duration ?? constantsSource.SHORT,
gravity,
xOffset,
yOffset,
processColors(options),
);
},
};
function processColors(options?: StylesIOS) {
if (Platform.OS === 'android' || !options) {
return undefined;
}
return {
// the types are not 100% correct
...options,
messageColor: processColor(options.textColor) as number | undefined,
backgroundColor: processColor(options.backgroundColor) as
| number
| undefined,
};
}