Skip to content

Commit 397ba5c

Browse files
authored
Improve iOS scroll locking (#1830)
* 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
1 parent d2a0bdb commit 397ba5c

File tree

4 files changed

+12
-20
lines changed

4 files changed

+12
-20
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Improve iOS scroll locking ([#1830](https://github.com/tailwindlabs/headlessui/pull/1830))
1113

1214
## [1.7.0] - 2022-09-06
1315

packages/@headlessui-react/src/components/dialog/dialog.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,10 @@ function useScrollLock(ownerDocument: Document | null, enabled: boolean) {
119119
}
120120

121121
if (isIOS()) {
122-
d.addEventListener(
123-
ownerDocument,
124-
'touchmove',
125-
(e) => {
126-
e.preventDefault()
127-
},
128-
{ passive: false }
129-
)
130-
131122
let scrollPosition = window.pageYOffset
123+
style(documentElement, 'position', 'fixed')
124+
style(documentElement, 'marginTop', `-${scrollPosition}px`)
125+
style(documentElement, 'width', `100%`)
132126
d.add(() => window.scrollTo(0, scrollPosition))
133127
}
134128

packages/@headlessui-vue/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Improve iOS scroll locking ([#1830](https://github.com/tailwindlabs/headlessui/pull/1830))
1113

1214
## [1.7.0] - 2022-09-06
1315

packages/@headlessui-vue/src/components/dialog/dialog.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,10 @@ export let Dialog = defineComponent({
249249
}
250250

251251
if (isIOS()) {
252-
d.addEventListener(
253-
owner,
254-
'touchmove',
255-
(e) => {
256-
e.preventDefault()
257-
},
258-
{ passive: false }
259-
)
260-
261252
let scrollPosition = window.pageYOffset
253+
style(documentElement, 'position', 'fixed')
254+
style(documentElement, 'marginTop', `-${scrollPosition}px`)
255+
style(documentElement, 'width', `100%`)
262256
d.add(() => window.scrollTo(0, scrollPosition))
263257
}
264258

0 commit comments

Comments
 (0)