Skip to content

Commit

Permalink
fix(detached): open keyboard on input focus
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan committed Nov 15, 2021
1 parent 742c1bd commit 31183f4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
{
"path": "packages/autocomplete-js/dist/umd/index.production.js",
"maxSize": "16.25 kB"
"maxSize": "16.5 kB"
},
{
"path": "packages/autocomplete-preset-algolia/dist/umd/index.production.js",
Expand Down
47 changes: 23 additions & 24 deletions packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
AutocompleteState,
} from './types';
import { userAgents } from './userAgents';
import { mergeDeep, setProperties } from './utils';
import { focusAndOpenKeyboard, mergeDeep, setProperties } from './utils';

export function autocomplete<TItem extends BaseItem>(
options: AutocompleteOptions<TItem>
Expand Down Expand Up @@ -351,32 +351,31 @@ export function autocomplete<TItem extends BaseItem>(
}

function setIsModalOpen(value: boolean) {
requestAnimationFrame(() => {
const prevValue = props.value.core.environment.document.body.contains(
const prevValue = props.value.core.environment.document.body.contains(
dom.value.detachedOverlay
);

if (value === prevValue) {
return;
}

if (value) {
props.value.core.environment.document.body.appendChild(
dom.value.detachedOverlay
);
props.value.core.environment.document.body.classList.add('aa-Detached');

if (value === prevValue) {
return;
}

if (value) {
props.value.core.environment.document.body.appendChild(
dom.value.detachedOverlay
);
props.value.core.environment.document.body.classList.add('aa-Detached');
dom.value.input.focus();
} else {
props.value.core.environment.document.body.removeChild(
dom.value.detachedOverlay
);
props.value.core.environment.document.body.classList.remove(
'aa-Detached'
);
autocomplete.value.setQuery('');
autocomplete.value.refresh();
}
});
focusAndOpenKeyboard(props.value.core.environment, dom.value.input);
} else {
props.value.core.environment.document.body.removeChild(
dom.value.detachedOverlay
);
props.value.core.environment.document.body.classList.remove(
'aa-Detached'
);
autocomplete.value.setQuery('');
autocomplete.value.refresh();
}
}

return {
Expand Down
30 changes: 30 additions & 0 deletions packages/autocomplete-js/src/utils/focusAndOpenKeyboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AutocompleteEnvironment } from '@algolia/autocomplete-core';

// Mobile devices don't open the keyboard when programmatically focusing inputs.
// See https://stackoverflow.com/a/55425845
export function focusAndOpenKeyboard(
environment: AutocompleteEnvironment,
inputElement: HTMLInputElement,
timeout: number = 0
) {
if (inputElement) {
const tempInput = environment.document.createElement('input');
const { top, left } = inputElement.getBoundingClientRect();

tempInput.style.position = 'absolute';
tempInput.style.top = `${top}px`;
tempInput.style.left = `${left}px`;
tempInput.style.height = '0';
tempInput.style.opacity = '0';

environment.document.body.appendChild(tempInput);
tempInput.focus();

setTimeout(() => {
inputElement.focus();
inputElement.click();

environment.document.body.removeChild(tempInput);
}, timeout);
}
}
1 change: 1 addition & 0 deletions packages/autocomplete-js/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './focusAndOpenKeyboard';
export * from './getHTMLElement';
export * from './mergeClassNames';
export * from './mergeDeep';
Expand Down

0 comments on commit 31183f4

Please sign in to comment.