Skip to content

Commit febae0c

Browse files
committed
feat: readme toc dropdown scroll to the selected item position
1 parent 225b6d4 commit febae0c

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

app/components/ReadmeTocDropdown.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ function handleKeydown(event: KeyboardEvent) {
139139
break
140140
}
141141
}
142+
143+
const itemScrollIntoView = (index: number) => {
144+
const item = props.toc[index]
145+
if (!item) return
146+
const el = document.getElementById(`${listboxId}-${item.id}`)
147+
if (el) {
148+
el.scrollIntoView({ block: 'center'})
149+
}
150+
}
151+
152+
watch(isOpen, (open) => {
153+
if (open && highlightedIndex.value >= 0) {
154+
itemScrollIntoView(highlightedIndex.value)
155+
}
156+
}, {
157+
flush: 'post',
158+
})
142159
</script>
143160

144161
<template>

0 commit comments

Comments
 (0)