diff --git a/apps/docs/components/Sidebar.tsx b/apps/docs/components/Sidebar.tsx index bd6b0dbcc..73b02775f 100644 --- a/apps/docs/components/Sidebar.tsx +++ b/apps/docs/components/Sidebar.tsx @@ -12,7 +12,7 @@ import { import * as Accordion from '@radix-ui/react-accordion' import Link from 'next/link' import { usePathname } from 'next/navigation' -import { createContext, useContext, useEffect } from 'react' +import { UIEvent, createContext, useContext, useEffect, useRef } from 'react' import { SectionLinks } from './Header' import { Chevron } from './Icons' import { Search } from './Search' @@ -28,21 +28,46 @@ const linkContext = createContext<{ sectionId: string | null } | null>(null) +// N.B. UGH, the sidebar's scroll position keeps getting lost when navigation occurs +// and we keep track of the last known position here outside of the component because +// it keeps re-rendering. +let scrollPosition = 0 + export function Sidebar({ headings, links, sectionId, categoryId, articleId }: SidebarProps) { const activeId = articleId ?? categoryId ?? sectionId + const sidebarRef = useRef(null) const pathName = usePathname() useEffect(() => { document.body.classList.remove('sidebar-open') - document.querySelector('.sidebar__nav [data-active=true]')?.scrollIntoView({ block: 'center' }) + const sidebarEl = sidebarRef.current + if (!sidebarEl) return + + sidebarEl.scrollTo(0, scrollPosition) + + const activeLink = document.querySelector('.sidebar__nav [data-active=true]') as HTMLElement + if ( + activeLink && + activeLink.offsetTop < sidebarEl.scrollTop && + activeLink.offsetTop > sidebarEl.scrollTop + sidebarEl.clientHeight + ) { + // The above will *mostly* work to keep the position but we have some accordions that will collapse + // (like in the Reference docs) and we need to scroll to the active item. + activeLink.scrollIntoView({ block: 'center' }) + } }, [pathName]) + const handleScroll = (e: UIEvent) => { + e.stopPropagation() + scrollPosition = sidebarRef.current?.scrollTop ?? 0 + } + return ( <> -
e.stopPropagation()}> +
diff --git a/apps/docs/styles/globals.css b/apps/docs/styles/globals.css index ffbe05583..30b2a11d9 100644 --- a/apps/docs/styles/globals.css +++ b/apps/docs/styles/globals.css @@ -675,7 +675,7 @@ body { } .footer__fancybox__item { - transition: all 3s; + transition: opacity 3s; opacity: 0; width: 28px; height: 28px; @@ -714,7 +714,7 @@ body { } .footer__fancybox__item:hover { - transition: all 0s; + transition: opacity 0s; opacity: 0.32; }