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" class="container">
  <success-box></success-box>
  
  <contact-form></contact-form>
</div>

              
            
!

CSS

              
                *
  margin: 0
  padding: 0
  box-sizing: border-box

html, body
  height: 100%
  line-height: 1.4
  font:
    size: 100%
    family: Roboto, sans-serif
    
.container
  display: flex
  height: 100%
  justify-content: center
  align-items: center
  overflow: hidden

.contact-form
  display: flex
  max-width: 1200px
  width: 75%
  flex-direction: column
  
  &.success
    animation: fly-out .5s ease-in-out .5s forwards
    
  &.success
    .form-field-container
      animation: fly-out .5s ease-in-out forwards
    
      &-2
        animation-delay: .1s
      
      &-3
        animation-delay: .2s
      
      &-4
        animation-delay: .3s
      
      &-5
        animation-delay: .4s

.form-field-container
  display: flex
  margin-top: 40px
  position: relative
  
  &:first-child
    margin-top: 0

.form-field
  padding: 20px 40px
  flex: 1
  background-color: white
  color: #707070
  font: inherit
  outline: none
  border: none
  border-radius: 9999px
  box-shadow: 0 1px 10px rgba(0,0,0,0.2)
  transition: all .2s ease-in-out
  
  &:focus
    box-shadow: 0 3px 30px rgba(0,0,0,0.1)
  
  &.has-error
    color: #d32f2f
    box-shadow: 0 1px 10px transparentize(#d32f2f, 0.8)
    
    &::placeholder
      color: inherit
    
    &:focus
      box-shadow: 0 3px 30px transparentize(#d32f2f, 0.9)
  
  &.success
    color: #4caf50
    box-shadow: 0 1px 10px transparentize(#4caf50, 0.8)
    
    &::placeholder
      color: inherit
    
    &:focus
      box-shadow: 0 3px 30px transparentize(#4caf50, 0.9)
  
  &--text
    resize: none
    border-radius: 20px
  
  &--button
    background-color: #3b4f5b
    color: white
    cursor: pointer
    font-size: 1.2rem
    text-transform: uppercase
    letter-spacing: 0.07em
    box-shadow: 0 15px 10px -10px rgba(0,0,0,0.4)
    
    &:hover
      box-shadow: 0 35px 20px -20px rgba(0,0,0,0.2)
      transform: scale(1.02) translateY(-10px)
    
.form-field-error
  display: block
  padding: 10px 40px
  position: absolute
  top: 0
  left: 0
  transform: translateY(-100%)
  color: #d32f2f
  font:
    size: 85%
    family: inherit
    
.success-box
  position: absolute
  height: 100%
  width: 100%
  display: flex
  flex-direction: column
  justify-content: center
  align-items: center
  opacity: 0
  visibility: hidden
  
  &.show
    animation: show .2s linear .5s forwards
  
.success-icon
  display: block
  height: 100px
  width: 100px
  position: relative
  background-color: #4caf50
  border-radius: 100%
  
  .hook
    display: block
    height: 50%
    width: 5px
    position: absolute
    top: 50%
    left: 60%
    transform: translateY(-50%) rotate(30deg)
    background-color: white
  
    &::after
      content: ''
      display: block
      height: 50%
      width: 100%
      position: absolute
      bottom: 0
      transform-origin: bottom right
      transform: rotate(-80deg)
      background-color: white
  
.success-message
  margin-top: 20px
  font-size: 1.2rem
    
@keyframes fly-out
  from
    transform: translate(0, 0)
    opacity: 1
    visibility: visible
  
  to
    transform: translate(200%, -300%)
    opacity: 0
    visibility: hidden
    
@keyframes show
  from
    opacity: 0
    visibility: hidden
 
  to
    opacity: 1
    visibility: visible
    
@media screen and (max-width: 800px)
  .contact-form
    width: 85%
    
@media screen and (max-width: 800px)
  .contact-form
    width: 90%
              
            
!

JS

              
                const capitalize = str => str[0].toUpperCase() + str.slice(1)

const successBox = {
  template: `
    <div class="success-box" :class="{ 'show': this.$parent.success }">
      <span class="success-icon"><span class="hook"></span></span>
      <span class="success-message">Your email is on the way!</span>
    </div>
  `
}

const formField = {
  props: [ 'value', 'fieldName', 'fieldId' ],
  computed: {
    capitalizedFieldName () {
      return capitalize(this.fieldName)
    }
  },
  data () {
    return {
      fieldValue: this.value
    }
  },
  watch: {
    fieldValue (val) {
      this.$emit('input', val)
    }
  },
  methods: {
    hasError (field) {
      return this.$parent.errors[field].length >= 1
    },
    getError (field) {
      return this.$parent.errors[field][0].msg
    }
  },
  template: `
    <div class="form-field-container" :class="'form-field-container-' + this.fieldId">
      <template v-if="hasError(this.fieldName)">
        <span class="form-field-error">{{ getError(this.fieldName) }}</span>
      </template>
      <input type="text" :placeholder="capitalizedFieldName" class="form-field" :class="{ 'has-error': hasError(this.fieldName), 'success': !hasError(this.fieldName) && this.$parent.sent }" v-model="fieldValue">
    </div>
  `
}

const formFieldText = {
  props: [ 'value', 'fieldName', 'fieldId' ],
  computed: {
    capitalizedFieldName () {
      return capitalize(this.fieldName)
    }
  },
  data () {
    return {
      fieldValue: this.value
    }
  },
  watch: {
    fieldValue (val) {
      this.$emit('input', val)
    }
  },
  methods: {
    hasError (field) {
      return this.$parent.errors[field].length >= 1
    },
    getError (field) {
      return this.$parent.errors[field][0].msg
    }
  },
  template: `
    <div class="form-field-container" :class="'form-field-container-' + this.fieldId">
      <template v-if="hasError(this.fieldName)">
        <span class="form-field-error">{{ getError(this.fieldName) }}</span>
      </template>
      <textarea cols="30" rows="10" :placeholder="capitalizedFieldName" class="form-field form-field--text" :class="{ 'has-error': hasError(this.fieldName), 'success': !hasError(this.fieldName) && this.$parent.sent }" v-model="fieldValue"></textarea>
    </div>
  `
}

const formFieldButton = {
  props: [ 'fieldId' ],
  template: `
    <div class="form-field-container" :class="'form-field-container-' + this.fieldId">
      <button type="submit" class="form-field form-field--button">
        <slot></slot>
      </button>
    </div>
  `
}

const contactForm = {
  components: {
    'form-field': formField,
    'form-field-text': formFieldText,
    'form-field-button': formFieldButton
  },
  data () {
    return {
      sent: false,
      name: null,
      email: null,
      subject: null,
      message: null,
      errorMessages: {
        name: {
          required: 'The name is required.'
        },
        email: {
          required: 'The email is required.',
          email: 'The email needs to be valid.'
        },
        subject: {
          required: 'The subject is required.'
        },
        message: {
          required: 'The message is required.'
        }
      },
      errors: {
        name: [],
        email: [],
        subject: [],
        message: []
      }
    }
  },
  methods: {
    handleContactForm (event) {
      this.sent = true
      this.errors = {
        name: [],
        email: [],
        subject: [],
        message: []
      }
      this.$parent.success = false
      
      this.checkName(this.name)
      
      this.checkEmail(this.email)
      
      this.checkSubject(this.subject)
      
      this.checkMessage(this.message)
      
      let errorCount = 0;
      for (let field in this.errors) {
        this.errors[field].length >= 1 && errorCount++
      }
      
      if (errorCount === 0) {
        this.$parent.success = true
        return this.$parent.success
      }
      
      this.$parent.success = false
      return this.$parent.success
    },
    checkName (name) {
      if (!this.filled(name)) {
        this.errors.name.push({ name: 'required', msg: this.errorMessages.name.required })
      }
    },
    checkEmail (email) {
      if (!this.filled(email)) {
        this.errors.email.push({ name: 'required', msg: this.errorMessages.email.required })
      }
      
      if (!this.validEmail(email)) {
        this.errors.email.push({ name: 'required', msg: this.errorMessages.email.email })
      }
    },
    checkSubject (subject) {
      if (!this.filled(this.subject)) {
        this.errors.subject.push({ name: 'required', msg: this.errorMessages.subject.required })
      }
    },
    checkMessage (message) {
      if (!this.filled(this.message)) {
        this.errors.message.push({ name: 'required', msg: this.errorMessages.message.required })
      }
    },
    filled (value) {
      return (value && value.trim()) ? true : false
    },
    validEmail (email) {
      const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
      return regex.test(email)
    }
  },
  template: `
    <form class="contact-form" :class="{ 'success': this.$parent.success }" @submit.prevent="handleContactForm">
      <form-field field-name="name" field-id="1" :value="name" @input="name = $event"></form-field>

      <form-field field-name="email" field-id="2" :value="email" @input="email = $event"></form-field>

      <form-field field-name="subject" field-id="3" :value="subject" @input="subject = $event"></form-field>

      <form-field-text field-name="message" field-id="4" :value="message" @input="message = $event"></form-field-text>

      <form-field-button field-id="5">Contact us</form-field-button>
    </form>
  `
}

const app = new Vue({
  el: '#app',
  data: {
    success: false
  },
  components: {
    'success-box': successBox,
    'contact-form': contactForm
  }
})
              
            
!
999px

Console