a tool for shared writing and social publishing
1import { isIOS } from "src/utils/isDevice";
2
3export const focusElement = (
4 el?: HTMLInputElement | HTMLTextAreaElement | null,
5) => {
6 if (!isIOS()) {
7 el?.focus();
8 return;
9 }
10
11 let fakeInput = document.createElement("input");
12 fakeInput.setAttribute("type", "text");
13 fakeInput.style.position = "fixed";
14 fakeInput.style.height = "0px";
15 fakeInput.style.width = "0px";
16 fakeInput.style.fontSize = "16px"; // disable auto zoom
17 document.body.appendChild(fakeInput);
18 fakeInput.focus();
19 setTimeout(() => {
20 if (!el) return;
21 el.style.transform = "translateY(-2000px)";
22 el?.focus();
23 fakeInput.remove();
24 el.value = " ";
25 el.setSelectionRange(1, 1);
26 requestAnimationFrame(() => {
27 if (el) {
28 el.style.transform = "";
29 }
30 });
31 setTimeout(() => {
32 if (!el) return;
33 el.value = "";
34 el.setSelectionRange(0, 0);
35 }, 50);
36 }, 20);
37};