forked from shipshapecode/shepherd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal.js
More file actions
204 lines (171 loc) · 5.38 KB
/
modal.js
File metadata and controls
204 lines (171 loc) · 5.38 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
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
const svgNS = 'http://www.w3.org/2000/svg';
const elementIds = {
modalOverlay: 'shepherdModalOverlayContainer',
modalOverlayMask: 'shepherdModalMask',
modalOverlayMaskRect: 'shepherdModalMaskRect',
modalOverlayMaskOpening: 'shepherdModalMaskOpening'
};
const classNames = {
isVisible: 'shepherd-modal-is-visible',
modalTarget: 'shepherd-modal-target'
};
/**
* <svg id="shepherdModalOverlayContainer" xmlns="http://www.w3.org/2000/svg">
*/
function _createModalContainer() {
const element = document.createElementNS(svgNS, 'svg');
element.setAttributeNS(null, 'id', elementIds.modalOverlay);
return element;
}
/**
* <mask id="shepherdModalMask" x="0" y="0" width="100%" height="100%">
*/
function _createMaskContainer() {
const element = document.createElementNS(svgNS, 'mask');
_setAttributes(element, {
height: '100%',
id: elementIds.modalOverlayMask,
width: '100%',
x: '0',
y: '0'
});
return element;
}
/**
* <rect id="modalOverlayMaskRect" x="0" y="0" width="100%" height="100%" fill="#FFFFFF"/>
*/
function _createMaskRect() {
const element = document.createElementNS(svgNS, 'rect');
_setAttributes(element, {
fill: '#FFFFFF',
height: '100%',
id: elementIds.modalOverlayMaskRect,
width: '100%',
x: '0',
y: '0'
});
return element;
}
/**
* <rect id="shepherdModalMaskOpening" fill="#000000"/>
*/
function _createMaskOpening() {
const element = document.createElementNS(svgNS, 'rect');
_setAttributes(element, {
fill: '#000000',
id: elementIds.modalOverlayMaskOpening
});
return element;
}
/**
* <rect x="0" y="0" width="100%" height="100%" mask="url(#shepherdModalMask)"/>
*/
function _createMaskConsumer() {
const element = document.createElementNS(svgNS, 'rect');
_setAttributes(element, {
height: '100%',
width: '100%',
x: '0',
y: '0'
});
element.setAttribute('mask', `url(#${elementIds.modalOverlayMask})`);
return element;
}
/**
* Generates an SVG with the following structure:
* ```html
* <svg id="shepherdModalOverlayContainer" xmlns="http://www.w3.org/2000/svg">
<defs>
<mask id="shepherdModalMask" x="0" y="0" width="100%" height="100%" >
<rect x="0" y="0" width="100%" height="100%" fill="#FFFFFF"/>
<!-- This element will "punch a hole" through the mask by preventing it from rendering within the perimeter -->
<rect id="shepherdModalMaskOpening"/>
</mask>
</defs>
<rect x="0" y="0" width="100%" height="100%" mask="url(#shepherdModalMask)"/>
</svg>
* ```
*/
function createModalOverlay() {
const containerElement = _createModalContainer();
const defsElement = document.createElementNS(svgNS, 'defs');
const maskContainer = _createMaskContainer();
const maskRect = _createMaskRect();
const maskOpening = _createMaskOpening();
const maskConsumer = _createMaskConsumer();
maskContainer.appendChild(maskRect);
maskContainer.appendChild(maskOpening);
defsElement.appendChild(maskContainer);
containerElement.appendChild(defsElement);
containerElement.appendChild(maskConsumer);
return containerElement;
}
/**
* Uses the bounds of the element we want the opening overtop of to set the dimensions of the opening and position it
* @param {HTMLElement} targetElement The element the opening will expose
* @param {SVGElement} openingElement The svg mask for the opening
*/
function positionModalOpening(targetElement, openingElement, modalOverlayOpeningPadding) {
if (targetElement.getBoundingClientRect && openingElement instanceof SVGElement) {
const { x, y, width, height, left, top } = targetElement.getBoundingClientRect();
// getBoundingClientRect is not consistent. Some browsers use x and y, while others use left and top
_setAttributes(openingElement, {
x: (x || left) - modalOverlayOpeningPadding,
y: (y || top) - modalOverlayOpeningPadding,
width: (width + (modalOverlayOpeningPadding * 2)),
height: (height + (modalOverlayOpeningPadding * 2))
});
}
}
function closeModalOpening(openingElement) {
if (openingElement && openingElement instanceof SVGElement) {
_setAttributes(openingElement, {
height: '0',
x: '0',
y: '0',
width: '0'
});
}
}
function getModalMaskOpening(modalElement) {
return modalElement.querySelector(`#${elementIds.modalOverlayMaskOpening}`);
}
function preventModalBodyTouch(event) {
event.preventDefault();
}
function preventModalOverlayTouch(event) {
event.stopPropagation();
}
/**
* Remove any leftover modal target classes and add the modal target class to the currentElement
* @param {HTMLElement} currentElement The element for the current step
*/
function toggleShepherdModalClass(currentElement) {
const shepherdModal = document.querySelector(`${classNames.modalTarget}`);
if (shepherdModal) {
shepherdModal.classList.remove(classNames.modalTarget);
}
currentElement.classList.add(classNames.modalTarget);
}
/**
* Set multiple attributes on an element, via a hash
* @param {HTMLElement|SVGElement} el The element to set the attributes on
* @param {Object} attrs A hash of key value pairs for attributes to set
* @private
*/
function _setAttributes(el, attrs) {
Object.keys(attrs).forEach((key) => {
el.setAttribute(key, attrs[key]);
});
}
export {
createModalOverlay,
positionModalOpening,
preventModalBodyTouch,
preventModalOverlayTouch,
closeModalOpening,
getModalMaskOpening,
elementIds,
classNames,
toggleShepherdModalClass
};