-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmodals.ts
More file actions
116 lines (106 loc) · 3.92 KB
/
modals.ts
File metadata and controls
116 lines (106 loc) · 3.92 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
import {
ModalWidgetStyleOptions,
getModalStyle
} from './modalsStyle'
import { style } from '../style'
export type ListItem = {
label: string,
link: string
}
// Two functions that need to be implemented to use the modal.
// When the user clicks the button, open the modal.
/* Click handler on the button to display it.
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks anywhere outside of the modal, close it.
// Window click handler so that the modal will close
// even if the user doesn't click close.
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
} */
const closeClickHandler = (modal: HTMLDivElement, returnFocusTo?: Element | null) => {
modal.style.display = 'none'
if (returnFocusTo && returnFocusTo instanceof HTMLElement) {
returnFocusTo.focus()
}
}
const createModal = (dom: HTMLDocument, options: ModalWidgetStyleOptions, returnFocusTo?: Element | null) => {
const modal = dom.createElement('div')
modal.classList.add('modal')
modal.setAttribute('role', 'dialog')
modal.setAttribute('aria-modal', options.withGreyedBackground ? 'true' : 'false')
modal.setAttribute('aria-label', options.ariaLabel || 'List dialog')
modal.setAttribute('tabindex', '-1')
modal.addEventListener('keydown', event => {
if (event.key === 'Escape') {
closeClickHandler(modal, returnFocusTo)
}
})
modal.setAttribute('style', getModalStyle(options))
return modal
}
const createModalContent = (dom: HTMLDocument) => {
const modalContent: HTMLDivElement = dom.createElement('div')
modalContent.setAttribute('style', style.modalContentStyle)
return modalContent
}
const createCloseButton = (dom: HTMLDocument, modal: HTMLDivElement, returnFocusTo?: Element | null) => {
const closeButton: HTMLButtonElement = dom.createElement('button')
closeButton.setAttribute('type', 'button')
closeButton.setAttribute('aria-label', 'Close modal')
closeButton.setAttribute('title', 'Close')
closeButton.textContent = 'x'
closeButton.addEventListener('click', () => closeClickHandler(modal, returnFocusTo))
closeButton.addEventListener('mouseenter', () => {
closeButton.setAttribute('style', style.modalCloseStyleHover)
})
closeButton.addEventListener('mouseleave', () => {
closeButton.setAttribute('style', style.modalCloseStyle)
})
closeButton.addEventListener('focus', () => {
closeButton.setAttribute('style', style.modalCloseStyleFocus)
})
closeButton.addEventListener('blur', () => {
closeButton.setAttribute('style', style.modalCloseStyle)
})
closeButton.setAttribute('style', style.modalCloseStyle)
return closeButton
}
const createListItems = (dom: HTMLDocument, list: ListItem) => {
const li:HTMLLIElement = dom.createElement('li')
li.setAttribute('style', style.modalListItemStyle)
const link: HTMLAnchorElement = dom.createElement('a')
link.setAttribute('style', style.modalAnchorStyle)
link.href = list.link
link.textContent = list.label
li.appendChild(link)
return li
}
const createUnorderedList = (dom: HTMLDocument, listOfLinks: ListItem[]) => {
const ul: HTMLUListElement = dom.createElement('ul')
ul.setAttribute('role', 'list')
ul.setAttribute('style', style.modalUnorderedListStyle)
listOfLinks.forEach(list => {
const li = createListItems(dom, list)
ul.appendChild(li)
})
return ul
}
export const createListModal = (dom: HTMLDocument, listOfLinks: ListItem[], options: ModalWidgetStyleOptions) => {
const returnFocusTo = dom.activeElement
const modal = createModal(dom, options, returnFocusTo)
const modalContent = createModalContent(dom)
const closeButton = createCloseButton(dom, modal, returnFocusTo)
const ul = createUnorderedList(dom, listOfLinks)
modalContent.appendChild(closeButton)
modalContent.appendChild(ul)
modal.appendChild(modalContent)
setTimeout(() => {
if (closeButton.isConnected) {
closeButton.focus()
}
}, 0)
return modal
}