Autofocus
Focuses the first matching field on connect() and again on relevant turbo:frame-load events. The native HTML
autofocus attribute does not fire on Turbo Drive visits or frame swaps — this controller fills that
gap so modals, frame-loaded forms and wizard steps land with the right field focused.
Identifier: autofocus
Loaded by: auto-loaded after php artisan hotwire:install; publish only to customize with
php artisan hotwire:controllers autofocus.
Requirements
- No external dependencies.
Targets
| Target | Description |
|---|---|
field |
The element to focus when strategy="target". Ignored by the other strategies. |
Values
| Value | Type | Default | Description |
|---|---|---|---|
strategy |
String | autofocus-attribute |
How to pick the element to focus. See Strategies. |
scroll-into-view |
Boolean | false |
When false (default), focus({ preventScroll: true }). When true, allow the page to scroll. |
Strategies
| Strategy | Picks |
|---|---|
autofocus-attribute |
First [autofocus] element inside the controller scope. Default. |
first-focusable |
First <input>, <select>, <textarea> or <button> inside the controller scope. |
first-invalid |
First visible, enabled, focusable control with aria-invalid="true". Does nothing without eligible errors. |
target |
The data-autofocus-target="field" element. |
All strategies skip elements that are [disabled], [type="hidden"], [tabindex="-1"], or inside a
[hidden] / [aria-hidden="true"] ancestor.
Behavior
- Runs on
connect()— covers the initial page load and any later mount of the controller. - Runs again on
turbo:frame-loadwhen the loaded frame is the controller element, an ancestor that owns it, or a descendant it contains. Unrelated sibling frames are ignored; a page-level controller still observes descendant frames. Does not run onturbo:render, so Drive's native focus restoration is left alone. - Never steals focus: if anything inside the controller scope is already the active element when the controller would run, the focus call is skipped.
first-invalidis stricter: it only recovers focus from the document body or the scope's own dialog container, preserving focus in another form or overlay too. It skips CSS-hidden, inert and disabled controls, non-focusable error wrappers and negative tabindex. On a form, controls associated with a different form are ignored.first-invalidalso observesrich-text:readywithin its scope. A pending first invalid Rich Text is not skipped for a later field, and readiness only recovers focus if the user has not already focused another control.
Basic Usage
The HTML autofocus attribute already says what to focus — drop the controller on the form so the
attribute keeps working across Turbo navigations.
<form data-controller="autofocus" action="/messages" method="POST">
<input type="text" name="title" autofocus/>
<textarea name="body"></textarea>
<button type="submit">Save</button>
</form>
Inside a modal
<hw:modal> can host frame-loaded content. Add the controller to the form returned into that frame so the right field is
focused after the modal opens.
{{-- Layout host --}}
<hw:modal frame="user-edit" />
{{-- Response to the frame request --}}
<hw:frame-or-page frame="user-edit">
<form data-controller="autofocus" action="..." method="POST">
<input type="text" name="name" autofocus />
...
</form>
</hw:frame-or-page>
Picking the first focusable field
Use first-focusable when the markup does not carry [autofocus] and you just want "whatever comes
first that the user can type into."
<form data-controller="autofocus" data-autofocus-strategy-value="first-focusable">
<input type="hidden" name="csrf" value="..."/>
<button type="button" tabindex="-1">Help</button>
<input type="text" name="title"/> {{-- focused --}}
<input type="text" name="slug"/>
</form>
Recovering focus after validation
Use <hw:form error-focus> for the packaged form integration, or compose the
strategy directly on a native form:
<form data-controller="autofocus" data-autofocus-strategy-value="first-invalid">
<label>Name <input name="name" value="Ada"></label>
<label>Email <input name="email" value="invalid" aria-invalid="true" aria-describedby="email-error"></label>
<p id="email-error">Enter a valid email address.</p>
<button type="submit">Save</button>
</form>
After a Frame replacement removes the focused input, this strategy focuses the first eligible invalid control rather than returning to the first field regardless of errors. Mark the interactive trigger or editor for custom widgets; a hidden transport input/select or a non-focusable error wrapper is not a focus destination. Focus does not scroll by default, and a relevant Frame load does not take focus from a control the user already chose.
In-place Turbo Drive morphs that preserve the form/controller do not start a new focus attempt: there is no new connect()
or Frame load, and the controller deliberately does not listen to turbo:render. Focus may stay in the input after
Enter, or fall back to the body if Turbo disables a clicked submit button; first-invalid does not redirect it after
that morph.
Picking an explicit target
When the field to focus is buried below a header or sidebar, mark it with the field target.
<section data-controller="autofocus" data-autofocus-strategy-value="target">
<header>
<button>Back</button>
</header>
<form>
<input type="text" name="search" data-autofocus-target="field"/>
</form>
</section>
Letting the page scroll
By default the controller focuses with { preventScroll: true } — useful inside modals and other
overlays. Set scroll-into-view-value="true" when you do want the page to scroll the field into
view, for example on a long settings page after a frame swap.
<form data-controller="autofocus"
data-autofocus-strategy-value="first-focusable"
data-autofocus-scroll-into-view-value="true">
...
</form>