Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                .grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* Creates 3 equal-width columns */

  padding: 20px; /* Adds padding around the grid container */
}

.grid-item {
  background-color: #e0e0e0;
  padding: 20px;
  text-align: center;
}

              
            
!

JS

              
                import React from "https://esm.sh/react@18.2.0";
import ReactDOM from "https://esm.sh/react-dom@18.2.0";

const heaterSoundsGroup = [
  {
    keyCode: 81,
    key: "Q",
    id: "Heater-1",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3"
  },
  {
    keyCode: 87,
    key: "W",
    id: "Heater-2",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3"
  },
  {
    keyCode: 69,
    key: "E",
    id: "Heater-3",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3"
  },
  {
    keyCode: 65,
    key: "A",
    id: "Heater-4",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3"
  },
  {
    keyCode: 83,
    key: "S",
    id: "Clap",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3"
  },
  {
    keyCode: 68,
    key: "D",
    id: "Open-HH",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3"
  },
  {
    keyCode: 90,
    key: "Z",
    id: "Kick-n'-Hat",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3"
  },
  {
    keyCode: 88,
    key: "X",
    id: "Kick",
    url: "https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3"
  },
  {
    keyCode: 67,
    key: "C",
    id: "Closed-HH",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3"
  }
];

const smoothPianoSoundsGroup = [
  {
    keyCode: 81,
    key: "Q",
    id: "Chord-1",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3"
  },
  {
    keyCode: 87,
    key: "W",
    id: "Chord-2",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Chord_2.mp3"
  },
  {
    keyCode: 69,
    key: "E",
    id: "Chord-3",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3"
  },
  {
    keyCode: 65,
    key: "A",
    id: "Shaker",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Give_us_a_light.mp3"
  },
  {
    keyCode: 83,
    key: "S",
    id: "Open-HH",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3"
  },
  {
    keyCode: 68,
    key: "D",
    id: "Closed-HH",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Bld_H1.mp3"
  },
  {
    keyCode: 90,
    key: "Z",
    id: "Punchy-Kick",
    url: "https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3"
  },
  {
    keyCode: 88,
    key: "X",
    id: "Side-Stick",
    url: "https://s3.amazonaws.com/freecodecamp/drums/side_stick_1.mp3"
  },
  {
    keyCode: 67,
    key: "C",
    id: "Snare",
    url: "https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3"
  }
];

const soundsGroup = [
  {
    name: "Heater Kit",
    sounds: heaterSoundsGroup
  },
  {
    name: "Smooth Piano Kit",
    sounds: smoothPianoSoundsGroup
  }
];

const DrumMachine = ({ soundGroup, handleChangeGroup }) => {
  const [soundName, setSoundName] = React.useState("");

  const handleSoundClick = (key, id) => {
    setSoundName(id);
    const audio = document.getElementById(key);
    audio.parentElement.style.backgroundColor = "#000000";
    audio.currentTime = 0;
    audio.play();
    setTimeout(() => {
      audio.parentElement.style.backgroundColor = "blue";
    }, 230);
  };

  React.useEffect(() => {
    setSoundName(soundGroup.name);
  }, [soundGroup]);
  return (
    <div
      id="drum-machine"
      className=" border border-2 border-dark shadow px-3 py-3 rounded rounded-3 d-flex align-items-center justify-content-between"
      style={{ width: "350px" }}
    >
      <Keyboard
        sounds={soundGroup.sounds}
        handleSoundClick={handleSoundClick}
      />
      <Control name={soundName} handleChangeGroup={handleChangeGroup} />
    </div>
  );
};

const KeyboardKey = ({ sound, handleSoundClick }) => {
  const handleKeyDown = (e) => {
    if (sound.keyCode === e.keyCode) {
      handleSoundClick(sound.key, sound.id);
    }
  };
  React.useEffect(() => {
    document.addEventListener("keydown", handleKeyDown);
  }, []);
  return (
    <div
      id={sound.id}
      className="drum-pad py-3 px-3 border-1 border dark d-flex justify-content-center align-items-center shadow border-dark m-1 row grid-item rounded rounded-pill"
      onClick={() => handleSoundClick(sound.key, sound.id)}
      style={{
        width: "50px",
        height: "50px",
        color: "#ffffff",
        backgroundColor: "blue"
      }}
    >
      <audio src={sound.url} id={sound.key} className="clip" />
      {sound.key}
    </div>
  );
};

const Keyboard = ({ sounds, handleSoundClick }) => {
  return (
    <div className="grid-container">
      {sounds.map((sound, index) => {
        return (
          <KeyboardKey sound={sound} handleSoundClick={handleSoundClick} />
        );
      })}
    </div>
  );
};

const Control = ({ name, handleChangeGroup }) => {
  return (
    <div>
      <span id="display" className="btn btn-dark " onClick={handleChangeGroup}>
        {name}
      </span>
    </div>
  );
};
const App = () => {
  const [currentGroupIndex, setCurrentGroupIndex] = React.useState(0);

  const handleChangeGroup = () => {
    setCurrentGroupIndex((currentGroupIndex + 1) % soundsGroup.length);
  };

  return (
    <div className="min-vh-100 d-flex flex-column justify-content-center align-items-center">
      <h1 className="h1">Drum Machine</h1>
      <DrumMachine
        soundGroup={soundsGroup[currentGroupIndex]}
        handleChangeGroup={handleChangeGroup}
      />
    </div>
  );
};

ReactDOM.render(<App />, document.querySelector("#app"));

              
            
!
999px

Console