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" @timedout="onTimedOut"></button-with-confirm>
  </div>
  
  <div class="author">
    <a rel="no-opener" target="_blank" href="https://www.damianofusco.com">Author: damianofusco.com</a>
  </div>
</div>
              
            
!

CSS

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

      &.confirm {
        background-color: #F90;
        color: #fff;
        
        .countdown-bar {
          opacity: 0.7;
          transition-property: width;
          transition-timing-function: linear;
          width: 0;
        }
      }
      
      .btn-label {
        color: #FFF;
        text-transform: uppercase;
        font-weight: bold;
      }
      
      .countdown-bar {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 10px;
        opacity: 0;
        background-color: white;
        transition: none;
      }
    }
  }
  
  .author {
    position: fixed;
    bottom: 0;
    font-size:10px;
    font-style: italic;
  }
}

              
            
!

JS

              
                
Vue.component('button-with-confirm', {
  template: `
    <button type="button" :class="computedCss" @click="onClick">
      <div class="countdown-bar" :style="countdownBarStyle"></div>
      <div class="ripple-container">
      </div>
      <span class="btn-label">{{ computedLabel }}</span>
    </button>
  `,
  props: {
    defaultLabel: {
      type: 'string',
      default: 'Confirm Button'
    },
    confirmLabel: {
      type: 'string',
      default: 'Are you sure?'
    },
    defaultCss: {
      type: 'string',
      default: 'btn'
    },
    timeoutSecs: {
      type: 'number',
      default: 5
    }
  },
  
  data() {
    return {
      defaultLabel: '',
      clickedOnce: false, // flag we'll use to track if button has been clicked already once
      countdown: 0 // optional for displaying a countdown progress
    };
  },
 
  computed: {
    computedLabel(){
      return this.clickedOnce ? `${ this.confirmLabel } ${ this.countdown }` : this.defaultLabel;
    },
    computedCss(){
      return this.clickedOnce ? `${ this.defaultCss } confirm` : this.defaultCss;
    },
    countdownBarStyle() {
      let transitionDuration = this.timeoutSecs;
      return `transition-duration: ${ transitionDuration }s`;
    }
  }, 
  
  created() {
    // private variables, non-reactive
    this.timeoutId = undefined;
    this.countdown = this.timeoutSecs;
    this.countdownIntervalId = 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
        // clear the timeoutId/countdownIntervalId
				this.reset();
        // Here we emit 'confirmed'
				this.$emit('confirmed');
			} 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.countdown = this.timeoutSecs;
				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
						self.reset();
				    self.$emit('timedout', this);
					}, timeoutMs);
        
        // this is just additional fancy stuff to show the countdown progress if you want:
				this.countdownIntervalId = setInterval(() => {
						--this.countdown;
						if (this.countdown <= 0) {
							// reset the countdown interval and value
              clearInterval(this.countdownIntervalId);
						}
					}, 1000);
			}
    },
    
    reset() {
      clearInterval(this.countdownIntervalId);
			clearTimeout(this.timeoutId);
			this.clickedOnce = false;
    }
  }
});

/* Vue app */
new Vue({
  el: '#app', 
 
  data: {
    debugMessage: 'Button with confirm'
  },
  
  methods: {
    onClickedOnce() {
      this.debugMessage = 'Clicked once';
    },
    onTimedOut() {
      this.debugMessage = 'Timed out';
    },
    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