Skip to content

Instantly share code, notes, and snippets.

@jschmidtnj
Created March 22, 2021 23:32
Show Gist options
  • Select an option

  • Save jschmidtnj/430947d21e7701458021a29729b11724 to your computer and use it in GitHub Desktop.

Select an option

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)
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