diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2021-09-09 15:45:04 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2021-09-09 15:45:04 +0200 |
| commit | 1e3aa75d5e57bee815b2812a5a539599af20c3b6 (patch) | |
| tree | 566593ba857a5e69fdd9e1003fc08f32e6c6b22d /frontend/site/projects/museum/views/petros.text.js | |
| parent | 2505269b350be255881b9498a92a6f0760ab56b3 (diff) | |
words fade in component
Diffstat (limited to 'frontend/site/projects/museum/views/petros.text.js')
| -rw-r--r-- | frontend/site/projects/museum/views/petros.text.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/frontend/site/projects/museum/views/petros.text.js b/frontend/site/projects/museum/views/petros.text.js new file mode 100644 index 0000000..96f5565 --- /dev/null +++ b/frontend/site/projects/museum/views/petros.text.js @@ -0,0 +1,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> + ) + } +} |
