Skip to content

Instantly share code, notes, and snippets.

@RafalFilipek
Last active October 3, 2017 04:02
Show Gist options
  • Save RafalFilipek/7cd7363f203a0778ebd5ca5b7d752517 to your computer and use it in GitHub Desktop.
Save RafalFilipek/7cd7363f203a0778ebd5ca5b7d752517 to your computer and use it in GitHub Desktop.
import Loader from './Loader'
// Simple ...
<Loader />
// Custom text
<Loader text="Wait for it" />
// Custom sign
<Loader sign="❤" />
// You can change max number of `dots`
<Loader max={10} />
// To slow? To fast?
<Loader interval={100} />
import React, { PropTypes } from 'react'
import { pure, mapProps, compose, withState, lifecycle, setDisplayName, setPropTypes, defaultProps, onlyUpdateForPropTypes } from 'recompose'
import { repeat } from 'lodash'
export default pure(
compose(
setDisplayName('Loader'),
onlyUpdateForPropTypes,
setPropTypes({
sign: PropTypes.string,
interval: PropTypes.number,
max: PropTypes.number,
text: PropTypes.string
}),
defaultProps({
sign: '.',
interval: 200,
max: 3,
text: 'Loading'
}),
withState('dotsCount', 'setDotsCount', 1),
withState('timer', 'setTimer', null),
mapProps(
({ setDotsCount, max, ...rest }) => ({
tick: () => setDotsCount(n => (n + 1) % (max + 1)),
...rest
})
),
lifecycle(
(c) => setTimeout(() => c.props.setTimer(setInterval(c.props.tick, c.props.interval)), 0),
(c) => clearInterval(c.props.timer)
)
)(({ text, sign, dotsCount }) => <div>{ text } { repeat(sign, dotsCount) }</div>)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment