Commit ccfa8040b8
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/Collapsible.vue +26 −5
| @@ -1,15 +1,36 @@ | |||
| 1 | <script setup lang="ts"> | 1 | <script setup lang="ts"> |
| 2 | import { ref } from 'vue' | 2 | import { useEventListener } from '@vueuse/core' |
| 3 | import { nextTick, onMounted, ref } from 'vue' | ||
| 3 | 4 | ||
| 4 | const props = defineProps<{ | 5 | defineProps<{ title?: string }>() |
| 5 | title?: string | ||
| 6 | }>() | ||
| 7 | 6 | ||
| 8 | const open = ref(false) | 7 | const open = ref(false) |
| 8 | const detailsRef = ref<HTMLDetailsElement | null>(null) | ||
| 9 | |||
| 10 | function openForHash(hash: string) { | ||
| 11 | if (!hash || !detailsRef.value) return | ||
| 12 | const id = decodeURIComponent(hash).slice(1) | ||
| 13 | const target = id && document.getElementById(id) | ||
| 14 | if (!target || !detailsRef.value.contains(target)) return | ||
| 15 | open.value = true | ||
| 16 | nextTick(() => target.scrollIntoView({ block: 'start' })) | ||
| 17 | } | ||
| 18 | |||
| 19 | onMounted(() => openForHash(location.hash)) | ||
| 20 | useEventListener(window, 'hashchange', () => openForHash(location.hash)) | ||
| 21 | useEventListener(document, 'click', (e) => { | ||
| 22 | const link = (e.target as HTMLElement)?.closest?.<HTMLAnchorElement>('a[href*="#"]') | ||
| 23 | if (link?.hash) nextTick(() => openForHash(link.hash)) | ||
| 24 | }, { capture: true }) | ||
| 9 | </script> | 25 | </script> |
| 10 | 26 | ||
| 11 | <template> | 27 | <template> |
| 12 | <details class="details custom-block" :open="open || undefined" @toggle="open = ($event.target as HTMLDetailsElement).open"> | 28 | <details |
| 29 | ref="detailsRef" | ||
| 30 | class="details custom-block" | ||
| 31 | :open="open || undefined" | ||
| 32 | @toggle="open = ($event.target as HTMLDetailsElement).open" | ||
| 33 | > | ||
| 13 | <summary>{{ title || 'Details' }}</summary> | 34 | <summary>{{ title || 'Details' }}</summary> |
| 14 | <slot /> | 35 | <slot /> |
| 15 | </details> | 36 | </details> |