Created
May 8, 2018 03:00
-
-
Save zoxon/151bb437e7a0bed2b6706aa5fa482aac to your computer and use it in GitHub Desktop.
React text loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
const DEFAULT_LOADER_CHARACTERS = '\\|/—'; // ⣾⣽⣻⢿⡿⣟⣯⣷ | |
const DEFAULT_LOADER_SPEED_MS = 100; | |
const LOADER_STYLE = { | |
fontFamily: 'monospace', | |
}; | |
export default class Loader extends React.Component { | |
static propTypes = { | |
characters: PropTypes.string, | |
speed: PropTypes.number, | |
}; | |
static defaultProps = { | |
characters: DEFAULT_LOADER_CHARACTERS, | |
speed: DEFAULT_LOADER_SPEED_MS, | |
}; | |
constructor(props) { | |
super(props); | |
this.state = {tick: 0}; | |
} | |
componentWillMount() { | |
this.interval = setInterval(this.nextStep, this.props.speed); | |
} | |
componentWillUnmount() { | |
clearInterval(this.interval); | |
} | |
nextStep = () => { | |
this.setState({ | |
tick: (this.state.tick + 1) % this.props.characters.length, | |
}); | |
}; | |
render() { | |
return ( | |
<span style={LOADER_STYLE}> | |
{this.props.characters[this.state.tick]} LOADING | |
</span> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment