Skip to content

Fix: modals closing unexpectedly when browser window loses focus - #4940

Open
Simciutoja wants to merge 1 commit into
Dokploy:canaryfrom
Simciutoja:canary
Open

Fix: modals closing unexpectedly when browser window loses focus#4940
Simciutoja wants to merge 1 commit into
Dokploy:canaryfrom
Simciutoja:canary

Conversation

@Simciutoja

@Simciutoja Simciutoja commented Jul 30, 2026

Copy link
Copy Markdown

Summary

  • Fixes modals (eg. Project → Create Service, and the "Create from Template" modal) closing on their own when the browser window loses/regains focus (clicking outside the browser, alt-tabbing, switching tabs).
  • Root cause: Radix's DismissableLayer (used under the hood by Dialog, 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 a DropdownMenu, as is the case for the "Create Service"/"Create from Template" triggers.
  • Added a small global tracker (wasWindowRecentlyBlurred) in nested-popup-context.ts that records the last window blur/focus/visibilitychange event, and wired it into onFocusOutside/onInteractOutside handlers on DialogContent, AlertDialogContent, SheetContent, PopoverContent, and DropdownMenuContent so 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

  • Open Project → "+" → Application (or Database/Compose) to trigger the "Create Service" dialog, click outside the browser window (eg. taskbar/another app), then click back into the browser - dialog should stay open.
  • Open the "Template" modal, repeat the same window blur/refocus test - modal should stay open, and the tag filter Popover inside it should behave the same way.
  • Verify clicking truly outside the dialog (on the page overlay) still closes it as before.
  • Verify pressing Escape still closes dialogs/sheets as before.
  • Smoke-test Sheet, Popover, and DropdownMenu usages 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.

  • Adds blur, focus, and visibility-change tracking.
  • Guards outside interactions in dialogs, sheets, popovers, and dropdown menus.
  • Extends nested-popup Escape suppression to sheets and alert dialogs, but omits the browser-focus guard from alert dialogs.

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

Greptile also left 1 inline comment on this PR.

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
Simciutoja requested a review from Siumauricio as a code owner July 30, 2026 15:39
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jul 30, 2026
Comment thread apps/dokploy/components/ui/alert-dialog.tsx
@damiogunsiji

Copy link
Copy Markdown

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants