One focused element
A vapors document has one focused element at a time. An element can take focus when it is a button or an input, or has tabindex="0" or tabindex="-1", unless it is disabled or has no box. This lesson follows the Bind an input lesson: that one connects a control to a signal, this one covers how a control gets focus and what the keyboard does once it has it.
The three controls in the lesson are demos you can run: the field demo, the checkbox demo and the number demo. Each has its source beside it.
Listen for focus and keys
onfocus, onblur and onkeydown take a closure, as onclick does. A closure that wants the event takes &mut Event; event.key() is the key as the DOM names it ("a", "Enter", " ").
<script>let focused: Signal<bool> = Signal::new(false);let last_key: Signal<String> = Signal::new(String::new());let on_focus = move || focused.set(true);let on_blur = move || focused.set(false);let on_key = move |event: &mut Event| last_key.set(event.key().to_owned());</script><style>:global(body) { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; padding: 16px; }input { width: 240px; }input:focus-visible { outline: 2px solid #3a7bd5; outline-offset: 2px; }</style><input onfocus={on_focus} onblur={on_blur} onkeydown={on_key} /><p>Focused: {focused}</p><p>Last key: {last_key}</p> Save this example over crates/demos/demos/hello.wisp and run cargo run -p vapors-app --example demo -- hello. Click the field, then press Tab: the first paragraph changes to Focused: true and back to false as focus arrives and leaves. The second paragraph shows the last key the field received.
How Tab moves
Tab and Shift+Tab move focus through the focusable elements whose tabindex is not -1, in document order. An element with tabindex="-1" can take focus from a press or from code, but Tab skips it. Past the last element, nothing has focus.
A positive tabindex is refused when the document updates, with a message that says a tabindex is 0 or -1; the order always follows the document.
When focus moves, blur and focusout fire on the element that loses it, then focus and focusin on the one that gains it.
What focus looks like
The document sets :focus on the focused element and :focus-within on it and its ancestors. :focus-visible is set when focus arrived by keyboard, or the element is a text field; a press on a button does not set it. The user-agent outline ring is drawn for :focus-visible unless the component sets outline, as the example’s input:focus-visible rule does.
What each control does with the keyboard
- Text field. Tab into it selects its text whole; a click puts the caret where it lands.
bind:valuereceives each edit as the field’sinputevent.changefires on Enter and on blur, when the text differs from the last committed text. - Number field. Typing, the left and right arrow keys, Home and End work as in a text field. Up and Down step the value by
step, andbind:valuetakes the stepped value at once; Shift with Up or Down steps by ten steps. The field commits on Enter and on blur, andbind:valuetakes the number then. A text that is no number, or is out of range, matches:invalid(the demo draws a red border). - Checkbox. Space toggles it, on the key coming up, as a click. Enter does not toggle a checkbox.
- Button. Enter presses it on the key going down, and Space on the key coming up.
Why does the number demo's readout not change while typing?
The number field commits on Enter or blur, and the signal takes the value then. A text field's signal changes with each edit.
Try the controls
Run the linked field, checkbox and number demos. Use Tab to reach each control, Shift+Tab to return, and the keys described above to edit or toggle it. Compare text-field updates while typing with number-field updates on Enter or blur.