Skip to content

Commit 4529965

Browse files
committed
feat: highlight active item in package docs sidebar
1 parent 32ba285 commit 4529965

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

app/pages/package-docs/[...path].vue

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,27 @@ const stickyStyle = computed(() => {
138138
'--combined-header-height': `${56 + (packageHeaderHeight.value || 44)}px`,
139139
}
140140
})
141+
142+
// Track active TOC item based on URL hash
143+
const tocContainerRef = useTemplateRef('tocContainerRef')
144+
145+
function updateActiveTocLink(hash: string) {
146+
const container = tocContainerRef.value
147+
if (!container) return
148+
149+
container.querySelector('a.toc-active')?.classList.remove('toc-active')
150+
151+
if (hash) {
152+
const link = container.querySelector(`a[href="${CSS.escape(hash)}"]`)
153+
link?.classList.add('toc-active')
154+
}
155+
}
156+
157+
if (import.meta.client) {
158+
watch(() => route.hash, hash => updateActiveTocLink(hash))
159+
watch(() => docsData.value?.toc, () => nextTick(() => updateActiveTocLink(route.hash)))
160+
onMounted(() => updateActiveTocLink(route.hash))
161+
}
141162
</script>
142163

143164
<template>
@@ -162,7 +183,7 @@ const stickyStyle = computed(() => {
162183
{{ $t('package.docs.contents') }}
163184
</h2>
164185
<!-- eslint-disable vue/no-v-html -->
165-
<div class="toc-content" v-html="docsData.toc" />
186+
<div ref="tocContainerRef" class="toc-content" v-html="docsData.toc" />
166187
</div>
167188
</aside>
168189

@@ -231,6 +252,10 @@ const stickyStyle = computed(() => {
231252
@apply text-xs text-fg-subtle hover:text-fg block py-0.5 truncate;
232253
}
233254
255+
.toc-content a.toc-active {
256+
@apply text-fg font-medium;
257+
}
258+
234259
/* Main docs content container - no max-width to use full space */
235260
.docs-content {
236261
@apply max-w-none;

0 commit comments

Comments
 (0)