1
1
import {
2
2
AfterViewInit ,
3
3
booleanAttribute ,
4
+ computed ,
4
5
Directive ,
5
6
DoCheck ,
6
7
ElementRef ,
7
- EventEmitter ,
8
- HostBinding ,
9
8
Input ,
9
+ input ,
10
10
OnChanges ,
11
11
OnDestroy ,
12
- Output ,
12
+ output ,
13
13
Renderer2 ,
14
14
SimpleChanges
15
15
} from '@angular/core' ;
@@ -26,11 +26,13 @@ import {
26
26
@Directive ( {
27
27
selector :'[cCollapse]' ,
28
28
exportAs :'cCollapse' ,
29
- standalone :true
29
+ standalone :true ,
30
+ host :{ '[class]' :'hostClasses()' }
30
31
} )
31
- export class CollapseDirective implements OnChanges , OnDestroy , DoCheck , AfterViewInit {
32
+ export class CollapseDirective implements OnDestroy , AfterViewInit , DoCheck , OnChanges {
32
33
/**
33
34
*@ignore
35
+ * todo: 'animate' input signal for navbar
34
36
*/
35
37
@Input ( { transform :booleanAttribute } ) animate :boolean = true ;
36
38
@@ -39,12 +41,13 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
39
41
*@type boolean
40
42
*@default false
41
43
*/
42
- @ Input ( { transform :booleanAttribute } ) horizontal : boolean = false ;
44
+ readonly horizontal = input ( false , { transform :booleanAttribute } ) ;
43
45
44
46
/**
45
47
* Toggle the visibility of collapsible element.
46
48
*@type boolean
47
49
*@default false
50
+ * todo: 'visible' input signal
48
51
*/
49
52
@Input ( { transform :booleanAttribute } )
50
53
set visible ( value ) {
@@ -62,21 +65,23 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
62
65
*@type boolean
63
66
*@default false
64
67
*/
65
- @ Input ( { transform :booleanAttribute } ) navbar : boolean = false ;
68
+ readonly navbar = input ( false , { transform :booleanAttribute } ) ;
66
69
67
70
/**
68
71
*@ignore
69
72
*/
70
- @Input ( ) duration = '350ms' ;
73
+ readonly duration = input ( '350ms' ) ;
74
+
71
75
/**
72
76
*@ignore
73
77
*/
74
- @Input ( ) transition = 'ease' ;
78
+ readonly transition = input ( 'ease' ) ;
79
+
75
80
/**
76
81
* Event emitted on visibility change. [docs]
77
82
*@type string
78
83
*/
79
- @ Output ( ) collapseChange = new EventEmitter < string > ( ) ;
84
+ readonly collapseChange = output < string > ( ) ;
80
85
81
86
private player ! :AnimationPlayer ;
82
87
private readonly host :HTMLElement ;
@@ -85,21 +90,20 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
85
90
private collapsing :boolean = false ;
86
91
87
92
constructor (
88
- private hostElement :ElementRef ,
89
- private renderer :Renderer2 ,
90
- private animationBuilder :AnimationBuilder
93
+ private readonly hostElement :ElementRef ,
94
+ private readonly renderer :Renderer2 ,
95
+ private readonly animationBuilder :AnimationBuilder
91
96
) {
92
97
this . host = this . hostElement . nativeElement ;
93
98
this . renderer . setStyle ( this . host , 'display' , 'none' ) ;
94
99
}
95
100
96
- @HostBinding ( 'class' )
97
- get hostClasses ( ) :any {
101
+ readonly hostClasses = computed ( ( ) => {
98
102
return {
99
- 'navbar-collapse' :this . navbar ,
100
- 'collapse-horizontal' :this . horizontal
101
- } ;
102
- }
103
+ 'navbar-collapse' :this . navbar ( ) ,
104
+ 'collapse-horizontal' :this . horizontal ( )
105
+ } as Record < string , boolean > ;
106
+ } ) ;
103
107
104
108
ngAfterViewInit ( ) :void {
105
109
if ( this . visible ) {
@@ -143,17 +147,17 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
143
147
this . renderer . removeStyle ( this . host , 'display' ) ;
144
148
}
145
149
146
- const duration = this . animate ?this . duration :'0ms' ;
150
+ const duration = this . animate ?this . duration ( ) :'0ms' ;
147
151
148
- const expand = this . horizontal ?expandHorizontalAnimation :expandAnimation ;
149
- const collapse = this . horizontal ?collapseHorizontalAnimation :collapseAnimation ;
152
+ const expand = this . horizontal ( ) ?expandHorizontalAnimation :expandAnimation ;
153
+ const collapse = this . horizontal ( ) ?collapseHorizontalAnimation :collapseAnimation ;
150
154
151
- const dimension = this . horizontal ?'width' :'height' ;
155
+ const dimension = this . horizontal ( ) ?'width' :'height' ;
152
156
const capitalizedDimension = dimension [ 0 ] . toUpperCase ( ) + dimension . slice ( 1 ) ;
153
157
const scrollSize = `scroll${ capitalizedDimension } ` ;
154
158
155
- const animationFactory = this . animationBuilder . build (
156
- useAnimation ( visible ?expand :collapse , { params :{ time :duration , easing :this . transition } } )
159
+ const animationFactory = this . animationBuilder ? .build (
160
+ useAnimation ( visible ?expand :collapse , { params :{ time :duration , easing :this . transition ( ) } } )
157
161
) ;
158
162
159
163
this . player = animationFactory . create ( this . host ) ;
@@ -169,8 +173,7 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
169
173
this . renderer . removeClass ( this . host , 'show' ) ;
170
174
this . collapsing = true ;
171
175
if ( visible ) {
172
- //@ts -ignore
173
- this . renderer . setStyle ( this . host , dimension , `${ this . host [ scrollSize ] } px` ) ;
176
+ this . renderer . setStyle ( this . host , dimension , `${ this . hostElement . nativeElement [ scrollSize ] } px` ) ;
174
177
} else {
175
178
this . renderer . setStyle ( this . host , dimension , '' ) ;
176
179
}
@@ -193,7 +196,7 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
193
196
194
197
setMaxSize ( ) {
195
198
// setTimeout(() => {
196
- if ( this . horizontal ) {
199
+ if ( this . horizontal ( ) ) {
197
200
this . scrollWidth = this . host . scrollWidth ;
198
201
this . scrollWidth > 0 && this . renderer . setStyle ( this . host , 'maxWidth' , `${ this . scrollWidth } px` ) ;
199
202
// } else {