@@ -110,6 +110,7 @@ export function GlobalDrawer({children}: any) {
110
110
const [ currentDrawerConfig , overwriteDrawerConfig ] = useState <
111
111
DrawerConfig | undefined
112
112
> ( ) ;
113
+ const currentDrawerConfigRef = useRef ( currentDrawerConfig ) ;
113
114
// If no config is set, the global drawer is closed.
114
115
const isDrawerOpen = ! ! currentDrawerConfig ;
115
116
const openDrawer = useCallback < DrawerContextType [ 'openDrawer' ] > ( ( renderer , options ) => {
@@ -126,28 +127,27 @@ export function GlobalDrawer({children}: any) {
126
127
closeDrawer ( ) ;
127
128
} , [ currentDrawerConfig , closeDrawer ] ) ;
128
129
130
+ currentDrawerConfigRef . current = currentDrawerConfig ;
131
+
129
132
// Close the drawer when the browser history changes.
130
133
useLayoutEffect (
131
134
( ) => {
132
- // Defaults to closing the drawer when the location changes
133
135
if (
134
- // Drawer should be closed already if `currentDrawerConfig` is undefined
135
- currentDrawerConfig !== undefined &&
136
- ( currentDrawerConfig ?. options . shouldCloseOnLocationChange ?.( location ) ?? true )
136
+ // No need to close drawer if it is not open
137
+ currentDrawerConfigRef . current !== undefined &&
138
+ // Otherwise, when the location changes, check callback or default to closing the drawer if it doesn't exist
139
+ ( currentDrawerConfigRef . current . options ?. shouldCloseOnLocationChange ?.(
140
+ location
141
+ ) ??
142
+ true )
137
143
) {
138
144
// Call `closeDrawer` without invoking `onClose` callback, since those callbacks often update the URL
139
145
closeDrawer ( ) ;
140
146
}
141
147
} ,
142
- // Ignoring changes to currentDrawerConfig?.options to prevent closing the drawer when it opens
143
- // eslint-disable-next-line react-hooks/exhaustive-deps
144
- [
145
- location ?. pathname ,
146
- location ?. search ,
147
- location ?. hash ,
148
- closeDrawer ,
149
- currentDrawerConfig ?. options . shouldCloseOnLocationChange ,
150
- ]
148
+ // Ignoring changes to currentDrawerConfig and currentDrawerConfig?.options
149
+ // to prevent closing the drawer when it opens.
150
+ [ closeDrawer , location ]
151
151
) ;
152
152
153
153
// Close the drawer when clicking outside the panel and options allow it.
0 commit comments