blob: 96f5565a7a1e6b9c0e8ecc45b33e7d553af07b52 (
plain)
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
|
/**
* Text special effect for Petros where each word fades in
*/
import React, { Component } from 'react'
export class PetrosText extends Component {
constructor(props) {
super(props)
this.state = { index: 0, words: [] }
this.next = this.next.bind(this)
}
componentDidUpdate(prevProps) {
if (this.props.ready && !prevProps.ready) {
this.start()
} else {
clearTimeout(this.timeout)
}
}
componentDidUnmount() {
clearTimeout(this.timeout)
}
start() {
const { perWord, text } = this.props
this.setState({
words: this.props.text.trim().split(" "),
index: -1
})
clearTimeout(this.timeout)
this.timeout = setTimeout(this.next, perWord)
}
next() {
const { timePerWord, onComplete } = this.props
const { index, words } = this.state
if (index < words.length) {
this.timeout = setTimeout(this.next, timePerWord)
this.setState({ index: index + 1 })
} else {
onComplete()
}
}
render() {
const { index, words } = this.state
return (
<div className="fade-words">
{words.map((word, i) => (
<span key={i} className={i <= index ? "visible" : ""}>{word}</span>
))}
</div>
)
}
}
|