Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
Fix body scroll lock on iOS
Browse files Browse the repository at this point in the history
This fixes an issue where all touchmove events on iOS were getting disabled within GlobalOverlays

Now attaching the attribute `body-scroll-lock-ignore` to an element will allow touchmove events on ios
  • Loading branch information
ghsteff committed Mar 11, 2024
1 parent 0865298 commit 165358f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
12 changes: 12 additions & 0 deletions docs/components/Overlay.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ Below is an example of a simple dialog component using the global overlay.

<Canvas of={GlobalOverlayStories.DialogExample} sourceState="shown" />

If you need scrolling within a dialog to work on iOS, you can use the `body-scroll-lock-ignore` attribute on the scrollable dialog content.

```tsx
<GlobalOverlay
// ...props
>
<div body-scroll-lock-ignore>
<p>Scrolling content</p>
</div>
</GlobalOverlay>
```

### Connected Overlay
A overlay component typically used by components like menus.

Expand Down
19 changes: 11 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"homepage": "https://github.com/reaviz/rdk#readme",
"dependencies": {
"body-scroll-lock": "^4.0.0-beta.0",
"body-scroll-lock-upgrade": "^1.1.0",
"classnames": "^2.3.2",
"framer-motion": "^11.0.6",
"popper.js": "^1.16.1"
Expand Down
29 changes: 26 additions & 3 deletions src/Overlay/GlobalOverlay/GlobalOverlay.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React, { FC, Fragment, useCallback, useEffect, useRef } from 'react';
import { disableBodyScroll } from 'body-scroll-lock';
import {
disableBodyScroll,
clearAllBodyScrollLocks
} from 'body-scroll-lock-upgrade';
import { OverlayContext } from '../OverlayContext';
import { AnimatePresence } from 'framer-motion';
import { OverlayPortal } from '../OverlayPortal';
Expand Down Expand Up @@ -40,9 +43,29 @@ export const GlobalOverlay: FC<GlobalOverlayProps> = ({
});

useEffect(() => {
if (open && children !== undefined) {
disableBodyScroll(children);
if (open && overlayRef.current !== undefined) {
disableBodyScroll(overlayRef.current, {
// allowTouchMove determines which elements to allow touchmove events for iOS https://github.com/rick-liruixin/body-scroll-lock-upgrade?tab=readme-ov-file#allowtouchmove
//@ts-expect-error: allowTouchMove is typed wrong: https://github.com/rick-liruixin/body-scroll-lock-upgrade/issues/21
allowTouchMove: (el: HTMLElement) => {
while (el && el !== document.body) {
if (el.getAttribute('body-scroll-lock-ignore') !== null) {
return true;
}
if (el.parentElement !== null) {
el = el.parentElement;
}
}
return false;
}
});
} else {
clearAllBodyScrollLocks();
}

return () => {
clearAllBodyScrollLocks();
};
}, [children, open]);

return (
Expand Down

0 comments on commit 165358f

Please sign in to comment.