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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405 | 18x
18x
18x
18x
18x
18x
18x
18x
18x
18x
| <template>
<div>
I <transition-group enter-class="hidden"
enter-to-class=""
E enter-active-class=""
@after-enter="focusFirst"
leave-class="show"
leave-active-class=""
leave-to-class="hidden"
>
<div :class="['modal',{fade: !noFade, show: is_visible}]"
:id="id || null"
role="dialog"
ref="modal"
key="modal"
v-show="is_visible"
@click="onClickOut()"
@keyup.esc="onEsc()"
>
<div :class="['modal-dialog','modal-'+size]">
<div class="modal-content"
tabindex="-1"
role="document"
ref="content"
:aria-labelledby="(hideHeader || !id) ? null : (id + '__BV_header_')"
:aria-describedby="id ? (id + '__BV_body_') : null"
@click.stop
>
<header class="modal-header"
ref="header"
:id="id ? (id + '__BV_header_') : null"
v-if="!hideHeader"
>
<slot name="modal-header">
<h5 :is="titleTag" class="modal-title">
<slot name="modal-title">{{title}}</slot>
</h5>
<button type="button"
v-if="!hideHeaderClose"
class="close"
:aria-label="headerCloseLabel"
@click="hide"
>
<span aria-hidden="true">×</span>
</button>
</slot>
</header>
<div class="modal-body" ref="body" :id="id ? (id + '__BV_body_') : null">
<slot></slot>
</div>
<footer class="modal-footer" ref="footer" v-if="!hideFooter">
<slot name="modal-footer">
<b-btn v-if="!okOnly"
variant="secondary"
:size="buttonSize"
@click="hide(false)"
><slot name="modal-cancel">{{ closeTitle }}</slot></b-btn>
<b-btn variant="primary"
:size="buttonSize"
:disabled="okDisabled"
@click="hide(true)"
><slot name="modal-ok">{{ okTitle }}</slot></b-btn>
</slot>
</footer>
</div>
</div>
</div>
<div key="modal-backdrop"
:class="['modal-backdrop',{fade: !noFade, show: is_visible}]"
v-if="is_visible"
></div>
</transition-group>
</div>
</template>
<style scoped>
.hidden {
opacity: 0 !important;
}
/* Make modal display as block instead of inline style, and because Vue's v-show deletes inline "display" style */
.modal {
display: block;
}
</style>
<script>
import bBtn from './button.vue';
import { listenOnRootMixin } from '../mixins';
import { from as arrayFrom } from '../utils/array'
const FOCUS_SELECTOR = [
'button:not([disabled])',
'input:not([disabled])',
'select:not([disabled])',
'textarea:not([disabled])',
'a:not([disabled]):not(.disabled)',
'[tabindex]:not([disabled]):not(.disabled)'
].join(',');
// Determine if an HTML element is visible - Faster than CSS check
function isVisible(el) {
return el && (el.offsetWidth > 0 || el.offsetHeight > 0);
}
// Find the first visible element contained in a given root element
function findFirstVisible(root, selector) {
if (!root || !root.querySelectorAll || !selector) {
return null;
}
let els = arrayFrom(root.querySelectorAll(selector));
// IE 10 & 11 do not support native array.find()
// So we try native find first, then fall back to a loop
let el = els.find ? els.find(el => isVisible(el)) : null;
for (let i = 0; !el && i < els.length; i++) {
if (isVisible(els[i])) {
el = els[i];
}
}
return el;
}
export default {
mixins: [listenOnRootMixin],
components: {bBtn},
data() {
return {
is_visible: false,
return_focus: this.returnFocus || null
};
},
model: {
prop: 'visible',
event: 'change'
},
computed: {
body() {
if (typeof document !== 'undefined') {
return document.querySelector('body');
}
}
},
watch: {
visible(new_val, old_val) {
if (new_val === old_val) {
return;
}
if (new_val) {
this.show();
} else {
this.hide();
}
}
},
props: {
id: {
type: String,
default: null
},
title: {
type: String,
default: ''
},
titleTag: {
type: String,
default: 'h5'
},
size: {
type: String,
default: 'md'
},
buttonSize: {
type: String,
default: 'md'
},
noFade: {
type: Boolean,
default: false
},
noCloseOnBackdrop: {
type: Boolean,
default: false
},
noCloseOnEsc: {
type: Boolean,
default: false
},
noAutoFocus: {
type: Boolean,
default: false
},
noEnforceFocus: {
type: Boolean,
default: false
},
hideHeader: {
type: Boolean,
default: false
},
hideFooter: {
type: Boolean,
default: false
},
hideHeaderClose: {
type: Boolean,
default: false
},
okOnly: {
type: Boolean,
default: false
},
okDisabled: {
type: Boolean,
default: false
},
visible: {
type: Boolean,
default: false
},
returnFocus: {
default: null
},
headerCloseLabel: {
type: String,
default: 'Close'
},
closeTitle: {
type: String,
default: 'Close'
},
okTitle: {
type: String,
default: 'OK'
}
},
methods: {
show() {
if (this.is_visible) {
return;
}
this.$emit('show');
this.is_visible = true;
this.$root.$emit('shown::modal', this.id);
this.body.classList.add('modal-open');
this.$emit('shown');
this.$emit('change', true);
if (typeof document !== 'undefined') {
// Guard against infinite focus loop
document.removeEventListener('focusin', this.enforceFocus, false);
// Handle constrained focus
document.addEventListener('focusin', this.enforceFocus, false);
}
},
hide(isOK) {
if (!this.is_visible) {
return;
}
// Create event object
let canceled = false;
const e = {
isOK,
cancel() {
canceled = true;
}
};
// Emit events
this.$emit('change', false);
this.$emit('hide', e);
if (isOK === true) {
this.$emit('ok', e);
} else if (isOK === false) {
this.$emit('cancel', e);
}
// Hide if not canceled
if (!canceled) {
if (typeof document !== 'undefined') {
// Remove focus handler
document.removeEventListener('focusin', this.enforceFocus, false);
// Return focus to original button/trigger element if provided
this.returnFocusTo();
}
this.is_visible = false;
this.$root.$emit('hidden::modal', this.id);
this.$emit('hidden', e);
this.body.classList.remove('modal-open');
}
},
onClickOut() {
// If backdrop clicked, hide modal
if (this.is_visible && !this.noCloseOnBackdrop) {
this.hide();
}
},
onEsc() {
// If ESC pressed, hide modal
if (this.is_visible && !this.noCloseOnEsc) {
this.hide();
}
},
focusFirst() {
// Don't try and focus if we are SSR
if (typeof document === 'undefined') {
return;
}
this.$nextTick(() => {
// If activeElement is child of content, no need to change focus
if (document.activeElement && this.$refs.content.contains(document.activeElement)) {
return;
}
let el;
if (!this.noAutoFocus) {
// Focus the modal's first focusable item, searching body, footer, then header
if (this.$refs.body) {
el = findFirstVisible(this.$refs.body, FOCUS_SELECTOR);
}
if (!el && this.$refs.footer) {
el = findFirstVisible(this.$refs.footer, FOCUS_SELECTOR);
}
if (!el && this.$refs.header) {
el = findFirstVisible(this.$refs.header, FOCUS_SELECTOR);
}
}
if (!el) {
// Focus the modal content wrapper
el = this.$refs.content;
}
if (el && el.focus) {
el.focus();
}
});
},
returnFocusTo() {
// Prrefer returnFocus prop over event specified value
let el = this.returnFocus || this.return_focus || null;
if (el) {
if (typeof el === 'string') {
// CSS Selector
el = document.querySelector(el);
}
if (el && el.$el && typeof el.$el.focus === 'function') {
// Component vm reference
el.$el.focus();
} else if (el && typeof el.focus === 'function') {
// Plain element
el.focus();
}
}
},
enforceFocus(e) {
// If focus leaves modal, bring it back
// Event Listener bound on document
if (!this.noEnforceFocus &&
this.is_visible &&
document !== e.target &&
this.$refs.content &&
this.$refs.content !== e.target &&
!this.$refs.content.contains(e.target)) {
this.$refs.content.focus();
}
},
showHandler(id, triggerEl) {
if (id === this.id) {
this.return_focus = triggerEl || null;
this.show();
}
},
hideHandler(id) {
if (id === this.id) {
this.hide();
}
}
},
created() {
this.listenOnRoot('show::modal', this.showHandler);
this.listenOnRoot('hide::modal', this.hideHandler);
},
mounted() {
if (this.visible === true) {
this.show();
}
},
destroyed() {
// Make sure event listener is rmoved
if (typeof document !== 'undefined') {
document.removeEventListener('focusin', this.enforceFocus, false);
}
}
};
</script>
|