forked from gunnartorfis/sonner-native-toasts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpositioner.tsx
More file actions
58 lines (50 loc) · 1.43 KB
/
Copy pathpositioner.tsx
File metadata and controls
58 lines (50 loc) · 1.43 KB
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
import React from 'react';
import { Platform, View, type ViewStyle } from 'react-native';
import type { ToasterProps } from './types';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useToastContext } from './context';
export const Positioner: React.FC<
React.PropsWithChildren<Pick<ToasterProps, 'position' | 'style'>>
> = ({ children, position, style, ...props }) => {
const { offset } = useToastContext();
const { top, bottom } = useSafeAreaInsets();
const containerStyle = React.useMemo<ViewStyle>(() => {
if (position === 'center') {
return {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
justifyContent: 'center',
alignItems: 'center',
};
}
return {
position: 'absolute',
width: '100%',
alignItems: 'center',
};
}, [position]);
const insetValues = React.useMemo(() => {
if (position === 'bottom-center') {
return { bottom: offset || bottom || 40 };
}
if (position === 'top-center') {
return { top: offset || top || 40 };
}
return {};
}, [position, bottom, top, offset]);
const hasChildren = React.Children.count(children) > 0;
return (
<View
style={[containerStyle, insetValues, style]}
pointerEvents={
Platform.OS === 'android' && !hasChildren ? 'none' : 'box-none'
}
{...props}
>
{children}
</View>
);
};