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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML

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

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Bowlby+One+SC&display=swap');

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}
body {
  font-family: 'Bowlby One SC', cursive;
  font-size: 3rem;
  color: #FFEBD6;
  background-color: #291500;
}
#drum-machine {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
#display {
  width: 290px;
  background-color: white;
  border-radius: 5px;
  margin-bottom: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
  color: #291500;
}
#square-pads {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  justify-items: center;
  border-radius: 5px;
  text-shadow: 2px 2px 2px black;
}
.drum-pad {
  width: 90px;
  padding: 10px;
  margin: 5px;
  background-color: #F57E00;
  cursor: pointer;
  border-radius: 5px;
  text-align: center;
  border: 3px outset #F57E00;
}
.drum-pad:active {
  background-color: #754668;
  border: 3px inset #754668;
}
#volume-label {
  font-size: 2.5rem;
  margin: 10px 0 0 0;
}
input {
  width: 200px;
  accent-color: #CC6900;
}
              
            
!

JS

              
                // Global scope
const bank = [
  {
    id: 'Q',
    animal: 'Cow',
    url: 'https://cdn.freesound.org/previews/58/58277_634166-lq.mp3',
  },
  {
    id: 'W',
    animal: 'Pig',
    url: 'https://cdn.freesound.org/previews/174/174614_3249786-lq.mp3',
  },
  {
    id: 'E',
    animal: 'Horse',
    url: 'https://cdn.freesound.org/previews/419/419231_5121236-lq.mp3',
  },
  {
    id: 'A',
    animal: 'Dog',
    url: 'https://cdn.freesound.org/previews/498/498394_10816113-lq.mp3',
  },
  {
    id: 'S',
    animal: 'Cat',
    url: 'https://cdn.freesound.org/previews/415/415209_5121236-lq.mp3',
  },
  {
    id: 'D',
    animal: 'Bird',
    url: 'https://cdn.freesound.org/previews/607/607902_9789704-lq.mp3',
  },
  {
    id: 'Z',
    animal: 'Monkey',
    url: 'https://cdn.freesound.org/previews/588/588623_5661246-lq.mp3',
  },
  {
    id: 'X',
    animal: 'Lion',
    url: 'https://cdn.freesound.org/previews/571/571288_11058236-lq.mp3',
  },
  {
    id: 'C',
    animal: 'Sheep',
    url: 'https://cdn.freesound.org/previews/34/34538_118241-lq.mp3',
  },
];


class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      display: '',
      volume: 0.5
    }
    this.updateDisplay = this.updateDisplay.bind(this);
    this.handleVolume = this.handleVolume.bind(this);
  }
  updateDisplay(animal) {
    this.setState({
      display: animal
    })
  };
  handleVolume(e) {
    this.setState({
      volume: e.target.value
    })
    document.getElementsByClassName('clip').forEach(item => item.volume = this.state.volume)
  }
  render() {
    // The map method iterate for each value of an array, without modifying it.
    // Every SinglePad is rendered with different properties
    let squarePads = bank.map((currValue) => {
      return (
        <SinglePad id={currValue.id}
          url={currValue.url}
          animal={currValue.animal}
          updateDisplay={this.updateDisplay}
        />
      )
    });
    return (
      // squarePads is an array of items.
      // Each items is a <SinglePad>, each one with same props but different values
      <div id="drum-machine">
        <div id="display">{this.state.display}</div>
        <div id="square-pads">{squarePads}</div>
        <label id="volume-label" for="volume">Volume</label>
        <input id="volume-input" name="volume"
          type="range" min="0" max="1" step="0.1"
          onChange={this.handleVolume} />
      </div>
    )
  }
}
class SinglePad extends React.Component {
  constructor(props) {
    super(props);
    this.playSound = this.playSound.bind(this);
    this.handleKeyDown = this.handleKeyDown.bind(this);
  }
  componentDidMount() {
    document.addEventListener('keydown', this.handleKeyDown);
  }
  handleKeyDown(e) {
    if (e.key.toUpperCase() === this.props.id) 
    { 
      this.playSound();
    }
  }
  playSound() {
    const sound = document.getElementById(this.props.id);
    sound.play();
    // When the sound is played, update the display string
    this.props.updateDisplay(this.props.animal);
  }
  render() {
    return(
      <div className="drum-pad"
        id={this.props.animal}
        onClick={this.playSound}
        tabIndex='0' // Necessary to use onKeyDown with a <div> element
        onKeyDown={this.handleKeyDown}>
        {this.props.id}
        <audio id={this.props.id}
          className="clip"
          src={this.props.url}>
        </audio>
      </div>
    )
  }
}
ReactDOM.render(<App></App>, document.getElementById("app"))
              
            
!
999px

Console