Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              .container
    fancy-form(:options.sync='loginForm')

template#fancy-form
    form(@submit.prevent='submitHandler', :class='{"fancy-form--processing": state === "processing", "fancy-form--failure": state === "failure", "fancy-form--success": state === "success"}').fancy-form
        fancy-input(v-for='input in options.inputs', :input.sync='input')

        .fancy-form__controls
            button(@click.prevent='').fancy-form__control.fancy-form__control--cancel cancel
            button(:disabled="state === 'processing' || state === 'failure' || state === 'success'").fancy-form__control.fancy-form__control--submit: span submit

        div(v-if='failureMessage').fancy-form__failure-message
            p {{failureMessage}}

template#fancy-input
    div(:class='{"fancy-form__input--error": input.error}').fancy-form__input
        label(:for='input.id') {{input.label}}
        input(@keyup='input.error = ""', v-if='input.type === "email"', type='email', :id='input.id', v-model='input.value', :required="input.validation.required")
        input(@keyup='input.error = ""', v-if='input.type === "password"', type='password', :id='input.id', v-model='input.value', :required="input.validation.required")
        span
        p(v-if='input.error') {{input.error}}
            
          
!
            
              body {
    background-color: #eee;
}

.fancy-form {
    width: 18em;
    max-width: 100%;
    
    margin-left: auto;
    margin-right: auto;
    margin-top: 2em;
    padding-left: 1em;
    padding-right: 1em;
    padding-top: 1.5em;
    padding-bottom: .5em;
    
    box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
    background-color: #fff;
    
    font-size: 1rem;
}


.fancy-form--processing,
.fancy-form--failure,
.fancy-form--success {
    .fancy-form__control--submit {
        span {
            opacity: 0;
        }
    }
}


@keyframes spinning {
    0% {
        transform: translate3d(-50%, -50%, 0) rotate3d(0, 0, 1, 0);
    }
    
    100% {
        transform: translate3d(-50%, -50%, 0) rotate3d(0, 0, 1, 360deg);
    }
}

.fancy-form--processing {
    .fancy-form__control--submit {
        &::after {
            content: ' ';
            display: block;

            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate3d(-50%, -50%, 0);
            
            width: .75rem;
            height: .75rem;
            
            border-radius: 50%;
            border-style: solid;
            border-width: 2px;
            border-color: #777;
            border-left-color: transparent;

            animation-name: spinning;
            animation-duration: 1s;
            animation-iteration-count: infinite;
            
            transition: opacity .3s;
        }
    }
}


.fancy-form--failure {
    .fancy-form__control--submit {
        &::before,
        &::after {
            content: ' ';
            display: block;
            
            position: absolute;
            left: 50%;
            top: 50%;
            
            width: .8rem;
            height: 2px;
            
            background-color: red;
        
            transition: opacity .3s;
        }
        
        &::before {
            transform: translate3d(-50%, -50%, 0) rotate3d(0, 0, 1, 45deg);
        }
        
        &::after {
            transform: translate3d(-50%, -50%, 0) rotate3d(0, 0, 1, -45deg);
        }
    }
}


.fancy-form--success {
    .fancy-form__control--submit {
        &::before,
        &::after {
            content: ' ';
            display: block;
            
            position: absolute;
            left: 50%;
            top: 50%;
            
            height: 2px;
            
            background-color: green;
        }
        
        &::before {
            width: .3rem;
            transform: translate3d(-150%, 5%, 0) rotate3d(0, 0, 1, 45deg);
        }
        
        &::after {
            width: .8rem;
            transform: translate3d(-30%, -50%, 0) rotate3d(0, 0, 1, -45deg);
        }
    }
}


.fancy-form__input {
    label {
        cursor: pointer;
        
        font-size: .75em;
        color: #ccc;
    }

    input {
        cursor: pointer;
        
        width: 100%;

        padding-left: 0;
        padding-right: 0;
        padding-top: .5em;
        padding-bottom: .2em;

        outline: none;
        border: none;
        background: none;

        &:hover {
            ~ span {
                width: 20%;
            }
        }

        &:focus {
            cursor: text;
            
            ~ span {
                width: 100%;
            }
        }
    }
    
    span {
        display: block;

        width: 0;
        height: 2px;
        transition: width .15s;
        
        margin-bottom: .75em;

        background-color: #ddd;
    }

    p {
        visibility: hidden;
        
        height: 1em;
        
        margin-top: 0;
        margin-bottom: 0;
        
        font-size: .75em;
        color: red;
    }
}


.fancy-form__input--error {
    input {
        color: red;

        &:hover {
            ~ span {
                width: 100%;
            }
        }
    }

    span {
        width: 100%;

        background-color: red;
    }

    p {
        visibility: visible;
    }
}


.fancy-form__controls {
    text-align: right;
}


.fancy-form__control {
    padding: 1em;
    
    outline: none;
    border: none;
    background: none;
}


.fancy-form__control--cancel {
    border: none;
    
    font-size: .75rem;
    color: #ccc;
}


.fancy-form__control--submit {
    position: relative;
    
    &:hover {
        span {
            &::after {
                width: 50%;
            }
        }
    }
    
    span {
        opacity: 1;
        
        position: relative;
        
        transition: opacity .1s;
        
        &::after {
            content: ' ';
            display: block;

            width: 0;
            height: 2px;

            position: absolute;
            left: 0;
            bottom: -.3em;

            background-color: #ccc;
            
            transition: width .15s;
        }
    }
}


.fancy-form__failure-message {
    margin-left: -1em;
    margin-right: -1em;
    margin-bottom: -.5em;
    padding: 1em;
    
    background-color: red;
    
    color: #fff;
    
    p {
        margin-top: 0;
        margin-bottom: 0;
        
        font-size: .75em;
    }
}
            
          
!
            
              Vue.component('fancy-form', {
    template: '#fancy-form',
    props: {
        options: {
            type: Object,
            twoWay: true,
        },
    },
    data: function() {
        return {
            state: 'default',
            failureMessage: '',
        };
    },
    methods: {
        submitHandler: function() {
            let error = false;
            this.state = 'processing';
            this.failureMessage = '';

            for (let i = 0; i < this.options.inputs.length; i++) {
                this.$delete(this.options.inputs[i], 'error');
                this.$set(this.options.inputs[i], 'error', '');
            }
            
            for (const input of this.options.inputs) {
                // Checks if the value is not empty
                if (input.validation.required) {
                    if (!input.value || input.value === '') {
                        error = true;
                        input.error = 'field is required';
                    };
                }
                
                 // Checks if the value is the same as the value of the referenced input field
                if (input.validation.sameAs) {
                    const referencedInputField = this.options.inputs.find(element => element.id === input.validation.sameAs);
                    if (input.value !== referencedInputField.value) {
                        error = true;
                        input.error = 'password does not match';
                    }
                }
            }
            
            if (error) {
                this.state = 'default';
                return;
            }
            
            this.options.submit(failureMessage => {
                if (failureMessage) {
                    this.state = 'failure';
                    this.failureMessage = failureMessage;
                } else {
                    this.state = 'success';
                }
                
                setTimeout(() => {
                    this.state = 'default';
                }, 3000);
            });
        },
    },
    components: {
        'fancy-input': {
            template: '#fancy-input',
            props: {
                input: {
                    type: Object,
                    twoWay: true,
                },
            },
        },
    },
});

new Vue({
    el: '.container',
    data: {
        loginForm: {
            submit: function(done) {
                // Simulating ajax requests/custom submit processing
                setTimeout(() => {
                    if (Math.random() < .5) {
                        done('Custom failure message');
                    } else {
                        done();
                    }
                }, Math.random() * (2000 - 500) + 500);
            },
            inputs: [
                {
                    type: 'email',
                    id: 'email',
                    label: 'e-mail address',
                    value: 'jon.doe@example.com',
                    validation: {
                        required: true,
                    },
                },
                {
                    type: 'password',
                    id: 'password',
                    label: 'password',
                    value: 'secretpassword',
                    validation: {
                        required: true,
                    },
                },
                {
                    type: 'password',
                    id: 'repeat_password',
                    label: 'repeat password',
                    value: 'secretpassword',
                    validation: {
                        required: true,
                        sameAs: 'password',
                    },
                },
            ],
        },
    },
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console