-
Notifications
You must be signed in to change notification settings - Fork 420
/
Accordion.vue
204 lines (185 loc) · 5.85 KB
/
Accordion.vue
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<template>
<div :class="ui.wrapper">
<HDisclosure
v-for="(item, index) in items"
v-slot="{ open, close }"
:key="index"
as="div"
:class="ui.container"
:default-open="defaultOpen || item.defaultOpen"
>
<HDisclosureButton
:ref="() => buttonRefs[index] = { open, close }"
as="template"
:disabled="item.disabled"
@click="closeOthers(index, $event)"
@keydown.enter="closeOthers(index, $event)"
@keydown.space="closeOthers(index, $event)"
>
<slot :item="item" :index="index" :open="open" :close="close">
<UButton v-bind="{ ...omit(ui.default, ['openIcon', 'closeIcon']), ...attrs, ...omit(item, ['slot', 'disabled', 'content', 'defaultOpen']) }">
<template #trailing>
<UIcon
:name="!open ? openIcon : closeIcon ? closeIcon : openIcon"
:class="[
open && !closeIcon ? '-rotate-180' : '',
uiButton.icon.size[item.size || uiButton.default.size],
ui.item.icon
]"
/>
</template>
</UButton>
</slot>
</HDisclosureButton>
<Transition
v-bind="ui.transition"
@enter="onEnter"
@after-enter="onAfterEnter"
@before-leave="onBeforeLeave"
@leave="onLeave"
>
<HDisclosurePanel
v-if="unmount"
:class="[ui.item.base, ui.item.size, ui.item.color, ui.item.padding]"
unmount
>
<slot :name="item.slot || 'item'" :item="item" :index="index" :open="open" :close="close">
{{ item.content }}
</slot>
</HDisclosurePanel>
<template v-else>
<div v-show="open">
<HDisclosurePanel
:class="[ui.item.base, ui.item.size, ui.item.color, ui.item.padding]"
static
>
<slot :name="item.slot || 'item'" :item="item" :index="index" :open="open" :close="close">
{{ item.content }}
</slot>
</HDisclosurePanel>
</div>
</template>
</Transition>
</HDisclosure>
</div>
</template>
<script lang="ts">
import { ref, computed, toRef, defineComponent, watch } from 'vue'
import type { PropType } from 'vue'
import { Disclosure as HDisclosure, DisclosureButton as HDisclosureButton, DisclosurePanel as HDisclosurePanel, provideUseId } from '@headlessui/vue'
import { UIcon, UButton } from '#components'
import { useUI } from '../../composables/useUI'
import { mergeConfig, omit } from '../../utils'
import type { AccordionItem, Strategy } from '../../types'
// @ts-expect-error
import appConfig from '#build/app.config'
import { accordion, button } from '#ui/ui.config'
import { useId } from '#imports'
const config = mergeConfig<typeof accordion>(appConfig.ui.strategy, appConfig.ui.accordion, accordion)
const configButton = mergeConfig<typeof button>(appConfig.ui.strategy, appConfig.ui.button, button)
export default defineComponent({
components: {
HDisclosure,
HDisclosureButton,
HDisclosurePanel,
UIcon,
UButton
},
inheritAttrs: false,
props: {
items: {
type: Array as PropType<AccordionItem[]>,
default: () => []
},
defaultOpen: {
type: Boolean,
default: false
},
openIcon: {
type: String,
default: () => config.default.openIcon
},
unmount: {
type: Boolean,
default: false
},
closeIcon: {
type: String,
default: () => config.default.closeIcon
},
multiple: {
type: Boolean,
default: false
},
class: {
type: [String, Object, Array] as PropType<any>,
default: () => ''
},
ui: {
type: Object as PropType<Partial<typeof config> & { strategy?: Strategy }>,
default: () => ({})
}
},
emits: ['open'],
setup (props, { emit }) {
const { ui, attrs } = useUI('accordion', toRef(props, 'ui'), config, toRef(props, 'class'))
const uiButton = computed<typeof configButton>(() => configButton)
const buttonRefs = ref<{ open: boolean, close: (e: EventTarget) => {} }[]>([])
const openedStates = computed(() => buttonRefs.value.map(({ open }) => open))
watch(openedStates, (newValue, oldValue) => {
for (const index in newValue) {
const isOpenBefore = oldValue[index]
const isOpenAfter = newValue[index]
if (!isOpenBefore && isOpenAfter) {
emit('open', index)
}
}
}, { immediate: true })
function closeOthers (currentIndex: number, e: Event) {
if (!props.items[currentIndex].closeOthers && props.multiple) {
return
}
buttonRefs.value.forEach((button) => {
if (button.open) {
button.close(e.target as EventTarget)
}
})
}
function onEnter (_el: Element, done: () => void) {
const el = _el as HTMLElement
el.style.height = '0'
el.offsetHeight // Trigger a reflow, flushing the CSS changes
el.style.height = el.scrollHeight + 'px'
el.addEventListener('transitionend', done, { once: true })
}
function onBeforeLeave (_el: Element) {
const el = _el as HTMLElement
el.style.height = el.scrollHeight + 'px'
el.offsetHeight // Trigger a reflow, flushing the CSS changes
}
function onAfterEnter (_el: Element) {
const el = _el as HTMLElement
el.style.height = 'auto'
}
function onLeave (_el: Element, done: () => void) {
const el = _el as HTMLElement
el.style.height = '0'
el.addEventListener('transitionend', done, { once: true })
}
provideUseId(() => useId())
return {
// eslint-disable-next-line vue/no-dupe-keys
ui,
uiButton,
attrs,
buttonRefs,
closeOthers,
omit,
onEnter,
onBeforeLeave,
onAfterEnter,
onLeave
}
}
})
</script>