-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(island-ui): Filter Dialog - Scroll doesn't work on iOS (#15896)
* Fix mobile scroll * Keep track of scroll position * Move hook into separate file * useRef instead of global variable --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
bd2eb08
commit a4c7324
Showing
2 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
libs/island-ui/core/src/lib/Filter/usePreventBodyScroll.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
export const usePreventBodyScroll = (preventBodyScroll: boolean) => { | ||
const initialBodyPosition = useRef<string | null>(null) | ||
const initialScrollPosition = useRef<number | null>(null) | ||
|
||
useEffect(() => { | ||
const isBrowser = typeof window !== 'undefined' | ||
if (!isBrowser || !preventBodyScroll) { | ||
return | ||
} | ||
|
||
if (initialBodyPosition.current === null) { | ||
initialBodyPosition.current = | ||
window.document.body.style.position || 'static' | ||
} | ||
if (initialScrollPosition.current === null) { | ||
initialScrollPosition.current = window.scrollY | ||
} | ||
|
||
// Prevent scrolling on the body element | ||
window.document.body.style.position = 'fixed' | ||
|
||
return () => { | ||
if (initialBodyPosition.current !== null) { | ||
window.document.body.style.position = initialBodyPosition.current | ||
initialBodyPosition.current = null | ||
} | ||
if (initialScrollPosition.current !== null) { | ||
// When setting the body position to fixed, the scroll position resets to 0 | ||
// Here we are restoring the scroll position | ||
window.scrollTo(0, initialScrollPosition.current) | ||
initialScrollPosition.current = null | ||
} | ||
} | ||
}, [preventBodyScroll]) | ||
} |