Skip to content

Connect login form: Refocus password field on submission error#55450

Merged
ravicious merged 2 commits intomasterfrom
r7s/login-focus
Jun 5, 2025
Merged

Connect login form: Refocus password field on submission error#55450
ravicious merged 2 commits intomasterfrom
r7s/login-focus

Conversation

@ravicious
Copy link
Copy Markdown
Member

Closes #54508.

To understand the fix, one first must understand how it worked before. I assumed that some normal browser behavior caused it to work, but that's not the case.

Prior to #47883, upon submission the form would immediately switch to the next "step", before a response from the login attempt even arrived. When the response arrived with an error, the form would go back to the previous step, thus re-firing all effects, including the one that would focus the password field.

const passwordInputRef = useRefAutoFocus<HTMLInputElement>({
shouldFocus: hasTransitionEnded && autoFocus && !!loggedInUserName,
});

Details https://github.com/user-attachments/assets/d834a228-65d3-4fbb-9cb8-fdf636e1b41e

After the cleanup from #47883, the form doesn't go into the next step immediately on submission. Instead it disables the fields and shows a progress bar. When an error response arrives, it enables the fields, but the focus is no longer on the form itself. The focus effects do not fire because their deps haven't changed.

Details https://github.com/user-attachments/assets/114d00f1-51b9-4a7a-babf-dfd3fc3993c4

The fix is to include the state of the form submission in the value that decides whether a particular field should be focused or not.

Details https://github.com/user-attachments/assets/df1c89fd-d793-4f34-81a9-4570f9f30dd3

I'm still not sure if regular forms lose focus on submission as well, or if the need to trigger those focus effects comes from the same place that the need to use useRefAutoFocus instead of autoFocus.

@ravicious ravicious added no-changelog Indicates that a PR does not require a changelog entry backport/branch/v17 backport/branch/v18 labels Jun 5, 2025
@ravicious ravicious requested a review from gzdunek June 5, 2025 07:19
@github-actions github-actions Bot requested review from kiosion and ryanclark June 5, 2025 07:19
Comment on lines +27 to 31
/**
* @deprecated Include items from refocusDeps into the calculation of shouldFocus instead.
* The list of useEffect deps should be statically known.
*/
refocusDeps?: DependencyList;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because arguably it's incompatible with hooks are "supposed to" be used.

In this specific scenario, it was easy to avoid using refocusDeps by just adding another comparison when constructing the value for shouldFocus. However, there are other places where we use this hook where this might be harder. For example:

const deviceNameInputRef = useRefAutoFocus<HTMLInputElement>({
shouldFocus,
refocusDeps: [mfaType.value],
});

Here the callsite would like to retrigger focus when mfaType.value changes, which necessitates tracking the previous value of it, something like in "Adjusting some state when a prop changes". Maybe there are some other ways to address this but nothing comes to my mind at the moment.

@ravicious ravicious removed the request for review from kiosion June 5, 2025 07:23
Copy link
Copy Markdown
Contributor

@gzdunek gzdunek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great 👍

@ravicious ravicious added this pull request to the merge queue Jun 5, 2025
Merged via the queue into master with commit a7f9fc6 Jun 5, 2025
45 checks passed
@ravicious ravicious deleted the r7s/login-focus branch June 5, 2025 15:50
@backport-bot-workflows
Copy link
Copy Markdown
Contributor

@ravicious See the table below for backport results.

Branch Result
branch/v17 Create PR
branch/v18 Create PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/branch/v17 backport/branch/v18 no-changelog Indicates that a PR does not require a changelog entry size/sm ui

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The login form in Connect loses focus after entering an incorrect password

3 participants