reactjs / react-modal

Accessible modal dialog component for React
http://reactcommunity.org/react-modal
MIT License
7.37k stars 808 forks source link

Avoid Scroll position being kept #1044

Closed kmthorsnes closed 8 months ago

kmthorsnes commented 8 months ago

Summary:

Steps to reproduce:

  1. Open Modal
  2. Scroll to bottom of content
  3. Exit modal
  4. Re-enter modal
  5. Observe the modal being in the scroll position it was left in step 3.

Expected behavior:

Additional notes:

diasbruno commented 8 months ago

@kmthorsnes Do you see this behavior in all browsers? Which version of react and react-modal are you using?

kmthorsnes commented 8 months ago

@diasbruno Thank you for replying!

Do you see this behavior in all browsers?

  • Yes, I have tested with brave, safari and firefox.

Which version of react and react-modal are you using?

  • React: 17.0.2
  • react-modal: 3.11.2
diasbruno commented 8 months ago

preventScroll was released on version 3.12.1. But we also don't have any "reported bugs" since its release.

diasbruno commented 8 months ago

Ah sorry.

If you have this configuration:

<modal>
   <your_component /> <----
</modal>

your_component will always be rendered, it's just not attached to the dom tree. The dom elements it produces may live longer and this explains why it persists the scroll position.

You'll probably need to, on exit, use container.scrollTo(0,0) (or something like this).

diasbruno commented 8 months ago

preventScroll actually focus the element that triggered the modal to open.

kmthorsnes commented 8 months ago

@diasbruno Thank you! I'll have a closer look at this!

diasbruno commented 8 months ago

I'll close this for now. Please feel free to reopen if that doesn't help. Thanks!