/
SMScene.js
320 lines (270 loc) · 7.43 KB
/
SMScene.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// @flow
import { default as React, Fragment } from 'react';
import type { Node } from 'react';
import { SMContext } from './SMController';
import { default as ScrollMagic } from 'scrollmagic';
import { TimelineMax as Timeline, TweenMax as Tween } from 'gsap/TweenMax';
import 'gsap/TextPlugin';
import animationGsap from './animation.gsap.js';
import debugAddIndicators from './debug.addIndicators.js';
animationGsap(ScrollMagic, Timeline, Tween);
debugAddIndicators(ScrollMagic);
type TweenType = {
target?: any,
duration?: number,
from?: any,
to?: any,
staggerFrom?: any,
staggerTo?: any,
stagger?: number,
onCompleteAll?: Function,
position?: number | string,
}
type Props = {
children: Node | Function,
// scene parameters
duration?: number | string,
offset?: number | string,
triggerElement?: any,
triggerHook?: any,
reverse?: boolean,
loglevel?: number,
indicators?: boolean,
enabled?: boolean,
/* setClassToggle */
classToggle?: string | Array<string>,
/* setPin */
pin?: boolean | string,
pinSettings?: {
pushFollowers?: boolean,
spacerClass?: string
},
/* setTween */
tween?: TweenType,
timeline?: {
tweens: Array<TweenType>,
delay?: number,
stagger?: number,
align?: 'sequence' | 'start' | 'normal',
repeat?: number,
repeatDelay?: number,
yoyo?: boolean,
},
}
type PropsBase = Props & {
context: any,
}
type State = {
event: string,
progress: number,
}
const getTweenFunction = (element, tween) => {
const {
target,
duration,
from,
to,
staggerFrom,
staggerTo,
stagger,
onCompleteAll,
position,
...vars
} = tween;
let tweenFunction;
const target$ = target || element;
const duration$ = duration || 1;
const stagger$ = stagger || 0;
const onCompleteAll$ = onCompleteAll || null;
if (from && to) {
tweenFunction = Tween.fromTo(target$, duration$, from, to);
}
else if (from) {
tweenFunction = Tween.from(target$, duration$, from);
}
else if (staggerFrom && staggerTo) {
tweenFunction = Tween.staggerFromTo(target$, duration$, staggerFrom, staggerTo, stagger$, onCompleteAll$);
}
else if (staggerFrom) {
tweenFunction = Tween.staggerFrom(target$, duration$, staggerFrom, stagger$, onCompleteAll$);
}
else if (staggerTo) {
tweenFunction = Tween.staggerTo(target$, duration$, staggerTo, stagger$, onCompleteAll$);
}
else {
tweenFunction = Tween.to(target$, duration$, to || vars);
}
return tweenFunction;
};
class SMSceneBase extends React.PureComponent<PropsBase, State> {
ref: HTMLElement;
scene: any;
state = {
event: 'init',
progress: 0,
}
constructor(props) {
super(props);
this.ref = React.createRef();
}
componentDidMount() {
const {
children,
classToggle,
pin,
pinSettings,
tween,
timeline,
context,
indicators,
enabled,
...sceneParams
} = this.props;
this.check(children, pin, sceneParams);
const element = this.ref.current;
sceneParams.triggerElement = sceneParams.triggerElement === null ? null : sceneParams.triggerElement || element;
this.scene = new ScrollMagic.Scene(sceneParams);
this.initEventHandlers();
if (classToggle) {
this.setClassToggle(this.scene, element, classToggle);
}
if (pin || pinSettings) {
this.setPin(this.scene, element, pin, pinSettings);
}
if (indicators) {
this.scene.addIndicators({ name: ' ' });
}
if (tween) {
this.scene.setTween(getTweenFunction(element, tween));
}
else if (timeline) {
this.setTimeline(this.scene, element, timeline);
}
this.scene.addTo(context.controller);
}
componentWillUnmount() {
this.scene.destroy();
}
check(children, pin, sceneParams) {
if (!children || (typeof children !== 'function' && children.type.displayName === 'SMScene')) {
if (pin === true) {
throw new Error('Prop pin cannot be true. Use an element or element selector if children is null or if you nest a SMScene in another SMScene.');
}
if (!sceneParams.triggerElement) {
throw new Error('You have to define a triggerElement if children is null or if you nest a SMScene in another SMScene.');
}
}
}
setClassToggle(scene, element, classToggle) {
if (Array.isArray(classToggle) && classToggle.length === 2) {
scene.setClassToggle(classToggle[0], classToggle[1]);
}
else {
scene.setClassToggle(element, classToggle);
}
}
setPin(scene, element, pin, pinSettings) {
scene.setPin(pin === true ? element : pin, pinSettings);
}
setTimeline(scene, element, timeline) {
const { tweens, ...vars } = timeline;
const timelineObject = new Timeline(vars);
if (tweens && Array.isArray(tweens)) {
tweens.forEach(tween => {
const { position } = tween;
timelineObject.add(getTweenFunction(element, tween), position || '+=0');
});
scene.setTween(timelineObject);
}
}
initEventHandlers() {
let { children } = this.props;
if (typeof children !== 'function') {
return;
}
this.scene.on('start', (event) => {
this.setState({
event: 'start'
});
});
this.scene.on('end', (event) => {
this.setState({
event: 'end'
});
});
this.scene.on('enter', (event) => {
this.setState({
event: 'enter'
});
});
this.scene.on('leave', (event) => {
this.setState({
event: 'leave'
});
});
this.scene.on('progress', (event) => {
this.setState({
progress: event.progress
});
});
}
render() {
let { children } = this.props;
const { event, progress } = this.state;
let childrenCloned = null;
if (children) {
if (typeof children === 'function') {
children = children(event, progress);
}
// only one root child component
children = React.Children.only(children);
// console.log(children, children.props.children);
let ref = null;
// HTML tag
if (typeof children.type === 'string') {
ref = { ref: this.ref };
}
// Forwarding Refs
else if (children.type.$$typeof && children.type.$$typeof.toString() === 'Symbol(react.forward_ref)') {
ref = { ref: this.ref };
}
// StyledComponent
else if (children.type.styledComponentId) {
ref = { innerRef: this.ref };
}
// SMScene
else if (children.type.displayName && children.type.displayName === 'SMScene') {
ref = { ref: this.ref };
}
// Stateful Component
else if (children.type.prototype && children.type.prototype.isReactComponent) {
// https://github.com/facebook/react/issues/11401#issuecomment-340543801
// throw new Error('Stateful components not yet supported by SMScene. Use a HTML wrapper.');
ref = null;
}
else {
// throw new Error('Stateless components not yet supported by SMScene. Use a HTML wrapper.');
ref = null;
}
childrenCloned = React.cloneElement(children, ref);
}
return (
<Fragment>
{childrenCloned}
</Fragment>
);
}
}
class SMScene extends React.PureComponent<Props, {}> {
static displayName = 'SMScene';
render() {
return (
<SMContext.Consumer>
{(context) => (
<SMSceneBase context={context} {...this.props} />
)}
</SMContext.Consumer>
);
}
}
export { SMScene };