-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathvaadin-confirm-dialog-mixin.js
More file actions
418 lines (367 loc) · 11.6 KB
/
vaadin-confirm-dialog-mixin.js
File metadata and controls
418 lines (367 loc) · 11.6 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* @license
* Copyright (c) 2018 - 2026 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { setAriaIDReference } from '@vaadin/a11y-base/src/aria-id-reference.js';
import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
import { DialogSizeMixin } from '@vaadin/dialog/src/vaadin-dialog-size-mixin.js';
/**
* @polymerMixin
* @mixes DialogSizeMixin
*/
export const ConfirmDialogMixin = (superClass) =>
class ConfirmDialogMixinClass extends DialogSizeMixin(superClass) {
static get properties() {
return {
/**
* Sets the `aria-describedby` attribute of the dialog.
*
* By default, the text contents of all elements inside the message area
* are combined into the `aria-description` attribute. However, there are
* cases where this can confuse screen reader users (e.g. the dialog
* may present a password confirmation form). For these cases,
* it's better to associate only the elements that will help describe
* the confirmation dialog through this API.
*/
accessibleDescriptionRef: {
type: String,
},
/**
* True if the dialog is visible and available for interaction.
*/
opened: {
type: Boolean,
reflectToAttribute: true,
value: false,
notify: true,
sync: true,
},
/**
* Set the confirmation dialog title.
*/
header: {
type: String,
value: '',
},
/**
* Set the message or confirmation question.
*/
message: {
type: String,
value: '',
},
/**
* Text displayed on confirm-button.
* This only affects the default button, custom slotted buttons will not be altered.
* @attr {string} confirm-text
*/
confirmText: {
type: String,
value: 'Confirm',
},
/**
* Theme for a confirm-button.
* This only affects the default button, custom slotted buttons will not be altered.
* @attr {string} confirm-theme
*/
confirmTheme: {
type: String,
value: 'primary',
},
/**
* Set to true to disable closing dialog on Escape press
* @attr {boolean} no-close-on-esc
*/
noCloseOnEsc: {
type: Boolean,
value: false,
},
/**
* Whether to show reject button or not.
* @attr {boolean} reject-button-visible
*/
rejectButtonVisible: {
type: Boolean,
reflectToAttribute: true,
value: false,
},
/**
* Text displayed on reject-button.
* This only affects the default button, custom slotted buttons will not be altered.
* @attr {string} reject-text
*/
rejectText: {
type: String,
value: 'Reject',
},
/**
* Theme for a reject-button.
* This only affects the default button, custom slotted buttons will not be altered.
* @attr {string} reject-theme
*/
rejectTheme: {
type: String,
value: 'error tertiary',
},
/**
* Whether to show cancel button or not.
* @attr {boolean} cancel-button-visible
*/
cancelButtonVisible: {
type: Boolean,
reflectToAttribute: true,
value: false,
},
/**
* Text displayed on cancel-button.
* This only affects the default button, custom slotted buttons will not be altered.
* @attr {string} cancel-text
*/
cancelText: {
type: String,
value: 'Cancel',
},
/**
* Theme for a cancel-button.
* This only affects the default button, custom slotted buttons will not be altered.
* @attr {string} cancel-theme
*/
cancelTheme: {
type: String,
value: 'tertiary',
},
/**
* A reference to the "Cancel" button which will be teleported to the overlay.
* @private
*/
_cancelButton: {
type: Object,
},
/**
* A reference to the "Confirm" button which will be teleported to the overlay.
* @private
*/
_confirmButton: {
type: Object,
},
/**
* A reference to the "header" node which will be teleported to the overlay.
* @private
*/
_headerNode: {
type: Object,
},
/**
* A list of message nodes which will be placed in the overlay default slot.
* @private
*/
_messageNodes: {
type: Array,
value: () => [],
},
/**
* A reference to the "Reject" button which will be teleported to the overlay.
* @private
*/
_rejectButton: {
type: Object,
},
};
}
static get observers() {
return [
'__updateConfirmButton(_confirmButton, confirmText, confirmTheme)',
'__updateCancelButton(_cancelButton, cancelText, cancelTheme, cancelButtonVisible)',
'__updateHeaderNode(_headerNode, header)',
'__updateMessageNodes(_messageNodes, message)',
'__updateRejectButton(_rejectButton, rejectText, rejectTheme, rejectButtonVisible)',
'__accessibleDescriptionRefChanged(_messageNodes, accessibleDescriptionRef)',
];
}
constructor() {
super();
this.__cancel = this.__cancel.bind(this);
this.__confirm = this.__confirm.bind(this);
this.__reject = this.__reject.bind(this);
}
/** @protected */
connectedCallback() {
super.connectedCallback();
// Restore opened state if overlay was opened when disconnecting
if (this.__restoreOpened) {
this.opened = true;
}
}
/** @protected */
disconnectedCallback() {
super.disconnectedCallback();
// Automatically close the overlay when dialog is removed from DOM
// Using a timeout to avoid toggling opened state, and dispatching change
// events, when just moving the dialog in the DOM
setTimeout(() => {
if (!this.isConnected) {
this.__restoreOpened = this.opened;
this.opened = false;
}
});
}
/** @protected */
ready() {
super.ready();
this.role = 'alertdialog';
this.setAttribute('aria-modal', 'true');
this.setAttribute('tabindex', '0');
this._headerController = new SlotController(this, 'header', 'h3', {
initializer: (node) => {
this._headerNode = node;
},
});
this.addController(this._headerController);
this._messageController = new SlotController(this, '', 'div', {
// Allow providing multiple custom nodes in the default slot
multiple: true,
observe: false,
initializer: (node) => {
this._messageNodes = [...this._messageNodes, node];
},
});
this.addController(this._messageController);
// NOTE: order in which buttons are added should match the order of slots in template
this._cancelController = new SlotController(this, 'cancel-button', 'vaadin-button', {
initializer: (button) => {
this.__setupSlottedButton('cancel', button);
},
});
this.addController(this._cancelController);
this._rejectController = new SlotController(this, 'reject-button', 'vaadin-button', {
initializer: (button) => {
this.__setupSlottedButton('reject', button);
},
});
this.addController(this._rejectController);
this._confirmController = new SlotController(this, 'confirm-button', 'vaadin-button', {
initializer: (button) => {
this.__setupSlottedButton('confirm', button);
},
});
this.addController(this._confirmController);
this._overlayElement = this.$.overlay;
}
/** @protected */
updated(props) {
super.updated(props);
if (props.has('header')) {
this.ariaLabel = this.header || 'confirmation';
}
}
/** @protected */
__onDialogOpened() {
if (this._confirmButton) {
this._confirmButton.focus();
}
}
/** @protected */
__onDialogClosed() {
this.dispatchEvent(new CustomEvent('closed'));
}
/** @private */
__accessibleDescriptionRefChanged(messageNodes, accessibleDescriptionRef) {
if (!messageNodes) {
return;
}
if (accessibleDescriptionRef) {
this.removeAttribute('aria-description');
setAriaIDReference(this, 'aria-describedby', {
newId: accessibleDescriptionRef,
oldId: this.__oldAccessibleDescriptionRef,
fromUser: true,
});
} else {
this.removeAttribute('aria-describedby');
const ariaDescription = messageNodes.map((node) => node.textContent.trim()).join(' ');
this.setAttribute('aria-description', ariaDescription);
}
this.__oldAccessibleDescriptionRef = accessibleDescriptionRef;
}
/** @private */
__setupSlottedButton(type, button) {
const property = `_${type}Button`;
const listener = `__${type}`;
if (this[property] && this[property] !== button) {
this[property].remove();
}
button.addEventListener('click', this[listener]);
this[property] = button;
}
/** @private */
__updateCancelButton(button, cancelText, cancelTheme, showCancel) {
if (button) {
if (button === this._cancelController.defaultNode) {
button.textContent = cancelText;
button.setAttribute('theme', cancelTheme);
}
button.toggleAttribute('hidden', !showCancel);
}
}
/** @private */
__updateConfirmButton(button, confirmText, confirmTheme) {
if (button && button === this._confirmController.defaultNode) {
button.textContent = confirmText;
button.setAttribute('theme', confirmTheme);
}
}
/** @private */
__updateHeaderNode(headerNode, header) {
// Only update text content for the default header node.
if (headerNode && headerNode === this._headerController.defaultNode) {
headerNode.textContent = header;
}
}
/** @private */
__updateMessageNodes(nodes, message) {
if (nodes && nodes.length > 0) {
const defaultNode = nodes.find((node) => node === this._messageController.defaultNode);
if (defaultNode) {
defaultNode.textContent = message;
}
}
}
/** @private */
__updateRejectButton(button, rejectText, rejectTheme, showReject) {
if (button) {
if (button === this._rejectController.defaultNode) {
button.textContent = rejectText;
button.setAttribute('theme', rejectTheme);
}
button.toggleAttribute('hidden', !showReject);
}
}
/** @protected */
_onOverlayEscapePress(event) {
if (this.noCloseOnEsc) {
event.preventDefault();
} else {
this.__cancel();
}
}
/** @protected */
_onOverlayOutsideClick(event) {
event.preventDefault();
}
/** @private */
__confirm() {
this.dispatchEvent(new CustomEvent('confirm'));
this.opened = false;
}
/** @private */
__cancel() {
this.dispatchEvent(new CustomEvent('cancel'));
this.opened = false;
}
/** @private */
__reject() {
this.dispatchEvent(new CustomEvent('reject'));
this.opened = false;
}
};