mirror of
https://github.com/streetwriters/notesnook.git
synced 2026-02-24 04:00:59 +01:00
81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
/*
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { Keyboard, KeyboardEventListener, KeyboardMetrics } from "react-native";
|
|
|
|
const emptyCoordinates = Object.freeze({
|
|
screenX: 0,
|
|
screenY: 0,
|
|
width: 0,
|
|
height: 0
|
|
});
|
|
const initialValue = {
|
|
start: emptyCoordinates,
|
|
end: emptyCoordinates
|
|
};
|
|
|
|
export default function useKeyboard() {
|
|
const [shown, setShown] = useState(false);
|
|
const [coordinates, setCoordinates] = useState<{
|
|
start: undefined | KeyboardMetrics;
|
|
end: KeyboardMetrics;
|
|
}>(initialValue);
|
|
const [keyboardHeight, setKeyboardHeight] = useState<number>(0);
|
|
|
|
const handleKeyboardWillShow: KeyboardEventListener = (e) => {
|
|
setCoordinates({ start: e.startCoordinates, end: e.endCoordinates });
|
|
};
|
|
const handleKeyboardDidShow: KeyboardEventListener = (e) => {
|
|
setShown(true);
|
|
setCoordinates({ start: e.startCoordinates, end: e.endCoordinates });
|
|
setKeyboardHeight(e.endCoordinates.height);
|
|
};
|
|
const handleKeyboardWillHide: KeyboardEventListener = (e) => {
|
|
setCoordinates({ start: e.startCoordinates, end: e.endCoordinates });
|
|
};
|
|
const handleKeyboardDidHide: KeyboardEventListener = (e) => {
|
|
setShown(false);
|
|
if (e) {
|
|
setCoordinates({ start: e.startCoordinates, end: e.endCoordinates });
|
|
} else {
|
|
setCoordinates(initialValue);
|
|
setKeyboardHeight(0);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const subscriptions = [
|
|
Keyboard.addListener("keyboardWillShow", handleKeyboardWillShow),
|
|
Keyboard.addListener("keyboardDidShow", handleKeyboardDidShow),
|
|
Keyboard.addListener("keyboardWillHide", handleKeyboardWillHide),
|
|
Keyboard.addListener("keyboardDidHide", handleKeyboardDidHide)
|
|
];
|
|
|
|
return () => {
|
|
subscriptions.forEach((subscription) => subscription.remove());
|
|
};
|
|
}, []);
|
|
return {
|
|
keyboardShown: shown,
|
|
coordinates,
|
|
keyboardHeight
|
|
};
|
|
}
|