Skip to content

Commit

Permalink
fix(island-ui): Filter Dialog - Scroll doesn't work on iOS (#15896)
Browse files Browse the repository at this point in the history
* 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
2 people authored and jonnigs committed Sep 12, 2024
1 parent edac709 commit f18c733
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
10 changes: 7 additions & 3 deletions libs/island-ui/core/src/lib/Filter/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Button } from '../Button/Button'
import { Inline } from '../Inline/Inline'
import { Stack } from '../Stack/Stack'
import { Text } from '../Text/Text'
import { usePreventBodyScroll } from './usePreventBodyScroll'

import * as styles from './Filter.css'

export interface FilterProps {
Expand Down Expand Up @@ -52,7 +54,7 @@ export interface FilterProps {
/**
* Datatype to use for Filter context.
* Provides the Filter's childs access to shared values,
* like the `isDialog` state with out bloating the childs props.
* like the `isDialog` state without bloating the childs props.
*/
interface FilterContextValue {
variant?: FilterProps['variant']
Expand All @@ -78,7 +80,7 @@ export const Filter: FC<React.PropsWithChildren<FilterProps>> = ({
children,
popoverFlip = true,
}) => {
const dialog = useDialogState()
const dialog = useDialogState({ modal: true })
const popover = usePopoverState({
placement: 'bottom-start',
unstable_flip: popoverFlip,
Expand All @@ -87,6 +89,8 @@ export const Filter: FC<React.PropsWithChildren<FilterProps>> = ({

const hasFilterInput = !!filterInput

usePreventBodyScroll(dialog.visible && variant === 'dialog')

return (
<FilterContext.Provider value={{ variant }}>
{variant === 'popover' && (
Expand Down Expand Up @@ -171,7 +175,7 @@ export const Filter: FC<React.PropsWithChildren<FilterProps>> = ({
/>
</Box>
</DialogDisclosure>
<Dialog {...dialog}>
<Dialog {...dialog} preventBodyScroll={false}>
<Box
background="white"
position="fixed"
Expand Down
37 changes: 37 additions & 0 deletions libs/island-ui/core/src/lib/Filter/usePreventBodyScroll.ts
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])
}

0 comments on commit f18c733

Please sign in to comment.