-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseIsVirtualKeyboardOpen.ts
More file actions
26 lines (20 loc) · 896 Bytes
/
useIsVirtualKeyboardOpen.ts
File metadata and controls
26 lines (20 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { useEffect, useRef, useState } from 'react';
export const useIsVirtualKeyboardOpen = () => {
const [isVirtualKeyboardOpen, setIsVirtualKeyboardOpen] =
useState<boolean>(false);
const prevHeight = useRef<number>();
const listener = () => {
// The visual viewport is the visual portion of a screen excluding on-screen keyboards,
// areas outside of a pinch-zoom area, or any other on-screen artifact that doesn't scale with the dimensions of a page.
const visualViewportHeight = window.visualViewport.height;
setIsVirtualKeyboardOpen(visualViewportHeight === prevHeight.current);
};
useEffect(() => {
prevHeight.current = window.innerHeight;
window.visualViewport.addEventListener('resize', listener);
return () => {
window.visualViewport.removeEventListener('resize', listener);
};
}, []);
return { isVirtualKeyboardOpen };
};