-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathCollapsibleSection.vue
More file actions
132 lines (116 loc) · 3.58 KB
/
CollapsibleSection.vue
File metadata and controls
132 lines (116 loc) · 3.58 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
132
<script setup lang="ts">
import { shallowRef, computed } from 'vue'
import { LinkBase } from '#components'
interface Props {
title: string
subtitle?: string
isLoading?: boolean
headingLevel?: `h${number}`
id: string
icon?: string
}
const props = withDefaults(defineProps<Props>(), {
isLoading: false,
headingLevel: 'h2',
})
const { localSettings } = useUserLocalSettings()
const buttonId = `${props.id}-collapsible-button`
const contentId = `${props.id}-collapsible-content`
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?.split(' ').includes(id)) {
document.documentElement.dataset.collapsed = (
document.documentElement.dataset.collapsed +
' ' +
id
).trim()
}
}
})
onMounted(() => {
if (document?.documentElement) {
isOpen.value = !(
document.documentElement.dataset.collapsed?.split(' ').includes(props.id) ?? false
)
}
})
function toggle() {
isOpen.value = !isOpen.value
const removed = localSettings.value.sidebar.collapsed.filter(c => c !== props.id)
if (isOpen.value) {
localSettings.value.sidebar.collapsed = removed
} else {
removed.push(props.id)
localSettings.value.sidebar.collapsed = removed
}
document.documentElement.dataset.collapsed = localSettings.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;
overflow: hidden;
}`,
},
],
})
</script>
<template>
<section :id="id" :data-anchor-id="id" class="scroll-mt-20 xl:scroll-mt-0">
<div class="flex items-center justify-between mb-3 ps-1">
<component
:is="headingLevel"
class="group text-xs text-fg-subtle uppercase tracking-wider flex gap-2"
:class="subtitle ? 'items-start' : 'items-center'"
>
<button
:id="buttonId"
type="button"
class="size-5 -me-1 flex items-center justify-center text-fg-subtle hover:text-fg-muted transition-colors duration-200 shrink-0 focus-visible:outline-accent/70 rounded"
:aria-expanded="isOpen"
:aria-controls="contentId"
:aria-label="ariaLabel"
@click="toggle"
>
<span v-if="isLoading" class="i-svg-spinners:ring-resize w-3 h-3" aria-hidden="true" />
<span
v-else
class="w-3 h-3 transition-transform duration-200"
:class="isOpen ? 'i-lucide:chevron-down' : 'i-lucide:chevron-right'"
aria-hidden="true"
/>
</button>
<span>
<LinkBase :to="`#${id}`">
{{ title }}
</LinkBase>
<span v-if="subtitle" class="block text-2xs normal-case tracking-normal">{{
subtitle
}}</span>
</span>
</component>
<!-- Actions slot for buttons or other elements -->
<div class="pe-1">
<slot name="actions" />
</div>
</div>
<div
:id="contentId"
class="grid ms-6 ps-1 grid-rows-[1fr] transition-[grid-template-rows] duration-200 ease-in-out collapsible-content"
:inert="!isOpen"
>
<div class="min-h-0 min-w-0">
<slot />
</div>
</div>
</section>
</template>