Skip to content

Commit

Permalink
Fix the error when touching the screen before <Sheet> is mounted (#152
Browse files Browse the repository at this point in the history
)

* refactor: `scrollable` may be undefined

* fix: ignore onTouchMove event if scrollable is undefined

* chore: add example to reproduce #151

* Revert "chore: add example to reproduce #151"

This reverts commit ac7ec0e.
  • Loading branch information
mizdra authored May 11, 2024
1 parent a25d023 commit 14d4585
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/use-prevent-scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function preventScrollStandard() {
// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
// to navigate to an input with the next/previous buttons that's outside a modal.
function preventScrollMobileSafari() {
let scrollable: Element;
let scrollable: Element | undefined;
let lastY = 0;
let onTouchStart = (e: TouchEvent) => {
// Store the nearest scrollable parent element from the element that the user touched.
Expand All @@ -118,6 +118,13 @@ function preventScrollMobileSafari() {
};

let onTouchMove = (e: TouchEvent) => {
// In special situations, `onTouchStart` may be called without `onTouchStart` being called.
// (e.g. when the user places a finger on the screen before the <Sheet> is mounted and then moves the finger after it is mounted).
// If `onTouchStart` is not called, `scrollable` is `undefined`. Therefore, such cases are ignored.
if (scrollable === undefined) {
return;
}

// Prevent scrolling the window.
if (
scrollable === document.documentElement ||
Expand Down

0 comments on commit 14d4585

Please sign in to comment.