Skip to content

Commit

Permalink
fix(collapse): fix collapse animation (child height) (#5316)
Browse files Browse the repository at this point in the history
* fix(collapse): fix collapse animation (child height)

* fix(tests): add fix for cy tests and fix for stackblitz example in IE
  • Loading branch information
Domainv committed Jul 9, 2019
1 parent c102ff0 commit f550605
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cypress/support/accordion.po.ts
Expand Up @@ -40,7 +40,7 @@ export class AccordionPo extends BaseComponent {

isItemContentVisible(baseSelector: string, itemIndex: number, visible: boolean) {
cy.get(`${baseSelector} .panel-body`)
.eq(itemIndex)
.eq(itemIndex, {timeout: 10000})
.should(visible ? 'be.visible' : 'not.be.visible');
}

Expand Down
2 changes: 2 additions & 0 deletions demo/src/app/components/+accordion/docs/usage.md
Expand Up @@ -13,3 +13,5 @@ import { AccordionModule } from 'ngx-bootstrap';
]
})
export class AppModule(){}

Also should be added web-animations-js polyfill for IE browser (Edge)
2 changes: 2 additions & 0 deletions demo/src/app/components/+collapse/docs/usage.md
Expand Up @@ -13,3 +13,5 @@ import { CollapseModule } from 'ngx-bootstrap';
]
})
export class AppModule(){}

Also should be added web-animations-js polyfill for IE browser (Edge)
Expand Up @@ -95,6 +95,7 @@ export class ExamplesComponent {
},
dependencies: {
'@angular/animations': 'latest',
'web-animations-js': 'latest',
'ngx-bootstrap': 'next'
},
title: 'stackblitz demo',
Expand Down
Expand Up @@ -32,6 +32,7 @@ export const polyfills = `/**
// import 'core-js/es6/regexp';
// import 'core-js/es6/map';
// import 'core-js/es6/set';
import 'web-animations-js';
/** IE10 and IE11 requires the following for NgClass support on SVG elements */
// import 'classlist.js'; // Run \`npm install --save classlist.js\`.
Expand Down
1 change: 1 addition & 0 deletions demo/src/polyfills.ts
Expand Up @@ -19,5 +19,6 @@ import 'core-js/es6/reflect';
import 'core-js/es7/reflect';
import 'zone.js/dist/zone';
import 'classlist-polyfill';
import 'web-animations-js';
// import 'intl';
// import 'intl/locale-data/jsonp/en';
20 changes: 6 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -25,7 +25,7 @@
"flow.github-release": "conventional-github-releaser -p angular",
"build": "run-s build.modules build:schematics build.sass dist-to-modules compiler-cli:fix",
"compiler-cli:fix": "scripts/ivy/compiler-cli-fix.sh",
"dist-to-modules": "cp -r -T ./dist ./node_modules/ngx-bootstrap",
"dist-to-modules": "cp -R ./dist/. ./node_modules/ngx-bootstrap",
"build.watch": "node scripts/build-modules --watch",
"build:schematics": "node scripts/schematics/build",
"build.modules": "node scripts/build-modules",
Expand Down Expand Up @@ -130,6 +130,7 @@
"bootstrap": "4.2.1",
"chai": "4.1.2",
"classlist-polyfill": "1.2.0",
"web-animations-js": "2.3.2",
"codecov": "3.1.0",
"codelyzer": "5.0.1",
"compression": "1.7.2",
Expand Down
23 changes: 19 additions & 4 deletions src/collapse/collapse.directive.ts
Expand Up @@ -4,7 +4,6 @@ import {
AnimationPlayer
} from '@angular/animations';

// todo: add animations when https://github.com/angular/angular/issues/9947 solved
import {
AfterViewChecked,
Directive,
Expand Down Expand Up @@ -73,8 +72,10 @@ export class CollapseDirective implements AfterViewChecked {
/** A flag indicating visibility of content (shown or hidden) */
@Input()
set collapse(value: boolean) {
this.isExpanded = value;
this.toggle();
if (!this._player || this._isAnimationDone) {
this.isExpanded = value;
this.toggle();
}
}

get collapse(): boolean {
Expand All @@ -84,6 +85,7 @@ export class CollapseDirective implements AfterViewChecked {
private _display = 'block';
private _factoryCollapseAnimation: AnimationFactory;
private _factoryExpandAnimation: AnimationFactory;
private _isAnimationDone: boolean;
private _player: AnimationPlayer;
private _stylesLoaded = false;

Expand All @@ -99,8 +101,15 @@ export class CollapseDirective implements AfterViewChecked {
this._factoryExpandAnimation = _builder.build(expandAnimation);
}

ngAfterViewChecked() {
ngAfterViewChecked(): void {
this._stylesLoaded = true;

if (!this._player || !this._isAnimationDone) {
return;
}

this._player.reset();
this._renderer.setStyle(this._el.nativeElement, 'height', '*');
}

/** allows to manually toggle content visibility */
Expand All @@ -121,7 +130,10 @@ export class CollapseDirective implements AfterViewChecked {

this.collapses.emit(this);

this._isAnimationDone = false;

this.animationRun(this.isAnimated, this._COLLAPSE_ACTION_NAME)(() => {
this._isAnimationDone = true;
this.collapsed.emit(this);
this._renderer.setStyle(this._el.nativeElement, 'display', 'none');
});
Expand All @@ -137,7 +149,10 @@ export class CollapseDirective implements AfterViewChecked {

this.expands.emit(this);

this._isAnimationDone = false;

this.animationRun(this.isAnimated, this._EXPAND_ACTION_NAME)(() => {
this._isAnimationDone = true;
this.expanded.emit(this);
});
}
Expand Down

0 comments on commit f550605

Please sign in to comment.