Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added animation to Collapse #348

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions components/collapse/collapse.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import {Directive, Input, HostBinding} from 'angular2/core';
import {Directive, OnInit, ElementRef, Input, HostBinding} from 'angular2/core';
import {AnimationBuilder} from 'angular2/src/animate/animation_builder'; // 'angular2/animate';

// todo: add animate
// todo: add init and on change
// TODO: remove ElementRef
// TODO: switch to angular2/animate when available
// TODO: add on change
@Directive({selector: '[collapse]'})
export class Collapse {
export class Collapse implements OnInit {
private animation: any;

// style
@HostBinding('style.height')
private height:string;
// @HostBinding('style.height')
// private height:string;
@HostBinding('style.display')
private display: string;
// shown
@HostBinding('class.in')
@HostBinding('attr.aria-expanded')
Expand All @@ -21,6 +27,9 @@ export class Collapse {
@HostBinding('class.collapsing')
private isCollapsing:boolean = false;

@Input('transition-duration')
private transitionDuration: number = 500; // Duration in ms

@Input()
private set collapse(value:boolean) {
this.isExpanded = value;
Expand All @@ -31,7 +40,12 @@ export class Collapse {
return this.isExpanded;
}

constructor() {
constructor(private _ab: AnimationBuilder, private _el: ElementRef) {
}

ngOnInit(): void {
this.animation = this._ab.css();
this.animation.setDuration(this.transitionDuration);
}

toggle() {
Expand All @@ -48,10 +62,28 @@ export class Collapse {

this.isExpanded = false;
this.isCollapsed = true;

setTimeout(() => {
this.height = '0';
this.isCollapse = true;
this.isCollapsing = false;
// this.height = '0';
// this.isCollapse = true;
// this.isCollapsing = false;
this.animation
.setFromStyles({
height: this._el.nativeElement.scrollHeight + 'px'
})
.setToStyles({
height: '0',
overflow: 'hidden'
});

this.animation.start(this._el.nativeElement).onComplete(() => {
if (this._el.nativeElement.offsetHeight === 0) {
this.display = 'none';
}

this.isCollapse = true;
this.isCollapsing = false;
});
}, 4);
}

Expand All @@ -61,11 +93,26 @@ export class Collapse {

this.isExpanded = true;
this.isCollapsed = false;

this.display = '';

setTimeout(() => {
this.height = 'auto';
// this.height = 'auto';
// this.isCollapse = true;
// this.isCollapsing = false;
this.animation
.setFromStyles({
height: this._el.nativeElement.offsetHeight,
overflow: 'hidden'
})
.setToStyles({
height: this._el.nativeElement.scrollHeight + 'px'
});

this.isCollapse = true;
this.isCollapsing = false;
this.animation.start(this._el.nativeElement).onComplete(() => {
this.isCollapse = true;
this.isCollapsing = false;
});
}, 4);
}
}