@@ -10,10 +10,12 @@ import { useEffect, useMemo, useRef, useState } from "react"
1010import { noop } from "@/utils/noop"
1111import useTocHeadingIds from "./heading-ids"
1212
13- import { navbarHeight } from "@/components/Navbar/styles.export.module.scss"
13+ import { navbarHeight as navbarHeightString } from "@/components/Navbar/styles.export.module.scss"
1414
1515import type { TocItem } from "@/lib/unified/toc/types"
1616
17+ const navbarHeight = parseInt ( navbarHeightString , 10 ) ;
18+
1719// If the anchor has no height and is just a "marker" in the dom we'll use the parent (normally the link text) rect boundaries instead
1820const getVisibleBoundingClientRect = ( element : HTMLElement | null ) : DOMRect | null => {
1921 if ( ! element ) { return null }
@@ -32,15 +34,16 @@ const isInViewportTopHalf = (boundingRect: DOMRect, threshold: number) => {
3234
3335const useAnchors = ( toc : TocItem [ ] ) => {
3436 const headingIds = useTocHeadingIds ( toc )
35- const headingIdsSelector = useMemo ( ( ) => headingIds . map ( ( id ) => `#${ id } ` ) . join ( ',' ) , [ headingIds ] )
36-
37- const [ anchors , setAnchors ] = useState < HTMLElement [ ] > ( [ ] )
3837
38+ const [ anchors , setAnchors ] = useState < HTMLElement [ ] > ( [ ] ) ;
39+
3940 useEffect ( ( ) => {
40- if ( headingIdsSelector ) {
41- setAnchors ( Array . from ( document . querySelectorAll ( headingIdsSelector ) ) as HTMLElement [ ] )
42- }
43- } , [ headingIdsSelector ] )
41+ const headingIdsSelector = headingIds . map ( ( id ) => `#${ id } ` ) . join ( ',' ) ;
42+ setAnchors ( headingIdsSelector
43+ ? Array . from ( document . querySelectorAll ( headingIdsSelector ) ) as HTMLElement [ ]
44+ : [ ]
45+ ) ;
46+ } , [ headingIds ] ) ;
4447
4548 return anchors
4649}
@@ -103,8 +106,6 @@ export type TocHighlightConfig = {
103106const useActiveTocItem = ( toc : TocItem [ ] , config ?: TocHighlightConfig ) => {
104107 const lastActiveLinkRef = useRef < HTMLAnchorElement | undefined > ( undefined )
105108
106- const anchorTopOffset = navbarHeight
107-
108109 const anchors = useAnchors ( toc )
109110
110111 const cancelScroll = useRef < ( ) => void > ( noop )
@@ -139,7 +140,7 @@ const useActiveTocItem = (toc: TocItem[], config?: TocHighlightConfig) => {
139140
140141 const updateActiveLink = ( ) => {
141142 const links = getLinks ( linkClassName )
142- const activeAnchor = getActiveAnchor ( anchors , { anchorTopOffset } )
143+ const activeAnchor = getActiveAnchor ( anchors , { anchorTopOffset : navbarHeight } )
143144
144145 links . forEach ( ( link ) => {
145146 if ( activeAnchor ?. id === getLinkAnchorValue ( link ) ) {
@@ -165,7 +166,7 @@ const useActiveTocItem = (toc: TocItem[], config?: TocHighlightConfig) => {
165166 document . removeEventListener ( 'scroll' , updateActiveLink )
166167 document . removeEventListener ( 'resize' , updateActiveLink )
167168 }
168- } , [ config , anchorTopOffset ] )
169+ } , [ config , anchors ] )
169170}
170171
171172export default useActiveTocItem
0 commit comments