/
DocsTocLinks.vue
83 lines (75 loc) · 1.92 KB
/
DocsTocLinks.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<script setup lang="ts">
import type { PropType } from 'vue'
import type { TocLink } from '@nuxt/content/dist/runtime/types'
defineProps({
links: {
type: Array as PropType<TocLink[]>,
default: () => []
}
})
const emit = defineEmits(['move'])
const router = useRouter()
const { activeHeadings, updateHeadings } = useScrollspy()
if (process.client) {
setTimeout(() => {
updateHeadings([
...document.querySelectorAll('.document-driven-page h1, .docus-content h1'),
...document.querySelectorAll('.document-driven-page h2, .docus-content h2'),
...document.querySelectorAll('.document-driven-page h3, .docus-content h3'),
...document.querySelectorAll('.document-driven-page h4, .docus-content h4')
])
}, 300)
}
function scrollToHeading (id: string) {
router.push(`#${id}`)
emit('move', id)
}
</script>
<template>
<ul class="docs-toc-links">
<li v-for="link in links" :key="link.text" :class="[`depth-${link.depth}`]">
<a
:href="`#${link.id}`"
:class="[activeHeadings.includes(link.id) && 'active']"
@click.prevent="scrollToHeading(link.id)"
>
{{ link.text }}
</a>
<DocsTocLinks v-if="link.children" :links="link.children" />
</li>
</ul>
</template>
<style scoped lang="ts">
css({
'.docs-toc-links': {
'.depth-3': {
paddingLeft: '{space.3}'
},
'.depth-4': {
paddingLeft: '{space.6}'
},
a: {
display: 'block',
padding: '{space.1} 0',
fontSize: '{text.sm.fontSize}',
lineHeight: '{text.sm.lineHeight}',
color: '{color.gray.500}',
truncate: true,
'@lg': {
paddingRight: '{space.3}'
},
'&:not(.active):hover': {
color: '{color.gray.900}',
},
'@dark': {
'&:not(.active):hover': {
color: '{color.gray.400}',
},
},
'&.active': {
color: '{color.primary.500}'
}
}
}
})
</style>