forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useIsDrawerOpen hook (facebook#299)
- Loading branch information
Showing
3 changed files
with
37 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as React from 'react'; | ||
import { useNavigation, ParamListBase } from '@react-navigation/native'; | ||
import { DrawerNavigationProp } from '../types'; | ||
|
||
/** | ||
* Hook to detect if the drawer is open in a parent navigator. | ||
*/ | ||
export default function useIsDrawerOpen() { | ||
const navigation = useNavigation(); | ||
|
||
let drawer = navigation as DrawerNavigationProp<ParamListBase>; | ||
|
||
// The screen might be inside another navigator such as stack nested in drawer | ||
// We need to find the closest drawer navigator and add the listener there | ||
while (drawer && drawer.dangerouslyGetState().type !== 'drawer') { | ||
drawer = drawer.dangerouslyGetParent(); | ||
} | ||
|
||
const [isDrawerOpen, setIsDrawerOpen] = React.useState(() => | ||
drawer ? drawer.dangerouslyGetState().isDrawerOpen : false | ||
); | ||
|
||
React.useEffect(() => { | ||
const unsubscribe = drawer.addListener('state', e => { | ||
setIsDrawerOpen(e.data.state.isDrawerOpen); | ||
}); | ||
|
||
return unsubscribe; | ||
}, [drawer]); | ||
|
||
return isDrawerOpen; | ||
} |