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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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">
    {{ debugMessage }}
    <button-with-confirm ref="btn" @once="onClickedOnce" @confirmed="onConfirmed"></button-with-confirm>
  </div>
</div>
              
            
!

CSS

              
                body {
  margin: 20px;
  
  .container {
    width: 250px;
    display: grid;
    grid-template-rows: 50px 50px auto;
  
    .btn {
      cursor: pointer;
      padding: 10px 20px;
      border: 1px solid #ddd;
      outline: none;
      color: #fff;
      background-color: #42b883;
      border-radius: 4px;

      &.confirm {
        background-color: #ff4949;
        color: #fff;
      }
      
      .btn-label {
        text-transform: uppercase;
      }
    }
  }
}

              
            
!

JS

              
                
Vue.component('button-with-confirm', {
  template: `
    <button type="button" :class="computedCss" @click="onClick">
      <span class="btn-label">{{ computedLabel }}</span>
      <div class="ripple-container">
      </div>
    </button>
  `,
  props: {
    defaultLabel: {
      type: 'string',
      default: 'Confirm Button'
    },
    confirmLabel: {
      type: 'string',
      default: 'Are you sure?'
    },
    defaultCss: {
      type: 'string',
      default: 'btn'
    },
    timeoutSecs: {
      type: 'number',
      default: 3
    }
  },
  
  data() {
    return {
      defaultLabel: '',
      clickedOnce: false // flag we'll use to track if button has been clicked already once
    };
  },
 
  computed: {
    computedLabel(){
      return this.clickedOnce ? this.confirmLabel : this.defaultLabel;
    },
    computedCss(){
      return this.clickedOnce ? `${ this.defaultCss } confirm` : this.defaultCss;
    }
  }, 
  
  created() {
    // private variables, non-reactive
    this.timeoutId = undefined;
  },
  
  methods: {
    onClick() {
      
      if (this.clickedOnce) {
			  // If clickedOnce is true, the user has already clicked once, so this is the 2nd click within the time specified by the timeouteSecs property
        // Here we emit 'confirmed'
				this.$emit('confirmed');
        // also need to clear the timeoutId
				clearTimeout(this.timeoutId);
			} else {
				// If clickedOnce is false, the user has clicked on the button for the first time
        // Here we set the clickedOnce flag to true and emit the 'once' event (usually you would not care about knowing it has been clicked the first time, but if you do you can respond to the @once event where you will consume this component)
				this.clickedOnce = true;
				this.$emit('once', this);
          
        // then we invoke setTimeout with the milliseconds parameter calculated from the timeoutSecs property value:
        const timeoutMs = (this.timeoutSecs * 1000); // convert secs to ms
			  const self = this;
			  this.timeoutId = setTimeout(function() {
            // after timeoutMs has passed, we clear timeoutId and reset clickedOnce to false
						clearTimeout(self.timeoutId);
						self.clickedOnce = false;
					}, timeoutMs);
			}
    },
    
    reset() {
      clearTimeout(this.timeoutId);
			this.clickedOnce = false;
    }
  }
});

/* Vue app */
new Vue({
  el: '#app', 
 
  data: {
    debugMessage: 'Button with confirm'
  },
  
  methods: {
    onClickedOnce() {
      this.debugMessage = 'Clicked once';
    },
    onConfirmed() {
      this.debugMessage = 'Clicked again within timeout';
      this.$refs.btn.reset(); // this is just so we can reset the btn state here in this demo
    }
  }

});

              
            
!
999px

Console