Commit ce307903f9

ce307903f9f36acefa6264a63046592b14ef161c

parent: 2f993c59f3

Unregistered key

M$erable <the_enshittificator@protonmail.com> · 2026-05-06 22:18 UTC
committer: <noreply@github.com>

Fix Incomplete Scroll Positioning for Collapsibles (#161)

* fix(layout): wait for repaint before scrolling to collapsibles

* refactor(collapsible): optimize scroll targeting and simplify dom traversal

* fix(search): improve collapsible scroll logic and reliability

Layout: unified · split

docs/.vitepress/theme/Layout.vue +64 −43
@@ -45,51 +45,88 @@ watch(() => route.path, () => {
4545 homeSidebarOpen.value = false
4646})
4747
48const openCollapsibles = () => {
49 const hash = window.location.hash.slice(1)
50 if (!hash) return
48let searchNavigated = false
5149
52 const target = document.getElementById(decodeURIComponent(hash)) || document.getElementById(hash)
53 if (!target) return
50if (!import.meta.env.SSR) {
51 useEventListener(window, 'search-nav', () => {
52 searchNavigated = true
53 tryOpenCollapsibles()
54 })
55}
5456
57const openCollapsibles = (target: HTMLElement) => {
58 let scrollTarget = target
5559 let el: HTMLElement | null = target
60
5661 while ((el = el.closest('details'))) {
5762 el.open = true
63 scrollTarget = el
5864 el = el.parentElement
5965 }
6066
61 if (/^H[1-6]$/.test(target.tagName)) {
62 const level = parseInt(target.tagName[1])
63 let sibling = target.nextElementSibling
67 const tagName = target.tagName
68 if (tagName.length === 2 && tagName[0] === 'H') {
69 const level = +tagName[1]
70 if (level >= 1 && level <= 6) {
71 let sibling = target.nextElementSibling
6472
65 while (sibling) {
66 if (/^H[1-6]$/.test(sibling.tagName)) {
67 if (parseInt(sibling.tagName[1]) <= level) break
68 }
73 while (sibling) {
74 const siblingTag = sibling.tagName
75 if (siblingTag.length === 2 && siblingTag[0] === 'H' && +siblingTag[1] <= level) break
6976
70 if (sibling.tagName === 'DETAILS') {
71 (sibling as HTMLDetailsElement).open = true
72 }
77 if (siblingTag === 'DETAILS') {
78 (sibling as HTMLDetailsElement).open = true
79 if (scrollTarget === target) scrollTarget = sibling as HTMLElement
80 }
7381
74 const nested = sibling.querySelectorAll('details')
75 for (let i = 0; i < nested.length; i++) {
76 nested[i].open = true
77 }
82 const nested = sibling.getElementsByTagName('details')
83 for (let i = 0, len = nested.length; i < len; i++) {
84 nested[i].open = true
85 if (scrollTarget === target) scrollTarget = nested[i]
86 }
7887
79 sibling = sibling.nextElementSibling
88 sibling = sibling.nextElementSibling
89 }
8090 }
8191 }
92
93 requestAnimationFrame(() => {
94 requestAnimationFrame(() => {
95 const navBar = document.querySelector<HTMLElement>('.VPNavBar')
96 const navOffset = navBar ? navBar.offsetHeight : 0
97 window.scrollTo({
98 top: scrollTarget.getBoundingClientRect().top + window.scrollY - navOffset,
99 behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth'
100 })
101 scrollTarget.setAttribute('tabindex', '-1')
102 scrollTarget.focus({ preventScroll: true })
103 })
104 })
82105}
83106
84watch(() => route.data, async () => {
107const tryOpenCollapsibles = async () => {
108 if (!searchNavigated) return
85109 await nextTick()
86 openCollapsibles()
87})
88110
89watch(() => route.hash, async () => {
90 await nextTick()
91 openCollapsibles()
92})
111 requestAnimationFrame(() => {
112 const hash = window.location.hash.slice(1)
113 if (!hash) {
114 searchNavigated = false
115 return
116 }
117
118 const id = decodeURIComponent(hash)
119 const target = document.getElementById(id) || document.getElementById(hash)
120
121 if (target) {
122 openCollapsibles(target)
123 searchNavigated = false
124 }
125 })
126}
127
128watch(() => route.data, tryOpenCollapsibles, { flush: 'post' })
129watch(() => route.hash, tryOpenCollapsibles, { flush: 'post' })
93130
94131function onSidebarEnter() {
95132 sidebarRef.value?.focus()
@@ -217,22 +254,6 @@ const reloadTakodachi = () => {
217254
218255onMounted(() => {
219256 if (import.meta.env.SSR) return
220 useEventListener(document, 'click', async (e: MouseEvent) => {
221 if ((e.target as HTMLElement).closest('a.result')) {
222 await nextTick()
223 requestAnimationFrame(() => requestAnimationFrame(openCollapsibles))
224 }
225 })
226
227 useEventListener(document, 'keydown', async (e: KeyboardEvent) => {
228 if (e.key === 'Enter') {
229 const searchBox = document.querySelector('.VPLocalSearchBox') || document.querySelector('.VPNavBarSearch')
230 if (searchBox && searchBox.contains(e.target as Node)) {
231 await nextTick()
232 requestAnimationFrame(() => requestAnimationFrame(openCollapsibles))
233 }
234 }
235 })
236257
237258 // Arrow key scrolling + Escape close for sidebars and content
238259 let activeScrollTarget: HTMLElement | Window = window
docs/.vitepress/theme/components/Search.vue +7 −1
@@ -417,11 +417,17 @@ onKeyStroke('Enter', (e) => {
417417 }
418418
419419 if (selectedPackage) {
420 window.dispatchEvent(new CustomEvent('search-nav'))
420421 router.go(selectedPackage.id)
421422 showSearch.value = false
422423 }
423424})
424425
426function onResultClick() {
427 window.dispatchEvent(new CustomEvent('search-nav'))
428 showSearch.value = false
429}
430
425431onKeyStroke('Escape', () => {
426432 showSearch.value = false
427433})
@@ -723,7 +729,7 @@ function onMouseMove(e: MouseEvent) {
723729 @mouseenter="!disableMouseOver &&
724730 (selectedIndex = index + 1)"
725731 @focusin="selectedIndex = index + 1"
726 @click="showSearch = false"
732 @click="onResultClick"
727733 :data-index="index + 1"
728734 >
729735 <div>