headlessui: scrollBehavior: "smooth" causes unintentional scroll on dialog open/close.

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

1.7.8

What browser are you using?

Chrome, Safari on iOS

Reproduction URL

Issue repo To run, simply clone the repo and run npm i and npm run dev in the root directory.

Describe your issue

To reproduce, simply navigate to the nextjs server in your browser on an iOS device and press the Open Modal button at the bottom of the page. The body element should scroll up, and then jump in a weird manner on the way down.

I’ve done some investigation, and it seems what’s happening is the scrollBehavior: smooth attribute on the Html element is interfering with how headlessui handles the open modal affecting the iOS scroll position. It seems that on iOS, when the modal is opened, the scroll position resets to 0, and headlessui applies a top margin to the body to maintain the content’s scrolled position on the page.

This normally works, but with scrollBehavior: smooth active, the top margin calculation is no longer correct and we see this scroll “jumping” behavior when the modal opens.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 15 (1 by maintainers)

Most upvoted comments

@headlessui/react: ^1.7.17 I’m still facing this issue.

We are encountering this issue as well

Hey, thank you for this bug report! 🙏

This should be fixed by #2635, and will be available in the next release.

You can already try it using:

  • npm install @headlessui/react@insiders.
  • npm install @headlessui/vue@insiders.

Here is an example video with the fix in place for the given reproduction repo:

https://github.com/tailwindlabs/headlessui/assets/1834413/853d68be-4804-4144-8ab5-847310df7633

Got the exact same problem

Investigated a bit more and we were able to resolve this issue by reverting to version 1.7.0. Obviously not an ideal solution but it worked for us. This issue was introduced in version 1.7.1, I believe due to #1830

@RobinMalfait Here is a recording of the issue as well in case it’s helpful. As you can see, opening and closing the dialog causes unintentional scrolling of the page in the background. This only occurs on iOS.

https://github.com/tailwindlabs/headlessui/assets/31812496/40aafc8d-2df5-412f-b649-0ad7d595becc

Cheers!

I got the exact same issue when I upgraded the headlessui version from 1.4.2 to 1.7.15.

Same here. I already made a #2627 regarding this. I also have a video with the same issue.