forked from gorhom/react-native-bottom-sheet
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBottomSheetContainer.tsx
95 lines (87 loc) · 2.25 KB
/
BottomSheetContainer.tsx
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
import React, { memo, useCallback, useMemo, useRef } from 'react';
import {
LayoutChangeEvent,
StatusBar,
StyleProp,
useWindowDimensions,
View,
ViewStyle,
} from 'react-native';
import { print } from '../../utilities';
import { styles } from './styles';
import type { BottomSheetContainerProps } from './types';
function BottomSheetContainerComponent({
containerHeight,
containerOffset,
topInset = 0,
bottomInset = 0,
shouldCalculateHeight = true,
detached,
style,
children,
}: BottomSheetContainerProps) {
const containerRef = useRef<View>(null);
//#region styles
const containerStyle = useMemo<StyleProp<ViewStyle>>(
() => [
style,
styles.container,
{
top: topInset,
bottom: bottomInset,
overflow: detached ? 'visible' : 'hidden',
},
],
[style, detached, topInset, bottomInset]
);
//#endregion
const windowDimensions = useWindowDimensions();
const windowHeight = windowDimensions.height;
//#region callbacks
const handleContainerLayout = useCallback(
function handleContainerLayout({
nativeEvent: {
layout: { height },
},
}: LayoutChangeEvent) {
containerHeight.value = height;
containerRef.current?.measure(
(_x, _y, _width, _height, _pageX, pageY) => {
containerOffset.value = {
top: pageY ?? 0,
left: 0,
right: 0,
bottom: Math.max(
0,
windowHeight -
((pageY ?? 0) + height + (StatusBar.currentHeight ?? 0))
),
};
}
);
print({
component: BottomSheetContainer.displayName,
method: 'handleContainerLayout',
params: {
height,
},
});
},
[containerHeight, containerOffset, containerRef, windowHeight]
);
//#endregion
//#region render
return (
<View
ref={containerRef}
pointerEvents="box-none"
onLayout={shouldCalculateHeight ? handleContainerLayout : undefined}
style={containerStyle}
children={children}
/>
);
//#endregion
}
const BottomSheetContainer = memo(BottomSheetContainerComponent);
BottomSheetContainer.displayName = 'BottomSheetContainer';
export default BottomSheetContainer;