-
Notifications
You must be signed in to change notification settings - Fork 13
/
A11yDialog.vue
101 lines (88 loc) · 2.39 KB
/
A11yDialog.vue
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
<template>
<portal :target-el="portalTarget">
<div :id="id" :class="classNames.base" ref="rootElement">
<div
data-a11y-dialog-hide
tabIndex="-1"
:class="classNames.overlay"
@click="role === 'alertdialog' ? undefined : close" />
<component :is="dialogElement"
:role="roleAttribute"
:class="classNames.element"
:aria-labelledby="titleId">
<div role="document" :class="classNames.document">
<button
data-a11y-dialog-hide
type="button"
:aria-label="closeButtonLabel"
@click="close"
:class="classNames.closeButton">
<slot name="closeButtonContent">
{{ '\u00D7' }}
</slot>
</button>
<h1 :id="fullTitleId" :class="classNames.title">
<slot name="title" />
</h1>
<slot />
</div>
</component>
</div>
</portal>
</template>
<script>
import A11yDialog from 'a11y-dialog'
import { Portal } from 'portal-vue'
export default {
name: 'VueA11yDialog',
props: {
id: { type: String, required: true },
appRoot: { type: [String, Array], required: true },
dialogRoot: { type: String, required: true },
classNames: { type: Object, default: () => ({}) },
titleId: { type: String },
closeButtonLabel: { type: String, default: 'Close this dialog window' },
disableNative: { type: Boolean, default: false },
role: { type: String, default: 'dialog' }
},
components: {
Portal
},
computed: {
fullTitleId () {
return this.titleId || this.id + '-title'
},
dialogElement () {
return this.disableNative ? 'div' : 'dialog'
},
roleAttribute () {
return ['dialog', 'alertdialog'].includes(this.role)
? this.role
: 'dialog'
},
portalTarget () {
return this.dialogRoot || this.appRoot
}
},
data: () => ({
dialog: null
}),
methods: {
close () {
this.dialog.hide()
}
},
mounted () {
this.$nextTick(() => {
this.dialog = new A11yDialog(this.$refs.rootElement, this.appRoot)
this.$emit('dialog-ref', this.dialog)
})
},
destroyed () {
if (this.dialog) {
this.dialog.destroy()
}
this.$emit('dialog-ref')
}
}
</script>