1- import { Directive , ElementRef , Input , HostBinding , Renderer } from '@angular/core' ;
1+ import { Directive , ElementRef , Input , HostBinding , Renderer2 , HostListener } from '@angular/core' ;
22import "web-animations-js" ;
33
44@Directive ( {
@@ -40,7 +40,14 @@ export class SuiCollapse {
4040 @Input ( )
4141 public collapseDuration :number ;
4242
43- public constructor ( private _element :ElementRef , private _renderer : Renderer ) {
43+ private get _animationDuration ( ) {
44+ return this . _pristine ? 0 : this . collapseDuration ;
45+ }
46+
47+ // Timer for window resize counter.
48+ private _resizeTimeout :number ;
49+
50+ public constructor ( private _element :ElementRef , private _renderer :Renderer2 ) {
4451 this . _pristine = true ;
4552
4653 // Collapse animation duration is 350ms by default.
@@ -55,10 +62,10 @@ export class SuiCollapse {
5562 this . _isExpanded = false ;
5663
5764 // Forcibly hide the overflow so that content is not visible past the boundaries of its container.
58- this . _renderer . setElementStyle ( this . _element . nativeElement , 'overflow' , 'hidden' ) ;
65+ this . _renderer . setStyle ( this . _element . nativeElement , 'overflow' , 'hidden' ) ;
5966
6067 // Animate the host element from its scroll height to 0.
61- this . animate ( this . _element . nativeElement . scrollHeight , 0 , ( ) => {
68+ this . animate ( this . _element . nativeElement . scrollHeight , 0 , false , ( ) => {
6269 this . _isCollapsing = false ;
6370 } ) ;
6471 }
@@ -67,38 +74,47 @@ export class SuiCollapse {
6774 this . _isCollapsing = true ;
6875
6976 // Animate the host element from its offset height to its scroll height.
70- this . animate ( this . _element . nativeElement . offsetHeight , this . _element . nativeElement . scrollHeight , ( ) => {
77+ this . animate ( this . _element . nativeElement . offsetHeight , this . _element . nativeElement . scrollHeight , true , ( ) => {
7178 // Remove the overflow override to enable user styling once again.
72- this . _renderer . setElementStyle ( this . _element . nativeElement , 'overflow' , null ) ;
79+ this . _renderer . removeStyle ( this . _element . nativeElement , 'overflow' ) ;
7380
7481 this . _isCollapsing = false ;
7582 this . _isExpanded = true ;
7683 } ) ;
7784 }
7885
79- private animate ( startHeight :number , endHeight :number , callback :( ) => void ) {
86+ private animate ( startHeight :number , endHeight :number , removeOnComplete :boolean = false , callback :( ) => void = ( ) => { } ) {
87+ const heightFrames = [
88+ {
89+ offset : 0 ,
90+ height : `${ startHeight } px`
91+ } ,
92+ {
93+ offset : 1 ,
94+ height : `${ endHeight } px`
95+ }
96+ ] ;
97+
98+ if ( removeOnComplete ) {
99+ heightFrames . push ( {
100+ offset : 1 ,
101+ height : `auto`
102+ } ) ;
103+ }
104+
80105 // Animate the collapse using the web animations API.
81- this . _renderer . invokeElementMethod (
82- this . _element . nativeElement ,
83- "animate" ,
84- [
85- [
86- {
87- height : `${ startHeight } px`
88- } ,
89- {
90- height : `${ endHeight } px`
91- }
92- ] ,
93- {
94- delay : 0 ,
95- // Disable animation on 1st collapse / expansion.
96- duration : this . _pristine ? 0 : this . collapseDuration ,
97- iterations : 1 ,
98- easing : "ease" ,
99- fill : "both"
100- }
101- ] ) ;
106+ // Using directly because Renderer2 doesn't have invokeElementMethod method anymore.
107+ this . _element . nativeElement . animate (
108+ heightFrames ,
109+ {
110+ delay : 0 ,
111+ // Disable animation on 1st collapse / expansion.
112+ duration : this . _animationDuration ,
113+ iterations : 1 ,
114+ easing : "ease" ,
115+ fill : "both"
116+ }
117+ ) ;
102118
103119 if ( this . _pristine ) {
104120 // Remove pristine flag when first hit.
0 commit comments