-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathAccordionItem.astro
236 lines (211 loc) · 5.09 KB
/
AccordionItem.astro
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
---
/**
* Accordion Item Component
*
* @description An individual accordion item that can be expanded/collapsed with smooth animations
*/
interface Props {
/**
* Optional name attribute for the details element
*/
name?: string
/**
* Title text displayed in the accordion header
*/
title: string
/**
* Additional classes to apply to the item
*/
class?: string
/**
* Content to be rendered inside the accordion item
*/
children: any
/**
* HTML tag to use for the title (h2-h6)
* @default 'h3'
*/
tagName?: string
/**
* Visual style variant of the accordion
* @default 'default'
*/
variant?: 'default' | 'chevron'
/**
* Whether the accordion item is initially open
* @default false
*/
open?: boolean
}
const {
name,
title,
class: classNames,
tagName = 'h3',
variant = 'default',
open = false,
} = Astro.props
const Tag = tagName
---
<li class:list={['item', classNames]}>
<details class="wrapper" name={name} data-variant={variant} {...open ? { open } : {}}>
<summary class="title">
<Tag>
{
variant === 'chevron' ? (
<>
{title}
<span class="icon-chevron">
<svg
aria-hidden="true"
width="12"
height="8"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1.4 1.6L6 6.2l4.6-4.6"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</span>
</>
) : (
<>
<span class="icon-plus">
<svg
aria-hidden="true"
width="20"
height="2"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path fill="currentColor" d="M20 .5v1H0v-1z" />
</svg>
<svg width="2" height="20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill="currentColor" d="M1.5 20h-1V0h1z" />
</svg>
</span>
{title}
</>
)
}
</Tag>
</summary>
<div class="content">
<slot />
</div>
</details>
</li>
<style>
:where(.wrapper) {
--transition-duration: 0.3s;
--transition-easing: cubic-bezier(0.165, 0.84, 0.44, 1);
border-block-start: 2px solid light-dark(hsl(0 0% 13%), hsl(0 0% 87%));
padding-block: 0;
}
.title h2,
.title h3,
.title h4,
.title h5,
.title h6 {
display: flex;
align-items: center;
gap: 1rem;
min-inline-size: 100%;
font-weight: normal;
font-size: 1.25rem;
}
.wrapper {
@media (prefers-reduced-motion: no-preference) {
interpolate-size: allow-keywords;
}
}
.wrapper::details-content {
opacity: 0;
transition: content-visibility var(--transition-duration) allow-discrete,
opacity var(--transition-duration) var(--transition-easing),
block-size var(--transition-duration) var(--transition-easing);
block-size: 0;
overflow-y: clip;
}
.wrapper[open]::details-content {
opacity: 1;
block-size: auto;
}
:where(.wrapper[data-variant='default']) .content {
padding: 0 0 1.5rem 2.2rem;
}
:where(.wrapper[data-variant='chevron']) {
.title h2,
.title h3,
.title h4,
.title h5,
.title h6 {
justify-content: space-between;
}
.content {
padding-block-end: 2.2rem;
}
}
.title {
display: flex;
cursor: pointer;
padding-block: 0.75rem;
list-style: none;
}
.title::marker,
.title::-webkit-details-marker {
display: none;
content: '';
}
.title:hover,
.title:focus-visible {
color: light-dark(hsl(215 25% 27%), hsl(215 25% 89%));
text-decoration: underline;
text-underline-offset: 4px;
}
:where(.icon-plus) {
display: inline-block;
position: relative;
aspect-ratio: 1;
block-size: 20px;
}
:where(.icon-plus svg) {
position: absolute;
}
:where(.icon-plus svg:first-of-type) {
transform: translateY(-50%);
inset-block-start: 50%;
inset-inline-start: 0;
}
:where(.icon-plus svg:last-of-type) {
transform: translateY(-50%);
inset-block-start: 50%;
inset-inline-start: 50%;
@media (prefers-reduced-motion: no-preference) {
transition: all var(--transition-duration) var(--transition-easing);
}
}
:where(.icon-chevron svg) {
@media (prefers-reduced-motion: no-preference) {
transition: transform var(--transition-duration) var(--transition-easing);
}
}
:where(.content a) {
display: inline-block;
}
:where(.content > *:not(:first-child)) {
margin-block-start: 0.75rem;
}
:where(.wrapper[open]) .icon-plus svg:last-of-type {
transform: rotate(90deg);
inset-block-start: 0;
}
:where(.wrapper[open]) .icon-chevron svg {
transform: scaleY(-1);
}
</style>