-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.tsx
144 lines (122 loc) · 4.03 KB
/
index.tsx
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
import React, { Component, ReactInstance } from 'react'
interface Events {
[index:string]: string[]
}
const events: Events = {
start: ['animationstart', 'webkitAnimationStart', 'mozAnimationStart', 'oanimationstart', 'MSAnimationStart'],
end: ['animationend', 'webkitAnimationEnd', 'mozAnimationEnd', 'oanimationend', 'MSAnimationEnd'],
startRemoved: [],
endRemoved: []
}
export interface Props {
children: any,
animate: boolean,
baseClassName: string,
animationClassName: string,
customTag?: string,
onAnimationEnd?: () => void,
}
export interface State {
animating: boolean,
clearAnimationClass: boolean
}
interface AnimateOnChange {
elm: HTMLElement,
setElementRef: (ref: HTMLElement) => void
}
/**
* # AnimateOnChange component.
* Adds `animationClassName` when `animate` is true, then removes
* `animationClassName` when animation is done (event `animationend` is
* triggered).
*
* @prop {string} baseClassName - Base class name.
* @prop {string} animationClassName - Class added when `animate == true`.
* @prop {bool} animate - Wheter to animate component.
*/
class AnimateOnChange extends Component<Props & any, State> implements AnimateOnChange {
constructor (props: Props) {
super(props)
this.state = { animating: false, clearAnimationClass: false }
this.animationStart = this.animationStart.bind(this)
this.animationEnd = this.animationEnd.bind(this)
this.setElementRef = (ref) => {
this.elm = ref
}
}
componentDidMount () {
this.addEventListener('start', this.elm, this.animationStart)
this.addEventListener('end', this.elm, this.animationEnd)
}
componentWillUnmount () {
this.removeEventListeners('start', this.elm, this.animationStart)
this.removeEventListeners('end', this.elm, this.animationEnd)
}
addEventListener (type: string, elm: HTMLElement, eventHandler: (e: Event) => void) {
// until an event has been triggered bind them all
events[type].map(event => {
// console.log(`adding ${event}`)
// @ts-ignore
elm.addEventListener(event, eventHandler)
})
}
removeEventListeners (type: string, elm: HTMLElement, eventHandler: (e: Event) => void) {
events[type].map(event => {
// console.log(`removing ${event}`)
// @ts-ignore
elm.removeEventListener(event, eventHandler)
})
}
updateEvents (type: string, newEvent: string) {
// console.log(`updating ${type} event to ${newEvent}`)
events[type + 'Removed'] = events[type].filter(e => e !== newEvent)
events[type] = [newEvent]
}
animationStart (e: Event) {
if (events['start'].length > 1) {
this.updateEvents('start', e.type)
this.removeEventListeners('startRemoved', this.elm, this.animationStart)
}
this.setState({ animating: true, clearAnimationClass: false })
}
animationEnd (e: Event) {
if (events['end'].length > 1) {
this.updateEvents('end', e.type)
this.removeEventListeners('endRemoved', this.elm, this.animationStart)
}
// send separate, animation state change will not render
this.setState({ clearAnimationClass: true }) // renders
this.setState({ animating: false, clearAnimationClass: false })
if (typeof this.props.onAnimationEnd === 'function') {
this.props.onAnimationEnd()
}
}
shouldComponentUpdate (nextProps: Props, nextState: State) {
if (this.state.animating !== nextState.animating) {
// do not render on animation change
return false
}
return true
}
render () {
const { clearAnimationClass } = this.state;
const {
baseClassName,
animate,
animationClassName,
customTag,
children,
onAnimationEnd, // unpack, such that otherProps does not contain it
...otherProps
} = this.props;
let className = baseClassName
if (animate && !clearAnimationClass) {
className += ` ${animationClassName}`
}
let Tag = customTag || 'span';
return <Tag ref={this.setElementRef} className={className} {...otherProps}>
{children}
</Tag>
}
}
export default AnimateOnChange