1
1
import {
2
- AfterContentInit ,
3
2
booleanAttribute ,
4
3
Component ,
5
- ContentChildren ,
6
- EventEmitter ,
7
- HostBinding ,
8
- HostListener ,
9
- Input ,
10
- Output ,
11
- QueryList
4
+ computed ,
5
+ contentChildren ,
6
+ effect ,
7
+ input ,
8
+ output ,
9
+ signal ,
10
+ TemplateRef
12
11
} from '@angular/core' ;
13
12
import { NgTemplateOutlet } from '@angular/common' ;
14
13
import { animate , AnimationEvent , state , style , transition , trigger } from '@angular/animations' ;
@@ -17,139 +16,142 @@ import { Colors } from '../coreui.types';
17
16
import { TemplateIdDirective } from '../shared' ;
18
17
import { ButtonCloseDirective } from '../button' ;
19
18
20
- type AnimateType = ( 'hide' | 'show' ) ;
19
+ type AnimateType = 'hide' | 'show' ;
21
20
22
21
@Component ( {
23
- selector : 'c-alert' ,
24
- templateUrl : './alert.component.html' ,
25
- styleUrls : [ './alert.component.scss' ] ,
26
- exportAs : 'cAlert' ,
27
- imports : [ NgTemplateOutlet , ButtonCloseDirective ] ,
28
- animations : [
29
- trigger ( 'fadeInOut' , [
30
- state ( 'show' , style ( { opacity : 1 , height : '*' , padding : '*' , border : '*' , margin : '*' } ) ) ,
31
- state ( 'hide' , style ( { opacity : 0 , height : 0 , padding : 0 , border : 0 , margin : 0 } ) ) ,
32
- state ( 'void' , style ( { opacity : 0 , height : 0 , padding : 0 , border : 0 , margin : 0 } ) ) ,
33
- transition ( 'show => hide' , [
34
- animate ( '.3s ease-out' )
35
- ] ) ,
36
- transition ( 'hide => show' , [
37
- animate ( '.3s ease-in' )
38
- ] ) ,
39
- transition ( 'show => void' , [
40
- animate ( '.3s ease-out' )
41
- ] ) ,
42
- transition ( 'void => show' , [
43
- animate ( '.3s ease-in' )
44
- ] )
45
- ] )
46
- ]
22
+ selector : 'c-alert' ,
23
+ templateUrl : './alert.component.html' ,
24
+ styleUrls : [ './alert.component.scss' ] ,
25
+ exportAs : 'cAlert' ,
26
+ imports : [ NgTemplateOutlet , ButtonCloseDirective ] ,
27
+ animations : [
28
+ trigger ( 'fadeInOut' , [
29
+ state ( 'show' , style ( { opacity : 1 , height : '*' , padding : '*' , border : '*' , margin : '*' } ) ) ,
30
+ state ( 'hide' , style ( { opacity : 0 , height : 0 , padding : 0 , border : 0 , margin : 0 } ) ) ,
31
+ state ( 'void' , style ( { opacity : 0 , height : 0 , padding : 0 , border : 0 , margin : 0 } ) ) ,
32
+ transition ( 'show => hide' , [ animate ( '.3s ease-out' ) ] ) ,
33
+ transition ( 'hide => show' , [ animate ( '.3s ease-in' ) ] ) ,
34
+ transition ( 'show => void' , [ animate ( '.3s ease-out' ) ] ) ,
35
+ transition ( 'void => show' , [ animate ( '.3s ease-in' ) ] )
36
+ ] )
37
+ ] ,
38
+ host : {
39
+ '[@.disabled]' : '!fade()' ,
40
+ '[@fadeInOut]' : 'animateType' ,
41
+ '[attr.role]' : 'role()' ,
42
+ '[class]' : 'hostClasses()' ,
43
+ '(@fadeInOut.start)' : 'onAnimationStart($event)' ,
44
+ '(@fadeInOut.done)' : 'onAnimationDone($event)'
45
+ }
47
46
} )
48
- export class AlertComponent implements AfterContentInit {
49
-
50
- hide ! : boolean ;
47
+ export class AlertComponent {
51
48
/**
52
49
* Sets the color context of the component to one of CoreUI’s themed colors.
53
- *
54
- * @type Colors
50
+ * @return Colors
55
51
* @default 'primary'
56
52
*/
57
- @Input ( ) color : Colors = 'primary' ;
53
+ readonly color = input < Colors > ( 'primary' ) ;
54
+
58
55
/**
59
56
* Default role for alert. [docs]
60
- * @type string
57
+ * @return string
61
58
* @default 'alert'
62
59
*/
63
- @ HostBinding ( 'attr.role' )
64
- @ Input ( ) role = 'alert' ;
60
+ readonly role = input ( 'alert' ) ;
61
+
65
62
/**
66
63
* Set the alert variant to a solid.
67
- * @type string
68
- */
69
- @Input ( ) variant ?: 'solid' | string ;
70
- /**
71
- * Event triggered on the alert dismiss.
64
+ * @return string
72
65
*/
73
- @Output ( ) visibleChange : EventEmitter < boolean > = new EventEmitter ( ) ;
74
- templates : any = { } ;
75
- @ContentChildren ( TemplateIdDirective , { descendants : true } ) contentTemplates ! : QueryList < TemplateIdDirective > ;
66
+ readonly variant = input < 'solid' > ( ) ;
76
67
77
68
/**
78
69
* Optionally adds a close button to alert and allow it to self dismiss.
79
- * @type boolean
70
+ * @return boolean
80
71
* @default false
81
72
*/
82
- @ Input ( { transform : booleanAttribute } ) dismissible : boolean = false ;
73
+ readonly dismissible = input ( false , { transform : booleanAttribute } ) ;
83
74
84
75
/**
85
76
* Adds animation for dismissible alert.
86
- * @type boolean
77
+ * @return boolean
87
78
*/
88
- @ Input ( { transform : booleanAttribute } ) fade : boolean = false ;
79
+ readonly fade = input ( false , { transform : booleanAttribute } ) ;
89
80
90
81
/**
91
82
* Toggle the visibility of alert component.
92
- * @type boolean
83
+ * @return boolean
93
84
*/
94
- @Input ( { transform : booleanAttribute } )
85
+ readonly visibleInput = input ( true , { transform : booleanAttribute , alias : 'visible' } ) ;
86
+
87
+ readonly #visibleInputEffect = effect ( ( ) => {
88
+ this . visible = this . visibleInput ( ) ;
89
+ } ) ;
90
+
95
91
set visible ( value : boolean ) {
96
92
if ( this . #visible !== value ) {
97
93
this . #visible = value ;
98
94
this . visibleChange . emit ( value ) ;
99
95
}
100
- } ;
96
+ }
101
97
102
98
get visible ( ) {
103
99
return this . #visible;
104
100
}
105
101
106
102
#visible: boolean = true ;
107
103
108
- @HostBinding ( '@.disabled' )
109
- get animationDisabled ( ) : boolean {
110
- return ! this . fade ;
111
- }
104
+ readonly hide = signal < boolean > ( false ) ;
105
+
106
+ /**
107
+ * Event triggered on the alert dismiss.
108
+ */
109
+ readonly visibleChange = output < boolean > ( ) ;
110
+
111
+ readonly contentTemplates = contentChildren ( TemplateIdDirective , { descendants : true } ) ;
112
+
113
+ readonly templates = computed ( ( ) => {
114
+ return this . contentTemplates ( ) . reduce (
115
+ ( acc , child ) => {
116
+ acc [ child . id ] = child . templateRef ;
117
+ return acc ;
118
+ } ,
119
+ { } as Record < string , TemplateRef < any > >
120
+ ) ;
121
+ } ) ;
112
122
113
- @HostBinding ( '@fadeInOut' )
114
123
get animateType ( ) : AnimateType {
115
124
return this . visible ? 'show' : 'hide' ;
116
125
}
117
126
118
- @HostBinding ( 'class' )
119
- get hostClasses ( ) : any {
127
+ readonly hostClasses = computed ( ( ) => {
128
+ const color = this . color ( ) ;
129
+ const variant = this . variant ( ) ;
120
130
return {
121
131
alert : true ,
122
- 'alert-dismissible' : this . dismissible ,
123
- fade : this . fade ,
124
- show : ! this . hide ,
125
- [ `alert-${ this . color } ` ] : ! ! this . color && this . variant !== 'solid' ,
126
- [ `bg-${ this . color } ` ] : ! ! this . color && this . variant === 'solid' ,
127
- 'text-white' : ! ! this . color && this . variant === 'solid'
128
- } ;
129
- }
132
+ 'alert-dismissible' : this . dismissible ( ) ,
133
+ fade : this . fade ( ) ,
134
+ show : ! this . hide ( ) ,
135
+ [ `alert-${ color } ` ] : ! ! color && variant !== 'solid' ,
136
+ [ `bg-${ color } ` ] : ! ! color && variant === 'solid' ,
137
+ 'text-white' : ! ! color && variant === 'solid'
138
+ } as Record < string , boolean > ;
139
+ } ) ;
130
140
131
- @HostListener ( '@fadeInOut.start' , [ '$event' ] )
132
141
onAnimationStart ( $event : AnimationEvent ) : void {
133
142
this . onAnimationEvent ( $event ) ;
134
143
}
135
144
136
- @HostListener ( '@fadeInOut.done' , [ '$event' ] )
137
145
onAnimationDone ( $event : AnimationEvent ) : void {
138
146
this . onAnimationEvent ( $event ) ;
139
147
}
140
148
141
- ngAfterContentInit ( ) : void {
142
- this . contentTemplates . forEach ( ( child : TemplateIdDirective ) => {
143
- this . templates [ child . id ] = child . templateRef ;
144
- } ) ;
145
- }
146
-
147
149
onAnimationEvent ( event : AnimationEvent ) : void {
148
- this . hide = event . phaseName === 'start' && event . toState === 'show' ;
150
+ this . hide . set ( event . phaseName === 'start' && event . toState === 'show' ) ;
149
151
if ( event . phaseName === 'done' ) {
150
- this . hide = ( event . toState === 'hide' || event . toState === 'void' ) ;
152
+ this . hide . set ( event . toState === 'hide' || event . toState === 'void' ) ;
151
153
if ( event . toState === 'show' ) {
152
- this . hide = false ;
154
+ this . hide . set ( false ) ;
153
155
}
154
156
}
155
157
}
0 commit comments