Commit cbbdbc2b3f

cbbdbc2b3fa8767f3caf510dde9a738429e48041

parent: 57cab79695

Unregistered key

M$erable <the_enshittificator@protonmail.com> · 2026-05-03 14:15 UTC
committer: <noreply@github.com>

fix(search): auto-open collapsibles on search navigation (#157)

Layout: unified · split

docs/.vitepress/theme/Layout.vue +63 −1
@@ -17,7 +17,7 @@ import type { DefaultTheme as Theme } from 'vitepress'
17import DefaultTheme from 'vitepress/theme' 17import DefaultTheme from 'vitepress/theme'
18import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue' 18import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue'
19import { getSidebarGroups } from 'vitepress/dist/client/theme-default/support/sidebar' 19import { getSidebarGroups } from 'vitepress/dist/client/theme-default/support/sidebar'
20import { computed, onMounted, onUnmounted, ref, watch } from 'vue' 20import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
21import { sidebar } from '../configs/constants' 21import { sidebar } from '../configs/constants'
22import AnnouncementPill from './components/AnnouncementPill.vue' 22import AnnouncementPill from './components/AnnouncementPill.vue'
23import NotFoundComponent from './components/NotFound.vue' 23import NotFoundComponent from './components/NotFound.vue'
@@ -45,6 +45,52 @@ watch(() => route.path, () => {
45 homeSidebarOpen.value = false 45 homeSidebarOpen.value = false
46}) 46})
47 47
48const openCollapsibles = () => {
49 const hash = window.location.hash.slice(1)
50 if (!hash) return
51
52 const target = document.getElementById(decodeURIComponent(hash)) || document.getElementById(hash)
53 if (!target) return
54
55 let el: HTMLElement | null = target
56 while ((el = el.closest('details'))) {
57 el.open = true
58 el = el.parentElement
59 }
60
61 if (/^H[1-6]$/.test(target.tagName)) {
62 const level = parseInt(target.tagName[1])
63 let sibling = target.nextElementSibling
64
65 while (sibling) {
66 if (/^H[1-6]$/.test(sibling.tagName)) {
67 if (parseInt(sibling.tagName[1]) <= level) break
68 }
69
70 if (sibling.tagName === 'DETAILS') {
71 (sibling as HTMLDetailsElement).open = true
72 }
73
74 const nested = sibling.querySelectorAll('details')
75 for (let i = 0; i < nested.length; i++) {
76 nested[i].open = true
77 }
78
79 sibling = sibling.nextElementSibling
80 }
81 }
82}
83
84watch(() => route.data, async () => {
85 await nextTick()
86 openCollapsibles()
87})
88
89watch(() => route.hash, async () => {
90 await nextTick()
91 openCollapsibles()
92})
93
48function onSidebarEnter() { 94function onSidebarEnter() {
49 sidebarRef.value?.focus() 95 sidebarRef.value?.focus()
50} 96}
@@ -171,6 +217,22 @@ const reloadTakodachi = () => {
171 217
172onMounted(() => { 218onMounted(() => {
173 if (import.meta.env.SSR) return 219 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 })
174 236
175 // Arrow key scrolling + Escape close for sidebars and content 237 // Arrow key scrolling + Escape close for sidebars and content
176 let activeScrollTarget: HTMLElement | Window = window 238 let activeScrollTarget: HTMLElement | Window = window