Skip to content

CCommandPalette: an owner's focus move in onAction is lost when the command closes the palette #420

Closed

Description

@JuroOravec

What happens

The CCommandPalette API page says that when onAction(value, detail) deliberately moves focus, "that connected focus destination wins over Dialog return-focus behavior" (ccommand_palette/api.md, "Choose action and close policy"). When the chosen command also closes the palette, focus ends up on <body> instead.

Reproduce

Use the command_actions snippet (packages/py/citry_ui/citry_ui/components/ccommand_palette/snippets/command_actions.py), shown on the Command Palette docs page.

  1. Tick Move owner focus. The snippet's onAction then calls $refs.focusTarget.focus() on the "Owner focus target" button.
  2. Open the palette and choose a command that closes it. Use any command without close_on_action=False; the default is to close.
  3. Check document.activeElement.

Expected: the "Owner focus target" button.
Actual: BODY.

Why

onAction runs synchronously before the close. While the palette's modal is open, the rest of the page is inert, so the owner's focus() call reaches a button that cannot take focus yet. The close then runs, and no focus move is left to apply.

Likely fix

Record the owner's focus request made during onAction, and apply it once the modal has closed and the target is no longer inert. If the owner moved focus, skip the usual return-focus step. CDialog, CAlertDialog, CDrawer and CPopover need checking for the same ordering problem, wherever an owner callback that runs while the layer is open moves focus outside it.

Found while converting snippet handlers for #141.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions