-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Modal.astro
222 lines (187 loc) · 5.48 KB
/
Modal.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
---
interface Props {
triggerId: string
title: string
closeText?: string
children?: HTMLElement | HTMLElement[]
}
const { triggerId, title, closeText = 'Close' } = Astro.props
---
<dialog class="modal" aria-labelledby={triggerId}>
<div class="modal__inner">
<div class="modal__content">
<h3 tabindex="-1">
{title}
</h3>
<slot>Modal description.</slot>
</div>
<div class="modal__close">
<button>{closeText}</button>
</div>
</div>
</dialog>
<script>
type FocusableElement =
| HTMLAnchorElement
| HTMLButtonElement
| HTMLInputElement
| HTMLTextAreaElement
| HTMLSelectElement
| HTMLDetailsElement
// variables
let modals = document.querySelectorAll<HTMLDialogElement>('.modal')
// abort controllers for global event listeners
let trapFocusController: AbortController | undefined
let keydownController: AbortController | undefined
const getKeyboardFocusableElements = (element: HTMLElement) => {
return [
...element.querySelectorAll<FocusableElement>(
'a, button, input, textarea, select, details,[tabindex]:not([tabindex="-1"])'
),
].filter((el) => !el.hasAttribute('disabled'))
}
const trapFocus = (event: KeyboardEvent, modal: HTMLDialogElement) => {
const focusables = getKeyboardFocusableElements(modal)
// These will not be undefined as a modal always has at least one <button>
const firstFocusable = focusables[0]!
const lastFocusable = focusables[focusables.length - 1]!
if (document.activeElement === lastFocusable && event.key === 'Tab' && !event.shiftKey) {
event.preventDefault()
firstFocusable.focus()
}
if (document.activeElement === firstFocusable && event.key === 'Tab' && event.shiftKey) {
event.preventDefault()
lastFocusable.focus()
}
}
const openModal = (modal: HTMLDialogElement) => {
const modalTitle = modal.querySelector('h3')
const modalInner = modal.querySelector<HTMLDivElement>('.modal__inner')
modal.showModal()
modalTitle!.focus()
trapFocusController = new AbortController()
keydownController = new AbortController()
document.addEventListener('keydown', (e) => trapFocus(e, modal), { signal: trapFocusController.signal })
modal.addEventListener(
'keydown',
(event) => {
if (event.key === 'Escape') {
closeModal()
}
},
{ signal: keydownController.signal }
)
modal.addEventListener(
'click',
() => {
closeModal()
},
{ signal: keydownController.signal }
)
modalInner!.addEventListener(
'click',
(event) => {
event.stopPropagation()
},
{ signal: keydownController.signal }
)
}
const closeModal = () => {
modals.forEach((modal) => {
const modalId = modal.getAttribute('aria-labelledby')
const modalTrigger = document.querySelector(`#${modalId}`) as HTMLButtonElement
modalTrigger.focus({ preventScroll: true })
modal.close()
trapFocusController?.abort()
keydownController?.abort()
})
}
// execution
modals.forEach((modal) => {
const modalId = modal.getAttribute('aria-labelledby')
const modalCloseButton = modal.querySelector('.modal__close button')
const modalTrigger = document.querySelector(`#${modalId}`)
if (!modalTrigger) {
throw new Error(`Trigger element not found. \n
Did you forget to add a trigger element with id: "${modalId}"?`)
}
modalTrigger.addEventListener('click', () => openModal(modal))
modalCloseButton!.addEventListener('click', closeModal)
})
window.close = closeModal
// Listen for view transitions
document.addEventListener('astro:after-swap', () => {
// reset variables
modals = document.querySelectorAll<HTMLDialogElement>('.modal')
// execution
modals.forEach((modal) => {
const modalId = modal.getAttribute('aria-labelledby')
const modalCloseButton = modal.querySelector('.modal__close button')
const modalTrigger = document.querySelector(`#${modalId}`)
if (!modalTrigger) {
throw new Error(`Trigger element not found. \n
Did you forget to add a trigger element with id: "${modalId}"?`)
}
modalTrigger.addEventListener('click', () => openModal(modal))
modalCloseButton!.addEventListener('click', closeModal)
})
})
</script>
<style is:global>
dialog::backdrop {
background-color: rgba(0, 0, 0, 0.5);
filter: blur(6px);
}
:where(.modal) {
color: black;
background-color: white;
border: 0.5rem solid black;
border-radius: 1rem;
padding: 0;
}
.modal__inner {
width: clamp(30ch, 70%, 75ch);
border-radius: 1rem;
width: 100%;
}
.modal__content {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.75rem;
padding: 2rem;
}
.modal__close {
width: 100%;
}
.modal__close button {
border: none;
background-color: lightgrey;
border-bottom-left-radius: 0.4rem;
border-bottom-right-radius: 0.4rem;
color: black;
text-align: right;
transition: background-color 0.15s ease-in-out;
width: 100%;
margin: 0;
padding: 0.5rem;
}
.modal__close button:hover,
.modal__close button:focus {
background-color: grey;
text-decoration: underline;
}
/* Animation */
dialog[open],
dialog[open]::backdrop {
animation: fadein 0.3s ease-in-out;
}
@keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
</style>