-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathReactRotatingText.js
More file actions
203 lines (178 loc) · 5.13 KB
/
ReactRotatingText.js
File metadata and controls
203 lines (178 loc) · 5.13 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
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
var React = require('react');
var PropTypes = require('prop-types');
var toArray = require('lodash.toarray');
class ReactRotatingText extends React.Component {
constructor(props) {
super(props);
const { items, random } = this.props;
this.state = {
index: random ? Math.floor(Math.random() * Math.floor(items.length)) : 0,
output: '',
substrLength: 0,
count: 0
};
this.timeouts = [];
}
componentDidMount() {
this._animate.bind(this)(); // begin the animation loop
}
componentWillUnmount() {
this.timeouts.map(x => clearTimeout(x)); // stop all the loops
}
_loop(loopingFunc, pause) {
if(this.state.count == this.props.count) return;
// save the timeouts so we can stop on unmount
const timeout = setTimeout(loopingFunc, pause);
this.timeouts.push(timeout);
// prevent memory leak
const maxTimeouts = 100;
if (this.timeouts.length > maxTimeouts) {
clearTimeout(this.timeouts[0]);
this.timeouts.shift();
}
}
_type(text, callback) {
const { output } = this.state;
const { typingInterval } = this.props;
const loopingFunc = this._type.bind(this, text, callback);
const word = toArray(text)
// set the string one character longer
this.setState({output: word.slice(0, toArray(output).length + 1).join('')});
// if we're still not done, recursively loop again
if (output.length < word.length) {
this._loop(loopingFunc, typingInterval);
}
else {
this.setState({
count: this.state.count + 1
});
if(this.props.count){
if(this.props.count == this.state.count){
this.timeouts.map(x => clearTimeout(x));
this.timeouts = [];
console.log("done");
}
}
if (typeof this.props.onTypingEnd == 'function') {
this.props.onTypingEnd();
}
callback();
}
}
_erase(callback) {
const { output } = this.state;
const { deletingInterval } = this.props;
const loopingFunc = this._erase.bind(this, callback);
const word = toArray(output)
if (typeof this.props.onDeletingStart == 'function') {
this.props.onDeletingStart();
}
// set the string one character shorter
this.setState({output: word.slice(0, word.length - 1).join('')});
// if we're still not done, recursively loop again
if (word.length !== 0) {
this._loop(loopingFunc, deletingInterval);
} else {
if (typeof this.props.onDeletingEnd == 'function') {
this.props.onDeletingEnd();
}
callback();
}
};
_overwrite(text, callback) {
const { output, substrLength } = this.state;
const { deletingInterval } = this.props;
const loopingFunc = this._overwrite.bind(this, text, callback);
const word = toArray(text)
const out = toArray(output)
this.setState({
output: word.slice(0, substrLength).concat(out.slice(substrLength)),
substrLength: substrLength + 1,
});
if (word.length !== substrLength) {
this._loop(loopingFunc, deletingInterval);
} else {
this.setState({
output: text,
substrLength: 0,
});
callback();
}
};
_animate() {
const { index } = this.state;
const { items, pause, emptyPause, eraseMode, random } = this.props;
const type = this._type;
const erase = this._erase;
const overwrite = this._overwrite;
const loopingFunc = this._animate.bind(this);
let nextIndex;
if (random) {
nextIndex = Math.floor(Math.random() * Math.floor(items.length));
} else {
nextIndex = index === items.length - 1 ? 0 : index + 1;
}
const nextWord = () => {
this.setState({index: nextIndex});
this._loop(loopingFunc, emptyPause);
};
if (typeof this.props.onTypingStart == 'function') {
this.props.onTypingStart();
}
type.bind(this)(items[index], () => {
if (eraseMode === 'overwrite') {
this._loop(overwrite.bind(this, items[nextIndex], nextWord), pause);
} else {
this._loop(erase.bind(this, nextWord), pause);
}
});
};
render() {
const {
color,
cursor,
deletingInterval,
emptyPause,
items,
pause,
eraseMode,
typingInterval,
random,
...other
} = this.props;
return (
<span style={{ color }} {...other}
aria-label={ this.props.items[this.state.index] }>>
{ this.state.output }
{ cursor ? <span className="react-rotating-text-cursor">|</span> : null }
</span>
);
}
}
ReactRotatingText.propTypes = {
color: PropTypes.string,
cursor: PropTypes.bool,
deletingInterval: PropTypes.number,
emptyPause: PropTypes.number,
eraseMode: PropTypes.string,
items: PropTypes.array,
pause: PropTypes.number,
typingInterval: PropTypes.number,
random: PropTypes.bool,
onTypingStart: PropTypes.func,
onTypingEnd: PropTypes.func,
onDeletingStart: PropTypes.func,
onDeletingEnd: PropTypes.func,
};
ReactRotatingText.defaultProps = {
color: 'inherit',
cursor: true,
deletingInterval: 50,
emptyPause: 1000,
eraseMode: 'erase',
items: ['first', 'second', 'third', 'fourth', 'fifth'],
pause: 1500,
typingInterval: 50,
random: false
};
export default ReactRotatingText;