-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathStep.js
65 lines (58 loc) · 1.43 KB
/
Step.js
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
import React, { useContext } from 'react';
import styled, { keyframes, css } from 'styled-components';
import StepContext from './StepContext';
const flash = keyframes`
0% {
opacity: 0.5;
}
50% {
opacity: 1;
}
100% {
opacity: 0.5
}
`;
const flashMixin = css`
animation: ${flash} 0.5s linear infinite;
`;
const StepButton = styled.button`
flex: 1;
background: ${props => (props.offsetColor ? '#25CCF7' : '#FD7272')};
opacity: ${props => (props.on ? 1 : 0.35)};
border-radius: 2;
margin: 2px;
${props => props.doubled && flashMixin}
`;
const isOffsetColor = index =>
(index > 3 && index < 8) || (index > 11 && index < 16);
export default React.memo(function Step({ on, index, name, doubled }) {
const context = useContext(StepContext);
function toggleStep(e) {
let shiftEnabled = e.shiftKey === true;
const { machineId, state, setSteps, updateDrumMachine } = context
let steps = [...state[name]];
let val =
steps[index] === 0
? shiftEnabled
? 2
: 1
: shiftEnabled && steps[index] === 1
? 2
: 0;
steps[index] = val;
const newBeats = {
...state,
[name]: steps,
}
updateDrumMachine(newBeats, machineId)
setSteps(newBeats)
}
return (
<StepButton
on={on}
offsetColor={isOffsetColor(index)}
doubled={doubled}
onClick={(e) => toggleStep(e)}
/>
);
});