The Sheet Component, after closing the sheet it always make unexpect scrolling down. #2127
-
I have make a table of content component (TOC) for heading anchor with the Sheet Component. However, when I click the Link and anchor to the specified heading, the page is always forced to scroll to the bottom. a.mp4Is there any way to prevent the page from scrolling when the Sheet exits, but to stay at the original place? My environment is Next.js 14.0.1, Edge 116+. Thank you very much My usage .... // some imports
export const FloatTOC = (props: { data: TTOCItem[] }) => {
return (
<Sheet>
<SheetTrigger>
<div className="bottom-7 right-2 fixed">
<MdMenuBookIcon className="w-12 h-12" />
</div>
</SheetTrigger>
<SheetContent>
<SheetHeader>
<SheetTitle>{"TABLE OF CONTENTS"}</SheetTitle>
</SheetHeader>
<div className="m-5 rounded-md border-2">
<ul className="flat-scrollbar flex flex-col h-[60vh] overflow-y-auto">
{props.data?.map((item) => (
<SheetClose key={`toc-${item.anchorId}`} asChild>
<Link className="hover:text-sky-500" href={`#${item.anchorId}`}>
<li
className="my-2 text-sm target:text-blue-500"
style={{ paddingLeft: `${item.level - 1}em` }}
>{`${item.title}`}</li>
</Link>
</SheetClose> // I wrap the Link with <SheetClose> for closing the sheet after anchoring headings here.
))}
</ul>
</div>
</SheetContent>
</Sheet>
);
}; |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
I'm not sure about it's will be fix your issue but you could try. In this example, when the link is clicked, event.preventDefault() prevents the default action (scrolling to the anchor), and then element.scrollIntoView({ behavior: 'smooth' }) manually scrolls to the element with the corresponding ID. <Link
className="hover:text-sky-500"
href={`#${item.anchorId}`}
onClick={(event) => {
event.preventDefault();
const element = document.getElementById(item.anchorId);
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
}
}}
>
<li
className="my-2 text-sm target:text-blue-500"
style={{ paddingLeft: `${item.level - 1}em` }}
>
{`${item.title}`}
</li>
</Link> |
Beta Was this translation helpful? Give feedback.
-
I have just faced a similar problem (though using chadcn-vue), my solution was to place the sheet in the absolute center. This works because the position of the sheet content is not relative to the parent sheet element, so that element (which is what is scrolled to) can be positioned in the center of the page without impacting the content. Scroll position won't change if it's always at the center of the screen. <style>
.absolute-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<div class="absolute-center">
<Sheet>
<SheetTrigger>
<div id="programaticallyClickedTrigger"></div>
</SheetTrigger>
<SheetContent></SheetContent>
</Sheet>
</div>
<script>
// open sheet
document.getElementById("programaticallyClickedTrigger").click()
<script/> My my Vue3 implementation my sheet trigger contains an empty div, which is clicked on a callback (in my case by a context menu). |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Thank you very much for your ardent help.
I have tried your solution and make subtle improvements then it works, although it's just a temporary solution.
I have give up using the
<SheetTrigger>
and<SheetClose>
to control whether open the Sheet, and replace it withuseState
usage