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">
</div>
<div class="author">
  ~ by <a href="https://codepen.io/antipasjiajia">Jia Guo</a> ~
</div>
              
            
!

CSS

              
                //variables
$bg-color: #F9F5EA;
$cal-color: #C7D1C6;
$red: #FD4D28;
$wht: #F9F5EA;
$dk-gray: #4A555B;
$lt-gray: #737B7E;
$bd-radius: 30px;

// minxin
@mixin make-btn($className: null, $btn-color: $dk-gray, $text-color: $wht, $text-size: 1rem){
  $div: if($className==null, "div", "div.");
  #{$div}#{$className} {
    background: $btn-color;
    color: $text-color;
    font-size: $text-size;
    &:hover{
      cursor: pointer;
      background: darken($btn-color, 7%);
      transition: background 0.2s ease-in-out;
    }
  }
}

div {
  box-sizing: border-box;
}
html, body {
  width: 100%; height: 100%;
  margin: 0; padding: 0;
  background: $bg-color;
  font-family: 'Varela Round', sans-serif;
  user-select: none;
}
.calculator {
  width: 320px; height: 510px;
  margin: 60px auto 20px auto;
  position: relative;
  background: transparentize($cal-color, 0.2);
  border: 2px solid lighten($wht, 10%);
  border-radius: $bd-radius;
}
.header {
  width: 100%; height: 27%;
  padding-top: 28%;
  position: relative;
  font-family: 'Raleway', sans-serif;
  background: transparentize($red, 0.1); 
  color: $dk-gray;
  border: 2px solid darken($red, 10%);
  border-top-left-radius: $bd-radius;
  border-top-right-radius: $bd-radius;
  .title {
    padding: 2px 20px;
    font-size: 1.4rem;
    font-weight: 600;
    text-align: right;
  }
  .bar {
    width: 100%; height: 3px;
    background: $dk-gray;
    position: absolute; bottom: 5px;
  }
}
.entry-box {
  width: 85%; height: 14%;
  margin: 4% auto;
  display: flex;
  justify-content: center;
  align-items: center;
  background: $dk-gray;
  border-radius: 10px;
  .screen {
    width: 85%; height: 65%;
    padding: 3px 10px;
    background: $lt-gray;
    text-align: right;
    color: darken($wht, 10%);
    .entry {
      font-size: 1.3rem;
    }
    .history {
      font-size: 0.8rem;
      overflow: hidden;
    }
  }
}
.buttons {
  width: 100%; height: 54%;
  padding: 10px 20px;
  border-top: 2px solid $wht;
  .row {
    width: 100%; height: 23%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    div {
      width: 40px; height: 40px;
      line-height: 42px;
      text-align: center;
      border-radius: 50%;
      &:active {
        transform: translate(0, 1px);
      }
    }
    @include make-btn;
    @include make-btn(red, $red, $dk-gray, 1.1rem);
    @include make-btn(white, $wht, $dk-gray, 1.1rem);
    .invisible {
      visibility: hidden;
    }
  }
}

.author {
  text-align: center;
  color: $dk-gray;
  font-family: 'Raleway', sans-serif;
  font-size: 0.9rem;
  font-weight: 100;
  a {
    text-decoration: none;
    color: $dk-gray;
    &:hover {
      text-decoration: underline;
    }
  }
}
              
            
!

JS

              
                const Button = (props) => (
  <div className={props.btnClass} onClick={()=>props.onClick()}>{props.btnVal}</div>
)

class Row extends React.Component {
  renderBtn(val) {
    // decide btn class
    let btnClass = '';
    let redBtns = ['AC', 'CE'];
    if (val === '') {
      btnClass = 'invisible';
    } else if (redBtns.includes(val)) {
      btnClass = 'red';
    } else if (/\D|[.]/.test(val)) {
      btnClass = 'white';
    }
    return <Button btnClass={btnClass} 
                   btnVal={val} 
                   onClick={()=>this.props.onClick(val)}/>
  }
  render() {
    return (
      <div className="row">
        {this.props.btns.map(val=>this.renderBtn(val))}
      </div>
    )
  }
}

class Buttons extends React.Component {
  renderRow(btns) {
    return <Row btns={btns} onClick={(btnVal)=>this.props.onClick(btnVal)}/>
  }
  render() {
    let btnVals = [
      ['AC', 7, 8, 9, '/'],
      ['CE', 4, 5, 6, 'x'],
      ['', 1, 2, 3, '-'],
      ['', 0, '.', '=', '+']
    ];
    return (
      <div className="buttons">
        {btnVals.map(btns=>this.renderRow(btns))}
      </div>
    )
  }
}

const Screen = (props) => (
  <div className="entry-box">
    <div className="screen">
      <div className="entry">{props.entry}</div>
      <div className="history">{props.history}</div>
    </div>
  </div>
)

class Calculator extends React.Component {
  constructor() {
    super();
    this.state = {
      entry: '0',
      history: '0'
    };
  }
  handleBtn(btnVal) {
    let entry = this.state.entry;
    let history = this.state.history;
    switch (true) {
      case /\d|[.]/.test(btnVal):
        if (btnVal === '.' && entry.includes('.')){
          return;
        }
        if ((entry === '0' && history ==='0') || history.includes('=')) {
          // start a new calculation
          this.setState({entry: String(btnVal), history: String(btnVal)});
        } else if (entry === '0'){
          // trim any 0 in the beginning of a number
          if(history[history.length-1] === '0'){
            this.setState({entry: String(btnVal), history: history.slice(0, history.length-1) + btnVal});
          } else {
          // entry's 0 value is the result of 'CE'
            this.setState({entry: String(btnVal), history: history + btnVal});
          }
        } else if (/[+\-x\/]/.test(entry)){
          // a new number starts after an operator
          this.setState({entry: String(btnVal), history: history + btnVal});
        } else if (entry.length < 10) {
          // add new digit to the current number
          this.setState({entry: entry + btnVal, history: history + btnVal});
        } else {
          // a number cannot have more than 10 digits
          this.setState({entry: '0', history: 'Digit Limit Met'});
        }
        break;
      case /[+\-x\/]/.test(btnVal):
        if(history.includes('=')){
          // start new calculation with history result
          this.setState({entry: btnVal, history: entry + btnVal});
        } else if(/\d|[.]/.test(history[history.length-1])){
          // if history is ended with a number, add the operator
          this.setState({entry: btnVal, history: history + btnVal});
        } else {
          // if history is ended with a operator, change the operator
          this.setState({entry: btnVal, history: history.slice(0, history.length-1) + btnVal});
        }
        break;
      case btnVal=="=":
        if(history.includes('=')){
          return;
        }
        let evalHis = history.replace(/x/g, '*');
        let result = eval(evalHis);
        if(parseInt(result) !== result){
          result = result.toFixed(2);
        }
        if(String(result).length > 10){
          this.setState({entry: '0', history: 'Digit Limit Met'});
        } else {
          this.setState({entry: String(result), history: history + '=' + result});
        }
        break;
      case btnVal=='AC':
        this.setState({entry: '0', history: '0'});
        break;
      case btnVal=='CE':
        if((entry == '0' && (history == '0' || history == 'Digit Limit Met')) 
           || history.includes('=')){
          this.setState({entry: '0', history: '0'});
        } else if(/\d/.test(entry)){
          // current entry is a number
          if(history == entry || !/[+\-x\/]/.test(history)){
            this.setState({entry: '0', history: '0'});
          } else {
            let nums = history.match(/([0-9.]+)|([+\-x\/])/g);
            this.setState({entry: '0', history: nums.slice(0, nums.length-1).join('')});
          }
        } else {
          // current entry is an operator
          this.setState({entry: '0', history: history.slice(0, history.length - 1)});
        }
        break;
    }
  }
  render() {
    return (
      <div className="calculator">
        <div className="header">
          <div className="title">Naive Calculator</div>
          <div className="bar"></div>
        </div>
        <Screen entry={this.state.entry} history={this.state.history} />
        <Buttons onClick={(btnVal)=>this.handleBtn(btnVal)}/> 
      </div>
    )
  }
}

ReactDOM.render(<Calculator />, document.getElementById('root'));
              
            
!
999px

Console