-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmodals.ts
More file actions
95 lines (87 loc) · 2.82 KB
/
modals.ts
File metadata and controls
95 lines (87 loc) · 2.82 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
import {
ModalWidgetStyleOptions,
modalStyles,
getModalStyle,
getModalContentStyle,
getModalCloseStyle
} from './modalsStyle'
import { getClasses } from '../jss'
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 = () => {
const modal: HTMLDivElement | null = document.querySelector('.modal')
if (modal) {
modal.style.display = 'none'
}
}
const createModal = (dom: HTMLDocument, options: ModalWidgetStyleOptions) => {
const modal = dom.createElement('div')
const style = getModalStyle(options)
const { classes } = getClasses(dom.head, {
modal: style
})
modal.classList.add(classes.modal)
return modal
}
const createModalContent = (dom: HTMLDocument) => {
const modalContent: HTMLDivElement = dom.createElement('div')
const style = getModalContentStyle()
const { classes } = getClasses(dom.head, {
modalContent: style
})
modalContent.classList.add(classes.modalContent)
return modalContent
}
const createCloseButton = (dom: HTMLDocument) => {
const closeButton: HTMLSpanElement = dom.createElement('span')
closeButton.addEventListener('click', closeClickHandler)
const style = getModalCloseStyle()
const { classes } = getClasses(dom.head, {
close: style
})
closeButton.classList.add(classes.close)
return closeButton
}
const createListItems = (dom: HTMLDocument, list: ListItem) => {
const li:HTMLLIElement = dom.createElement('li')
li.setAttribute('style', modalStyles.listItemStyle)
const link: HTMLAnchorElement = dom.createElement('a')
link.setAttribute('style', modalStyles.anchorStyle)
link.href = list.link
link.innerHTML = list.label
li.appendChild(link)
return li
}
const createUnOrderedList = (dom: HTMLDocument, listOfLinks: ListItem[]) => {
const ul: HTMLUListElement = dom.createElement('ul')
ul.setAttribute('style', modalStyles.unorderedListStyle)
listOfLinks.forEach(list => {
const li = createListItems(dom, list)
ul.appendChild(li)
})
return ul
}
export const createWindow = (dom: HTMLDocument, listOfLinks: ListItem[], options: ModalWidgetStyleOptions) => {
const modal = createModal(dom, options)
const modalContent = createModalContent(dom)
const closeButton = createCloseButton(dom)
const ul = createUnOrderedList(dom, listOfLinks)
modalContent.appendChild(closeButton)
modalContent.appendChild(ul)
modal.appendChild(modalContent)
}