Skip to content

Owner focus set in onOpenChange(false) is lost in controlled dialogs and on a popover outside click #442

Open

Description

@JuroOravec

Problem

An owner who moves focus inside onOpenChange(false) loses that focus move in two cases. These were found while fixing the same problem for CCommandPalette's onAction (#420), and each case now has a strict-xfail e2e test.

  1. Controlled CDialog, CAlertDialog and CDrawer. Close the popup with Escape, the close button, or (for CAlertDialog) the cancel or action button. Focus then ends on the trigger, not on the element the owner focused. This happens whether the owner focuses before or after setting open = false in the same callback.
    • In controlled mode the component calls onOpenChange(false) before it closes (requestOpen in cdialog/cdialog.py notifies and returns). The owner's focus() therefore runs while the modal still makes the rest of the page inert, so the browser ignores it. When the owner's open = false is applied, the shared dialog controller returns focus to the trigger.
    • Uncontrolled mode closes first and then notifies, and there the owner's focus is kept.
  2. CPopover outside click, controlled and uncontrolled. Focus ends on body. The popover closes on pointerdown (_anchored_layer.py), so onOpenChange(false) runs before the browser's own mousedown focus step, which then moves focus to body. With Escape, the owner's focus is kept.

CContextMenu keeps the owner's focus in all six cases tested.

Options

  • Controlled dialogs. After a controlled close applies, run any focus the owner asked for. Or skip the return focus to the trigger when focus already moved outside the dialog. Since the browser ignores focus() on an inert element, the owner cannot move focus inside the callback today, so the fix probably needs a documented way to name the element that should get focus on close.
  • CPopover outside click. Leave focus where the pointer put it. Then document that an owner who wants to move focus on an outside click does it after the pointer event, for example in a requestAnimationFrame. Or delay the notification until after the mousedown default action.

Done when

  • The strict-xfail tests named test_owner_focus_move_in_on_open_change_survives_* in the cdialog, calert_dialog, cdrawer and cpopover e2e suites pass, and their xfail marks are removed.
  • Each component's api.md says when onOpenChange(false) runs relative to the close and to focus return.

Activity

  1. added
    next-sprint
    on Oct 27, 2026
  2. JuroOravec commented on Oct 27, 2026

    @JuroOravec
    MemberAuthor

    Parked for after 0.7.0. The proposed design (a finalFocus(reason) prop shared by CDialog, CAlertDialog, CDrawer, CPopover and the palette's onOpenChange) is approved. When this is picked up, the strict xfail tests that define "done" may be rewritten to test finalFocus instead of a plain focus() inside the callback.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions