-
Notifications
You must be signed in to change notification settings - Fork 42
/
alert-group.element.ts
147 lines (132 loc) · 4.17 KB
/
alert-group.element.ts
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
/*
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import { html, LitElement } from 'lit';
import { query } from 'lit/decorators/query.js';
import { baseStyles, property, querySlot, querySlotAll, syncProps } from '@cds/core/internal';
import { CdsAlert } from './alert.element.js';
import { AlertGroupTypes, AlertStatusTypes, AlertSizes } from './alert.interfaces.js';
import styles from './alert-group.element.scss';
/**
* Alert groups are containers for a set of alerts. Alert groups can hold one or many alerts
* inside of them with the expectation that all alerts will be of the same type. The exception
* to this rule is the `loading` alert type, which will be displayed regardless of the type
* of alert group containing it.
*
* ```typescript
* import '@cds/core/alert/register.js';
* ```
*
* ```html
* <cds-alert-group type="danger">
* <cds-alert closable="true">
* Single alert
* <cds-alert-actions>
* buttons, links
* </cds-alert-actions>
* </cds-alert>
* <cds-alert type="loading">
* Single Alert
* </cds-alert>
* <cds-alert closable="true">
* Another alert
* <cds-alert-actions>
* buttons, links
* </cds-alert-actions>
* </cds-alert>
* </cds-alert-group>
* ```
*
* @element cds-alert-group
* @slot - Content slot for the alerts
* @cssprop --color
* @cssprop --icon-color
* @cssprop --icon-size
* @cssprop --font-size
* @cssprop --font-weight
* @cssprop --letter-spacing
* @cssprop --padding
* @cssprop --background
* @cssprop --border-color
* @cssprop --border-width
* @cssprop --border-radius
*/
export class CdsAlertGroup extends LitElement {
/**
* Sets the overall height and width of the alerts inside the alert group
* @type {default | sm}
*/
@property({ type: String })
size: AlertSizes = 'default';
/**
* Passed down into the alerts inside the alert-group
* @type {default | banner | light}
*/
@property({ type: String })
type: AlertGroupTypes = 'default';
/**
* Sets the status of the alerts inside the alert group
* @type {neutral | info | success | warning | danger | alt | loading}
*/
@property({ type: String })
status: AlertStatusTypes = 'neutral';
@querySlotAll('cds-alert') private alerts: NodeListOf<CdsAlert>;
/** @private */
@querySlot('.pager', { assign: 'pager' }) pager: HTMLElement;
@query('.alerts') private alertSlot: HTMLElement;
render() {
return html`
<div
cds-layout="${this.pager ? 'horizontal wrap:none' : 'horizontal'}"
class="${this.pager ? 'private-host' : 'private-host no-pager'}"
>
<div class="pager-wrapper" cds-layout="p-x:lg">
<slot name="pager"></slot>
</div>
<div class="alert-group-wrapper">
<div
class="alerts"
cds-layout="vertical wrap:none align:horizontal-stretch fill ${this.size === 'sm' ? 'gap:none' : 'gap:sm'}"
>
<slot></slot>
</div>
</div>
</div>
`;
}
connectedCallback() {
super.connectedCallback();
this.role = 'region';
}
firstUpdated(props: Map<string, any>) {
super.firstUpdated(props);
this.setupAlertsUpdate();
}
private setupAlertsUpdate() {
const propsToSync = { status: true, type: true, size: true };
this.alertSlot?.addEventListener('slotchange', () => this.syncAlerts(propsToSync));
}
private async syncAlerts(propsToSync: { status: boolean; type: boolean; size: boolean }) {
await Promise.all(Array.from(this.alerts).map(a => a.updateComplete));
this.alerts.forEach(alert =>
syncProps(alert, this, {
status: propsToSync.status && this.type !== 'light' && alert.status !== 'loading',
type: propsToSync.type,
size: propsToSync.size,
})
);
}
updated(props: Map<string, any>) {
super.updated(props);
this.syncAlerts({
status: props.has('status'),
type: props.has('type'),
size: props.has('size'),
});
}
static get styles() {
return [baseStyles, styles];
}
}