docs: refactor elements example (#24840)

This makes the closing behavior more deterministic, which makes it
easier to be e2e-tested.

PR Close #24840
This commit is contained in:
George Kalpakas 2018-07-14 21:24:36 +03:00 committed by Victor Berchet
parent 453693fd33
commit b137f09345

View File

@ -1,14 +1,15 @@
// #docregion // #docregion
import { Component, EventEmitter, Input, Output } from '@angular/core'; import { Component, EventEmitter, Input, Output } from '@angular/core';
import { AnimationEvent } from '@angular/animations';
import { animate, state, style, transition, trigger } from '@angular/animations'; import { animate, state, style, transition, trigger } from '@angular/animations';
@Component({ @Component({
selector: 'my-popup', selector: 'my-popup',
template: 'Popup: {{message}}', template: `
<span>Popup: {{message}}</span>
<button (click)="closed.next()">&#x2716;</button>
`,
host: { host: {
'[@state]': 'state', '[@state]': 'state',
'(@state.done)': 'onAnimationDone($event)',
}, },
animations: [ animations: [
trigger('state', [ trigger('state', [
@ -27,13 +28,17 @@ import { animate, state, style, transition, trigger } from '@angular/animations'
height: 48px; height: 48px;
padding: 16px; padding: 16px;
display: flex; display: flex;
justify-content: space-between;
align-items: center; align-items: center;
border-top: 1px solid black; border-top: 1px solid black;
font-size: 24px; font-size: 24px;
} }
button {
border-radius: 50%;
}
`] `]
}) })
export class PopupComponent { export class PopupComponent {
private state: 'opened' | 'closed' = 'closed'; private state: 'opened' | 'closed' = 'closed';
@ -41,18 +46,10 @@ export class PopupComponent {
set message(message: string) { set message(message: string) {
this._message = message; this._message = message;
this.state = 'opened'; this.state = 'opened';
setTimeout(() => this.state = 'closed', 2000);
} }
get message(): string { return this._message; } get message(): string { return this._message; }
_message: string; _message: string;
@Output() @Output()
closed = new EventEmitter(); closed = new EventEmitter();
onAnimationDone(e: AnimationEvent) {
if (e.toState === 'closed') {
this.closed.next();
}
}
} }