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="root" style="height: 100%"></div>

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

              
            
!

CSS

              
                
@import url('https://fonts.googleapis.com/css?family=Quicksand:500|Kaushan+Script|Source+Code+Pro:600|Abril+Fatface|Audiowide|Comfortaa:700|Kelly+Slab&display=swap');


@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js');

$lavender: #cfafff;
$yellow: #f4f1af;
$babyBlue: #aadff6;
$darkerBlue: #2c7ce8;
$green: #d6f3a1;
$khaki: #dad67b;
$gogreen: #70dd34;
$disabled: #b6bfc9;
$yellow1: #f4f1af;
$yellow2: #e7e283;
$yellow3: #dbd55b;
$yellow4: #cec637;


body, html {
    height: 100%;
    margin: 0px;
    font-family: "Raleway", sans-serif;

}

.yellowMode {
	--shade1: #f4f1af;
    --shade2: #e7e283;
    --shade3: #dbd55b;
    --shade4: #cec637;
}

.blueMode {
	--shade1: #aadff6;
    --shade2: #7ec9e9;
    --shade3: #57b5dd;
    --shade4: #32a1d0;
}
@mixin border-radius($x) {
    -webkit-border-radius: $x;
    -moz-border-radius: $x;
    -ms-border-radius: $x;
    border-radius: $x;
}
@mixin height-calc($percentage, $value) {
    height: -moz-calc(#{$percentage} - #{$value});
    /* WebKit */
    height: -webkit-calc(#{$percentage} - #{$value});
    /* Opera */
    height: -o-calc(#{$percentage} - #{$value});
    /* Standard */
    height: calc(#{$percentage} - #{$value});
}


:export { gogreen: $gogreen;  darkerBlue: $darkerBlue; babyBlue: $babyBlue; yellow: $yellow;}

.outerDiv {
    height: 100%;
    width: 100%;
    background-color: var(--shade1);
    transition: all .3s ease-in-out;
}
.innerDiv {
    background: white;
    padding: 0px;
    overflow: hidden;
    position: relative;
    min-width: 200px;
    min-height: 200px;
    @include border-radius(7px)
}

.header {

	padding-right: 5px;
	padding-top: 3px;
	position: relative;
	top: 0;
	left: 0;
	width: 100%;
	background-color: var(--shade4);
  	transition: all .3s ease-in-out;
}

.padContainer {

	
}


.detailContainer {

}

.padRow {
	margin-left: 0px;
	margin-right: 0px;
}



.drum-pad {
	min-width: 3em;
	min-height: 3em;
	color: grey;
	margin: 7px;
	background-color: var(--shade2);
	@include border-radius(15px);
	font-family: 'Anton', sans-serif;
	font: 1.5em bold;
	transition: all 0.3s ease-in-out;
}

.drum-pad:disabled {
	background-color: $disabled;
	color: lightgrey;
	box-shadow: inset 0px 0px 5px 5px ;
	transition: all 0.3s ease-in-out;
}

.drum-pad:disabled:hover {
	box-shadow: inset 0px 0px 5px 5px ;
	transition: all 0.3s ease-in-out;

}

.drum-pad:hover {

  box-shadow: inset 0 0 2em rgba(255, 255, 255, .4);
  transition: all 0.3s ease-in-out;

}

.toggleContainer {

	height: 25px;
	margin-bottom: 2px;

}
.toggle {
	height: 0;
	width: 0;
	visibility: hidden;
}

.toggleBackground {
	width: 40px;
	height: 25px;
	background-color:  var(--bg-leftpos, $disabled);
	position: relative;
	@include border-radius(3px);
	transition: background-color 0.1s ease-in-out, box-shadow .1s ease-in-out;
	box-shadow: inset 0px 0px 2px 2px rgba(0, 0, 0, 0.1);
	margin-bottom: 0px;

}
/** slider left position */
.toggleBackground::after {
	content: '';
	position: absolute;
	top: 1px;
	left:  1px;
	width: 15px;
	height: 23px;
	background-color: var(--slider-leftpos, #fff);
	box-shadow: inset 0px 0px 3px 3px rgba(0, 0, 0, 0.1);
	@include border-radius(3px);

	transition: transform 0.3s ease-in-out, background-color 0.3s ease-in-out, ;
}

.leftText {
	position: absolute;
	width: 21px;
	top: 5px;
	left:  3px;
	display: inherit;
	font-size: 8pt;
	font-weight: bold;
	color: white;
	font-family: 'Quicksand', sans-serif;
	visibility: hidden;
	text-align: center;
	overflow: hidden;
}
.rightText {
	position: absolute;
	width: 21px;
	top: 5px;
	left: 16px;
	display: inherit;
	font-size: 8pt;
	font-weight: bold;
	color: white;
	font-family: 'Quicksand', sans-serif;
	visibility: visible;
	text-align: center;
	overflow: hidden;

}

.toggle:checked + .toggleBackground::after {

	background-color: var(--slider-rightpos, #fff);
	transform: translateX(23px);
}

.title {
	width: 100%;
	padding-left: 10px;
	font-size: 1.3em;
  color: var(--shade1);
	font-family: 'Kaushan Script', cursive;
}

.toggle:checked + .toggleBackground {

	background-color: var(--bg-rightpos, $gogreen);
	box-shadow: inset 0px 0px 0px 0px rgba(0, 0, 0, 0.1);
}


.toggle:checked + .toggleBackground>.leftText{
	visibility: visible;
}
.toggle:checked + .toggleBackground>.rightText{
	visibility: hidden;
}



.testClass {
	background-color: $gogreen;
}

.sampleText {
	box-shadow: inset 0px 0px 3px 3px rgba(0, 0, 0, 0.1);
	width: 100%;
	height: 1.7em;
	@include border-radius(3px);
	animation: 10s slide-down;
  	margin-top:0%;
  	padding-left: 8px;
}

.slider {
  -webkit-appearance: none;  /* Override default CSS styles */
  width: 100%; /* Full-width */
  height: 5px; /* Specified height */
  background: #d3d3d3; /* Grey background */
  outline: none; /* Remove outline */

}
.slider::-webkit-slider-thumb {
-webkit-appearance: none; /* Override default look */
  appearance: none;
  width: 15px; /* Set a specific slider handle width */
  height: 15px; /* Slider handle height */
  background: var(--shade2);
  @include border-radius(7px);
  border: 1px solid var(--shade3);

}

.slider::-moz-range-thumb {
  width: 15px; /* Set a specific slider handle width */
  height: 15px; /* Slider handle height */
  background: var(--shade2);
  @include border-radius(7px);
  border: 1px solid var(--shade3);
}


              
            
!

JS

              
                
const {render} =  ReactDOM

const {Provider, connect} = ReactRedux; 
const {createStore} = Redux;

console.log("load log");

// Section for setting up state
const defaultState = {
	sample: "", 
	power: true, 
	volume: 50,
	bank: true
}

const PLAY = "play";
const POWER = 'power';
const BANK = 'bank';
const VOLUME = 'volume';

const createPlayAction = (id) => {
	return {
		type: PLAY, 
		id: id
	}

}

const messageReducer = (state=defaultState, action) => {

	let holder = {...state}

	switch(action.type){
		case PLAY:
			holder.sample =  action.id
			break;
		case POWER: 
			holder.power = !holder.power;
			break;
		case BANK:
			holder.bank = !holder.bank;
			break;
    case VOLUME:
      holder.volume = action.volume;
      break;
		default: 
		break;
	}
	//console.log("messageReducer final state: " + JSON.stringify(holder))
	return holder;
}

const store = createStore(messageReducer);

const DrumPad = (props) => {
	return (
		<button className="drum-pad" 
			id={props.id} 
			key={props.id}
			disabled={props.disabled}
			onClick={((event) => {
				let sound = document.getElementById(props.letter);
				sound.currentTime = 0;
        sound.volume = props.volume/100;
				sound.play();
				props.callback(props.id);
			})}>
			<audio className="clip" src={props.clip} id={props.letter}></audio> {props.letter} 
		</button>
	)
}


// end section for setting up redux

/////// Setting up the drum machine component
class DrumPads extends React.Component {
	constructor(props){
		super(props);
		this.state =  {

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

		this.generatePads = this.generatePads.bind(this)
		this.handleKeyPress = this.handleKeyPress.bind(this);
		this.getPadByCode = this.getPadByCode.bind(this);
		this.playCallback = this.playCallback.bind(this);
	}

	getPadByCode(code){
		let bank = this.props.bank?0:1;

		return this.pads[bank].find(x=>x.code===code)
	}

	handleKeyPress(event){
		//alert("saw key prese event " + event.keyCode);
		let pad = this.getPadByCode(event.keyCode);
		//alert(JSON.stringify(pad));
		if(pad)
			document.getElementById(pad.id).click()
	}

	playCallback(id){
		//qconsole.log("DrumPad callback  " + id);
		this.props.playClip(id);
	}

	generatePads(){
		let rowArray = [];
		for(let i=0;i<3;i++){

      			let padArray = []
			let bank = this.props.bank?0:1;
			for(let j=0;j<3;j++){
				let index = i*3 + j;

				padArray.push(<DrumPad 
					id={this.pads[bank][index].id}
					key={this.pads[bank][index].id}
					letter={this.pads[bank][index].letter}
					clip={this.pads[bank][index].clip}
					callback={this.playCallback} 
					disabled={!this.props.power}
					volume={this.props.volume}/>);
			}
			rowArray.push(<div className="padRow"  key={'row' + i}>{padArray}</div>);
		}
		

		return <div className="padContainer ">{rowArray}</div>;
	}
	componentDidMount() {
    	document.addEventListener('keydown', this.handleKeyPress);
  	}
  	componentWillUnmount() {
    	document.removeEventListener('keydown', this.handleKeyPress);
  	}

	render(){
		return(
			
			this.generatePads()

		);
	}
}

DrumPads = connect(
	function mapPropsToState(state, ownProps){
		//console.log("calling map props in the drum pads connect");
		return state;
	},
	function mapDispatchToState(dispatch, ownProps){
		return {
			playClip: (id)=>{

				dispatch(createPlayAction(id))
			}
		}
	}
)(DrumPads);
/////// End Drum Machine Component

//// START slider component
class ToggleComponent extends React.Component{
	constructor(props){
		super(props);
		let style = {};

		if(props.backgroundWhenOn){
			style["--bg-rightpos"] = props.backgroundWhenOn;
		}
		if(props.backgroundWhenOff){
			style["--bg-leftpos"] = props.backgroundWhenOff;
		}

		console.log("sliders are: " + JSON.stringify(style))

		let text = [];
		if(props.leftText){
			text.push(<span className="leftText" key={this.props.id + "leftText"}>{props.leftText}</span>);
		}
		if(props.rightText){
			text.push(<span className="rightText" key={this.props.id + "rightText"}>{props.rightText}</span>)
		}
		this.state = {
			checked: props.checked,
			style: style,
			text: text
		}


		this.handleClick = this.handleClick.bind(this);

	}

	handleClick(event){
		this.setState({checked: !this.state.checked})
		this.props.clickCallback(event);
	}

	render(){

		return(
			<div className="toggleContainer">
				
				<input type="checkbox" className="toggle" id={this.props.id}  checked={this.state.checked} onChange={this.handleClick}></input>
				<label className="toggleBackground" style={this.state.style}  htmlFor={this.props.id} > 
					{this.state.text}
				</label>
				
			</div>
		)
	}
}
//// END slider component
////// DrumDetails component
class DrumDetails extends React.Component {
	constructor(props){
		super(props);
		this.state = {};
		this.clickCallback = this.clickCallback.bind(this);
	}

	clickCallback(event) {
	//	console.log('in click callback');
		this.props.changeBank();
	}

	render(){

		//console.log("rendering DrumDetails, state is " + JSON.stringify(this.props));


		return(

						<div id="details" className="detailContainer container">
 				<div className="row" >
 					<div className="col-4 d-flex justify-content-end align-items-center">Sounds</div>
					<div id="bankContainer" className="col-8 d-flex justify-content-left align-items-center" style={{paddingLeft: '0px'}}>

							<ToggleComponent id="bank" checked={true}  
							clickCallback={this.clickCallback} 
							leftText="1" rightText="2"/>

					</div>
				</div>
				<div className="row">
				 	<div className="col-4 d-flex justify-content-end align-items-center">Clip</div>
				 	<div className="col-8" style={{paddingLeft: 0}} >
				 		<div id="sample" className="sampleText" >{this.props.sample}</div>
				 	</div>
					
    			</div>
    			<div className="row">
    				<div className="col-4 d-flex justify-content-end align-items-center">Volume</div>
    				<div className="col-8" style={{paddingLeft: '0px'}}>
						<input type="range" className="slider" value={this.props.volume} min="0" max="100" id="customRange1" onChange={this.props.changeVolume} />

    				</div>
    			</div>

			</div>
		)
	}
}

DrumDetails = connect(
	(state) => {
		return state
	}, 
	(dispatch) => {
		return{
			changeBank : () => {
				dispatch({type: BANK});
			},
			changeVolume: (event) => {
				dispatch({type: VOLUME, volume: event.target.value});
			}
		}

	})(DrumDetails);

// App component section

class App extends React.Component{
  constructor(props){
    super(props)
    this.state ={
      
    }
    this.inputHandler = this.inputHandler.bind(this)

  }
  
  inputHandler(event){
   // console.log("observed change in parent input handler. " )

  }

  componentDidMount(prevProp, prevState){
    console.log("componentDidMount")

  }
  render(){
  	let theme = this.props.bank?"yellowMode":"blueMode";
    return (

        <div className={"outerDiv d-flex justify-content-center align-items-center " + theme} id="drum-machine"> 

        	<div className="innerDiv"  id="display">
        		<div className="header d-flex justify-content-end align-items-center">
        			<div className="title">Awesome Drums</div>
        			<div id="powerContainer">

						<ToggleComponent id="power" checked={true}  
							
							clickCallback={this.props.togglePower} 
							leftText="On" rightText="Off"/>
					</div>
        		</div>
        		<DrumPads />
        		<DrumDetails />

            </div>

    
        </div>
    )
  }
}

App = connect(
	function mapStateToProps(state, ownProps){
		//console.log("observed mapStateToProps, state is " + JSON.stringify(state));
		let myState = {
			sample: state.sample, 
			power: state.power, 
			volume: state.volume, 
			bank: state.bank
		}

		return myState;

	}, 
	function mapDispatchToProps(dispatch){
		return {
			testFunction: (testInput) => {

			},
			togglePower: (event) => {
				dispatch({type: POWER})
			}
		}
	}
)(App);

// End App component

render(
	<Provider store={store}>
		<App />
	</Provider>
	, document.getElementById('root'));


              
            
!
999px

Console