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="container">
        <div class="row">
            <div>
                Boolean value: {{ exampleBool }}
            </div>
            
            <input-checkbox
                name="example-1"
                label="I like citrus"
                :value="true"
                v-model="exampleBool"
            ></input-checkbox>
        </div>
        
        
        <div class="row">
            <div>
                Single value: {{ exampleSingle }} <br>
                (should use 
                <a href="https://codepen.io/Samuel87/pen/vYYLeRr" target="_blank">radios</a> 
                for cases like this)
            </div>
            
            <input-checkbox
                name="example-2"
                label="Orange"
                value="Orange"
                v-model="exampleSingle"
            ></input-checkbox>
            
            <input-checkbox
                name="example-2"
                label="Lime"
                value="Lime"
                v-model="exampleSingle"
            ></input-checkbox>
            
            <input-checkbox
                name="example-2"
                label="Lemon"
                value="Lemon"
                v-model="exampleSingle"
            ></input-checkbox>
        </div>
        
        
        <div class="row">
            <div>
                Array model: {{ exampleArr }}
            </div>
            
            <input-checkbox
                name="example-3"
                label="Orange"
                value="Orange"
                v-model="exampleArr"
            ></input-checkbox>
            
            <input-checkbox
                name="example-3"
                label="Lime"
                value="Lime"
                v-model="exampleArr"
            ></input-checkbox>
            
            <input-checkbox
                name="example-3"
                label="Lemon"
                value="Lemon"
                v-model="exampleArr"
            ></input-checkbox>
        </div>
        
        
        <div class="row">
            <div>
                Array model of objects: {{ exampleArrObj }}
            </div>
            
            <input-checkbox
                name="example-4"
                label="Orange"
                :value="{ name: 'Orange' }"
                v-model="exampleArrObj"
            ></input-checkbox>
            
            <input-checkbox
                name="example-4"
                label="Lime"
                :value="{ name: 'Lime' }"
                v-model="exampleArrObj"
            ></input-checkbox>
            
            <input-checkbox
                name="example-4"
                label="Lemon"
                :value="{ name: 'Lemon' }"
                v-model="exampleArrObj"
            ></input-checkbox>
        </div>
        
        <div class="row">
            <div>
                States
            </div>
            
            <input-checkbox
                name="example-disabled"
                label="Disabled"
                disabled
                :value="true"
                v-model="exampleBool"
            ></input-checkbox>
            
            <input-checkbox
                name="example-err"
                label="Errored"
                class="error"
                :value="true"
                v-model="exampleBool"
            ></input-checkbox>
        </div>
        
        <div class="row">
            <div>
                Multiline label clamped (in supported browsers)
            </div>
            
            <input-checkbox
                name="example-longtext"
                label="Lorem ipsum dolor sit amet consectetur adipisicing elit. Quas consectetur sint quod voluptatibus. Facere possimus nisi eos explicabo consectetur ipsa libero recusandae laudantium, porro sequi iusto, aliquam unde dolores blanditiis."
                :value="true"
                v-model="exampleBool"
            ></input-checkbox>
        </div>
    </div>
</div>
              
            
!

CSS

              
                $input-check: (
    'size':                 1.25em,
    'font-size':            16px,
    'label-color':          black,
    'theme-color':          #7f9cf5,
    'error-color':          #f56565,
    'border-width':         1px,
    'border-radius':        2px,
    'disabled-foreground':  #ccc,
    'disabled-background':  #f0f0f0,
);


@mixin line-clamp($max-lines: 2) {
    // clamp text in modern browsers
    // careful not to un-prefix these
    // with autoprefixer, etc
    display: -webkit-inline-box;
    -webkit-line-clamp: $max-lines;
    line-clamp: $max-lines;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-wrap: break-word;

    // if IE10+
    @media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
        word-wrap: normal;
    }
}


*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    line-height: 1.5;
}


// COMPONENT

.input-checkbox {
    position: relative;
    display: inline-flex;
    line-height: 1.4;
    font-size: map-get($input-check, 'font-size');
    flex-shrink: 0;
    color: map-get($input-check, 'label-color');
    cursor: pointer;

    .input-label {
        vertical-align: top;
        padding-left: 1em;

        @include line-clamp();

        &::before,
        &::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
        }

        &::before {
            width: map-get($input-check, 'size');
            height: map-get($input-check, 'size');
            border: map-get($input-check, 'border-width') solid;
            border-color: map-get($input-check, 'theme-color');
            border-radius: map-get($input-check, 'border-radius');
            background: white;
            transition: all 200ms;
        }

        &::after {
            opacity: 0;
            $checkmark-size: map-get($input-check, 'size') - 2 * .25em;
            $checkmark-translate-y: (
                map-get($input-check, 'size') / 2) - ($checkmark-size / 2.6
            );

            transform: translate(.25em, $checkmark-translate-y) rotate(-45deg);
            width: $checkmark-size;
            height: $checkmark-size / 2;
            border: .125em solid white;
            border-top-style: none;
            border-right-style: none;
        }
    }

    input {
        outline: 0;
        opacity: 0;
        width: map-get($input-check, 'size');
        flex-shrink: 0;

        &:active:not(:disabled) + .input-label::before {
            border-color: lighten(map-get($input-check, 'theme-color'), 20%);
            background-color: lighten(map-get($input-check, 'theme-color'), 20%);
        }
    }

    &.checked {
        .input-label::before {
            background: map-get($input-check, 'theme-color');
            border-color: map-get($input-check, 'theme-color');
        }

        .input-label::after {
            opacity: 1;
        }
    }

    &.focused {
        .input-label::before {
            border-color: lighten(map-get($input-check, 'theme-color'), 10%);
        }

        &.checked {
            .input-label::before {
                background-color: lighten(map-get($input-check, 'theme-color'), 10%);
            }
        }
    }

    &.disabled {
        color: map-get($input-check, 'disabled-foreground');
        cursor: default;

        .input-label::before {
            border-color: currentColor;
        }

        &.checked {
            .input-label::before {
                background-color: currentColor;
            }
        }
    }

    &.error {
        .input-label::before {
            color: map-get($input-check, 'error-color');
            border-color: currentColor;
        }

        &.checked {
            .input-label::before {
                border-color: currentColor;
                background-color: currentColor;
            }
        }

        &.focused {
            .input-label::before {
                border-color: lighten(map-get($input-check, 'error-color'), 10%);
            }
            
            &.checked {
                .input-label::before {
                    background-color: lighten(map-get($input-check, 'error-color'), 10%);
                }
            }
        }

        input:active {
            + .input-label::before {
                border-color: lighten(map-get($input-check, 'error-color'), 15%);
                background-color: lighten(map-get($input-check, 'error-color'), 15%);
            }
        }
    }
}



// LAYOUT

.container {
    max-width: 768px;
    margin: 50px auto 50px;
}

.row {
    margin: .5rem 0 3rem;
    
    > div:first-child {
        margin-bottom: .5rem;
        font-size: 18px;
    }
}

.input-checkbox {
    + #{&} {
        margin-left: 30px;
    }
}
              
            
!

JS

              
                /**
 * Input Checkbox
 *
 * @component
 * @example
 * {
 *   data: {
 *     value: false,
 *   },
 *   template: `
 *     <input-checkbox
 *       name="example"
 *       label="do you like turtles?"
 *       :value="true"
 *       v-model="value"
 *     ></input-checkbox>
 *   `,
 * }
 */
const InputCheckbox = {
    name: 'input-checkbox',
    model: {
        prop: 'model',
        event: 'change',
    },
    
    props: {
        model: {
            type: [String, Boolean, Object, Number, Array],
            required: true,
        },
        value: {
            type: [String, Boolean, Object, Number],
            required: true,
        },
        falseValue: {
            default: false,
        },
        label: String,
        disabled: Boolean,
        checked: Boolean,
    },
    
    data: () => ({
        focused: false,
    }),
    
    computed: {
        isModelArray() {
            return Array.isArray(this.model);
        },
        isValueObject() {
            return this.value !== null && typeof this.value === 'object';
        },
        isSelected() {
            const { model, value, isEqual, isValueObject, isModelArray } = this;
            
            if (isModelArray) {
                if (isValueObject) {
                    return model.some((obj) => isEqual(obj, value));
                } else {
                    return model.includes(value);
                }
            }

            if (isValueObject) {
                return isEqual(model, value);
            }

            return model === value;
        },
        stateClasses() {
            return {
                'checked': this.isSelected,
                'focused': this.focused,
                'disabled': this.disabled,
            }
        },
    },
    
    methods: {
        toggleCheck() {
            if (!this.disabled) {
                if (this.isModelArray) {
                    this.handleArrayCheckbox();
                } else {
                    this.handleSingleSelectCheckbox();
                }
            }
        },
        handleArrayCheckbox() {
            const newModel = this.model;

            if (!this.isSelected) {
                this.addValueToModel(newModel);
            } else {
                this.removeValueFromModel(newModel);
            }
        },
        handleSingleSelectCheckbox() {
            this.$emit('change', this.isSelected ? this.falseValue : this.value);
        },
        removeValueFromModel(newModel) {
            if (this.isValueObject) {
                newModel = newModel.filter((obj) => !this.isEqual(obj, this.value));
            } else {
                const index = newModel.indexOf(this.value);

                if (index !== -1) {
                    newModel.splice(index, 1);
                }
            }

            this.$emit('change', newModel);
        },
        addValueToModel(newModel) {
            newModel.push(this.value);

            this.$emit('change', newModel);
        },
        isEqual(a, b) {
            return JSON.stringify(a) === JSON.stringify(b);
        },
    },
    
    template: `
        <label :class="['input-checkbox', stateClasses]">
            <input
                type="checkbox"
                :value="value"
                :checked="isSelected"
                :disabled="disabled"
                @change="toggleCheck"
                @focus="focused = true"
                @blur="focused = false"
            />

            <span :class="['input-label']">
                {{ label }}
            </span>
        </label>
    `,
};


new Vue({
    el: '#app',
    data: {
        exampleBool: false,
        exampleSingle: 'Orange',
        exampleArr: ['Lime'],
        exampleArrObj: [{ name: 'Lime' }],
    },
    components: {
        InputCheckbox,
    },
});
              
            
!
999px

Console