Skip to content

Commit 2212214

Browse files
committed
fix(collapse): animations streamlined, refactor
1 parent 251f910 commit 2212214

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed
Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
1-
import { animation, animate, style } from '@angular/animations';
1+
import { animate, animation } from '@angular/animations';
22

33
export const expandAnimation = animation([
4-
style({ height: 0, visibility: 'hidden', paddingTop: 0, paddingBottom: 0 }),
5-
animate('{{ time }} {{ easing }}', style({ height: '*', visibility: 'visible', paddingTop: '*', paddingBottom: '*', minHeight: '*' })
6-
),
7-
animate('{{ time }}', style({opacity: '*'})),
4+
animate('{{ time }} {{ easing }}')
85
]);
96

107
export const collapseAnimation = animation([
11-
style({ height: '*', visibility: 'visible', paddingTop: '*', paddingBottom: '*', minHeight: '*' }),
12-
animate(
13-
'{{ time }} {{ easing }}',
14-
style({ height: 0, visibility: 'hidden', opacity: 0, paddingTop: 0, paddingBottom: 0, minHeight: 0 })
15-
)
8+
animate('{{ time }} {{ easing }}')
169
]);
1710

1811
export const expandHorizontalAnimation = animation([
19-
style({ width: 0, visibility: 'hidden', paddingLeft: 0, paddingRight: 0 }),
20-
animate('{{ time }} {{ easing }}', style({ width: '*', visibility: 'visible', paddingLeft: '*', paddingRight: '*', minWidth: '*' })
21-
),
22-
animate('{{ time }}', style({opacity: '*'})),
12+
animate('{{ time }} {{ easing }}')
2313
]);
2414

2515
export const collapseHorizontalAnimation = animation([
26-
style({ width: '*', visibility: 'visible', paddingLeft: '*', paddingRight: '*', minWidth: '*' }),
27-
animate(
28-
'{{ time }} {{ easing }}',
29-
style({ width: 0, visibility: 'hidden', opacity: 0, paddingLeft: 0, paddingRight: 0, minWidth: 0 })
30-
)
16+
animate('{{ time }} {{ easing }}')
3117
]);

projects/coreui-angular/src/lib/collapse/collapse.directive.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
3333

3434
static ngAcceptInputType_horizontal: BooleanInput;
3535
static ngAcceptInputType_navbar: BooleanInput;
36+
static ngAcceptInputType_visible: BooleanInput;
3637

3738
/**
3839
* @ignore
@@ -41,9 +42,11 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
4142
set animate(value: boolean) {
4243
this._animate = value;
4344
}
45+
4446
get animate(): boolean {
4547
return this._animate;
4648
}
49+
4750
private _animate = true;
4851

4952
/**
@@ -53,21 +56,25 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
5356
set horizontal(value: boolean) {
5457
this._horizontal = coerceBooleanProperty(value);
5558
}
59+
5660
get horizontal(): boolean {
5761
return this._horizontal;
5862
}
63+
5964
private _horizontal: boolean = false;
6065

6166
/**
6267
* Toggle the visibility of collapsible element.
6368
*/
6469
@Input()
6570
set visible(value) {
66-
this._visible = value;
71+
this._visible = coerceBooleanProperty(value);
6772
}
73+
6874
get visible(): boolean {
6975
return this._visible;
7076
}
77+
7178
private _visible = false;
7279

7380
/**
@@ -77,9 +84,11 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
7784
set navbar(value: boolean) {
7885
this._navbar = coerceBooleanProperty(value);
7986
};
87+
8088
get navbar() {
8189
return this._navbar;
8290
}
91+
8392
private _navbar = false;
8493

8594
/**
@@ -115,10 +124,7 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
115124
get hostClasses(): any {
116125
return {
117126
'navbar-collapse': this.navbar,
118-
show: this.visible,
119-
'collapse-horizontal': this.horizontal,
120-
collapsing: this.collapsing
121-
// collapse: !this.collapsing && !this.visible
127+
'collapse-horizontal': this.horizontal
122128
};
123129
}
124130

@@ -169,32 +175,58 @@ export class CollapseDirective implements OnChanges, OnDestroy, DoCheck, AfterVi
169175
const expand = this.horizontal ? expandHorizontalAnimation : expandAnimation;
170176
const collapse = this.horizontal ? collapseHorizontalAnimation : collapseAnimation;
171177

178+
const dimension = this.horizontal ? 'width' : 'height';
179+
const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
180+
const scrollSize = `scroll${capitalizedDimension}`;
181+
172182
const animationFactory = this.animationBuilder.build(
173183
useAnimation(visible ? expand : collapse, { params: { time: duration, easing: this.transition } })
174184
);
175185

176186
this.player = animationFactory.create(this.host);
187+
188+
this.renderer.setStyle(this.host, dimension, visible ? 0 : `${this.host.getBoundingClientRect()[dimension]}px`);
189+
190+
!visible && this.host.offsetHeight;
191+
177192
this.player.onStart(() => {
178193
this.setMaxSize();
179-
this.visible = visible;
194+
this.renderer.removeClass(this.host, 'collapse');
195+
this.renderer.addClass(this.host, 'collapsing');
196+
this.renderer.removeClass(this.host, 'show');
180197
this.collapsing = true;
198+
if (visible) {
199+
// @ts-ignore
200+
this.renderer.setStyle(this.host, dimension, `${this.host[scrollSize]}px`);
201+
} else {
202+
this.renderer.setStyle(this.host, dimension, '');
203+
}
181204
this.collapseChange.emit(visible ? 'opening' : 'collapsing');
182205
});
183206
this.player.onDone(() => {
207+
this.visible = visible;
184208
this.collapsing = false;
209+
this.renderer.removeClass(this.host, 'collapsing');
210+
this.renderer.addClass(this.host, 'collapse');
211+
if (visible) {
212+
this.renderer.addClass(this.host, 'show');
213+
this.renderer.setStyle(this.host, dimension, '');
214+
} else {
215+
this.renderer.removeClass(this.host, 'show');
216+
}
185217
this.collapseChange.emit(visible ? 'open' : 'collapsed');
186218
});
187219
}
188220

189221
setMaxSize() {
190-
setTimeout(() => {
191-
if (this.horizontal) {
192-
this.scrollWidth = this.host.scrollWidth;
193-
this.scrollWidth > 0 && this.renderer.setStyle(this.host, 'maxWidth', `${this.scrollWidth}px`);
194-
// } else {
195-
// this.scrollHeight = this.host.scrollHeight;
196-
// this.scrollHeight > 0 && this.renderer.setStyle(this.host, 'maxHeight', `${this.scrollHeight}px`);
197-
}
198-
});
222+
// setTimeout(() => {
223+
if (this.horizontal) {
224+
this.scrollWidth = this.host.scrollWidth;
225+
this.scrollWidth > 0 && this.renderer.setStyle(this.host, 'maxWidth', `${this.scrollWidth}px`);
226+
// } else {
227+
// this.scrollHeight = this.host.scrollHeight;
228+
// this.scrollHeight > 0 && this.renderer.setStyle(this.host, 'maxHeight', `${this.scrollHeight}px`);
229+
}
230+
// });
199231
}
200232
}

0 commit comments

Comments
 (0)