@@ -53,18 +53,6 @@ function useContextValue(): ContextValue {
5353
5454 const [ shown , setShown ] = useState ( false ) ;
5555
56- // Close mobile sidebar on navigation pop
57- // Most likely firing when using the Android back button (but not only)
58- useHistoryPopHandler ( ( ) => {
59- if ( shown ) {
60- setShown ( false ) ;
61- // Prevent pop navigation; seems desirable enough
62- // See https://github.com/facebook/docusaurus/pull/5462#issuecomment-911699846
63- return false ;
64- }
65- return undefined ;
66- } ) ;
67-
6856 const toggle = useCallback ( ( ) => {
6957 setShown ( ( s ) => ! s ) ;
7058 } , [ ] ) ;
@@ -81,13 +69,45 @@ function useContextValue(): ContextValue {
8169 ) ;
8270}
8371
72+ // A component hook wrapper enables conditional rendering
73+ // See reason here: https://github.com/facebook/docusaurus/issues/10988
74+ function OnHistoryPop ( {
75+ handler,
76+ } : {
77+ handler : Parameters < typeof useHistoryPopHandler > [ 0 ] ;
78+ } ) {
79+ useHistoryPopHandler ( handler ) ;
80+ return null ;
81+ }
82+
8483export function NavbarMobileSidebarProvider ( {
8584 children,
8685} : {
8786 children : ReactNode ;
8887} ) : ReactNode {
8988 const value = useContextValue ( ) ;
90- return < Context . Provider value = { value } > { children } </ Context . Provider > ;
89+ return (
90+ < >
91+ {
92+ // Close mobile sidebar on navigation pop
93+ // Most likely firing when using the Android back button (but not only)
94+ // Important: we can only have a single history blocker at a time
95+ // That's why this needs to be rendered conditionally
96+ // See bug report https://github.com/facebook/docusaurus/issues/10988
97+ value . shown && (
98+ < OnHistoryPop
99+ handler = { ( ) => {
100+ value . toggle ( ) ;
101+ // Prevent pop navigation; seems desirable enough
102+ // See https://github.com/facebook/docusaurus/pull/5462#issuecomment-911699846
103+ return false ;
104+ } }
105+ />
106+ )
107+ }
108+ < Context . Provider value = { value } > { children } </ Context . Provider >
109+ </ >
110+ ) ;
91111}
92112
93113export function useNavbarMobileSidebar ( ) : ContextValue {
0 commit comments