#22342·Avalonia

Keyboard focus stays on a removed element when an overlay popup/flyout closes

Author: dariostrmCreated Sep 29, 2026Updated Sep 29, 2026
Labelsbug

Describe the bug

The current behavior: When we attempt to restore focus inside Focus(null), but the element we're restoring to cannot be focused (anymore), we leave the keyboard focus as is. However, if Focus(null) was called because the focus scope we were in was destroyed, we do not want to leave the keyboard focus in that destroyed scope because that traps it.

This doesn't matter if native popups/flyouts are used, I'm not quite sure why

I will provide a PR shortly, should be a simple fix

To Reproduce

FlyoutFocusStuckBrowser.zip Small browser app (because the browser uses overlay popups/flyouts) In this app, you can open a flyout with the first button but while the flyout is open, the button is disabled After pressing the button inside the flyout (which closes the flyout), the keyboard focus is trapped inside it. Pressing tab/shift+tab doesn't do anything and space/enter press the removed button again

Image

Expected behavior

Closing the flyout should clear the focus if it cannot be restored and if the currently focused element has been removed/hidden

Avalonia version

12.1.3

OS

WebAssembly

Additional context

No response