Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <div id="root"></div>
            
          
!

CSS

            
              * {
  margin: 0;
  padding: 0;
  background-color: black;
  color: white;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  font-family: 'Lato', sans-serif;
}

#root {
  max-width: 1200px;
  margin: 20px auto;
}

#drum-machine {
  margin: 0 auto;
  max-width: 90vw;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}

#drum-machine:focus {
  outline: 0;
}

#display, .info, .footer {
  font-size: 26px;
  line-height: 46px;
  width: 100%;
  height: 50px;
  margin: 10px 0 0;
  text-align: center;
  color: aqua;
}

.title {
  font-size: 30px;
  margin-bottom: 10px;
  padding: 10px 0;
  text-align: center;
  width: 100%;
}

.title-text {
  color: aqua;
  font-weight: bold;
  -webkit-text-stroke: 1px black;
  text-shadow: 0 0 6px aqua;
}

.info {
  height: 90px;
  line-height: 30px;
  font-size: 20px;
  padding: 13px 0px;
  margin: 0;
}

.drum-pads {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
      flex-wrap: wrap;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -ms-flex-line-pack: center;
      align-content: center;
  width: 190px;
  height: 190px;
  border: 2px solid black;
}

.drum-pad {
  display: inline-block;
  width: 50px;
  height: 50px;
  background-color: white;
  color: black;
  text-align: center;
  border: 2px solid black;
  font-size: 26px;
  font-weight: bold;
  line-height: 46px;
  margin: 5px;
  border-radius: 5px;
  -webkit-box-shadow: 0 0 3px 1px aqua, 2px 2px 3px 1px aqua;
          box-shadow: 0 0 3px 1px aqua, 2px 2px 3px 1px aqua;
  -webkit-transition: -webkit-box-shadow 0.2s;
  transition: -webkit-box-shadow 0.2s;
  transition: box-shadow 0.2s;
  transition: box-shadow 0.2s, -webkit-box-shadow 0.2s;
}

.drum-pad:active {
  -webkit-box-shadow: 0 0 4px 2px #80ffff, 2px 2px 4px 2px #80ffff;
          box-shadow: 0 0 4px 2px #80ffff, 2px 2px 4px 2px #80ffff;
}

.drum-pad:hover {
  cursor: pointer;
}

.drum-pad span {
  background-color: white;
  color: black;
}

.footer {
  text-align: center;
  color: white;
  font-size: 15px;
  padding: 10px;
}

@media (min-width: 401px) {
  #drum-machine {
    width: 400px;
  }
  .title, .footer {
    width: 100%;
  }
  #display {
    margin-top: 15px;
  }
  .row {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
        -ms-flex-direction: row;
            flex-direction: row;
    -webkit-box-pack: justify;
        -ms-flex-pack: justify;
            justify-content: space-between;
    width: 100%;
  }
  .info {
    margin-top: 15px;
  }
}

            
          
!

JS

            
              class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      sounds: null,
      display: ''
    };
    this.keyboard = React.createRef();
  }

  componentDidMount() {
    this.keyboard.current.focus();
    let newSounds =[];
    const sounds = document.querySelectorAll('audio');
    for (let audio of sounds) { newSounds.push(audio); }
    this.setState({sounds: newSounds});
  }

  handleClick = (e) => {
    let pad = e.target.id;
    if (pad !== '') {
      this.state.sounds[pad].play();
      let url = this.state.sounds[pad].src;
      let nameToDisplay = url.substr(44, url.length - 44 - 4);
      this.setState({ display : nameToDisplay });
    }
  }

  handleKey = (e) => {
    let key = e.key.toUpperCase();
    let soundToPlay = this.state.sounds.filter((sound)=>{
      let id = sound.getAttribute('id');
      if (id === key) return true;
      return false;
    });
    if (soundToPlay.length !== 0) {
      soundToPlay[0].play();
      let url = soundToPlay[0].src;
      let nameToDisplay = url.substr(44, url.length - 44 - 4);
      this.setState({ display : nameToDisplay });
    }
  }

  render() {
    return (
      <div id="drum-machine" onKeyPress={(e)=>this.handleKey(e)} tabIndex="0" ref={this.keyboard}>
        <div className="title"><span className="title-text">The Drum Machine</span></div>
        <div className="row">
          <div className="drum-pads" onClick={this.handleClick} >
            <div className="drum-pad" id="0">Q
              <audio className="clip" id="Q" src="https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3" />
            </div>
            <div className="drum-pad" id="1">W
              <audio className="clip" id="W" src="https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3" />
            </div>
            <div className="drum-pad" id="2">E
              <audio className="clip" id="E" src="https://s3.amazonaws.com/freecodecamp/drums/Bld_H1.mp3" />
            </div>
            <div className="drum-pad" id="3">A
              <audio className="clip" id="A" src="https://s3.amazonaws.com/freecodecamp/drums/Give_us_a_light.mp3" />
            </div>
            <div className="drum-pad" id="4">S
              <audio className="clip" id="S" src="https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3" />
            </div>
            <div className="drum-pad" id="5">D
              <audio className="clip" id="D" src="https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3" />
            </div>
            <div className="drum-pad" id="6">Z
              <audio className="clip" id="Z" src="https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3" />
            </div>
            <div className="drum-pad" id="7">X
              <audio className="clip" id="X" src="https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3" />
            </div>
            <div className="drum-pad" id="8">C
              <audio className="clip" id="C" src="https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3" />
            </div>
          </div>
          <div>
            <div id="display">{this.state.display}</div>
            <div className="info">Click a drum pad <br /> or press a key</div>
          </div>
        </div>
        <footer className="footer">Created by&nbsp;
          <a className="link" href="http://mybytes.pl/" target="_blank" rel="noopener">Sebastian Sporek</a>
        </footer>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

            
          
!
999px

Console