summaryrefslogtreecommitdiff
path: root/frontend/site/projects/museum/views/petros.text.js
blob: e28eb7a0266bca2a3a5ecf8a82df4e3f93ce4f35 (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 default class PetrosText extends Component {
  constructor(props) {
    super(props)
    this.state = { index: -1, words: [] }
    this.next = this.next.bind(this)
  }

  componentDidUpdate(prevProps) {
    if (this.props.ready && !prevProps.ready) {
      this.start()      
    } else if (!this.props.ready) {
      clearTimeout(this.timeout)
    }
  }

  componentDidUnmount() {
    clearTimeout(this.timeout)
  }

  start() {
    const { timePerWord, text } = this.props
    this.setState({
      words: this.props.text.trim().split(" "),
      index: -1
    })
    clearTimeout(this.timeout)
    this.timeout = setTimeout(this.next, timePerWord)
  }
  
  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>
    )
  }
}