Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 22x 22x 22x 22x 22x 22x 22x 18x 1x 1x 38x 38x 2x 2x 3x 3x 3x 3x 3x 4x 4x 4x 21x 21x 3x 18x 18x 18x 18x 18x 5x 5x 1x 4x 4x 4x 4x 25x 25x 20x 5x 5x 5x 5x 5x 5x 1x 1x 1x 60x 60x 60x 60x 57x 3x 60x 60x 60x 60x 60x 60x 60x 60x 3x 3x | import React from 'react' import PropTypes from 'prop-types' import styled from 'styled-components' import { timeFormatterSeconds } from './timeFormatters' import { unitFormatterSeconds } from './unitFormatters' class Countdown360 extends React.Component { constructor (props) { super(props) const seconds = Math.max(props.seconds, 0) this.state = { totalSeconds: seconds, secondsLeft: seconds * 1000, started: false, lastTick: null } this.interval = null this.handleTick = this.handleTick.bind(this) } componentDidMount () { const { autoStart } = this.props if (autoStart) { this.start() } } componentWillUnmount () { clearInterval(this.interval) this.interval = null } componentDidUpdate (prevProps) { const { smooth } = this.props if (prevProps.smooth !== smooth) { clearInterval(this.interval) this.interval = setInterval(this.handleTick, smooth ? 16 : 1000) } } extendTimer (value) { let { secondsLeft, totalSeconds } = this.state totalSeconds += value totalSeconds = Math.max(totalSeconds, 0) secondsLeft = Math.min(secondsLeft, totalSeconds * 1000) this.setState({ secondsLeft, totalSeconds }) } addSeconds (value) { const { secondsLeft, totalSeconds } = this.state value *= 1000 this.setState({ secondsLeft: Math.max(Math.min(secondsLeft + value, totalSeconds * 1000), 0) }) } start () { const { secondsLeft, started } = this.state if (started || secondsLeft <= 0) { return } const { smooth } = this.props const timerInterval = smooth ? 16 : 1000 this.handleTick() this.interval = setInterval(this.handleTick, timerInterval) this.setState({ started: true }) } stop () { const { started } = this.state if (!started) { return } clearInterval(this.interval) this.interval = null this.handleTick() this.setState({ started: false, lastTick: null }) } handleTick () { const { secondsLeft, started } = this.state if (!started || secondsLeft <= 0) { return } const now = new Date() const lastTick = this.state.lastTick || now const diff = Math.abs(now - lastTick) const updatedSecondsLeft = Math.max(secondsLeft - diff, 0) this.setState({ lastTick: now, secondsLeft: updatedSecondsLeft }, () => { if (updatedSecondsLeft <= 0) { const { onComplete } = this.props onComplete && onComplete() this.stop() } }) } render () { const { backgroundColor, borderUnfillColor, borderFillColor, borderWidth, smooth, width } = this.props const { fontColor, fontFamily, fontSize, fontWeight, timeFormatter, unitFormatter } = this.props const { secondsLeft, totalSeconds } = this.state let angle if (!smooth) { angle = (Math.round(secondsLeft / 1000) / totalSeconds) * 360 } else { angle = (secondsLeft / (totalSeconds * 1000)) * 360 } const x = angle >= 180 ? (90 - (360 - angle)) : 90 const y = angle >= 180 ? 90 : (-90 + angle) const Wrapper = styled.div` align-items: center; background-image: linear-gradient(${x}deg, ${angle >= 180 ? borderFillColor : borderUnfillColor} 50%, transparent 50%), linear-gradient(${y}deg, ${borderUnfillColor} 50%, ${borderFillColor} 50%); border-radius: 50%; display: flex; height: ${width}px; justify-content: center; width: ${width}px;` const Label = styled.div` align-items: center; background: ${backgroundColor}; border-radius: 50%; box-sizing: border-box; color: ${fontColor}; display: flex; flex-flow: column; font-family: ${fontFamily}; font-size: ${fontSize}px; font-weight: ${fontWeight}; height: ${width - 2 * borderWidth}px; justify-content: center; text-align: center; text-transform: uppercase; width: ${width - 2 * borderWidth}px;` const Unit = styled.div` text-transform: uppercase; font-size: ${fontSize / 3}px;` const value = timeFormatter(secondsLeft) const unit = unitFormatter(value) return ( <Wrapper> <Label> {value}<br /> <Unit>{unit}</Unit> </Label> </Wrapper> ) } } Countdown360.defaultProps = { autoStart: true, backgroundColor: '#fff', borderFillColor: '#f11', borderUnfillColor: '#e6e2e7', borderWidth: 20, fontColor: '#111', fontFamily: 'sans-serif', fontSize: 45, fontWeight: 700, smooth: false, timeFormatter: timeFormatterSeconds, unitFormatter: unitFormatterSeconds, width: 200 } Countdown360.propTypes = { autoStart: PropTypes.bool, backgroundColor: PropTypes.string, borderFillColor: PropTypes.string, borderUnfillColor: PropTypes.string, borderWidth: PropTypes.number, fontColor: PropTypes.string, fontFamily: PropTypes.string, fontSize: PropTypes.number, fontWeight: PropTypes.number, onComplete: PropTypes.func, seconds: PropTypes.number.isRequired, smooth: PropTypes.bool, timeFormatter: PropTypes.func, unitFormatter: PropTypes.func, width: PropTypes.number } export default Countdown360 |