|
1 | 1 | import * as React from 'react'; |
2 | | -import Collecter, { CommonProps, AnimationCallback } from './Collector'; |
| 2 | +import { unstable_renderSubtreeIntoContainer, unmountComponentAtNode } from 'react-dom'; |
| 3 | +import Collecter, { CommonProps, AnimationCallback, Data, Actions } from './Collector'; |
| 4 | +import * as math from '../lib/math'; |
| 5 | +import SimpleTween from './SimpleTween'; |
3 | 6 |
|
4 | 7 | interface Props extends CommonProps { |
5 | 8 | duration?: number; |
6 | 9 | } |
7 | 10 |
|
8 | 11 | export default class Move extends React.Component<Props> { |
9 | | - animate: AnimationCallback = data => { |
10 | | - console.log('starting'); |
11 | | - console.log(data); |
| 12 | + static defaultProps = { |
| 13 | + duration: 300000, |
| 14 | + }; |
12 | 15 |
|
| 16 | + // This animation will transition the from node to the to node |
| 17 | + // and the to node to the to node, with a fade in between so we |
| 18 | + // get a nice seamless transition. |
| 19 | + animate: AnimationCallback = data => { |
13 | 20 | return new Promise(resolve => { |
14 | | - setTimeout(() => { |
15 | | - console.log('finished'); |
16 | | - resolve(); |
17 | | - }, this.props.duration || 100); |
| 21 | + const x = data.toTarget.location.left - data.fromTarget.location.left; |
| 22 | + const y = data.toTarget.location.top - data.fromTarget.location.top; |
| 23 | + const xx = data.fromTarget.location.left - data.toTarget.location.left; |
| 24 | + const yy = data.fromTarget.location.top - data.toTarget.location.top; |
| 25 | + const element = document.createElement('div'); |
| 26 | + const duration = this.props.duration as number; |
| 27 | + const noTransform = 'translate3d(0, 0, 0) scale3d(1, 1, 1)'; |
| 28 | + document.body.appendChild(element); |
| 29 | + |
| 30 | + const from = { |
| 31 | + transition: `transform ${duration}ms, opacity ${duration / 2}ms`, |
| 32 | + position: 'absolute', |
| 33 | + transformOrigin: '0 0', |
| 34 | + }; |
| 35 | + |
| 36 | + unstable_renderSubtreeIntoContainer( |
| 37 | + data.caller, |
| 38 | + <> |
| 39 | + <SimpleTween |
| 40 | + duration={this.props.duration as number} |
| 41 | + from={{ |
| 42 | + ...data.fromTarget.location, |
| 43 | + ...from, |
| 44 | + transform: noTransform, |
| 45 | + opacity: 1, |
| 46 | + }} |
| 47 | + to={{ |
| 48 | + transform: `translate3d(${x}px, ${y}px, 0) scale3d(${math.percentageDifference( |
| 49 | + data.toTarget.size.width, |
| 50 | + data.fromTarget.size.width |
| 51 | + )}, ${math.percentageDifference( |
| 52 | + data.toTarget.size.height, |
| 53 | + data.fromTarget.size.height |
| 54 | + )}, 1)`, |
| 55 | + opacity: 0, |
| 56 | + }} |
| 57 | + onFinish={() => { |
| 58 | + unmountComponentAtNode(element); |
| 59 | + document.body.removeChild(element); |
| 60 | + resolve(); |
| 61 | + }} |
| 62 | + > |
| 63 | + {data.fromTarget.reactNode} |
| 64 | + </SimpleTween> |
| 65 | + |
| 66 | + <SimpleTween |
| 67 | + duration={this.props.duration as number} |
| 68 | + from={{ |
| 69 | + ...data.toTarget.location, |
| 70 | + ...from, |
| 71 | + opacity: 0, |
| 72 | + transform: `translate3d(${xx}px, ${yy}px, 0) scale3d(${math.percentageDifference( |
| 73 | + data.fromTarget.size.width, |
| 74 | + data.toTarget.size.width |
| 75 | + )}, ${math.percentageDifference( |
| 76 | + data.fromTarget.size.height, |
| 77 | + data.toTarget.size.height |
| 78 | + )}, 1)`, |
| 79 | + }} |
| 80 | + to={{ |
| 81 | + transform: noTransform, |
| 82 | + opacity: 1, |
| 83 | + }} |
| 84 | + // TODO: Add a TweenManager or something so we receive one |
| 85 | + // hook to tell us all tweens are finished. |
| 86 | + onFinish={() => {}} |
| 87 | + > |
| 88 | + {React.cloneElement(data.toTarget.reactNode as any, { |
| 89 | + // This element will have style passed down from a Baba |
| 90 | + // componet, let's remove it so we have full control. |
| 91 | + style: {}, |
| 92 | + })} |
| 93 | + </SimpleTween> |
| 94 | + </>, |
| 95 | + element |
| 96 | + ); |
18 | 97 | }); |
19 | 98 | }; |
20 | 99 |
|
21 | 100 | render() { |
22 | | - return ( |
23 | | - <Collecter |
24 | | - data={{ |
25 | | - action: 'animation', |
26 | | - payload: this.animate, |
27 | | - }} |
28 | | - > |
29 | | - {this.props.children} |
30 | | - </Collecter> |
31 | | - ); |
| 101 | + const data: Data = { |
| 102 | + action: Actions.animation, |
| 103 | + payload: this.animate, |
| 104 | + }; |
| 105 | + |
| 106 | + return <Collecter data={data}>{this.props.children}</Collecter>; |
32 | 107 | } |
33 | 108 | } |
0 commit comments