Skip to content

Commit a50520c

Browse files
authored
Merge pull request github#17450 from github/repo-sync
repo sync
2 parents ae7dd5a + 2e2ced9 commit a50520c

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

components/sidebar/RestCollapsibleSection.tsx

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export const RestCollapsibleSection = (props: SectionProps) => {
2929
const { routePath, defaultOpen, title, page, isStandaloneCategory } = props
3030
const [isOpen, setIsOpen] = useState(defaultOpen)
3131
const [currentAnchor, setCurrentAnchor] = useState('')
32+
const [visibleAnchor, setVisibleAnchor] = useState('')
33+
3234
const onToggle = (e: SyntheticEvent) => {
3335
const newIsOpen = (e.target as HTMLDetailsElement).open
3436
setIsOpen(newIsOpen)
@@ -38,6 +40,14 @@ export const RestCollapsibleSection = (props: SectionProps) => {
3840
})
3941
}
4042

43+
const miniTocItems =
44+
router.query.productId === 'rest' ||
45+
// These pages need the Article Page mini tocs instead of the Rest Pages
46+
router.asPath.includes('/rest/guides') ||
47+
router.asPath.includes('/rest/overview')
48+
? []
49+
: useRestContext().miniTocItems
50+
4151
useEffect(() => {
4252
if (!currentAnchor) {
4353
setCurrentAnchor(window.location.hash)
@@ -54,14 +64,34 @@ export const RestCollapsibleSection = (props: SectionProps) => {
5464
}
5565
}, [])
5666

57-
const miniTocItems =
58-
router.query.productId === 'rest' ||
59-
// These pages need the Article Page mini tocs instead of the Rest Pages
60-
router.asPath.includes('/rest/guides') ||
61-
router.asPath.includes('/rest/overview')
62-
? []
63-
: useRestContext().miniTocItems
67+
useEffect(() => {
68+
const observer = new IntersectionObserver(
69+
(entries) => {
70+
entries.forEach((entry) => {
71+
if (entry.target.id) {
72+
const anchor = '#' + entry.target.id.split('--')[0]
73+
if (entry.isIntersecting === true) setVisibleAnchor(anchor)
74+
} else if (router.asPath.includes('#')) {
75+
setVisibleAnchor('#' + router.asPath.split('#')[1])
76+
} else {
77+
setVisibleAnchor('')
78+
}
79+
})
80+
},
81+
{ rootMargin: '0px 0px -85% 0px' }
82+
)
83+
// TODO: When we add the ## About the {title} API to each operation
84+
// we can remove the h2 here
85+
const headingsList = Array.from(document.querySelectorAll('h2, h3'))
6486

87+
headingsList.forEach((heading) => {
88+
observer.observe(heading)
89+
})
90+
91+
return () => {
92+
observer.disconnect()
93+
}
94+
}, [miniTocItems])
6595
// This wrapper solves the issue of having standalone categories not
6696
// link to the new page. We want standalone categories to have links
6797
// just like maptopics/subcategories.
@@ -71,8 +101,7 @@ export const RestCollapsibleSection = (props: SectionProps) => {
71101
const renderRestAnchorLink = (miniTocItem: MiniTocItem) => {
72102
const miniTocAnchor = miniTocItem.contents.href
73103
const title = miniTocItem.contents.title
74-
const isCurrent = currentAnchor === miniTocAnchor
75-
104+
const isCurrent = visibleAnchor === miniTocAnchor
76105
return {
77106
text: title,
78107
renderItem: () => (
@@ -96,6 +125,7 @@ export const RestCollapsibleSection = (props: SectionProps) => {
96125
'd-block pl-6 pr-5 py-1 no-underline width-full',
97126
isCurrent ? 'color-fg-accent' : 'color-fg-default'
98127
)}
128+
onClick={() => setVisibleAnchor(miniTocAnchor)}
99129
href={miniTocAnchor}
100130
>
101131
{title}

0 commit comments

Comments
 (0)