Commit 8adb654966
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/Collapsible.vue +5 −26
| @@ -1,36 +1,15 @@ | ||
| 1 | 1 | <script setup lang="ts"> |
| 2 | import { useEventListener } from '@vueuse/core' | |
| 3 | import { nextTick, onMounted, ref } from 'vue' | |
| 2 | import { ref } from 'vue' | |
| 4 | 3 | |
| 5 | defineProps<{ title?: string }>() | |
| 4 | const props = defineProps<{ | |
| 5 | title?: string | |
| 6 | }>() | |
| 6 | 7 | |
| 7 | 8 | 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 }) | |
| 25 | 9 | </script> |
| 26 | 10 | |
| 27 | 11 | <template> |
| 28 | <details | |
| 29 | ref="detailsRef" | |
| 30 | class="details custom-block" | |
| 31 | :open="open || undefined" | |
| 32 | @toggle="open = ($event.target as HTMLDetailsElement).open" | |
| 33 | > | |
| 12 | <details class="details custom-block" :open="open || undefined" @toggle="open = ($event.target as HTMLDetailsElement).open"> | |
| 34 | 13 | <summary>{{ title || 'Details' }}</summary> |
| 35 | 14 | <slot /> |
| 36 | 15 | </details> |