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 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

              
                <header>
    <div class="container">
        <div class="text-center">
            <div class="heading">
                <h3>Drum Machine</h3>
                <h4>FreeCodeCamp - Front End Developement</h4>
            </div>
        </div>
</header>
</br>
<div id="app"></div>
</br> 
<!-- Footer -->
<div id="footer" class="container-fluid text-center">
    <div class="footer text-center">
        <a href="https://www.facebook.com/lindakovacsart" target="_blank"><span class="fa fa-facebook-f"></span> Facebook | </a>
        <a href="https://twitter.com/lindakovacsart" target="_blank"><span class="fa fa-twitter"></span> Twitter | </a>
        <a href="https://plus.google.com/u/1/112468176480762133222" target="_blank"><span class="fa fa-google-plus"></span> Google+ | </a>
        <a href="https://www.linkedin.com/in/lindakovacsart" target="_blank"><span class="fa fa-linkedin"></span> LinkedIn | </a>
        <a href="https://github.com/lindakovacs" target="_blank"><span class="fa fa-github"></span> GitHub | </a>
        <a href="https://www.freecodecamp.org/lindakovacs" target="_blank"><i class="fa fa-fire"></i>
            FreeCodeCamp | </a> <!-- class="fa fa-free-code-camp" -->
        <a href="https://codepen.io/lindakovacs" target="_blank"><i class="fa fa-codepen"></i>Codepen</a>
    </div>
    <div class="footer text-center">&copy 2018 Made with &#10084; by Linda Kovacs</div>
</div>
              
            
!

CSS

              
                body {
    padding-bottom: 20px;
    font-family: 'Raleway', sans-serif;
}
header {
	padding: 10px 0;
	margin-bottom: 3px;
	text-align: center;
	color: #fff;
  background-color: #000000;
  font-family: 'Raleway', sans-serif;
  font-size: 25px;
}
%center {
  position:relative;
  left:0; right:0;
  top:0; bottom:0;
  margin:auto;
}

/* Horizontally and vertically center elements with flex */
%flex-center {
  display:flex;
  justify-content:center;
  align-items:center;
}

#drum-machine {  
  @extend %center;
  height:400px;
  width:800px;  
  max-width:95%;
  max-height:95%;
  overflow:auto;
  @extend %flex-center;
  background-color: #3861F3;
  color:#EAEBEC;
}


/* Controls container */
#controlsContainer{
  display:inline-block;
  position:relative;
  height:100%;
  width:50%;
  max-width:50%;
}

#keysContainer {
  position:relative;
  height:75%;
  width:50%;
  max-width:50%;
  @extend %flex-center;
  flex-wrap:wrap;
}

/* drum pads */
.drum-pad {
  height: 25%;
  width: 25%;
  margin:2%;
  border-radius:50%;
  color: #ffffff;
  outline:none;
  border:none;
  font-size:30px;
  background-color:#9733ee;
}

.drum-pad:active {
  transform: translateY(1px);
}

/* Power switch */
#powerSwitch {  
  height:33%;
  width:100%;
  @extend %flex-center;
}

#onOffContainer {
  background-color:#ffffff;
  width:15%;
  height:20%;
  margin:0 1%;
  @extend %flex-center;
}

#on, #off {
  width:45%;
  height:80%;
  margin:5%;
}

#on {
  margin-right:5%;
}

/* Display component */
#display {
  font-size:1.2em;
  height:50%;
  width:60%;  
  @extend %flex-center;
  background-color:#ffffff;
  color:#000000;
}

#displayContainer {
  height:33%;
  width:100%; 
  @extend %flex-center;
}

/* Volume slider */
#volumeSliderContainer {
  height:33%;
  width:100%; 
  @extend %flex-center;
}

#volumeSlider {
  -webkit-appearance: none;
  outline:none;
  width:70%;
  height:5%;
  opacity:0.7;
  -webkit-transition: .2s;
  transition: opacity .2s;
}
#volumeSlider:hover{
  opacity:1;
}
#volumeSlider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    background: #9733ee;
    cursor: pointer;
    border-radius:100%;
}

#volumeSlider::-moz-range-thumb {
    width: 20px;
    height: 20px;
    background: #9733ee;
    cursor: pointer;
}

#footer {
	width: 100%;
	color: #fff;
	padding: 20px;
	width: 100%;
	text-align: center;
	background-color: #000;
	font-size: 15px;
}

.footer a {
	color: #fff;
	position: relative;
	text-decoration: none;
	font-weight: bold;
}
.footer a:hover {
	color: #000;
	background-color: #fff;
}
.footer p {
	font-size: 17px;
}
              
            
!

JS

              
                const keys = ['Q', 'W', 'E', 'A', 'S', 'D', 'Z', 'X', 'C'];
const url = 'https://s3.amazonaws.com/freecodecamp/drums/';
const clips = [
    'Heater-1.mp3',
    'Heater-2.mp3',
    'Heater-3.mp3',
    'Heater-4_1.mp3',
    'Heater-6.mp3',
    'Dsc_Oh.mp3',
    'Kick_n_Hat.mp3',
    'RP4_KICK_1.mp3',
    'Cev_H2.mp3'
    ];

function formatClipName(clip) {
    return clip.slice(0,-4).split('-').join(' ');
}

class App extends React.Component {
    constructor(props){
      super(props);
      this.state = {
        currentClip: String.fromCharCode(160),
        switchedOn:true,
        volume:50
      }
      this.handleSelectedClip = this.handleSelectedClip.bind(this);
      this.handleOnOffSwitch = this.handleOnOffSwitch.bind(this);
      this.handleVolumeChange = this.handleVolumeChange.bind(this);
      this.handleKeyDown = this.handleKeyDown.bind(this);
      this.play = this.play.bind(this);
    }      

    componentDidMount() {
        document.addEventListener('keydown', this.handleKeyDown)
    }

    play(audio) {
        audio.volume = this.state.volume / 100;
        audio.currentTime = 0;
        audio.play();
    }

    handleSelectedClip (key, clip) {
        const audio = this[key];
        if (this.state.switchedOn) {
            this.setState({
                currentClip: formatClipName(clip)
            })
            this.play(audio);
        }        
    }

    handleKeyDown(e) {
        let key = e.key
        if (this.state.switchedOn && keys.some( el => el.toLowerCase() === key)) {               
            const idx = keys.findIndex( el => el.toLowerCase() === key );
            this.handleSelectedClip(key.toUpperCase(), clips[idx]); 
        }        
    }

    handleOnOffSwitch() {
        this.setState({
            switchedOn: !this.state.switchedOn,
            currentClip:String.fromCharCode(160)
        })
    }

    handleVolumeChange(e) {
        const volume = e.target.value;
        this.setState({
            volume,
            currentClip: 'Volume: ' + volume
        })        
        setTimeout( () => this.setState({ currentClip:'' }), 1500)
    }
    
    render() {  
  
      return (
        <div 
            >        
            <div id='drum-machine'>
                <div id='keysContainer'>
                   { 
                       keys.map((key, idx) => 
                            <Key
                                key = {idx}
                                keyName = {key}
                                clip = {clips[idx]}         
                                handleClick = {this.handleSelectedClip}
                                activated = {this.state.switchedOn}          
                                volume = {this.state.volume / 100}
                                audio = { el => this[key] = el}
                            />
                        )
                    }
                </div> 

                <div id='controlsContainer' >
                    <PowerButton
                        handleClick = {this.handleOnOffSwitch}
                        turnedOn = {this.state.switchedOn}
                    />
                    <Display 
                        clip = {this.state.currentClip}
                    />
                    <VolumeSlider
                        handleChange={this.handleVolumeChange}
                    />
                </div>            

            </div>
        </div>
      );
    }    
}
  
class Key extends React.Component {
    constructor(props) {
        super(props);
    }    

    render() {
        const {handleClick, activated, keyName, clip, audio } = this.props;
        return (
            <button                
                className = 'drum-pad' 
                id = {clip.slice(0,-4)}
                { ...!activated && {disabled:true} }
                onClick={ () => {
                    handleClick(keyName, clip);               
                }}
            >
               <audio src={url + clip} className='clip' id={keyName} ref={audio}>
                </audio> 
                {keyName}
            </button>
        )
    }     
}

function PowerButton(props) {
    const {turnedOn, handleClick} = props;
    return (
        <div id='powerSwitch'>
            <div>ON</div>
            <div id='onOffContainer' onClick={() => handleClick()}>
                <div id='on' style={ turnedOn ? {backgroundColor:'#9733ee'} : {} } ></div>
                <div id='off' style={ turnedOn ? {} : {backgroundColor:'#9733ee'} } ></div>
            </div>              
            <div>OFF</div>
        </div>       
    )
}

function Display({clip}) {
    return (
        <div id='displayContainer'>
            <p id="display" >
                {clip}
            </p>        
        </div>
    )
}

function VolumeSlider({handleChange}) {
    return (
        <div id='volumeSliderContainer'>
            <input 
              id='volumeSlider'
              type="range" 
              min="1" 
              max="100" 
              onChange={(e) => handleChange(e)}
            />
        </div>
    )
}

ReactDOM.render(
  <App/>,
  document.getElementById('app')
)
              
            
!
999px

Console