Skip to content

Commit

Permalink
fix: inputs repositioning (#441)
Browse files Browse the repository at this point in the history
* Fix inputs repositioning

* Fix input repositioning logic

* Add buffer
  • Loading branch information
emilkowalski authored Sep 24, 2024
1 parent 824ea35 commit 2b1be8b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export function Root({

React.useEffect(() => {
function onVisualViewportChange() {
if (!drawerRef.current) return;
if (!drawerRef.current || !repositionInputs) return;

const focusedElement = document.activeElement as HTMLElement;
if (isInput(focusedElement) || keyboardIsOpen.current) {
Expand All @@ -391,7 +391,7 @@ export function Root({
}
const offsetFromTop = drawerRef.current.getBoundingClientRect().top;

// 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.
// visualViewport height may change due to somq e 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;
}
Expand All @@ -408,7 +408,7 @@ export function Root({
let newDrawerHeight = height;

if (height > visualViewportHeight) {
newDrawerHeight = visualViewportHeight - WINDOW_TOP_OFFSET;
newDrawerHeight = visualViewportHeight - offsetFromTop;
}
// When fixed, don't move the drawer upwards if there's space, but rather only change it's height so it's fully scrollable when the keyboard is open
if (fixed) {
Expand Down
5 changes: 4 additions & 1 deletion src/use-prevent-scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { useEffect, useLayoutEffect } from 'react';

const KEYBOARD_BUFFER = 24;

export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;

interface PreventScrollOptions {
Expand Down Expand Up @@ -310,7 +312,8 @@ function scrollIntoView(target: Element) {
let scrollableTop = scrollable.getBoundingClientRect().top;
let targetTop = target.getBoundingClientRect().top;
let targetBottom = target.getBoundingClientRect().bottom;
const keyboardHeight = scrollable.getBoundingClientRect().bottom;
// Buffer is needed for some edge cases
const keyboardHeight = scrollable.getBoundingClientRect().bottom + KEYBOARD_BUFFER;

if (targetBottom > keyboardHeight) {
scrollable.scrollTop += targetTop - scrollableTop;
Expand Down

0 comments on commit 2b1be8b

Please sign in to comment.