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

              
                <template id="masker">
  <transition @enter="maskEnter" @leave="maskLeave" :css="false">
    <div v-if="isActive" class="c-masker"></div>
  </transition>
</template>

<template id="mask-to-modal">
  <component :is="tag">
    <slot></slot>
  </component>
</template>

<template id="target">
  <transition appear @enter="targetEnter" @after-enter="transitionAfterEnter" @leave="targetLeave" :css="false">
    <slot></slot>
  </transition>
</template>

<template id="trigger">
  <transition appear @enter="triggerEnter" @after-enter="transitionAfterEnter" @leave="triggerLeave" :css="false">
    <slot></slot>
  </transition>
</template>

<div id="app">
	<mask-to-modal tag="main">
		<trigger>
			<button class="btn"
				v-if="!isOpen"
				@click="isOpen = true"
			>
				Open Modal
			</button>
   	</trigger>
		
		<target>
			<div class="modal" v-if="isOpen">
				<div class="modal-bg"></div>
				<div class="modal-card card" ref="modal">
					Hi person!
					<br/>
					
					<button class="btn" @click="isOpen = false">
						Close Modal
					</button>
				</div>
			</div>
 		</target>
		
		<masker :is-active="isOpen"></masker>
	</mask-to-modal>
</div>
              
            
!

CSS

              
                $bgLight: royalblue;
$radius: 3px;

body {
	box-sizing: border-box;
	font-family: sans-serif;
	text-align: center;
	font-size: 1.2rem;
}

* { box-sizing: inherit };

.c-masker {
  pointer-events: none;
  position: fixed;
  opacity: 0;
  background: $bgLight;
  border-radius: $radius;
  z-index: 60;
	will-change: width, height, top, left;
}

// -----

.btn {
	margin: 1rem;
	padding: 0.5rem 2rem;
	background: $bgLight;
	color: white;
	border: none;
	border-radius: $radius;
	font-size: 1rem;
	cursor: pointer;
}

.modal {
  position: absolute;
	bottom: 0; left: 0;
  right: 0; top: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	
	&-card {
		background: white;
		border-radius: $radius;
		position: relative;
		text-align: center;
		padding: 2rem;
	}
	
	&-bg {
		bottom: 0;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
		background-color: rgba(10, 10, 10, 0.86);
	}
}
              
            
!

JS

              
                // Originally implemented to GroupIncome:
// - https://github.com/okTurtles/group-income-simple/tree/master/frontend/views/components/Transitions/MaskToModal
 
const mixinTransitions = {
  data () {
    return {
      config: {
        // Trigger/Target time to fadeIn/Out - give a delay, so the Masker starts first.
        fade: 75,
        fadeDelay: 50,
        // Target/Masker time to fade back to original position - slightly faster
        fadeBack: 50,
        // Masker time to animate between the Target and a Trigger (and vice versa)
        maskerTime: 250,
        // A little big less than the time for Target/Trigger to fadeIn + maskerTime (adjust to the eye perception)
        delay: 300
      }
    }
  },
  inject: ['MaskToModal'],
  beforeMount () {
    // Triggers & Target need to be invisible at the first frame
    // Safari has a flickering bug on the first frame when entering.
    // That happens right before elementStartsInvisible() is called.
    // so we need to force it to be invisible before it enters the DOM.
    this.$vnode.data.style = { opacity: 0 }
  },
  methods: {
    // -- Trigger animations
    triggerEnter (el, complete) {
      console.log('triggerEnter')
      this.updateSpecsOf(el, 'trigger')

      this.elementStartsInvisible(el)
      // Fades In only after the masker animation is completed.
      Velocity(el, { opacity: 1 }, { duration: this.config.fade, delay: this.config.delay, complete })
    },
    triggerLeave (el, complete) {
      console.log('triggerLeave')
      this.updateSpecsOf(el, 'trigger')

      // Fade out the targetInner after the user interaction...
      Velocity(el, { opacity: 0 }, { duration: this.config.fade, delay: this.config.fadeDelay, complete })
    },

    // -- Target animations
    targetEnter (el, complete) {
      console.log('targetEnter')
      const targetInner = this.getTargetCard()
      this.updateSpecsOf(targetInner, 'target')

      this.elementStartsInvisible(el)
      this.elementStartsInvisible(targetInner)

      // Fade in the Target after the Trigger has fade out...
      Velocity(el, { opacity: 1 }, { duration: this.config.fade, delay: this.config.fade })
      // And Fade in the targetInner only after the Masker has completed its transition animation
      Velocity(targetInner, { opacity: 1 }, { duration: this.config.fade, delay: this.config.delay, complete })
    },
    transitionAfterEnter (el) {
      console.log('transitionAfterEnter')
      // Trigger/Target has opacity: 0 by default, so let's force to stay 1 after the animation finishes.
      // Velocity(el, { opacity: 1 }, { duration: 0 })
      el.style.opacity = 1
    },
    targetLeave (el, complete) {
      console.log('targetLeave')
      const targetInner = this.getTargetCard()
      this.updateSpecsOf(targetInner, 'target')

      // Fade out the targetInner after the user interaction...
      Velocity(targetInner, { opacity: 0 }, { duration: this.config.fadeBack / 2, delay: this.config.fadeBack / 2 })

      // But only fades out complety the Target (that has the dark background)
      // after Masker goes back to the initial position
      Velocity(el, { opacity: 0 }, { duration: this.config.fade, delay: this.config.maskerTime, complete })
    },
    elementStartsInvisible (el) {
      // Use only opacity to fadeIn/Out because it's faster than
      // Velocity's 'fadeIn' feature (it doesn't use display)
      // Velocity(el, { opacity: 0 }, { duration: 0 })
      el.style.opacity = 0
    },

    // -- Masker animations
    // when animates to Target
    maskEnter (el, complete) {
      console.log('maskLeave')

      this.maskerTakesImmediatelyTheShapeOf(el, 'trigger', this.config.fade)
      this.maskerAnimatesToTheShapeOf(el, 'target', complete)
    },

    // when animates back to a Trigger
    maskLeave (el, complete) {
      console.log('maskEnter')

      this.maskerTakesImmediatelyTheShapeOf(el, 'target', this.config.fadeBack)
      this.maskerAnimatesToTheShapeOf(el, 'trigger', complete)
    },

    // -- Animation utils
    maskerTakesImmediatelyTheShapeOf (maskerEl, originalElement, duration) {
      // Masker takes imediately the specs (dimensions and position)
      // of the originalElement (Trigger or Target) size...
      Velocity(maskerEl, { opacity: 0, ...this.getSpecsOf(originalElement) }, { duration: 0 })

      // And fadesIn at the same time as the originalElement (trigger or target) is fading Out
      Velocity(maskerEl, { opacity: 1 }, { duration })
    },
    maskerAnimatesToTheShapeOf (el, originalElement, complete) {
      // The animation need to wait for the element to be loaded in the dom
      Vue.nextTick(() => {
        // Then it animates to the the originalElement specs creating the "growing effect"
        Velocity(el, { ...this.getSpecsOf(originalElement) }, { duration: this.config.maskerTime, easing: 'ease-out' })
        // And finally it fades out at the same time Target is fading in.
        Velocity(el, { opacity: 0 }, { duration: this.config.fade, complete })
      })
    },
    updateSpecsOf (el, elementId) {
      this.MaskToModal.updateSpecsOf(el, elementId)
    },
    getSpecsOf (elementId) {
      return this.MaskToModal.elementsSpecs[elementId]
    },
    getTargetCard () {
			//debugger;
			// OPTIMIZE: Only strange part... to get .card element
			// is different depending if is going to enter or leave.
			const card = this.$el.tagName ? // if rendered
						this.$el.getElementsByClassName('card')[0] :
						this.$slots.default[0].elm.lastElementChild;
			console.log(card);
      return card // know target size
    }
  }
}

Vue.component('masker', {
  template: '#masker',
  name: 'Masker',
  mixins: [ mixinTransitions ],
  props: {
	 isActive: Boolean,
	 elementsSize: Object
  },
});

Vue.component('mask-to-modal', {
	template: "#mask-to-modal",
  name: 'MaskToModal',
  props: {
    tag: {
      type: String,
      default: 'div'
    }
  },
  data () {
    return {
      config: {
        MaskToModal: {
          // Store the dimensions and position of an element (Trigger or Target)
          // so that Masker can access them when it needs to transform
          elementsSpecs: {}, // a Trigger or Target
          updateSpecsOf (el, elementId) {
            const { width, height, top, left } = el.getBoundingClientRect()

            this.elementsSpecs[elementId] = { width, height, top, left }
          }
        }
      }
    }
  },
  provide () {
    return {
      MaskToModal: this.config.MaskToModal
    }
  }
});

Vue.component('target', {
	template: '#target',
	name: 'Target',
  mixins: [ mixinTransitions ],
  props: ['targetCard'] // vue ref to modal's card
});

Vue.component('trigger', {
	template: '#trigger',
	name: 'Trigger',
  mixins: [ mixinTransitions ],
});

new Vue({
	el: '#app',
	data: () => ({
			isOpen: false,
	}),
	methods: {
		handleClick () {
			this.isOpen = !this.isOpen;
		}
	}
});
              
            
!
999px

Console