ui icon indicating copy to clipboard operation
ui copied to clipboard

When the drawer is closed, the scroll position undesirably jumps to the top of the page and then back again

Open yoannakr opened this issue 1 year ago • 6 comments

When the drawer is closed, the scroll position undesirably jumps to the top of the page and then back again

https://github.com/shadcn-ui/ui/assets/55032687/ec4af7be-5a19-4840-8879-e7e3cec87829

yoannakr avatar Jan 03 '24 12:01 yoannakr

facing the same issue here

useit015 avatar Jan 05 '24 15:01 useit015

I guess it is supposed to take the window height rather than the full page?

broisnischal avatar Jan 05 '24 17:01 broisnischal

https://github.com/shadcn-ui/ui/assets/145198186/f817cb37-f2b1-4f1b-bc50-d1cb0c2fdfba

same issue

begalinsaf avatar Jan 09 '24 11:01 begalinsaf

I think I found a solution, the problem is scroll-behavior: smooth from html class css attribute I just removed scroll-behavior: smooth or scroll-smooth from the html.

https://github.com/shadcn-ui/ui/assets/145198186/4b6b10e4-79d8-4929-8fde-dd13fa05d6f6

begalinsaf avatar Jan 09 '24 12:01 begalinsaf

I think I found a solution, the problem is scroll-behavior: smooth from html class css attribute I just removed scroll-behavior: smooth or scroll-smooth from the html.

Video.tanpa.judul.Dibuat.dengan.Clipchamp.mp4

@begalinsaf Yes, it seems to do the trick, I still want to preserve the smooth scroll behavior on my site so I added a simple hook to fix this issue:

// use-scroll-behavior.tsx
import { useEffect } from 'react'

function useScrollBehavior(isOpen: boolean) {
  useEffect(() => {
    if (isOpen) {
      document.documentElement.style.scrollBehavior = 'auto'
    } else {
      setTimeout(() => {
        document.documentElement.style.scrollBehavior = 'smooth'
      }, 1000)
    }
  }, [isOpen])
}

export default useScrollBehavior

To use it:

//...
const [open, setOpen] = useState(false)
// Add this line below
useScrollBehavior(open)
return (
<Drawer open={open} onOpenChange={setOpen}>
  <DrawerTrigger asChild>
    <Button variant="outline">Button</Button>
  </DrawerTrigger>
  <DrawerContent>
    {/* ... */}
  </DrawerContent>
</Drawer>
)
}

fluid-design-io avatar Jan 15 '24 03:01 fluid-design-io

I had the same issue and fixed it by setting document.documentElement.style.scrollBehavior to 'auto' when the drawer is open, and back to 'smooth' after a 400ms timeout when it closes. This short delay prevents unwanted scroll to the top on rapid interactions.

felipemenezes98 avatar Jan 21 '24 20:01 felipemenezes98

It looks like the issue was caused by vaul Take a look here: vaul/issue

yoannakr avatar Jan 22 '24 19:01 yoannakr

The same is happening on the "Sheet" component.

albertocubeddu avatar Apr 18 '24 00:04 albertocubeddu

The same is happening on the "Sheet" component.

Try updating vaul dependency, the issue is already closed and it was the solution for me at the time

vitormarkis avatar Apr 18 '24 04:04 vitormarkis

can anyone post code ?

hedysnike avatar Apr 29 '24 19:04 hedysnike

The scroll-behavior: smooth property was the issue for me, i just got around it by using responsive breakpoints, so mobile wont have smooth behavior. Very annoying issue with radix ui..

maytees avatar Jun 27 '24 22:06 maytees