1
- import { createElement , ReactNode , ReactElement , useCallback , useState , useRef , Children } from "react" ;
1
+ import { createElement , ReactNode , ReactElement , useCallback , useState , useRef , Children , useMemo } from "react" ;
2
2
import { Dimensions , LayoutChangeEvent , SafeAreaView , StyleSheet , View } from "react-native" ;
3
3
import BottomSheet , { BottomSheetView } from "@gorhom/bottom-sheet" ;
4
4
import { BottomSheetStyle } from "../ui/Styles" ;
@@ -26,23 +26,29 @@ export const ExpandingDrawer = (props: ExpandingDrawerProps): ReactElement => {
26
26
const isSmallContentValid = Children . count ( props . smallContent ) > 0 ;
27
27
const isLargeContentValid = Children . count ( props . largeContent ) > 0 ;
28
28
29
- const onLayoutHandlerHeader = ( event : LayoutChangeEvent ) : void => {
30
- const height = event . nativeEvent . layout . height ;
31
- if ( height > 0 && height <= maxHeight ) {
32
- setHeightHeader ( height ) ;
33
- }
34
- } ;
29
+ const onLayoutHandlerHeader = useCallback (
30
+ ( event : LayoutChangeEvent ) : void => {
31
+ const height = event . nativeEvent . layout . height ;
32
+ if ( height > 0 && height <= maxHeight ) {
33
+ setHeightHeader ( height ) ;
34
+ }
35
+ } ,
36
+ [ maxHeight ]
37
+ ) ;
35
38
36
- const onLayoutHandlerContent = ( event : LayoutChangeEvent ) : void => {
37
- const height = event . nativeEvent . layout . height ;
38
- if ( height > 0 ) {
39
- if ( height <= maxHeight ) {
40
- setHeightContent ( height ) ;
41
- } else if ( ! props . fullscreenContent ) {
42
- setHeightContent ( maxHeight ) ;
39
+ const onLayoutHandlerContent = useCallback (
40
+ ( event : LayoutChangeEvent ) : void => {
41
+ const height = event . nativeEvent . layout . height ;
42
+ if ( height > 0 ) {
43
+ if ( height <= maxHeight ) {
44
+ setHeightContent ( height ) ;
45
+ } else if ( ! props . fullscreenContent ) {
46
+ setHeightContent ( maxHeight ) ;
47
+ }
43
48
}
44
- }
45
- } ;
49
+ } ,
50
+ [ maxHeight , props . fullscreenContent ]
51
+ ) ;
46
52
47
53
const onLayoutFullscreenHandler = ( event : LayoutChangeEvent ) : void => {
48
54
const height = event . nativeEvent . layout . height ;
@@ -54,6 +60,19 @@ export const ExpandingDrawer = (props: ExpandingDrawerProps): ReactElement => {
54
60
const containerStyle =
55
61
props . fullscreenContent && isOpen ? props . styles . containerWhenExpandedFullscreen : props . styles . container ;
56
62
63
+ const snapPoints = useMemo ( ( ) => {
64
+ if ( props . fullscreenContent && heightContent ) {
65
+ return [ fullscreenHeight , heightContent , heightHeader ] ;
66
+ }
67
+ if ( props . fullscreenContent ) {
68
+ return [ fullscreenHeight , heightHeader ] ;
69
+ }
70
+ if ( isLargeContentValid ) {
71
+ return [ heightContent , heightHeader ] ;
72
+ }
73
+ return [ heightHeader ] ;
74
+ } , [ props . fullscreenContent , heightContent , fullscreenHeight , heightHeader , isLargeContentValid ] ) ;
75
+
57
76
const renderContent = useCallback ( ( ) : ReactNode => {
58
77
const content = (
59
78
< View onLayout = { onLayoutHandlerContent } pointerEvents = "box-none" >
@@ -77,7 +96,15 @@ export const ExpandingDrawer = (props: ExpandingDrawerProps): ReactElement => {
77
96
) ;
78
97
}
79
98
return content ;
80
- } , [ props . smallContent , props . largeContent , props . fullscreenContent , isOpen , fullscreenHeight ] ) ;
99
+ } , [
100
+ props . smallContent ,
101
+ props . largeContent ,
102
+ props . fullscreenContent ,
103
+ fullscreenHeight ,
104
+ isSmallContentValid ,
105
+ onLayoutHandlerContent ,
106
+ onLayoutHandlerHeader
107
+ ] ) ;
81
108
82
109
if ( props . fullscreenContent && fullscreenHeight === 0 ) {
83
110
return (
@@ -91,18 +118,9 @@ export const ExpandingDrawer = (props: ExpandingDrawerProps): ReactElement => {
91
118
return < View style = { { position : "absolute" , bottom : - maxHeight } } > { renderContent ( ) } </ View > ;
92
119
}
93
120
94
- const snapPoints =
95
- props . fullscreenContent && heightContent
96
- ? [ fullscreenHeight , heightContent , heightHeader ]
97
- : props . fullscreenContent
98
- ? [ fullscreenHeight , heightHeader ]
99
- : isLargeContentValid
100
- ? [ heightContent , heightHeader ]
101
- : [ heightHeader ] ;
102
-
103
121
const collapsedIndex = 0 ;
104
122
105
- const onChange = ( index : number ) => {
123
+ const onChange = ( index : number ) : void => {
106
124
const hasOpened = lastIndexRef === - 1 && index === 0 ;
107
125
const hasClosed = index === - 1 ;
108
126
0 commit comments