-
Notifications
You must be signed in to change notification settings - Fork 2k
/
StopWatch.jsx
107 lines (90 loc) · 2.21 KB
/
StopWatch.jsx
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
import { h, Component } from 'preact'
class StopWatch extends Component {
constructor (props) {
super(props)
this.state = { elapsedTime: 0 }
this.wrapperStyle = {
width: '100%',
height: '100%',
display: 'flex',
}
this.overlayStyle = {
position: 'absolute',
width: '100%',
height: '100%',
background: 'black',
opacity: 0.7,
}
this.infoContainerStyle = {
marginLeft: 'auto',
marginRight: 'auto',
marginTop: 'auto',
marginBottom: 'auto',
zIndex: 1,
color: 'white',
}
this.infotextStyle = {
marginLeft: 'auto',
marginRight: 'auto',
marginBottom: '1rem',
fontSize: '1.5rem',
}
this.timeStyle = {
display: 'block',
fontWeight: 'bold',
marginLeft: 'auto',
marginRight: 'auto',
fontSize: '3rem',
fontFamily: 'Courier New',
}
}
startTimer () {
this.timerTick()
this.timerRunning = true
}
resetTimer () {
clearTimeout(this.timer)
this.setState({ elapsedTime: 0 })
this.timerRunning = false
}
timerTick () {
this.timer = setTimeout(() => {
this.setState(state => ({ elapsedTime: state.elapsedTime + 1 }))
this.timerTick()
}, 1000)
}
// eslint-disable-next-line class-methods-use-this
fmtMSS (s) {
// eslint-disable-next-line no-return-assign, no-param-reassign
return (s - (s %= 60)) / 60 + (s > 9 ? ':' : ':0') + s
}
render () {
const { recording, i18n } = { ...this.props }
const { elapsedTime } = this.state
// second to minutes and seconds
const minAndSec = this.fmtMSS(elapsedTime)
if (recording && !this.timerRunning) {
this.startTimer()
}
if (!recording && this.timerRunning) {
this.resetTimer()
}
if (recording) {
return (
<div style={this.wrapperStyle}>
<div style={this.overlayStyle} />
<div style={this.infoContainerStyle}>
<div style={this.infotextStyle}>
{i18n('recording')}
</div>
<div style={this.timeStyle}>
{minAndSec}
</div>
</div>
</div>
)
}
return null
}
}
export default StopWatch