@@ -142,22 +142,36 @@ const stickyStyle = computed(() => {
142142// Track active TOC item based on URL hash
143143const tocContainerRef = useTemplateRef (' tocContainerRef' )
144144
145- function updateActiveTocLink(hash : string ) {
145+ function updateActiveTocLink() {
146146 const container = tocContainerRef .value
147147 if (! container ) return
148148
149+ const hash = window .location .hash
150+
149151 container .querySelector (' a.toc-active' )?.classList .remove (' toc-active' )
150152
151153 if (hash ) {
152- const link = container .querySelector (` a[href="${CSS .escape (hash )}"] ` )
154+ const link = container .querySelector (` a[href="# ${CSS .escape (hash . slice ( 1 ) )}"] ` )
153155 link ?.classList .add (' toc-active' )
154156 }
155157}
156158
159+ function onTocClick(e : Event ) {
160+ const link = (e .target as HTMLElement ).closest (' a[href^="#"]' )
161+ if (link ) {
162+ nextTick (updateActiveTocLink )
163+ }
164+ }
165+
157166if (import .meta .client ) {
158- watch (() => route .hash , hash => updateActiveTocLink (hash ))
159- watch (() => docsData .value ?.toc , () => nextTick (() => updateActiveTocLink (route .hash )))
160- onMounted (() => updateActiveTocLink (route .hash ))
167+ watch (() => docsData .value ?.toc , () => nextTick (updateActiveTocLink ))
168+ onMounted (() => {
169+ window .addEventListener (' hashchange' , updateActiveTocLink )
170+ updateActiveTocLink ()
171+ })
172+ onBeforeUnmount (() => {
173+ window .removeEventListener (' hashchange' , updateActiveTocLink )
174+ })
161175}
162176 </script >
163177
@@ -183,7 +197,7 @@ if (import.meta.client) {
183197 {{ $t('package.docs.contents') }}
184198 </h2 >
185199 <!-- eslint-disable vue/no-v-html -->
186- <div ref =" tocContainerRef" class =" toc-content" v-html =" docsData.toc" />
200+ <div ref =" tocContainerRef" class =" toc-content" @click = " onTocClick " v-html =" docsData.toc" />
187201 </div >
188202 </aside >
189203
0 commit comments