Keep keyboard focus on a control while it is disabled (e.g. a Button whose async command is running)
Is your feature request related to a problem? Please describe.
A focused Button whose command reports CanExecute = false while it runs loses keyboard focus every time it's activated with the keyboard. That's the default for ReactiveUI's ReactiveCommand, CommunityToolkit.Mvvm's AsyncRelayCommand. InputElement calls Focus(null) when the focused element is disabled (added in #9627). Focus doesn't come back when the command completes, and the next Tab starts over at the first control. The same happens when such a button opens a popup or dialog: when it closes, focus can't return to the button and is lost.
Repro: Tab to button 2, 3 or 4 and press Space. Nothing is focused afterwards, and the next Tab goes to button 1. Button 1 (plain command, never disabled) keeps its focus. CommandButtonFocusLoss.zip
This was reported in #12705 and #12978 and considered by design. I'd like to propose changing that, while keeping what #9627 fixed (#7294: a disabled but focused TextBox still accepted typing) because I think it would be better for UX, because the user doesn't expect to have to start over with the tab navigation after clicking a button.
Describe the solution you'd like
Disabled controls should be able to keep focus, but not gain it:
- The focus outline (:focus-visible) stays, and Tab/Shift+Tab continue from it.
- While disabled, it still gets no keyboard or text input, as today. I think key presses would go to its ancestors then.
- Disabled controls still shouldn't be able to gain focus
- Returning focus to where it was should be done even if that control is disabled, e.g. if the popup was opened by a button and the button is disabled until the popup closes, when the user closes the popup, that button should be focused
I have a working implementation with tests in a fork and would open a PR if this direction is OK. If changing the default is too much, it could be opt-in instead?
Describe alternatives you've considered
- Browser behavior: drop the focus as today, but remember the control as the starting point for Tab/Shift+Tab while nothing is focused, so the user doesn't lose their place. That leaves the question of whether to give focus back once the control is enabled again. Without restoring, the user has to press Tab to get focus back after every command. With restoring, it gets complicated because you would need to track whether the user moved on in the meantime so we cancel the restore (clicked or activated something else). I think the downside is here that the the focus outline would still disappear, so it's a bit worse in terms of UX
Additional context
For comparison, when the focused control is disabled:
- WPF moves focus to the window, so the user loses their place, as in Avalonia today.
- WinUI moves focus to the next focusable control (or the first, if there is no next one). Slightly worse UX than what I'm proposing imo
- WinUI also has the
AllowFocusWhenDisabledproperty but when that is set to true, the control can also gain focus which isn't intuitive imo because tabbing through would also stop at that control even if it is disable (I mean it is intuitive for how the property is called, but not an intuitive solution to the problem because of the tradeoff) - Browsers (tested in Chromium) also drop the focus, but Tab and Shift+Tab continue from where the disabled button was. That's the best experience of the three, although nothing is focused while the command runs.
Finally, a library that solves this in a similar way I'm proposing: Adobe's React Aria, which is known for its focus on accessibility and UX. Its Button has a pending state (isPending) for exactly this case, e.g. while a form is being submitted: "Pending buttons remain focusable, but are otherwise disabled." So the focus stays on the button while its action runs, presses are ignored until it's done, and screen readers announce the pending state.
Source: AvaloniaUI/Avalonia