-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathCollapsibleSection.vue
More file actions
131 lines (115 loc) · 3.63 KB
/
CollapsibleSection.vue
File metadata and controls
131 lines (115 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<script setup lang="ts">
import { shallowRef, computed } from 'vue'
interface Props {
title: string
isLoading?: boolean
headingLevel?: `h${number}`
id: string
}
const props = withDefaults(defineProps<Props>(), {
isLoading: false,
headingLevel: 'h2',
})
const appSettings = useSettings()
const buttonId = `${props.id}-collapsible-button`
const contentId = `${props.id}-collapsible-content`
const headingId = `${props.id}-heading`
const isOpen = shallowRef(true)
onPrehydrate(() => {
const settings = JSON.parse(localStorage.getItem('npmx-settings') || '{}')
const collapsed: string[] = settings?.sidebar?.collapsed || []
for (const id of collapsed) {
if (!document.documentElement.dataset.collapsed?.includes(id)) {
document.documentElement.dataset.collapsed = (
document.documentElement.dataset.collapsed +
' ' +
id
).trim()
}
}
})
onMounted(() => {
if (document?.documentElement) {
isOpen.value = !(document.documentElement.dataset.collapsed?.includes(props.id) ?? false)
}
})
function toggle() {
isOpen.value = !isOpen.value
const removed = appSettings.settings.value.sidebar.collapsed.filter(c => c !== props.id)
if (isOpen.value) {
appSettings.settings.value.sidebar.collapsed = removed
} else {
removed.push(props.id)
appSettings.settings.value.sidebar.collapsed = removed
}
document.documentElement.dataset.collapsed =
appSettings.settings.value.sidebar.collapsed.join(' ')
}
const ariaLabel = computed(() => {
const action = isOpen.value ? 'Collapse' : 'Expand'
return props.title ? `${action} ${props.title}` : action
})
useHead({
style: [
{
innerHTML: `
:root[data-collapsed~='${props.id}'] section[data-anchor-id='${props.id}'] .collapsible-content {
grid-template-rows: 0fr;
}`,
},
],
})
</script>
<template>
<section class="scroll-mt-20" :data-anchor-id="id">
<div class="flex items-center justify-between mb-3">
<component
:is="headingLevel"
:id="headingId"
class="group text-xs text-fg-subtle uppercase tracking-wider flex items-center gap-2"
>
<button
:id="buttonId"
type="button"
class="w-4 h-4 flex items-center justify-center text-fg-subtle hover:text-fg-muted transition-colors duration-200 shrink-0 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 rounded"
:aria-expanded="isOpen"
:aria-controls="contentId"
:aria-label="ariaLabel"
@click="toggle"
>
<span
v-if="isLoading"
class="i-carbon:rotate-180 w-3 h-3 motion-safe:animate-spin"
aria-hidden="true"
/>
<span
v-else
class="w-3 h-3 transition-transform duration-200"
:class="isOpen ? 'i-carbon:chevron-down' : 'i-carbon:chevron-right'"
aria-hidden="true"
/>
</button>
<a
:href="`#${id}`"
class="inline-flex items-center gap-1.5 text-fg-subtle hover:text-fg-muted transition-colors duration-200 no-underline"
>
{{ title }}
<span
class="i-carbon:link w-3 h-3 block opacity-0 group-hover:opacity-100 transition-opacity duration-200"
aria-hidden="true"
/>
</a>
</component>
<!-- Actions slot for buttons or other elements -->
<slot name="actions" />
</div>
<div
:id="contentId"
class="grid ms-6 transition-[grid-template-rows] duration-200 ease-in-out collapsible-content overflow-hidden"
>
<div class="min-h-0">
<slot />
</div>
</div>
</section>
</template>