A drawer component for React.
Improve viewport calculation (#60)
Emil Kowalski7bf2ec5dbd55
diff --git a/src/index.tsx b/src/index.tsxindex 351ea18..50d06b9 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -302,7 +302,7 @@ function Root({const drawerHeight = drawerRef.current?.getBoundingClientRect().height || 0;const offsetFromTop = drawerRef.current?.getBoundingClientRect().top;- // visualViewport height may change duo to some subtle changes to the keyboard. Checking if the height changed by 60 or more will make sure that they keyboard is really closed.+ // visualViewport height may change due to some subtle changes to the keyboard. Checking if the height changed by 60 or more will make sure that they keyboard really changed its open state.if (Math.abs(previousDiffFromInitial.current - diffFromInitial) > 60) {keyboardIsOpen.current = !keyboardIsOpen.current;}@@ -310,7 +310,14 @@ function Root({previousDiffFromInitial.current = diffFromInitial;// We don't have to change the height if the input is in view, when we are here we are in the opened keyboard state so we can accuretly check if the input is in viewif (drawerHeight > visualViewportHeight || keyboardIsOpen.current) {- drawerRef.current.style.height = `${visualViewportHeight - offsetFromTop}px`;+ const height = drawerRef.current?.getBoundingClientRect().height;+ let newDrawerHeight = height;++ if (height > visualViewportHeight) {+ newDrawerHeight = visualViewportHeight - WINDOW_TOP_OFFSET;+ }++ drawerRef.current.style.height = `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`;} else {drawerRef.current.style.height = 'initial';}diff --git a/src/use-position-fixed.ts b/src/use-position-fixed.tsindex ac76891..79c5a95 100644--- a/src/use-position-fixed.ts+++ b/src/use-position-fixed.ts@@ -60,7 +60,6 @@ export function usePositionFixed({ isOpen, isFullyClosed }: { isOpen: boolean; iReact.useEffect(() => {function onScroll() {- console.log(window.scrollY, 'coming from onScroll');scrollPos.current = window.scrollY;}diff --git a/src/use-prevent-scroll.ts b/src/use-prevent-scroll.tsindex 2b5a04f..28122d2 100644--- a/src/use-prevent-scroll.ts+++ b/src/use-prevent-scroll.ts@@ -309,9 +309,10 @@ function scrollIntoView(target: Element) {if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== target) {let scrollableTop = scrollable.getBoundingClientRect().top;let targetTop = target.getBoundingClientRect().top;+ let targetBottom = target.getBoundingClientRect().bottom;const keyboardHeight = scrollable.getBoundingClientRect().bottom;- if (targetTop > keyboardHeight) {+ if (targetBottom > keyboardHeight) {scrollable.scrollTop += targetTop - scrollableTop;}}