Fix: modals closing unexpectedly when browser window loses focus - #4940
Open
Simciutoja wants to merge 1 commit into
Open
Fix: modals closing unexpectedly when browser window loses focus#4940Simciutoja wants to merge 1 commit into
Simciutoja wants to merge 1 commit into
Conversation
focus Radix's DismissableLayer can mistake a window blur/refocus (eg. clicking outside the browser, alt-tabbing, or switching tabs) for an outside interaction and dismiss the topmost overlay. This closed modals such as "Create Service" and "Create from Template" whenever the page lost focus. Track window blur/focus globally and ignore spurious onFocusOutside/onInteractOutside dismissals in Dialog, AlertDialog, Sheet, Popover and DropdownMenu content components.
|
@Simciutoja, thanks for this. It is really frustrating to fill out a modal form, switch tabs to copy some information, and return only to find the modal has closed and all the progress is lost. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DismissableLayer(used under the hood byDialog,AlertDialog,Sheet,Popover,DropdownMenu) listens for focus moving "outside" the overlay to decide when to dismiss it. When the window itself loses/regains OS-level focus, some browsers fire a native blur/focus event on the page that Radix mistakes for a genuine outside click, closing the overlay even though the user never interacted with anything behind it. This is a known Radix issue class (see radix-ui/primitives#499) and is more likely to surface for dialogs nested inside aDropdownMenu, as is the case for the "Create Service"/"Create from Template" triggers.wasWindowRecentlyBlurred) innested-popup-context.tsthat records the lastwindowblur/focus/visibilitychangeevent, and wired it intoonFocusOutside/onInteractOutsidehandlers onDialogContent,AlertDialogContent,SheetContent,PopoverContent, andDropdownMenuContentso dismissal is ignored for a short window (150ms) around a focus change caused by the browser window itself, while normal "click outside to close" and Escape behavior are untouched.Test plan
Popoverinside it should behave the same way.Escapestill closes dialogs/sheets as before.Sheet,Popover, andDropdownMenuusages elsewhere in the app still open/close normally.Greptile Summary
This PR introduces a global browser-focus timestamp and uses it to prevent Radix overlays from dismissing shortly after window focus transitions.
Confidence Score: 4/5
The AlertDialog omission should be fixed before merging because that overlay remains exposed to the unexpected window-focus dismissal targeted by this PR.
The new focus-transition guard is consistently installed on the other Radix content wrappers, but AlertDialogContent only handles Escape and therefore does not receive the intended fix.
Files Needing Attention: apps/dokploy/components/ui/alert-dialog.tsx
Reviews (1): Last reviewed commit: "fix: prevent dialogs/popovers from closi..." | Re-trigger Greptile