Skip to content

Commit 3b569b6

Browse files
salomvaryHaroenv
andauthored
fix: focus detached input on iOS (#653) (#1231)
--------- Co-authored-by: Haroen Viaene <[email protected]>
1 parent 186ff9b commit 3b569b6

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

packages/autocomplete-js/src/__tests__/detached.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@ describe('detached', () => {
6262
// Open detached overlay
6363
searchButton.click();
6464

65-
await waitFor(() => {
66-
const input = document.querySelector<HTMLInputElement>('.aa-Input')!;
65+
const input = document.querySelector<HTMLInputElement>('.aa-Input')!;
6766

68-
expect(document.querySelector('.aa-DetachedOverlay')).toBeInTheDocument();
69-
expect(document.body).toHaveClass('aa-Detached');
70-
expect(input).toHaveFocus();
67+
expect(document.querySelector('.aa-DetachedOverlay')).toBeInTheDocument();
68+
expect(document.body).toHaveClass('aa-Detached');
7169

72-
fireEvent.input(input, { target: { value: 'a' } });
73-
});
70+
// Input should immediately be focused, to ensure the keyboard is shown on mobile
71+
expect(input).toHaveFocus();
72+
73+
// Type a query in the focused input
74+
fireEvent.input(input, { target: { value: 'a' } });
7475

7576
// Wait for the panel to open
7677
await waitFor(() => {
@@ -391,16 +392,15 @@ describe('detached', () => {
391392
// Open detached overlay
392393
searchButton.click();
393394

394-
// Type a query in the focused input
395-
await waitFor(() => {
396-
const input = document.querySelector<HTMLInputElement>('.aa-Input')!;
395+
const input = document.querySelector<HTMLInputElement>('.aa-Input')!;
397396

398-
expect(document.querySelector('.aa-DetachedOverlay')).toBeInTheDocument();
399-
expect(document.body).toHaveClass('aa-Detached');
400-
expect(input).toHaveFocus();
397+
expect(document.querySelector('.aa-DetachedOverlay')).toBeInTheDocument();
398+
expect(document.body).toHaveClass('aa-Detached');
399+
// Input should immediately be focused, to ensure the keyboard is shown on mobile
400+
expect(input).toHaveFocus();
401401

402-
fireEvent.input(input, { target: { value: 'a' } });
403-
});
402+
// Type a query in the focused input
403+
fireEvent.input(input, { target: { value: 'a' } });
404404

405405
// Wait for the panel to open
406406
await waitFor(() => {

packages/autocomplete-js/src/autocomplete.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -375,30 +375,28 @@ export function autocomplete<TItem extends BaseItem>(
375375
}
376376

377377
function setIsModalOpen(value: boolean) {
378-
requestAnimationFrame(() => {
379-
const prevValue = props.value.core.environment.document.body.contains(
380-
dom.value.detachedOverlay
381-
);
378+
const prevValue = props.value.core.environment.document.body.contains(
379+
dom.value.detachedOverlay
380+
);
382381

383-
if (value === prevValue) {
384-
return;
385-
}
382+
if (value === prevValue) {
383+
return;
384+
}
386385

387-
if (value) {
388-
props.value.core.environment.document.body.appendChild(
389-
dom.value.detachedOverlay
390-
);
391-
props.value.core.environment.document.body.classList.add('aa-Detached');
392-
dom.value.input.focus();
393-
} else {
394-
props.value.core.environment.document.body.removeChild(
395-
dom.value.detachedOverlay
396-
);
397-
props.value.core.environment.document.body.classList.remove(
398-
'aa-Detached'
399-
);
400-
}
401-
});
386+
if (value) {
387+
props.value.core.environment.document.body.appendChild(
388+
dom.value.detachedOverlay
389+
);
390+
props.value.core.environment.document.body.classList.add('aa-Detached');
391+
dom.value.input.focus();
392+
} else {
393+
props.value.core.environment.document.body.removeChild(
394+
dom.value.detachedOverlay
395+
);
396+
props.value.core.environment.document.body.classList.remove(
397+
'aa-Detached'
398+
);
399+
}
402400
}
403401

404402
warn(

0 commit comments

Comments
 (0)