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

              
                <div id="app">
    <div class="abs-center calculator">
        <div class="rela-block display-section">
            <div class="rela-block display">
                <div class="rela-block small">{{fullFormula}}</div>
                <div class="rela-block">{{(currentInput?currentSign:'')}}{{currentInput||result}}</div>
            </div>
        </div>
        <div class="rela-block button-section">
            <div class="flex-r button-row">
                <div class="flex button blue" @click="clear">AC</div>
                <div class="flex button blue" @click="inputClear">C</div>
                <div class="flex button" @click="changeSign()">+/-</div>
                <div class="flex button" @click="addOperator('/')">/</div>
            </div>
            <div class="flex-r button-row">
                <div class="flex button" @click="updateNumber('7');">7</div>
                <div class="flex button" @click="updateNumber('8');">8</div>
                <div class="flex button" @click="updateNumber('9');">9</div>
                <div class="flex button" @click="addOperator('*')">*</div>
            </div>
            <div class="flex-r button-row">
                <div class="flex button" @click="updateNumber('4');">4</div>
                <div class="flex button" @click="updateNumber('5');">5</div>
                <div class="flex button" @click="updateNumber('6');">6</div>
                <div class="flex button" @click="addOperator('-')">-</div>
            </div>
            <div class="flex-r button-row">
                <div class="flex button" @click="updateNumber('1');">1</div>
                <div class="flex button" @click="updateNumber('2');">2</div>
                <div class="flex button" @click="updateNumber('3');">3</div>
                <div class="flex button" @click="addOperator('+')">+</div>
            </div>
            <div class="flex-r button-row">
                <div class="flex button wide" @click="updateNumber('0');">0</div>
                <div class="flex button" @click="updateNumber('.');">.</div>
                <div class="flex button blue" @click="if(currentInput){execute()}">=</div>
            </div>
        </div>
    </div>
</div>

              
            
!

CSS

              
                // --- COLORS ---

// Positioning Mixin
setup(pos, top = null, right = null, bottom = null, left = null)
    position pos
    top top
    left left
    right right
    bottom bottom

// --- SETUP STUFF ---
* { box-sizing: border-box; transition: 0.25s ease; }
.abs-center { setup(absolute,50%,null,null,50%); transform: translate(-50%, -50%); }
.rela-block { display: block; position: relative; }
.rela-inline { display: inline-block; position: relative; }
.flex-r { display: flex; flex-wrap: wrap; flex-direction: row; }
.flex-c { display: flex; flex-wrap: wrap; flex-direction: column; }
.flex { flex: 1 1 0; }
    
body
    transition 0s
    font-family monospace
    font-size 18px
    background-color #111
    color #EEE

.small
    font-size 14px
    margin-bottom 10px
    
// --- PAGE STYLING ---
.calculator
    width 420px
    padding 10px
    
.display
    margin 10px 5px
    background-color #000
    font-size 36px
    letter-spacing 4px
    text-align right
    padding 15px 10px
    overflow hidden
    border 1px solid #444
    border-radius 3px
    & *::before
        content "."
        color rgba(0,0,0,0)
    
.button
    margin 5px
    text-align center
    padding 20px 5px
    max-width 90px
    background-color rgba(255,255,255,0.1)
    border-radius 3px
    &:hover, &:active
        background-color rgba(255,255,255,0.3)
        cursor pointer
    &.wide
        flex 2 1 0
        max-width 500px
    &.wider
        flex 3 1 0
        max-width 500px
    &.blue
        background-color rgba(0,50,215,0.5)
        &:hover, &:active
            background-color rgba(0,50,215,1)
    &.hidden
        cursor default
        opacity 0
    &.disabled
        cursor default
        color rgba(255,255,255,0.2)
        &:hover, &:active
            background-color rgba(255,255,255,0.1)
              
            
!

JS

              
                // --- Notes ---
// Make a Calculator
// https://codepen.io/Paolo-Duzioni/pen/Jyxzom?editors=1010

// - Other stuff -
// https://dribbble.com/InVisionApp
// https://dribbble.com/shots/3750290-Muzli-Extension-Redesign
// https://dribbble.com/shots/1790652-Google-Material-exploration/attachments/294056

// - To Do -
// Limit input and output lengths to 12 - 14 digits.
// Add parentheses (and have a parentheses check function)

// Vue Stuff
var app = new Vue({
    el: '#app',
    data: {
        currentSign: '',
        currentInput: '',
        fullFormula: '',
        result: '',
    },
    //computed: {},
    methods: {
        updateNumber: function(digit) {
            if(this.result) { app.clear(); }
            
            if(digit === '.') {
                if(!this.currentInput || !(this.currentInput*1)) { this.currentInput = '0'; }
                if(!(this.currentInput.includes('.'))) { this.currentInput += '.'; }
            } else {
                (!this.currentInput || (!(this.currentInput*1) && !(this.currentInput.includes('.')))) ? (
                    this.currentInput = digit
                ):(
                    this.currentInput += digit
                );
            }
        },
        changeSign: function() {
            (this.currentSign)?(this.currentSign = ''):(this.currentSign = '-');
        },
        addOperator: function(op) {
            if(this.result) {
                this.fullFormula = (this.result+' '+op+' ');
                this.result = '';
            } else {
                if(this.currentInput) {
                    this.fullFormula += (this.currentSign+this.currentInput+' '+op+' ');
                    this.currentSign = '';
                    this.currentInput = '';
                } else {
                    if(this.fullFormula.includes(' ')) {
                        var temp = this.fullFormula.split('');
                        temp[temp.length-2] = op;
                        this.fullFormula = temp.join('');
                    }
                }
            }
        },
        execute: function() {
            if(this.fullFormula) {
                this.fullFormula += (this.currentSign+this.currentInput);
                this.result = eval(this.fullFormula);
            } else {
                this.fullFormula = this.currentSign+this.currentInput;
                this.result = this.currentInput;
            }
            this.currentSign = '';
            this.currentInput = '';
        },
        clear: function() {
            this.currentSign = '';
            this.currentInput = '';
            this.fullFormula = '';
            this.result = '';
        },
        inputClear: function() {
            this.currentSign = '';
            this.currentInput = '';
        },
        /*
        // For Parentheses
        pCheck: function(str) {
            var counter = 0;
            for(var i in str) {
                if(str[i] === '(') { counter++; }
                else if(str[i] === ')') { counter--; }
                if(counter < 0) {return false;}
            }
            return counter === 0;
        },
        */
    },
});

              
            
!
999px

Console