Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738import { isIOS } from "src/utils/isDevice";
export const focusElement = ( el?: HTMLInputElement | HTMLTextAreaElement | null,) => { if (!isIOS()) { el?.focus(); return; }
let fakeInput = document.createElement("input"); fakeInput.setAttribute("type", "text"); fakeInput.style.position = "fixed"; fakeInput.style.height = "0px"; fakeInput.style.width = "0px"; fakeInput.style.fontSize = "16px"; // disable auto zoom document.body.appendChild(fakeInput); fakeInput.focus(); setTimeout(() => { if (!el) return; el.style.transform = "translateY(-2000px)"; el?.focus(); fakeInput.remove(); el.value = " "; el.setSelectionRange(1, 1); requestAnimationFrame(() => { if (el) { el.style.transform = ""; } }); setTimeout(() => { if (!el) return; el.value = ""; el.setSelectionRange(0, 0); }, 50); }, 20);};