Focus a text input as soon as it appears - #85
Open
levi730 wants to merge 1 commit into
Open
Conversation
A screen whose whole purpose is one field should not also ask the user to tap it. The pattern this comes from is a two-step flow — draw a signature, then name the signer — where the second step exists only to collect a name, and arriving there with the keyboard down reads as a dead end. The focus state was already there on both platforms; nothing exposed it. iOS holds a @focusstate bound to the field for `keep-focus-on-submit`, and this adds an .onAppear path to it. Android had no requester, so one is added as a Modifier extension shared by all three renderers. Both fire on first appearance only. iOS defers a runloop because @focusstate does not take while the view is still being installed; Android keys its LaunchedEffect on Unit rather than on the flag, so a recomposition cannot steal focus back from wherever the user has since moved it. iOS also skips disabled and read-only fields, which cannot hold first responder anyway. Blade `autofocus` / `auto-focus`, fluent `->autofocus()`. Opt-in, so no existing screen changes behaviour. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
autofocusto the text inputs, so a field can take focus and raise the keyboard as soon as it appears.Blade
autofocus/auto-focus, fluent->autofocus(). Opt-in, so no existing screen changes behaviour.Why
A screen whose whole purpose is one field shouldn't also ask the user to tap it.
This came out of a driver app with a two-step signature flow — draw a signature, then name the signer. The second step exists only to collect a name, and arriving there with the keyboard down reads as a dead end.
keep-focus-on-submitcovers "stay focused after return", but there's currently nothing for "start focused".How
The focus state was already there on both platforms; nothing exposed it.
NativeUITextInputCorealready holds a@FocusStatebound to the field forkeep-focus-on-submit. This adds an.onAppearpath to it, deferred a runloop because@FocusStatedoesn't take while the view is still being installed. It also skipsdisabled/read-onlyfields, which can't hold first responder anyway.Modifier.nuiAutofocus()is added alongsidenuiA11yinTextInputShared.ktand applied by all three renderers.Both fire on first appearance only. Android's
LaunchedEffectis keyed onUnitrather than on the flag, so a recomposition can't steal focus back from wherever the user has since moved it.Notes
autofocus="{{ $condition }}"behaves when the condition is false.Tests
tests/BaseTextInputAutofocusTest.php— 12 cases across all three variants: absent by default, both attribute spellings, falsy values ignored, and the fluent setter both ways. Verified failing without the source change (8 fail), andpint --testis clean.The renderer changes are Swift and Kotlin, so they're not covered by the PHP suite. I've run the iOS side on device; the Android path is written but untested on a device — worth a look from someone with an emulator handy.