Focus
Keyboard focus identifies the control that receives typing and other keyboard input. A visible focus indicator and a predictable order help people move through a screen without a pointer.
Bind keyboard focus to a signal to observe the active control or move focus from your app. Text inputs support this across toolkits. Focusable containers and non-text controls have platform limits described below.
The UI snippets use generated res::str accessors from an app’s Fluent catalog; see the
example messages and catalog setup.
Authoring
Focus binds to a signal with .focused(…), exactly like every other two-way control binding.
Use a boolean for one control, or an optional key to coordinate a group:
// One field: a Bool signal. Native focus writes it; writing it moves focus.
let editing = Signal::new(false);
text_field(query).focused(editing)
// A form: one Option<K> signal for the whole group (the recommended shape).
#[derive(Clone, Copy, PartialEq, Eq)]
enum Field { User, Pass }
let focus = Signal::new(Some(Field::User));
column((
text_field(user)
.on_submit(move || focus.set(Some(Field::Pass)))
.focused((focus, Field::User)),
text_field(pass).focused((focus, Field::Pass)),
button(res::str::log_in()).action(move || {
if user.with(|s| s.is_empty()) { focus.set(Some(Field::User)); return; }
// Continue with the app's sign-in action.
}),
))
- Native → signal: when the control gains focus the signal becomes
true/Some(K); when it loses focus,false/None, unless another bound control gained it in the same turn, in which case the group signal moves straight to that control’s value (noNonein between). - Signal → native: writing
Some(K)/truerequests native focus for the bound control. WritingNone/falseresigns it; on iOS and Android that dismisses the soft keyboard, matching the platform convention SwiftUI set. - Read the binding.
focus.get() == Some(Field::User)is a tracked read like any other, with nothing else to wire. - Field chaining is a write in
on_submit(f), to move to the next field after submission.
Focus requests and lifetime
Writing a focus binding updates the signal immediately and requests a native focus change. The platform may refuse the request, for example when the control cannot take focus. A signal read immediately after the write does not confirm that the native control has gained focus; subsequent native focus events reconcile the binding with the actual state.
When a bound control mounts and its binding already selects it, it requests focus. This lets
an app select a field before presenting a sheet. An initial false or None does not resign
another control’s focus.
A text field that should select its whole text on focus, as a browser’s address bar does,
fires its .select trigger when the binding turns on
(docs/textfield). A selection fired in the same handler that writes
the binding is applied after the focus request.
Moving between controls in the same group updates the selected key. Losing focus without a successor clears it. Tab and Shift-Tab traversal follows the platform’s focus order; Day does not expose custom focus-order or dialog focus-scope builders.
Which controls take focus
Text fields take focus on every toolkit. Desktop buttons, toggles, and sliders follow the
platform’s keyboard-access settings; macOS button traversal depends on Full Keyboard Access.
On touch platforms, non-text controls generally do not take keyboard focus. A canvas can
receive focus and key events; on Android, press-to-focus applies when it has an .on_key handler.
A canvas that declared .keyboard()
is a text-input target as far as the platform is concerned: focusing it raises the soft keyboard
on iOS, Android and the Web, clearing its focus binding dismisses it, exactly as for a text
field, and a composition in progress is dropped with the focus (docs/menus.md “The
full route”).
.focusable() makes a composed container a keyboard target. A focusable piece:
- joins the platform’s focus order, at its position in the tree, and takes focus when
.focused(binding)asks for it; - takes focus when pressed, before the press reaches its gesture handlers;
- reports gaining and losing focus, so a
.focusedbinding and anInteractionStatefollow it; - hears keys through
.on_keyand.on_key_upwhile it holds focus, and only then (menus “Keys”); - shows the platform’s focus indicator when focus arrived from the keyboard.
capability(Cap::Focusable) reports whether the toolkit honors it. Where it answers
Unsupported, the piece renders and never takes the keyboard: bind focus to a native control
or a canvas inside the container there, and offer the same commands through buttons or a menu.
Toolkit support
| Toolkit | Limitation |
|---|
Mobile keyboards
Clearing an editor’s focus binding dismisses the keyboard. UIKit and Android also end editing when navigation, cover dismissal, or subtree removal takes its editing surface away. Moving between text fields keeps the keyboard open. Button actions that leave a form visible can clear its focus binding explicitly after successful submission.
Place long forms in a scroll view. Android and UIKit resize the available content area when the keyboard appears and reveal the focused field, in a cover or sheet as well as in the window, so a button row at the bottom of a sheet stays above the keyboard. HarmonyOS uses ArkUI’s resize avoidance mode. Test field visibility at the bottom of the form and after changing keyboard height. The smaller content area does not change the window’s size class, so a tablet layout stays a tablet layout while the keyboard is up.