forked from ember-bootstrap/ember-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbs-alert.js
222 lines (196 loc) · 4.95 KB
/
bs-alert.js
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
import { action } from '@ember/object';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { later } from '@ember/runloop';
import usesTransition from 'ember-bootstrap/utils/decorators/uses-transition';
import deprecateSubclassing from 'ember-bootstrap/utils/deprecate-subclassing';
import arg from 'ember-bootstrap/utils/decorators/arg';
/**
Implements [Bootstrap alerts](http://getbootstrap.com/components/#alerts)
### Usage
By default it is a user dismissible alert with a fade out animation, both of which can be disabled. Be sure to set the
`type` property for proper styling.
```hbs
<BsAlert @type="success">
<strong>Well done!</strong> You successfully read this important alert message.
</BsAlert>
```
Optionally you can render a header for the alert using named blocks syntax:
```hbs
<BsAlert>
<:header>
Well done!
</:header>
<:body>
You successfully read this important alert message.
</:body>
</BsAlert>
```
The header is rendered using a `<h4>` element by default. You can customize
that one by setting `@headerTag` argument of `<BsAlert>`.
Using named block syntax as shown above may require to install
[ember-named-blocks-polyfill](https://github.com/ember-polyfills/ember-named-blocks-polyfill)
in your project depending on the Ember version used.
*Note that only invoking the component in a template as shown above is considered part of its public API. Extending from it (subclassing) is generally not supported, and may break at any time.*
@class Alert
@namespace Components
@extends Glimmer.Component
@public
*/
@deprecateSubclassing
export default class Alert extends Component {
/**
* A dismissible alert will have a close button in the upper right corner, that the user can click to dismiss
* the alert.
*
* @property dismissible
* @type boolean
* @default true
* @public
*/
@arg
dismissible = true;
/**
* If true the alert is completely hidden. Will be set when the fade animation has finished.
*
* @property hidden
* @type boolean
* @default false
* @readonly
* @private
*/
@tracked
hidden = !this.visible;
/**
* This property controls if the alert should be visible. If false it might still be in the DOM until the fade animation
* has completed.
*
* When the alert is dismissed by user interaction this property will not update by using two-way bindings in order
* to follow DDAU best practices. If you want to react to such changes, subscribe to the `onDismiss` action
*
* @property visible
* @type boolean
* @default true
* @public
*/
get visible() {
return this._visible ?? true;
}
/**
* @property _visible
* @private
*/
@tracked
_visible = this.args.visible;
/**
* Set to false to disable the fade out animation when hiding the alert.
*
* @property fade
* @type boolean
* @default true
* @public
*/
@arg
fade = true;
get showAlert() {
return this.visible && this.args.fade !== false;
}
/**
* The duration of the fade out animation
*
* @property fadeDuration
* @type number
* @default 150
* @public
*/
@arg
fadeDuration = 150;
/**
* Property for type styling
*
* For the available types see the [Bootstrap docs](https://getbootstrap.com/docs/4.3/components/alerts/)
*
* @property type
* @type String
* @public
*/
/**
* Use CSS transitions?
*
* @property usesTransition
* @type boolean
* @readonly
* @private
*/
@usesTransition('fade')
usesTransition;
/**
* The action to be sent after the alert has been dismissed (including the CSS transition).
*
* @event onDismissed
* @public
*/
/**
* The action is called when the close button is clicked.
*
* You can return false to prevent closing the alert automatically, and do that in your action by
* setting `visible` to false.
*
* @event onDismiss
* @public
*/
@action
dismiss() {
if (this.args.onDismiss?.() !== false) {
this._visible = false;
}
}
/**
* Call to make the alert visible again after it has been hidden
*
* @method show
* @private
*/
show() {
this.hidden = false;
}
/**
* Call to hide the alert. If the `fade` property is true, this will fade out the alert before being finally
* dismissed.
*
* @method hide
* @private
*/
hide() {
if (this.hidden) {
return;
}
if (this.usesTransition) {
later(
this,
function () {
if (!this.isDestroyed) {
this.hidden = true;
this.args.onDismissed?.();
}
},
this.fadeDuration
);
} else {
this.hidden = true;
this.args.onDismissed?.();
}
}
@action
showOrHide() {
if (this.visible) {
this.show();
} else {
this.hide();
}
}
@action
updateVisibility() {
this._visible = this.args.visible;
}
}