Skip to content

Commit

Permalink
Improve iOS scroll locking (#1830)
Browse files Browse the repository at this point in the history
* use a simpler `position: fixed` approach to prevent scroll locking

This isn't super ideal, but just preventing the default behavior on the
entire document while `touchmove`-ing isn't ideal either because then
you can't scroll inside the dialog or on the backdrop if your dialog
panel is larger than the viewport.

Again, this is not 100% correct, but it is better because you will be
able to scroll the dialog, and not the body.

* update changelog
  • Loading branch information
RobinMalfait committed Sep 7, 2022
1 parent d2a0bdb commit 397ba5c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
4 changes: 3 additions & 1 deletion packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Improve iOS scroll locking ([#1830](https://github.com/tailwindlabs/headlessui/pull/1830))

## [1.7.0] - 2022-09-06

Expand Down
12 changes: 3 additions & 9 deletions packages/@headlessui-react/src/components/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,10 @@ function useScrollLock(ownerDocument: Document | null, enabled: boolean) {
}

if (isIOS()) {
d.addEventListener(
ownerDocument,
'touchmove',
(e) => {
e.preventDefault()
},
{ passive: false }
)

let scrollPosition = window.pageYOffset
style(documentElement, 'position', 'fixed')
style(documentElement, 'marginTop', `-${scrollPosition}px`)
style(documentElement, 'width', `100%`)
d.add(() => window.scrollTo(0, scrollPosition))
}

Expand Down
4 changes: 3 additions & 1 deletion packages/@headlessui-vue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Improve iOS scroll locking ([#1830](https://github.com/tailwindlabs/headlessui/pull/1830))

## [1.7.0] - 2022-09-06

Expand Down
12 changes: 3 additions & 9 deletions packages/@headlessui-vue/src/components/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,10 @@ export let Dialog = defineComponent({
}

if (isIOS()) {
d.addEventListener(
owner,
'touchmove',
(e) => {
e.preventDefault()
},
{ passive: false }
)

let scrollPosition = window.pageYOffset
style(documentElement, 'position', 'fixed')
style(documentElement, 'marginTop', `-${scrollPosition}px`)
style(documentElement, 'width', `100%`)
d.add(() => window.scrollTo(0, scrollPosition))
}

Expand Down

0 comments on commit 397ba5c

Please sign in to comment.