Skip to content

Instantly share code, notes, and snippets.

@zoxon
Created May 8, 2018 03:00
Show Gist options
  • Save zoxon/151bb437e7a0bed2b6706aa5fa482aac to your computer and use it in GitHub Desktop.
Save zoxon/151bb437e7a0bed2b6706aa5fa482aac to your computer and use it in GitHub Desktop.
React text loader
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