-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathReadmeTocDropdown.vue
More file actions
248 lines (220 loc) · 7.05 KB
/
ReadmeTocDropdown.vue
File metadata and controls
248 lines (220 loc) · 7.05 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<script setup lang="ts">
import type { TocItem } from '#shared/types/readme'
import { onClickOutside, useEventListener } from '@vueuse/core'
const props = defineProps<{
toc: TocItem[]
activeId?: string | null
}>()
interface TocNode extends TocItem {
children: TocNode[]
}
function buildTocTree(items: TocItem[]): TocNode[] {
const result: TocNode[] = []
const stack: TocNode[] = []
for (const item of items) {
const node: TocNode = { ...item, children: [] }
// Find parent: look for the last item with smaller depth
while (stack.length > 0 && stack[stack.length - 1]!.depth >= item.depth) {
stack.pop()
}
if (stack.length === 0) {
result.push(node)
} else {
stack[stack.length - 1]!.children.push(node)
}
stack.push(node)
}
return result
}
const tocTree = computed(() => buildTocTree(props.toc))
// Create a map from id to index for efficient lookup
const idToIndex = computed(() => {
const map = new Map<string, number>()
props.toc.forEach((item, index) => map.set(item.id, index))
return map
})
const listRef = useTemplateRef('listRef')
const triggerRef = useTemplateRef('triggerRef')
const isOpen = shallowRef(false)
const highlightedIndex = shallowRef(-1)
const dropdownPosition = shallowRef<{ top: number; right: number } | null>(null)
function getDropdownStyle(): Record<string, string> {
if (!dropdownPosition.value) return {}
return {
top: `${dropdownPosition.value.top}px`,
right: `${document.documentElement.clientWidth - dropdownPosition.value.right}px`,
}
}
// Close on scroll (but not when scrolling inside the dropdown)
function handleScroll(event: Event) {
if (!isOpen.value) return
if (listRef.value && event.target instanceof Node && listRef.value.contains(event.target)) {
return
}
close()
}
useEventListener('scroll', handleScroll, { passive: true })
// Generate unique ID for accessibility
const inputId = useId()
const listboxId = `${inputId}-toc-listbox`
function toggle() {
if (isOpen.value) {
close()
} else {
const rect = triggerRef.value?.getBoundingClientRect()
if (rect) {
dropdownPosition.value = {
top: rect.bottom + 4,
right: rect.right,
}
}
isOpen.value = true
// Highlight active item if any
const activeIndex = idToIndex.value.get(props.activeId ?? '')
highlightedIndex.value = activeIndex ?? 0
}
}
function close() {
isOpen.value = false
highlightedIndex.value = -1
}
function select() {
close()
triggerRef.value?.focus()
}
function getIndex(id: string): number {
return idToIndex.value.get(id) ?? -1
}
// Check for reduced motion preference
const prefersReducedMotion = useMediaQuery('(prefers-reduced-motion: reduce)')
onClickOutside(listRef, close, { ignore: [triggerRef] })
function handleKeydown(event: KeyboardEvent) {
if (!isOpen.value) return
const itemCount = props.toc.length
switch (event.key) {
case 'ArrowDown':
event.preventDefault()
highlightedIndex.value = (highlightedIndex.value + 1) % itemCount
break
case 'ArrowUp':
event.preventDefault()
highlightedIndex.value =
highlightedIndex.value <= 0 ? itemCount - 1 : highlightedIndex.value - 1
break
case 'Enter': {
event.preventDefault()
const item = props.toc[highlightedIndex.value]
if (item) {
select()
}
break
}
case 'Escape':
close()
triggerRef.value?.focus()
break
}
}
</script>
<template>
<ButtonBase
ref="triggerRef"
type="button"
:aria-expanded="isOpen"
aria-haspopup="listbox"
:aria-label="$t('package.readme.toc_title')"
:aria-controls="listboxId"
@click="toggle"
@keydown="handleKeydown"
classicon="i-lucide:list"
class="px-2.5"
block
>
<span
class="i-lucide:chevron-down w-3 h-3"
:class="[
{ 'rotate-180': isOpen },
prefersReducedMotion ? '' : 'transition-transform duration-200',
]"
aria-hidden="true"
/>
</ButtonBase>
<Teleport to="body">
<div
:id="listboxId"
ref="listRef"
role="listbox"
:aria-hidden="!isOpen"
:aria-activedescendant="
isOpen && highlightedIndex >= 0 ? `${listboxId}-${toc[highlightedIndex]?.id}` : undefined
"
:aria-label="$t('package.readme.toc_title')"
:style="
isOpen
? getDropdownStyle()
: { visibility: 'hidden', position: 'fixed', pointerEvents: 'none' }
"
:class="[
'fixed bg-bg-subtle border border-border rounded-md shadow-lg z-50 max-h-80 overflow-y-auto w-56 overscroll-contain',
!isOpen ? 'opacity-0' : prefersReducedMotion ? '' : 'transition-opacity duration-150',
]"
>
<template v-for="node in tocTree" :key="node.id">
<NuxtLink
:id="`${listboxId}-${node.id}`"
:to="`#${node.id}`"
role="option"
:aria-selected="activeId === node.id"
class="flex items-center gap-2 px-3 py-1.5 text-sm cursor-pointer transition-colors duration-150"
:class="[
activeId === node.id ? 'text-fg font-medium' : 'text-fg-muted',
highlightedIndex === getIndex(node.id) ? 'bg-bg-elevated' : 'hover:bg-bg-elevated',
]"
dir="auto"
@click="select()"
@mouseenter="highlightedIndex = getIndex(node.id)"
>
<span class="truncate">{{ node.text }}</span>
</NuxtLink>
<template v-for="child in node.children" :key="child.id">
<NuxtLink
:id="`${listboxId}-${child.id}`"
:to="`#${child.id}`"
role="option"
:aria-selected="activeId === child.id"
class="flex items-center gap-2 px-3 py-1.5 ps-6 text-sm cursor-pointer transition-colors duration-150"
:class="[
activeId === child.id ? 'text-fg font-medium' : 'text-fg-subtle',
highlightedIndex === getIndex(child.id) ? 'bg-bg-elevated' : 'hover:bg-bg-elevated',
]"
dir="auto"
@click="select()"
@mouseenter="highlightedIndex = getIndex(child.id)"
>
<span class="truncate">{{ child.text }}</span>
</NuxtLink>
<NuxtLink
v-for="grandchild in child.children"
:id="`${listboxId}-${grandchild.id}`"
:to="`#${grandchild.id}`"
:key="grandchild.id"
role="option"
:aria-selected="activeId === grandchild.id"
class="flex items-center gap-2 px-3 py-1.5 ps-9 text-sm cursor-pointer transition-colors duration-150"
:class="[
grandchild.id === activeId ? 'text-fg font-medium' : 'text-fg-subtle',
highlightedIndex === getIndex(grandchild.id)
? 'bg-bg-elevated'
: 'hover:bg-bg-elevated',
]"
dir="auto"
@click="select()"
@mouseenter="highlightedIndex = getIndex(grandchild.id)"
>
<span class="truncate">{{ grandchild.text }}</span>
</NuxtLink>
</template>
</template>
</div>
</Teleport>
</template>