-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
Copy pathkeyboard.ts
47 lines (39 loc) · 1.1 KB
/
keyboard.ts
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { Ref } from "vue";
import { ref, onBeforeUnmount, getCurrentInstance } from "vue";
export interface UseKeyboardResult {
isOpen: Ref<boolean>;
keyboardHeight: Ref<number>;
unregister: () => void;
}
export const useKeyboard = (): UseKeyboardResult => {
const isOpen = ref(false);
const keyboardHeight = ref(0);
const showCallback = (ev: CustomEvent) => {
isOpen.value = true;
keyboardHeight.value = ev.detail.keyboardHeight;
};
const hideCallback = () => {
isOpen.value = false;
keyboardHeight.value = 0;
};
const unregister = () => {
if (typeof (window as any) !== "undefined") {
window.removeEventListener("ionKeyboardDidShow", showCallback);
window.removeEventListener("ionKeyboardDidHide", hideCallback);
}
};
if (typeof (window as any) !== "undefined") {
window.addEventListener("ionKeyboardDidShow", showCallback);
window.addEventListener("ionKeyboardDidHide", hideCallback);
}
if (getCurrentInstance()){
onBeforeUnmount(() => {
unregister()
});
}
return {
isOpen,
keyboardHeight,
unregister,
};
};