Skip to content

Commit

Permalink
fix: goal edit modal not placed properly above on screen keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaybadgujar102 committed Nov 10, 2024
1 parent 0dafc9b commit 913cd8f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/components/GoalsComponents/GoalConfigModal/ConfigGoal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import CustomDatePicker from "./components/CustomDatePicker";
import HintToggle from "./components/HintToggle";
import useVirtualKeyboardOpen from "../../../hooks/useVirtualKeyBoardOpen";
import ArchivedAutoComplete from "./components/ArchivedAutoComplete";
import useOnScreenKeyboardScrollFix from "./useOnScreenKeyboardScrollFix";

const onDays = [...calDays.slice(1), "Sun"];

Expand Down Expand Up @@ -63,6 +64,7 @@ const ConfigGoal = ({ type, goal, mode }: { type: TGoalCategory; mode: TGoalConf
const addGoalSound = new Audio(plingSound);

const isKeyboardOpen = useVirtualKeyboardOpen();
useOnScreenKeyboardScrollFix();

const setShowToast = useSetRecoilState(displayToast);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useEffect } from "react";

const useOnScreenKeyboardScrollFix = () => {
useEffect(() => {
const handleScroll = () => {
window.scrollTo(0, 0);
};

window.addEventListener("scroll", handleScroll);

return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);
};

export default useOnScreenKeyboardScrollFix;
10 changes: 7 additions & 3 deletions src/hooks/useVirtualKeyBoardOpen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { useState, useEffect } from "react";
function useVirtualKeyboardOpen() {
const [isKeyboardOpen, setIsKeyboardOpen] = useState(false);
const initialHeight = useState(window.visualViewport?.height || 0)[0];
const [currentHeight, setCurrentHeight] = useState(window.visualViewport?.height || 0);

useEffect(() => {
const onResize = () => {
const currentHeight = window.visualViewport?.height || 0;
if (currentHeight < initialHeight * 0.75) {
const newHeight = window.visualViewport?.height ?? 0;
setCurrentHeight(newHeight);

if (newHeight < initialHeight * 0.75) {
setIsKeyboardOpen(true);
} else {
setIsKeyboardOpen(false);
Expand All @@ -19,8 +22,9 @@ function useVirtualKeyboardOpen() {
return () => {
window.removeEventListener("resize", onResize);
};
}, [initialHeight]);
}, [initialHeight, currentHeight]);

return isKeyboardOpen;
}

export default useVirtualKeyboardOpen;

0 comments on commit 913cd8f

Please sign in to comment.