-
Notifications
You must be signed in to change notification settings - Fork 90
/
animated-container.js
152 lines (132 loc) · 3.82 KB
/
animated-container.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { alias } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { Resize } from '../motions/resize';
import { task } from '../-private/ember-scheduler';
import Sprite from '../-private/sprite';
import { afterRender, microwait } from '..';
/**
Provides a boundary between animator components and the surrounding document
which smoothly resizes as animators change. Use animated-container whenever you
need to "hold a place for" some animated content while that content is animating.
```hbs
<button {{action toggleThing}}>Toggle</button>
{{#animated-container}}
{{#animated-if showThing use=transition }}
<div class="message" {{action "toggleThing"}}>
Hello!
</div>
{{/animated-if}}
{{/animated-container}}
<p>
This is outside of the container.
</p>
```
```js
import Component from '@ember/component';
import move from 'ember-animated/motions/move';
import {easeOut, easeIn } from 'ember-animated/easings/cosine';
export default Component.extend({
showThing: false,
toggleThing() {
this.set('showThing', !this.get('showThing'));
},
transition: function * ({ insertedSprites, keptSprites, removedSprites }) {
insertedSprites.forEach(sprite => {
sprite.startAtPixel({ x: window.innerWidth });
move(sprite, { easing: easeOut });
});
keptSprites.forEach(move);
removedSprites.forEach(sprite => {
sprite.endAtPixel({ x: window.innerWidth });
move(sprite, { easing: easeIn });
});
},
});
```
@class animated-container
@public
*/
export default Component.extend({
classNames: ['animated-container'],
motionService: service('-ea-motion'),
/**
* Whether to animate the initial render. You will probably also need to set
* initialInsertion=true on a child component of animated-container.
* Defaults to false.
@argument onInitialRender
@type Boolean
*/
onInitialRender: false,
init() {
this._super();
this._signals = null;
this._signalPromise = null;
this._signalResolve= null;
this._inserted = false;
this._startingUp = false;
this.maybeAnimate = this.maybeAnimate.bind(this);
this.sprite = null;
this.get('motionService')
.register(this)
.observeDescendantAnimations(this, this.maybeAnimate);
},
didInsertElement() {
this._inserted = true;
},
willDestroyElement() {
this.get('motionService')
.unregister(this)
.unobserveDescendantAnimations(this, this.maybeAnimate);
},
isAnimating: alias('animate.isRunning'),
maybeAnimate({ duration, task }) {
if (!this._startingUp) {
this.get('animate').perform(duration, task);
}
},
beginStaticMeasurement() {
if (this.sprite) {
this.sprite.unlock();
}
},
endStaticMeasurement() {
if (this.sprite) {
this.sprite.lock();
}
},
animate: task(function * (duration, animationTask) {
this._startingUp = true;
let service = this.get('motionService');
let sprite;
let useMotion;
if (this._inserted){
sprite = Sprite.sizedStartingAt(this.element);
this.sprite = sprite;
sprite.lock();
useMotion = true;
} else {
useMotion = this.get('onInitialRender');
}
try {
yield afterRender();
yield microwait();
} finally {
this._startingUp = false;
}
yield * service.staticMeasurement(() => {
if (!sprite) {
sprite = Sprite.sizedEndingAt(this.element);
this.sprite = sprite;
} else {
sprite.measureFinalBounds();
}
});
if (useMotion) {
yield* new (this.motion || Resize)(this.sprite, { duration })._run();
}
yield animationTask;
this.sprite.unlock();
this.sprite = null;
}).restartable()
});