/
App.js
109 lines (92 loc) · 2.42 KB
/
App.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
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
import React from 'react';
import ReactDOM from 'react-dom';
import './style.css';
const clips = [
{
key: 'Q',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3'
},
{
key: 'W',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3'
},
{
key: 'E',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3'
},
{
key: 'A',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3'
},
{
key: 'S',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3'
},
{
key: 'D',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3'
},
{
key: 'Z',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3'
},
{
key: 'X',
url: 'https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3'
},
{
key: 'C',
url: 'https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3'
},
];
const App = () => (
<div id="display" className="display">
<h1>Drum Machinery</h1>
{clips.map((sound, index) => (
<DrumPad text={sound.key} key={index} audio={sound.url} />
))}
</div>
);
class DrumPad extends React.Component {
constructor(props) {
super(props);
this.audio = React.createRef();
}
componentDidMount() {
this.audio.current.addEventListener('ended', (e) => {
const parent = e.target.parentNode;
parent.classList.remove('active');
});
}
playSound = () => {
this.audio.current.play();
const id = this.audio.current.id;
const parent = this.audio.current.parentNode;
parent.classList.add('active');
const display = parent.parentNode;
display.querySelector('h1').innerText = `${id} is in Effect`;
}
render() {
const { text, audio } = this.props;
return (
<div className="drum-pad" onClick={this.playSound} id={`drum-${text}`}>
{text}
<audio ref={this.audio} src={audio} className="clip" id={text} />
</div>
)
}
}
document.addEventListener('keydown', (e) => {
const id = e.key.toUpperCase();
const audio = document.getElementById(id);
if(audio) {
audio.currentTime = 0;
const parent = audio.parentNode;
parent.classList.add('active');
const display = parent.parentNode;
display.querySelector('h1').innerText = `${id} is in Effect`;
audio.play();
}
});
ReactDOM.render(<App />, document.getElementById('drum-machine'));
export default App