1- import React , { useEffect , useMemo } from 'react'
1+ import React , { useEffect , useMemo , useRef } from 'react'
22import { Sheet , SheetProps } from 'react-modal-sheet'
3-
43import { useSheetStore , useChatStore , useStore } from '@stores'
54import FilterModal from '@components/pages/document/components/FilterModal'
65import NotificationModal from './notificationPanel/mobile/NotificationModal'
76import ChatContainerMobile from './pages/document/components/chat/ChatContainerMobile'
8- import useKeyboardHeight from '@hooks/useKeyboardHeight'
97import { EmojiPanel } from './chat/components/EmojiPanel'
108import { CHAT_OPEN } from '@services/eventsHub'
119
@@ -15,8 +13,11 @@ const BottomSheet = () => {
1513 const closeChatRoom = useChatStore ( ( state ) => state . closeChatRoom )
1614 const destroyChatRoom = useChatStore ( ( state ) => state . destroyChatRoom )
1715 const setSheetContainerRef = useSheetStore ( ( state ) => state . setSheetContainerRef )
18- const { height : keyboardHeight } = useKeyboardHeight ( )
16+ const setSheetState = useSheetStore ( ( state ) => state . setSheetState )
17+ const { keyboardHeight, virtualKeyboardState } = useStore ( ( state ) => state )
1918 const { deviceDetect } = useStore ( ( state ) => state . settings )
19+ const sheetContentRef = useRef < HTMLDivElement > ( null )
20+
2021 const isDeviceIOS = useMemo ( ( ) => {
2122 return deviceDetect ?. os ( ) === 'iOS'
2223 } , [ deviceDetect ] )
@@ -75,58 +76,60 @@ const BottomSheet = () => {
7576 }
7677 }
7778
78- const getSheetContentProps = ( ) => {
79- switch ( activeSheet ) {
80- case 'filters' :
81- return {
82- // Fix the bottom sheet height when keyboard is open
83- style : { paddingBottom : isDeviceIOS ? keyboardHeight : 0 }
84- }
85- case 'chatroom' :
86- return {
87- style : {
88- paddingBottom : isDeviceIOS ? keyboardHeight : 0
89- }
90- }
91- case 'emojiPicker' :
92- return {
93- style : {
94- paddingBottom : isDeviceIOS ? keyboardHeight : 0
95- }
96- }
97- default :
98- return { }
99- }
100- }
101-
10279 const handleClose = ( ) => {
10380 if ( activeSheet === 'chatroom' ) {
10481 closeChatRoom ( )
10582 destroyChatRoom ( )
10683 } else if ( activeSheet === 'emojiPicker' && sheetData . chatRoomState ) {
10784 const { headingId } = sheetData . chatRoomState
108- closeSheet ( )
109- setTimeout ( ( ) => {
110- PubSub . publish ( CHAT_OPEN , {
111- headingId : headingId ,
112- focusEditor : true ,
113- clearSheetState : true
114- } )
115- } , 100 )
85+ PubSub . publish ( CHAT_OPEN , {
86+ headingId : headingId ,
87+ focusEditor : true ,
88+ clearSheetState : true ,
89+ switchSheet : 'chatroom'
90+ } )
11691 } else {
11792 closeSheet ( )
11893 }
11994 }
12095
96+ useEffect ( ( ) => {
97+ if ( ! sheetContentRef . current ) return
98+
99+ const paddingBottom = isDeviceIOS ? Math . round ( keyboardHeight ) . toString ( ) + 'px' : '0px'
100+
101+ switch ( activeSheet ) {
102+ case 'filters' :
103+ sheetContentRef . current . style . paddingBottom = paddingBottom
104+ case 'chatroom' :
105+ sheetContentRef . current . style . paddingBottom = paddingBottom
106+ case 'emojiPicker' :
107+ sheetContentRef . current . style . paddingBottom =
108+ virtualKeyboardState === 'open' ? paddingBottom : '0px'
109+ default :
110+ return
111+ }
112+ } , [ sheetContentRef , activeSheet , keyboardHeight , isDeviceIOS , virtualKeyboardState ] )
113+
114+ // NOTE: these events are more reliable than the other events for sheet state
115+ const onOpenEndHandler = ( ) => setSheetState ( 'closed' )
116+ const onViewportEnterHandler = ( ) => setSheetState ( 'open' )
117+ const onOpenStartHandler = ( ) => setSheetState ( 'opening' )
118+ const onCloseStartHandler = ( ) => setSheetState ( 'closing' )
119+
121120 return (
122121 < Sheet
123122 className = "bottom-sheet"
124123 isOpen = { ! ! activeSheet }
125124 onClose = { handleClose }
125+ onCloseStart = { onCloseStartHandler }
126+ onOpenStart = { onOpenStartHandler }
127+ onOpenEnd = { onOpenEndHandler }
128+ onViewportEnter = { onViewportEnterHandler }
126129 { ...getSheetProps ( ) } >
127- < Sheet . Container ref = { setSheetContainerRef } >
130+ < Sheet . Container ref = { setSheetContainerRef } onViewportEnter = { onViewportEnterHandler } >
128131 < Sheet . Header />
129- < Sheet . Content { ... getSheetContentProps ( ) } > { renderContent ( ) } </ Sheet . Content >
132+ < Sheet . Content ref = { sheetContentRef } > { renderContent ( ) } </ Sheet . Content >
130133 </ Sheet . Container >
131134 < Sheet . Backdrop onTap = { handleClose } />
132135 </ Sheet >
0 commit comments