Skip to content

Commit f4f78e1

Browse files
authored
Merge pull request #20779 from mozilla/fix-persisting-dropdown-worktree
fix(UI): Add short delay before auto focus on email first
2 parents 63d0717 + c4bfd4b commit f4f78e1

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

packages/fxa-settings/src/pages/Index/index.test.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { screen, waitFor } from '@testing-library/react';
5+
import { act, screen, waitFor } from '@testing-library/react';
66
import { userEvent } from '@testing-library/user-event';
77
import { createMockIndexOAuthNativeIntegration, Subject } from './mocks';
88
import { renderWithLocalizationProvider } from 'fxa-react/lib/test-utils/localizationProvider';
@@ -252,4 +252,30 @@ describe('Index page', () => {
252252
).toHaveAttribute('data-glean-id', 'email_first_submit');
253253
});
254254
});
255+
256+
describe('email input auto-focus', () => {
257+
beforeEach(() => {
258+
jest.useFakeTimers();
259+
});
260+
261+
afterEach(() => {
262+
jest.useRealTimers();
263+
});
264+
265+
it('does not focus the email input until the defer timeout elapses', () => {
266+
renderWithLocalizationProvider(<Subject />);
267+
268+
expect(screen.getByLabelText('Enter your email')).not.toHaveFocus();
269+
});
270+
271+
it('focuses the email input after the defer timeout elapses', () => {
272+
renderWithLocalizationProvider(<Subject />);
273+
274+
act(() => {
275+
jest.advanceTimersByTime(10);
276+
});
277+
278+
expect(screen.getByLabelText('Enter your email')).toHaveFocus();
279+
});
280+
});
255281
});

packages/fxa-settings/src/pages/Index/index.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,22 @@ export const Index = ({
8080
const legalTerms = integration.getLegalTerms();
8181

8282
const emailEngageEventEmitted = useRef(false);
83+
const emailInputRef = useRef<HTMLInputElement>(null);
8384

8485
useEffect(() => {
8586
GleanMetrics.emailFirst.view();
8687
}, []);
8788

89+
// Defer auto-focus once. If the container redirects an already-signed-in user
90+
// away, intermittently the browser's email autocomplete dropdown can be shown on
91+
// the next page, presumably from a redirect mid-render. See FXA-14009
92+
useEffect(() => {
93+
const timer = setTimeout(() => {
94+
emailInputRef.current?.focus();
95+
}, 10);
96+
return () => clearTimeout(timer);
97+
}, []);
98+
8899
const onSubmit = async ({ email }: IndexFormData) => {
89100
setIsSubmitting(true);
90101
try {
@@ -199,7 +210,7 @@ export const Index = ({
199210
inputMode="email"
200211
label="Enter your email"
201212
inputRef={register()}
202-
autoFocus
213+
inputRefDOM={emailInputRef}
203214
errorText={tooltipErrorMessage}
204215
onChange={handleInputChange}
205216
autoCapitalize="off"

0 commit comments

Comments
 (0)