Unverified Commit 145d7613 authored by Rob Pruzan's avatar Rob Pruzan Committed by GitHub

fix: update useEffect closure when menu is truly mounted (#81531)

Previously we mounted event handlers on the the menu's
[`ownerDocument`](https://developer.mozilla.org/en-US/docs/Web/API/Node/ownerDocument)
to listen for clicks outside the menu. This causes a bug where the ref
the useEffect captures is not set yet, and when it is set the effect
does not try to run again. The fix is to derive active state from the
true state tracking if the menu is mounted, not if the menu __will be
open__

### Before

https://github.com/user-attachments/assets/d88f9b46-2203-45f3-a184-46db39584679

### After


https://github.com/user-attachments/assets/57a0f68f-8d46-41ea-b761-222c82e4b6bc
parent 3be29d88
...@@ -163,8 +163,8 @@ function DevToolsPopover({ ...@@ -163,8 +163,8 @@ function DevToolsPopover({
) )
// Features to make the menu accessible // Features to make the menu accessible
useFocusTrap(menuRef, triggerRef, isMenuOpen) useFocusTrap(menuRef, triggerRef, menuMounted)
useClickOutside(menuRef, triggerRef, isMenuOpen, closeMenu) useClickOutside(menuRef, triggerRef, menuMounted, closeMenu)
useShortcuts(hideShortcut ? { [hideShortcut]: hideDevTools } : {}, triggerRef) useShortcuts(hideShortcut ? { [hideShortcut]: hideDevTools } : {}, triggerRef)
useEffect(() => { useEffect(() => {
......
...@@ -33,11 +33,11 @@ export function DevToolsInfo({ ...@@ -33,11 +33,11 @@ export function DevToolsInfo({
exitDelay: MENU_DURATION_MS, exitDelay: MENU_DURATION_MS,
}) })
useFocusTrap(ref, triggerRef, isOpen, () => { useFocusTrap(ref, triggerRef, mounted, () => {
// Bring focus to close button, so the user can easily close the overlay // Bring focus to close button, so the user can easily close the overlay
closeButtonRef.current?.focus() closeButtonRef.current?.focus()
}) })
useClickOutside(ref, triggerRef, isOpen, close) useClickOutside(ref, triggerRef, mounted, close)
if (!mounted) { if (!mounted) {
return null return null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment