Skip to content

Commit 007c090

Browse files
committed
fix: typing fixes
1 parent b9b067f commit 007c090

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

website/src/components/Toc/utils/active-heading.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import { useEffect, useMemo, useRef, useState } from "react"
1010
import { noop } from "@/utils/noop"
1111
import 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

1515
import 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
1820
const getVisibleBoundingClientRect = (element: HTMLElement | null): DOMRect | null => {
1921
if (!element) { return null }
@@ -32,15 +34,16 @@ const isInViewportTopHalf = (boundingRect: DOMRect, threshold: number) => {
3234

3335
const 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 = {
103106
const 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

171172
export default useActiveTocItem

website/src/pages/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const Home: NextPage<HomePageProps> = ({ sidebar }) => {
2929
<h1>ACM AI Wiki</h1>
3030
<p>Welcome to the ACM AI Wiki!</p>
3131
<p>The ACM AI wiki serves as a central repository for various resources produced by ACM AI.</p>
32+
</ContentContainer>
3233
</ContentWrapper>
3334
</MainWrapper>
3435
</>

0 commit comments

Comments
 (0)