Skip to content

Commit

Permalink
fix: sidebar激活的锚点不对bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-Bill committed Apr 28, 2024
1 parent 1360754 commit ec9000a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
registry=https://registry.npmjs.org/
registry=https://registry.npmmirror.com/
13 changes: 12 additions & 1 deletion src/client/theme-default/slots/Toc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const Toc: FC = () => {
const { loading } = useSiteData();
const prevIndexRef = useRef(0);
const [sectionRefs, setSectionRefs] = useState<RefObject<HTMLElement>[]>([]);
const [headerHeight, setHeaderHeight] = useState(0);

const memoToc = React.useMemo(() => {
let toc = meta.toc;
if (tabMeta) {
Expand All @@ -44,8 +46,17 @@ const Toc: FC = () => {
}
}, [pathname, search, loading, memoToc]);

useEffect(() => {
if (sectionRefs.length > 0) {
// find the header height, and set it to scrollspy offset
// because the header is sticky, so we need to set the offset to avoid the active item is hidden by the header
const header = document.querySelector('.dumi-default-header');
setHeaderHeight(header ? header.clientHeight : 0);
}
}, [sectionRefs]);

return sectionRefs.length ? (
<ScrollSpy sectionRefs={sectionRefs}>
<ScrollSpy sectionRefs={sectionRefs} offset={-headerHeight}>
{({ currentElementIndexInViewport }) => {
// for keep prev item active when no item in viewport
if (currentElementIndexInViewport > -1)
Expand Down

0 comments on commit ec9000a

Please sign in to comment.