This repository was archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
133 lines (121 loc) · 3.41 KB
/
index.js
File metadata and controls
133 lines (121 loc) · 3.41 KB
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
const {createElement, Component, createRef} = require('react')
const {css, keyframes} = require('emotion')
function translateAndScale (source, target) {
const sx = source.width / target.width
const sy = source.height / target.height
const dx = source.x - target.x
const dy = source.y - target.y
return keyframes`
from {
transform: translate(${dx}px, ${dy}px) scale(${sx}, ${sy});
}
`
}
function sameBounds (source, target) {
return !['top', 'right', 'bottom', 'left'].find(key => {
return source[key] !== target[key]
})
}
const childByFlip = new WeakMap()
module.exports = ({
transformOrigin = '0 0',
duration = 500,
timingFunction = 'ease',
delay = 0,
iterationCount = 1,
direction = 'normal',
fillMode = 'both',
playState = 'running',
keyframes = translateAndScale,
playCss = '',
extractAdditionalDomFromSource = (element) => {},
extractAdditionalDomFromTarget = (element) => {},
unmountAsSource = false
} = {}) => {
if (typeof duration !== 'number') {
throw new Error('duration must be an integer')
}
const Flip = class extends Component {
constructor (props) {
super(props)
this.containerRef = createRef()
this.saveNextFirst = () => {
const child = this.containerRef.current
childByFlip.set(Flip, {
source: child.getBoundingClientRect(),
additionalSourceProperties: extractAdditionalDomFromSource(child)
})
}
this.cleanup = () => {}
this.animate = () => {
this.cleanup()
// FIRST
const {source, additionalSourceProperties} = childByFlip.get(Flip) || {}
// LAST
const child = this.containerRef.current
const target = child.getBoundingClientRect()
// LAST IS NEXT FIRST
this.saveNextFirst()
// INVERT
if (!source) {
return
}
if (sameBounds(source, target)) {
return
}
const additionalTargetProperties = extractAdditionalDomFromTarget(child)
const animation = keyframes(
source,
target,
additionalSourceProperties,
additionalTargetProperties
)
const animationClass = css`
transform-origin: ${transformOrigin};
animation-duration: ${duration}ms;
animation-timing-function: ${timingFunction};
animation-delay: ${delay}ms;
animation-iteration-count: ${iterationCount};
animation-direction: ${direction};
animation-fill-mode: ${fillMode};
animation-play-state: ${playState};
animation-name: ${animation};
${playCss};
`
// PLAY
child.classList.add(animationClass)
const timeout = setTimeout(() => {
this.cleanup()
}, duration + 10)
this.cleanup = () => {
clearTimeout(timeout)
child.classList.remove(animationClass)
this.cleanup = () => {}
}
}
}
componentDidMount () {
this.animate()
}
componentWillUnmount () {
if (!unmountAsSource) {
return
}
this.saveNextFirst()
}
getSnapshotBeforeUpdate () {
this.saveNextFirst()
return null
}
componentDidUpdate () {
this.animate()
}
render () {
const {props: {children}, containerRef} = this
return createElement('div', {
ref: containerRef
}, children)
}
}
return Flip
}