@@ -29,6 +29,8 @@ export const RestCollapsibleSection = (props: SectionProps) => {
29
29
const { routePath, defaultOpen, title, page, isStandaloneCategory } = props
30
30
const [ isOpen , setIsOpen ] = useState ( defaultOpen )
31
31
const [ currentAnchor , setCurrentAnchor ] = useState ( '' )
32
+ const [ visibleAnchor , setVisibleAnchor ] = useState ( '' )
33
+
32
34
const onToggle = ( e : SyntheticEvent ) => {
33
35
const newIsOpen = ( e . target as HTMLDetailsElement ) . open
34
36
setIsOpen ( newIsOpen )
@@ -38,6 +40,14 @@ export const RestCollapsibleSection = (props: SectionProps) => {
38
40
} )
39
41
}
40
42
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
+
41
51
useEffect ( ( ) => {
42
52
if ( ! currentAnchor ) {
43
53
setCurrentAnchor ( window . location . hash )
@@ -54,14 +64,34 @@ export const RestCollapsibleSection = (props: SectionProps) => {
54
64
}
55
65
} , [ ] )
56
66
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' ) )
64
86
87
+ headingsList . forEach ( ( heading ) => {
88
+ observer . observe ( heading )
89
+ } )
90
+
91
+ return ( ) => {
92
+ observer . disconnect ( )
93
+ }
94
+ } , [ miniTocItems ] )
65
95
// This wrapper solves the issue of having standalone categories not
66
96
// link to the new page. We want standalone categories to have links
67
97
// just like maptopics/subcategories.
@@ -71,8 +101,7 @@ export const RestCollapsibleSection = (props: SectionProps) => {
71
101
const renderRestAnchorLink = ( miniTocItem : MiniTocItem ) => {
72
102
const miniTocAnchor = miniTocItem . contents . href
73
103
const title = miniTocItem . contents . title
74
- const isCurrent = currentAnchor === miniTocAnchor
75
-
104
+ const isCurrent = visibleAnchor === miniTocAnchor
76
105
return {
77
106
text : title ,
78
107
renderItem : ( ) => (
@@ -96,6 +125,7 @@ export const RestCollapsibleSection = (props: SectionProps) => {
96
125
'd-block pl-6 pr-5 py-1 no-underline width-full' ,
97
126
isCurrent ? 'color-fg-accent' : 'color-fg-default'
98
127
) }
128
+ onClick = { ( ) => setVisibleAnchor ( miniTocAnchor ) }
99
129
href = { miniTocAnchor }
100
130
>
101
131
{ title }
0 commit comments