Commit d2a6817e0c

d2a6817e0c86db86d5b072d0f52798aa5d159927

parent: 2e0bd5e0a1

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-06-09 06:28 UTC

Add anchors for tab headers

Layout: unified · split

docs/.vitepress/theme/Layout.vue +59
@@ -103,6 +103,52 @@ const getScrollTargetForAnchor = (target: HTMLElement) => {
103103 return target
104104}
105105
106const getHeadingAnchor = (heading: HTMLElement) => {
107 const href = heading.querySelector<HTMLAnchorElement>('a[href^="#"]')
108 ?.getAttribute('href')
109
110 if (href?.startsWith('#') && href.length > 1) return href.slice(1)
111
112 return heading.id || undefined
113}
114
115const getSelectedTabHeadingAnchor = (tabs: HTMLElement) => {
116 const selectedTab = tabs.querySelector<HTMLButtonElement>(
117 '.plugin-tabs--tab[aria-selected="true"]'
118 )
119 const panelId = selectedTab?.getAttribute('aria-controls')
120 const panel = panelId
121 ? document.getElementById(panelId)
122 : tabs.querySelector<HTMLElement>('.plugin-tabs--content')
123 const heading = panel?.querySelector<HTMLElement>('.tab-search-heading')
124
125 return heading ? getHeadingAnchor(heading) : undefined
126}
127
128const updateHashForSelectedTab = async (tabs: HTMLElement) => {
129 await nextTick()
130 await nextFrame()
131
132 const anchor = getSelectedTabHeadingAnchor(tabs)
133 if (!anchor || window.location.hash.slice(1) === anchor) return
134
135 window.history.pushState(
136 null,
137 '',
138 `${window.location.pathname}${window.location.search}#${anchor}`
139 )
140}
141
142const queueHashUpdateForTabSelection = (target: EventTarget | null) => {
143 if (!(target instanceof HTMLElement)) return
144
145 const button = target.closest<HTMLButtonElement>(
146 '.plugin-tabs--tab[role="tab"]'
147 )
148 const tabs = button?.closest<HTMLElement>('.plugin-tabs')
149 if (tabs) void updateHashForSelectedTab(tabs)
150}
151
106152const scrollToElement = (target: HTMLElement, smooth = true) => {
107153 requestAnimationFrame(() => {
108154 requestAnimationFrame(() => {
@@ -502,6 +548,19 @@ onMounted(() => {
502548 document.addEventListener('mouseout', hideTooltip)
503549 document.addEventListener('scroll', hideTooltip, true)
504550
551 useEventListener(document, 'click', (e: MouseEvent) => {
552 if (!e.isTrusted) return
553 queueHashUpdateForTabSelection(e.target)
554 })
555
556 useEventListener(document, 'keydown', (e: KeyboardEvent) => {
557 if (!e.isTrusted || (e.key !== 'ArrowLeft' && e.key !== 'ArrowRight')) {
558 return
559 }
560
561 queueHashUpdateForTabSelection(e.target)
562 })
563
505564 onUnmounted(() => {
506565 document.removeEventListener('mouseover', showTooltip)
507566 document.removeEventListener('mouseout', hideTooltip)