Created
March 22, 2021 23:32
-
-
Save jschmidtnj/430947d21e7701458021a29729b11724 to your computer and use it in GitHub Desktop.
device detection script and android input fix (uses onBlurCapture because onkeydown is broken on chrome)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import type { FocusEvent, KeyboardEvent } from 'react'; | |
| const [isDesktop, setIsDesktop] = useState<boolean>(false); | |
| useEffect(() => { | |
| (async () => { | |
| setIsDesktop((await deviceDetect()).isDesktop); | |
| })(); | |
| }, []); | |
| export const deviceDetect = async (): Promise< | |
| typeof import('react-device-detect') | |
| > => { | |
| return await import('react-device-detect'); | |
| }; | |
| export const handleTabInputElemMobile = ( | |
| evt: FocusEvent<HTMLInputElement>, | |
| callback: () => void | |
| ): void => { | |
| // from https://stackoverflow.com/a/11160055 | |
| const element = evt.currentTarget; | |
| element.setAttribute('readonly', 'readonly'); | |
| element.setAttribute('disabled', 'true'); | |
| // wait until the attributes are updated | |
| setTimeout(() => { | |
| element.blur(); // close keyboard | |
| element.removeAttribute('readonly'); | |
| element.removeAttribute('disabled'); | |
| }, 100); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment