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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>

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

              
            
!

CSS

              
                $dark-color: #2d3436;
$white-color: #fff;
$green-color: #00b894;
$blue-color: #0984e3;
$red-color: #d63031;

*,
*::after,
*::before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 62.5%;
}

body {
  background-image: linear-gradient(to top, #00c6fb 0%, #005bea 100%);
}

#root {
  font-family: "Roboto", sans-serif;
  font-weight: 400;
  font-size: 1.6rem;
  height: 100vh;
  width: 100%;
  display: grid;
  justify-content: center;
  align-items: center;
}

.drum_machine {
  background-color: $dark-color;
  color: $white-color;
  width: 50rem;
  border-radius: 5px;
  padding: 3rem;
  box-shadow: 20px 20px 5px 0px rgba(0,0,0,0.75);

  &__header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    &--text {
      text-transform: uppercase;
      letter-spacing: 2px;
    }

    &--power {
      width: 3.5rem;
      height: 3.5rem;
      border: 2px solid $green-color;
      color: $green-color;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 50%;
      cursor: pointer;
      
      &.red {
        border: 2px solid $red-color;
        color: $red-color;
      }
    }
  }

  &__display {
    margin-top: 3rem;
    height: 20rem;
    border: 2px solid #555;
    border-radius: 5px;
    padding: 2rem;
    color: $white-color;
    font-family: "Press Start 2P", cursive;

    &--screen {
      background-color: $blue-color;
      height: 100%;
      border-radius: 5px;
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      grid-template-rows: 2fr 1fr;
      text-transform: uppercase;
      font-size: 1.2rem;
      border: 1px solid #aaa;

      & > * {
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }

    &--center {
      grid-column: span 2;
      border-bottom: 1px solid #aaa;
    }

    &--left {
      border-right: 1px solid #aaa;
    }
  }

  &__pad {
    margin: 3rem 0;
    border: 2px solid #555;
    border-radius: 5px;
    padding: 2rem;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 2rem;

    &--button {
      background: $green-color;
      color: $dark-color;
      border: none;
      output: none;
      height: 6rem;
      border-radius: 5px;
      font-size: 2rem;
      font-weight: 700;
      cursor: pointer;
    }
  }

  &__footer {
    border: 2px solid #555;
    border-radius: 5px;
    padding: 2rem;

    &--volume {
      display: grid;
      grid-template-columns: 1fr 4fr;
      align-items: center;
    }

    &--icon {
      font-size: 2.5rem;
      color: $green-color;
    }

    &--buttons {
      margin-top: 2rem;
      display: flex;
      justify-content: space-evenly;
    }

    &--button {
      width: 10rem;
      height: 4rem;
      border: none;
      outline: none;
      border-radius: 5px;
      background: $green-color;
      color: $dark-color;
      font-weight: 700;
      text-transform: uppercase;
      cursor: pointer;
    }
  }
}

input[type="range"] {
  -webkit-appearance: none;
  margin: 18px 0;
  width: 100%;
}

input[type="range"]:focus {
  outline: none;
}

input[type="range"]::-webkit-slider-runnable-track {
  width: 100%;
  height: 8.4px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 1px 1px 1px black, 0px 0px 1px slategrey;
  background: $green-color;
  border-radius: 1.3px;
  border: 0.2px solid slategrey;
}

input[type="range"]::-webkit-slider-thumb {
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px slategrey;
  border: 1px solid slategrey;
  height: 36px;
  width: 16px;
  border-radius: 3px;
  background: $blue-color;
  cursor: pointer;
  -webkit-appearance: none;
  margin-top: -14px;
}

input[type="range"]:focus::-webkit-slider-runnable-track {
  background: $green-color;
}

input[type="range"]::-moz-range-track {
  width: 100%;
  height: 100%;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 1px 1px 1px black, 0px 0px 1px slategrey;
  background: $green-color;
  border-radius: 1.3px;
  border: 0.2px solid slategrey;
}

input[type="range"]::-moz-range-thumb {
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px slategrey;
  border: 1px solid slategrey;
  height: 36px;
  width: 16px;
  border-radius: 3px;
  background: $blue-color;
  cursor: pointer;
}

input[type="range"]::-ms-track {
  width: 100%;
  height: 8.4px;
  cursor: pointer;
  animate: 0.2s;
  background: transparent;
  border-color: transparent;
  border-width: 16px 0;
  color: transparent;
}

input[type="range"]::-ms-fill-lower {
  background: slategrey;
  border: 0.2px solid slategrey;
  border-radius: 2.6px;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px black;
}

input[type="range"]::-ms-fill-upper {
  background: $blue-color;
  border: 0.2px solid #cccccc;
  border-radius: 2.6px;
  box-shadow: 1px 1px 1px #000000, 0px 0px 1px #cccccc;
}

input[type="range"]::-ms-thumb {
  box-shadow: 1px 1px 1px slategrey, 0px 0px 1px slategrey;
  border: 1px solid black;
  height: 36px;
  width: 16px;
  border-radius: 3px;
  background: $green-color;
  cursor: pointer;
}

input[type="range"]:focus::-ms-fill-lower {
  background: #cccccc;
}

input[type="range"]:focus::-ms-fill-upper {
  background: #cccccc;
}

@media (max-width: 50rem) {
  html {
    font-size: 56.75%;
  }

  .drum_machine {
    max-width: 45rem;
  }
}

@media (max-width: 30rem) {
  html {
    font-size: 50%;
  }

  .drum_machine {
    max-width: 40rem;
  }
}

              
            
!

JS

              
                const { useState, useEffect } = React;

/*
 * Function to create React component that is drum machine app.
 * App contains in itself four Component (Header, Display, Pad and Footer)
 * It has option to play music on keypress and on press of the button.
 */
function DrumMachine(props) {
  const { data } = props;

  const [volume, setVolume] = useState("0");
  const [mode, setMode] = useState("drums");
  const [display, setDisplay] = useState("");
  const [power, setPower] = useState(true);

  /*
   * Event Listener to react to pressing of the key
   */
  function handleKeyPress(e) {
    if (power) {
      const elem = document.getElementById(e.key.toUpperCase());

      if (elem) {
        const descr = data[mode].filter(
          (x) => x.keyTrigger === e.key.toUpperCase()
        )[0].id;
        setDisplay(descr);
        elem.play();
      }
    }
  }

  /*
   * Effect that is lunched every time that power button is pressed and 
   * on the beggining of the app.
   */
  useEffect(() => {
    if (power) {
      setDisplay("Loading Presets...");
      setVolume(40);
      document.addEventListener("keydown", handleKeyPress);
    } else {
      setDisplay("Power off");
      setVolume(0);
    }

    return () => {
      document.removeEventListener("keydown", handleKeyPress);
    };
  }, [power]);
  
  useEffect(() => {
    document.querySelectorAll("audio").forEach(item => {
      item.volume = Number(volume) / 100;
    });
  }, [volume])

  return (
    <div id="drum-machine" class="drum_machine">
      <Header setPower={setPower} power={power} />
      <Display display={display} mode={mode} volume={volume} />
      <Pad
        data={data}
        mode={mode}
        setDisplay={setDisplay}
        volume={volume}
        power={power}
      />
      <Footer
        volume={volume}
        setVolume={setVolume}
        setMode={setMode}
        power={power}
      />
    </div>
  );
}

/*
 * Function that creates React Component that only displays header of the
 * drum machine.
 */
function Header(props) {
  const { power, setPower } = props;

  return (
    <div class="drum_machine__header">
      <h1 class="drum_machine__header--text">Drum Machine</h1>
      <div class={`drum_machine__header--power ${power ? "" : "red"}`}>
        <i class="fas fa-power-off" onClick={() => setPower(!power)}></i>
      </div>
    </div>
  );
}

/*
 * Function that creates React component that displays information about sound
 * that was played, volume of the sound and type of the sound.
 */
function Display(props) {
  const { display, mode, volume } = props;

  return (
    <div class="drum_machine__display">
      <div class="drum_machine__display--screen">
        <div class="drum_machine__display--center" id="display">
          {display}
        </div>
        <div class="drum_machine__display--left">
          Volume: {Math.floor(volume)}%
        </div>
        <div class="drum_machine__display--right">Mode: {mode}</div>
      </div>
    </div>
  );
}

function Pad(props) {
  const { data, mode, setDisplay, volume, power } = props;

  function handleClick(item, volume, power) {
    if (power) {
      setDisplay(item.id);

      document.getElementById(item.keyTrigger).play();
    }
  }

  const buttons = data[mode].map((item) => (
    <button
      onClick={() => handleClick(item, volume, power)}
      id={item.id}
      class="drum_machine__pad--button drum-pad"
      key={item.keyCode}
    >
      {item.keyTrigger}
      <audio class="clip" src={item.url} id={item.keyTrigger}></audio>
    </button>
  ));

  return <div class="drum_machine__pad">{buttons}</div>;
}

/*
 * Function that creates React component that allows user to change volume of the
 * sound and type of the sound to be played
 */
function Footer(props) {
  const { volume, setVolume, setMode, power } = props;
  
  function handleRangeChange(value) {
    if(power) {
      setVolume(value);
    }
  }
  
  function handleModeChange(value) {
    if(power) {
      setMode(value);
    }
  }

  return (
    <div class="drum_machine__footer">
      <div class="drum_machine__footer--volume">
        <i class="drum_machine__footer--icon fas fa-volume-up"></i>
        <input
          tabindex="1"
          class="drum_machine__footer--range"
          value={volume}
          type="range"
          min="0"
          max="100"
          step="1"
          onChange={(e) => handleRangeChange(e.target.value)}
        />
      </div>
      <div class="drum_machine__footer--buttons">
        <button
          tabindex="2"
          class="drum_machine__footer--button"
          onClick={() => handleModeChange("drums")}
        >
          Drums
        </button>
        <button
          tabindex="3"
          class="drum_machine__footer--button"
          onClick={() => handleModeChange("synth")}
        >
          Synth
        </button>
      </div>
    </div>
  );
}

const data = {
  drums: [
    {
      keyCode: 81,
      keyTrigger: "Q",
      id: "Heater-1",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3"
    },
    {
      keyCode: 87,
      keyTrigger: "W",
      id: "Heater-2",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3"
    },
    {
      keyCode: 69,
      keyTrigger: "E",
      id: "Heater-3",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3"
    },
    {
      keyCode: 65,
      keyTrigger: "A",
      id: "Heater-4",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3"
    },
    {
      keyCode: 83,
      keyTrigger: "S",
      id: "Clap",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3"
    },
    {
      keyCode: 68,
      keyTrigger: "D",
      id: "Open-HH",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3"
    },
    {
      keyCode: 90,
      keyTrigger: "Z",
      id: "Kick-n'-Hat",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3"
    },
    {
      keyCode: 88,
      keyTrigger: "X",
      id: "Kick",
      url: "https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3"
    },
    {
      keyCode: 67,
      keyTrigger: "C",
      id: "Closed-HH",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3"
    }
  ],
  synth: [
    {
      keyCode: 81,
      keyTrigger: "Q",
      id: "Chord-1",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3"
    },
    {
      keyCode: 87,
      keyTrigger: "W",
      id: "Chord-2",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Chord_2.mp3"
    },
    {
      keyCode: 69,
      keyTrigger: "E",
      id: "Chord-3",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3"
    },
    {
      keyCode: 65,
      keyTrigger: "A",
      id: "Shaker",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Give_us_a_light.mp3"
    },
    {
      keyCode: 83,
      keyTrigger: "S",
      id: "Open-HH",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3"
    },
    {
      keyCode: 68,
      keyTrigger: "D",
      id: "Closed-HH",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Bld_H1.mp3"
    },
    {
      keyCode: 90,
      keyTrigger: "Z",
      id: "Punchy-Kick",
      url: "https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3"
    },
    {
      keyCode: 88,
      keyTrigger: "X",
      id: "Side-Stick",
      url: "https://s3.amazonaws.com/freecodecamp/drums/side_stick_1.mp3"
    },
    {
      keyCode: 67,
      keyTrigger: "C",
      id: "Snare",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3"
    }
  ]
};

ReactDOM.render(<DrumMachine data={data} />, document.getElementById("root"));

              
            
!
999px

Console