Skip to content

A modal that returns focus to nothing lets it land later on a control behind the dialog #3284

Done

Description

@Orkunnnn

What happens

A modal opened while nothing had focus puts focus in one of its fields. When Escape closes it, the modal machine returns focus to where it was before: nowhere. A few commits later focus appears on a control behind the dialog, with its focus ring showing, and Return or Space then presses that control.

This affects apps that keep focus empty most of the time, where a dialog is often opened with nothing focused.

Reproduction

On dev at 2251512, added to crates/rune/src/runtime/input/focus_routing_tests.rs (it uses that file's button helper):

struct Dialog { open: bool, field: crate::TextEdit }
fn dialog_view(s: &Dialog, _: &mut Cx<Dialog>) -> El {
    col().size(300.0, 200.0)
        .child(button("status"))
        .children(s.open.then(|| col().key("dialog").absolute(100.0, 0.0).size(160.0, 100.0)
            .modal(true, |s: &mut Dialog| s.open = false)
            .child(crate::text_input(&s.field, crate::TextStyle::new(14.0, 400.0)).key("name").size(160.0, 30.0)
                .on_edit(|s: &mut Dialog| &mut s.field))))
}

#[test]
fn a_dialog_closed_from_the_keyboard_returns_focus_to_nothing_when_nothing_had_it() {
    let mut rt = Runtime::new(Dialog { open: false, field: crate::TextEdit::new("") }, dialog_view,
        (300.0, 200.0), Env::new(true, 1.0));
    rt.set_tab_visits_controls(true);
    rt.frame(0.0);
    rt.update(|s, tx| { s.open = true; tx.focus_when_shown("name", true); });
    for _ in 0..30 { rt.frame(1.0 / 60.0); }
    rt.key(KeyEvent { key: Key::Escape, mods: Mods::default() });
    rt.frame(1.0 / 60.0);
    assert!(!rt.state.open);
    // Commits while the field's exit plays and after it ends, as an app's polls make.
    for _ in 0..60 { rt.update(|_, _| {}); rt.frame(1.0 / 60.0); }
    assert!(!rt.has_focus(), "focus went back to where it was before the dialog opened: nowhere");
}

cargo test -p rune --lib a_dialog_closed_from_the_keyboard fails on the last assertion. Focus is on #0/status, the button behind the dialog, with focus_visible: true.

Why

The field the modal took focus from is still in the tree while the dialog's exit plays, and the blur leaves it recorded as the blurred focus (now_or_blurred in focus_machine.rs). Once the field is gone, the next commit's focus rescue (focus_fallback in crates/rune/src/runtime/input/focus/set.rs) reads that and moves focus to the nearest control left. The return in modal_focus.rs does not drop that hand-off the way Tx::blur does.

Expected

Focus stays on nothing after the dialog closes. focus_fallback's own documentation says: "A modal's own focus is left to the modal machine, which returns it where it was." The machine returned it to nowhere, and the rescue then overrode that.

Activity

  1. Hona commented on Dec 2, 2026

    @Hona
    Member

    Fixed in #3311 (commit c2cf592108d5b2764db87633522fda3a0edcddb9).

    Design decision: When ModalEffect::Return leaves focus on nowhere (either because nothing was focused before the modal opened or because the saved restore target can no longer take focus), it now steps FocusEvent::Dropped and FocusEvent::Settled—matching Tx::blur—so an exiting text field inside the closed modal cannot hand focus off via focus_fallback to a control behind it on a later commit.

    Verification:

    • Verified locally on Windows with headless unit tests (reproduced pre-fix failures and verified post-fix passes for Escape and programmatic close with no prior focus, disabled/removed restore target with a focused modal text field, and pane-scoped modals), cargo test -p rune --lib modal (94 passed), cargo test -p rune --lib focus (322 passed), cargo clippy -p rune --lib --tests --no-deps, cargo check -p rune --target wasm32-unknown-unknown, and scripts/file-size.sh, plus green GitHub CI (check, windows-check, clippy, android, size).
    • Not run interactively on live native GUI windows or real hardware (macOS, Linux/Wayland/X11, browser, or Android/iOS).

    Please reopen if you still see focus land behind a closed modal.

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