fix(swap): auto-focus input field when swap view opens - #5619
Conversation
When the user opens the swap page, the source token input field is now automatically focused so they can immediately start typing. Resolves REOWN-3541 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
9 Skipped Deployments
|
|
Thank you for your contribution! We ask that you please read and sign our CTA Document before we can accept your contribution. You can sign the CTA simply by posting a Pull Request Comment with the following text: I have read the CTA Document and I hereby sign the CTA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
Summary
Auto-focuses the source token input field when the swap view opens so users can immediately start typing (REOWN-3541).
Technical Report
Problem
When the user opens the swap page, the input field is not active — they have to manually click on it before typing an amount. This adds an unnecessary interaction step.
Root Cause Analysis
The w3m-swap-input component had no mechanism to auto-focus the input element on mount. The swap view renders two instances of w3m-swap-input (source token and destination token), and neither received focus automatically.
Approach & Reasoning
Added autoFocus boolean property to w3m-swap-input:
Why firstUpdated() and not connectedCallback():
firstUpdated() runs after the first render when the shadow DOM is fully constructed. connectedCallback() fires before the first render — the input element doesn't exist yet. firstUpdated() is the correct Lit lifecycle for DOM-dependent initialization.
Why firstUpdated() and not updated():
Focus should only be set once on initial mount, not on every re-render. firstUpdated() runs exactly once per element lifecycle. If the swap view re-renders without destroying the component, focus won't be re-stolen.
Only source token gets focus:
In w3m-swap-view, the attribute is set as ?autoFocus=${target === 'sourceToken'}. The destination token input is disabled/read-only (shows calculated output), so focusing it would be useless.
Optional chaining on querySelector result:
input?.focus() safely handles the (unlikely) case where the input element isn't found in the shadow DOM.
Files Changed
Verification
Test plan
🤖 Generated with Claude Code