11import {
22 AfterViewInit ,
33 booleanAttribute ,
4+ computed ,
45 Directive ,
56 DoCheck ,
67 ElementRef ,
7- EventEmitter ,
8- HostBinding ,
98 Input ,
9+ input ,
1010 OnChanges ,
1111 OnDestroy ,
12- Output ,
12+ output ,
1313 Renderer2 ,
1414 SimpleChanges
1515} from '@angular/core' ;
@@ -26,11 +26,13 @@ import {
2626@Directive ( {
2727 selector : '[cCollapse]' ,
2828 exportAs : 'cCollapse' ,
29- standalone : true
29+ standalone : true ,
30+ host : { '[class]' : 'hostClasses()' }
3031} )
31- export class CollapseDirective implements OnChanges , OnDestroy , DoCheck , AfterViewInit {
32+ export class CollapseDirective implements OnDestroy , AfterViewInit , DoCheck , OnChanges {
3233 /**
3334 * @ignore
35+ * todo: 'animate' input signal for navbar
3436 */
3537 @Input ( { transform : booleanAttribute } ) animate : boolean = true ;
3638
@@ -39,12 +41,13 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
3941 * @type boolean
4042 * @default false
4143 */
42- @ Input ( { transform : booleanAttribute } ) horizontal : boolean = false ;
44+ readonly horizontal = input ( false , { transform : booleanAttribute } ) ;
4345
4446 /**
4547 * Toggle the visibility of collapsible element.
4648 * @type boolean
4749 * @default false
50+ * todo: 'visible' input signal
4851 */
4952 @Input ( { transform : booleanAttribute } )
5053 set visible ( value ) {
@@ -62,21 +65,23 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
6265 * @type boolean
6366 * @default false
6467 */
65- @ Input ( { transform : booleanAttribute } ) navbar : boolean = false ;
68+ readonly navbar = input ( false , { transform : booleanAttribute } ) ;
6669
6770 /**
6871 * @ignore
6972 */
70- @Input ( ) duration = '350ms' ;
73+ readonly duration = input ( '350ms' ) ;
74+
7175 /**
7276 * @ignore
7377 */
74- @Input ( ) transition = 'ease' ;
78+ readonly transition = input ( 'ease' ) ;
79+
7580 /**
7681 * Event emitted on visibility change. [docs]
7782 * @type string
7883 */
79- @ Output ( ) collapseChange = new EventEmitter < string > ( ) ;
84+ readonly collapseChange = output < string > ( ) ;
8085
8186 private player ! : AnimationPlayer ;
8287 private readonly host : HTMLElement ;
@@ -85,21 +90,20 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
8590 private collapsing : boolean = false ;
8691
8792 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
9196 ) {
9297 this . host = this . hostElement . nativeElement ;
9398 this . renderer . setStyle ( this . host , 'display' , 'none' ) ;
9499 }
95100
96- @HostBinding ( 'class' )
97- get hostClasses ( ) : any {
101+ readonly hostClasses = computed ( ( ) => {
98102 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+ } ) ;
103107
104108 ngAfterViewInit ( ) : void {
105109 if ( this . visible ) {
@@ -143,17 +147,17 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
143147 this . renderer . removeStyle ( this . host , 'display' ) ;
144148 }
145149
146- const duration = this . animate ? this . duration : '0ms' ;
150+ const duration = this . animate ? this . duration ( ) : '0ms' ;
147151
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 ;
150154
151- const dimension = this . horizontal ? 'width' : 'height' ;
155+ const dimension = this . horizontal ( ) ? 'width' : 'height' ;
152156 const capitalizedDimension = dimension [ 0 ] . toUpperCase ( ) + dimension . slice ( 1 ) ;
153157 const scrollSize = `scroll${ capitalizedDimension } ` ;
154158
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 ( ) } } )
157161 ) ;
158162
159163 this . player = animationFactory . create ( this . host ) ;
@@ -169,8 +173,7 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
169173 this . renderer . removeClass ( this . host , 'show' ) ;
170174 this . collapsing = true ;
171175 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` ) ;
174177 } else {
175178 this . renderer . setStyle ( this . host , dimension , '' ) ;
176179 }
@@ -193,7 +196,7 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
193196
194197 setMaxSize ( ) {
195198 // setTimeout(() => {
196- if ( this . horizontal ) {
199+ if ( this . horizontal ( ) ) {
197200 this . scrollWidth = this . host . scrollWidth ;
198201 this . scrollWidth > 0 && this . renderer . setStyle ( this . host , 'maxWidth' , `${ this . scrollWidth } px` ) ;
199202 // } else {
0 commit comments