-
-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opening a drawer scrolls to the top again #152
Comments
I can't reproduce this on Safari iOS 17. Can you provide reproduction instructions and which device you are using? |
I also encountered the problem of page scrolling up in my projects. How to play: I'm using next js 14 with an app router, the drawer is located in the layout and accordingly is initialized only once. When I open the first page of the site and open the drawer, it does not scroll to the top, but if I then go to another page of the site and open the drawer, after closing it, the page scrolls to the top. This is due to the fact that the drawer is initialized once and in the file use-position-fixed.ts the address of the page during initialization is written in the activeUrl, but when I go to another page, the activeUrl does not update and when I open the drawer, this code is executed, blocking scroll up. if (activeUrl !== window.location.href) {
setActiveUrl(window.location.href);
return;
} For myself, I solved the problem by adding const pathname = usePathname();
React.useEffect(() => {
if (!isOpen) {
setActiveUrl(window.location.href);
}
}, [pathname, isOpen]); I don’t know if this requires any changes, but just in case I described the problem I encountered =) |
Im actually experiencing this unwanted behavior on closing the drawer. The scroll position is undesirably jumping to the top of the page. |
Best I can do to prevent this and "keep" scroll position was double up the notion of scroll restoration... const searchParams = useSearchParams();
const modal = searchParams.get('modal');
<Drawer
open={!!modal}
onClose={() => {
drawerRestorePosition.current = document.body.style.getPropertyValue('top');
}}
onOpenChange={(open) => {
if (!open) {
requestAnimationFrame(() => {
window.scrollTo(0, -parseInt(drawerRestorePosition.current, 10));
});
router.replace('/', { scroll: false });
}
}}
>
<DrawerContent>
{/* ... */}
</DrawerContent>
</Drawer> I don't love this. While it does effectively restore my scroll position it feels hack af and comes with an unavoidable content flicker on iOS devices. I'm beginning to think the whole approach to locking body scroll along with setPositionFixed and restorePositionSetting might need adjusting or even an alt solution. The underlying radix components effectively prevent body scrolling without the need for these techniques. I'm skeptical about the ability to "keep" the pre-drawer scroll position without a jump/flicker given the current approach of toggling from fixed + overflow hidden back to the natural document flow and then calling scrollTo. |
I am facing the same issue in next14. I used nextui's modal, which you can change the position. So by this I was able to replicate drawer link: NextUI's Modal |
@emilkowalski I'm facing similar problem, on closing drawer the scroll position undesirably jumps to the top of the page and then back again. drawer_problem.mp4 |
Can someone create a codesandbox demo that shows this problem and include your browser and device? It's hard to reproduce these issues consistently for me so I'd appreciate a demo, thank you! |
@emilkowalski Seems like the issue is caused by |
@yoannakr Thank you, I will try to fix it somewhere this week once I have some time. |
@emilkowalski I'm facing the exact issue @yoannakr is facing, any updates? |
same here :DD my temporary way to fix that, if u have scroll-behavior: smooth inside ur html tag: React.useEffect(() => { |
This has been fixed in #219, I updated the package on npm so you guys can update and get it working right away. Have a nice Sunday! |
Seems like this issue is still present when used with @tanstack/react-virtual. Unfortunately, I don't have a repro currently, will try to make one when i can. I'm currently not sure what specifically causing this though, but it seems to work all good without the tanstack virtual |
Couldn't replicate the exact behaviour, but if you try scrolling down a bit (seems like the items lower the initial 100vh) and click the drawer trigger then the drawer won't open - https://codesandbox.io/p/devbox/drawer-scrolls-to-top-forked-mx755p. I pretty much put the code from tanstack window example into the OP's repro. I think the scroll to the top issue also may occur if window + infinite scroll is used (rough guess, since that's what im using) |
Can confirm that the issue is caused by the height change in body as noted by @OrenMag . Only way for me to avoid the issues is to set a height with |
@franzwilhelm Opened an issue here: |
To all the folks who are still encountering the issue, as @yoannakr pointed out, you have |
That's not the case for us, no
|
This is the same issue in #55, but the fix for it seems to have broken. Here is the bug again in the v0.7.5:
https://codesandbox.io/p/sandbox/drawer-scrolls-to-top-8lpqx9
The text was updated successfully, but these errors were encountered: