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

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

              
                
              
            
!

CSS

              
                @mixin select($opt)
  -webkit-touch-callout: #{$opt}
  -webkit-user-select: #{$opt}
  -khtml-user-select: #{$opt}
  -moz-user-select: #{$opt}
  -ms-user-select: #{$opt}
  user-select: #{$opt}

*
  // border: solid 0.5px
  position: relative
  box-sizing: border-box
  font-family: Dosis
  +select(none)
  text-align: center
  font-weight: 600
  font-size: 1.02em

body,html
  height: 100vh
  margin: 0
  overflow: hidden

.app
  width: 38%
  position: absolute
  left: 50%
  top: 50%
  transform: translate(-50%,-50%)
  background-color: rgba(255,255,255,0.1)
  box-shadow: 0 0 10px 5px rgba(0,0,0,0.15)
  & .input
    & .prev
      margin: 0
      padding-right: 1em
      height: 3em
      line-height: 3em
      text-align: right
    & .calc
      width: 80%
      height: 1.5em
      line-height: 1.5em
      padding: 0.5em
      padding-top: 0
      margin: 0 auto
      font-size: 3em
      overflow: hidden
      border-bottom: dashed 2px
      font-weight: 500
      font-family: Concert One
      +select(auto)
      
  & .evals
    display: flex
    width: 100%
    & .key
      width: 33.3%
  & .box
    width: 100%
    display: flex
    flex-wrap: wrap
    & .nums
      width: 80%
      display: flex
      flex-wrap: wrap
    & .key
      width: 33.3%
    & .operators
      width: 20%
      display: flex
      flex-direction: column
      & .key
        width: 100%
  & .color
    width: 100%
    padding: 0.5em
    & .bgc
      border: solid 1px
      display: inline-block
      width: 43%
      padding: 0.2em
      +select(auto)
    & .copy
      padding: 0.2em
      border: solid 1px
      display: inline-block
      width: 25%
      cursor: pointer
      background-color: #fff
    & .toggle
      display: inline-block
      width: 18%
      text-align: left
      cursor: pointer
      // border-left: solid
      padding: 0.2em
      & span
        

.key
  height: 4em
  cursor: pointer
  line-height: 4.5em
  &:hover
    color: SkyBlue
  
@media screen and (max-width:480px)
  body,html
    height: 91vh
  .app
    width: 90%
  .key
    height: 3.5em
    line-height: 3.5em
  .color
    margin: 0.5em
  .app .input .calc
    height: 1.7em
    line-height: 1.7em
              
            
!

JS

              
                //components
const Input = React.createClass({
  render: function(){
    return (
    <div className="input">
    <p className="prev">{this.props.prevString}</p>
    <p className="calc">{this.props.calcString}</p>
      </div>
    );
  }
});

const Key = React.createClass({
  render: function(){
    return <div className="key" onClick={this.props.onClick}>{this.props.content}</div>;
  }
});

const Nums = React.createClass({
  render: function(){
    return (
    <div className ="nums">
    <Key content = "1" onClick = {this.props.Input}/>
    <Key content = "2" onClick = {this.props.Input}/>
    <Key content = "3" onClick = {this.props.Input}/>
    <Key content = "4" onClick = {this.props.Input}/>
    <Key content = "5" onClick ={this.props.Input}/>
    <Key content = "6" onClick = {this.props.Input}/>
    <Key content = "7" onClick = {this.props.Input}/>
    <Key content = "8" onClick = {this.props.Input}/>
    <Key content = "9" onClick ={this.props.Input}/>
    <Key content = "0" onClick = {this.props.Input}/>
    <Key content = "." onClick = {this.props.Input}/>
    <Key content = "00" onClick = {this.props.Input}/>     
    </div>
    );
  }
});

const Operators = React.createClass({
  render: function(){
    return (
    <div className = "operators">
    <Key content = "+" onClick = {this.props.Input}/>
    <Key content = "-" onClick = {this.props.Input}/>
    <Key content = "*" onClick = {this.props.Input}/>
    <Key content = "/" onClick = {this.props.Input}/>
    </div>
    );
  }
});

const Evals = React.createClass({
  render: function(){
    return (
    <div className = "evals">
      <Key content = "C" onClick = {this.props.clear}/>
      <Key content = "DEL" onClick = {this.props.del}/>
      <Key content = "=" onClick = {this.props.calc}/>
    </div>
    );
  }
});

const Toggle = React.createClass({
  render: function(){
    return (<span className = "toggle" onClick = {this.props.onClick}>{this.props.colorType}</span>);
  }
});

const Color = React.createClass({
  render: function(){
    return (
      <p className = "color">
      <Toggle colorType = {this.props.colorType} onClick = {this.props.toggle}/>
      <span className = "bgc">{this.props.color}</span>
    <span className = "copy" style={{color:this.props.color, backgroundColor: this.props.textColor, borderColor: this.props.textColor}} onClick={this.props.copy}>COPY</span>
    </p>
    );
  }
});

const Keyboard = React.createClass({
    render: function(){
  return (
        <div className = "keyborad">
    <Evals clear={this.props.clear} del={this.props.del} calc = {this.props.calc}/>  
    <div className = "box">
    <Nums Input = {this.props.Input}/>
    <Operators Input = {this.props.Input}/>
    <Color colorType = {this.props.colorType} color = {this.props.color} textColor = {this.props.textColor} copy = {this.props.copy} toggle = {this.props.toggle}/>
      </div>
      </div>
    );
  }
});

// putting together
const App = React.createClass({
  getInitialState: function(){
    return {
      prevString: "",
      calcString: "",
      color:"#2abacd",
      textColor:"#fff",
      colorType: "HEX ▸"
    };
  },
// Methods
  Input: function(e){
    let string = this.state.calcString + e.target.innerHTML;
    this.setState({calcString : string});
    this.shine();
  },
  clear: function(){        
    this.setState({
      calcString:"",
      prevString:""});
  }
  ,
  del: function(){
    let string = this.state.calcString.slice(0,-1);
    this.setState({calcString:string});
  },
  calc: function(){
	let result = eval(this.state.calcString),         
      prev = this.state.calcString;
	this.setState({calcString: result, prevString: prev});
  },
  hexToRgb: function(hex) {
    let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}
  ,
rgbToHex: function(color) {
if (color.charAt(0) === "#") {
return color; }

let nums = /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/i.exec(color),
    r = parseInt(nums[2], 10).toString(16), 
    g = parseInt(nums[3], 10).toString(16), 
    b = parseInt(nums[4], 10).toString(16);

return "#"+ ( (r.length == 1 ? "0"+ r : r) + (g.length == 1 ? "0"+ g : g) + (b.length == 1 ? "0"+ b : b) ); 

},
  
  shine: function(){
    let body = document.querySelector('body'),
        color = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6),
        rgb = this.hexToRgb(color),      rgbColor = `rgb(${rgb.r},${rgb.g},${rgb.b})`,
        colorType=this.state.colorType;

   let o = Math.round(((rgb.r * 299) + (rgb.g * 587) + (rgb.b * 114)) / 1000);

(o > 180) ? this.state.textColor="#594741": this.state.textColor="#fff";

    colorType == "HEX ▸" ?
    this.setState({color: color}):this.setState({color: rgbColor})
  },
  copy: function(){
  let copyArea = document.querySelector('.bgc'),
      range = document.createRange(); 
      range.selectNode(copyArea); 
      window.getSelection().addRange(range);
      document.execCommand("Copy"); 
  },
  toggle: function(){
    let color = this.state.color,
        colorType = this.state.colorType;
    
    if(colorType == "HEX ▸"){
      let rgb = this.hexToRgb(color), 
          rgbColor = `rgb(${rgb.r},${rgb.g},${rgb.b})`;
      
      this.setState({colorType:"◂  RGB",color:rgbColor});
    }else{
      let hexColor = this.rgbToHex(color);
      this.setState({colorType:"HEX ▸",color:hexColor});
    }
  },

// Render app
  render: function(){
  document.querySelector('body').style.backgroundColor=this.state.color;  document.querySelector('body').style.color=this.state.textColor;
    
    return (
    <div className="app">
    
    <Input prevString={this.state.prevString} calcString={this.state.calcString}/>
    
    <Keyboard Input = {this.Input} colorType = {this.state.colorType} color = {this.state.color} textColor = {this.state.textColor} copy = {this.copy} toggle = {this.toggle} calc={this.calc} del={this.del} clear = {this.clear}/>
        
    </div>
    );
  }
});

ReactDOM.render(<App/>,document.querySelector('body'));

//learning redux to improve code
              
            
!
999px

Console