JSFiddle - React, Tailwind, and code Playground

by Christian Gambardella

HTML

<script src="https://cdn.jsdelivr.net/vue/1.0.24/vue.js"></script>
<script src="https://cdn.jsdelivr.net/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/bluebird/3.4.1/bluebird.min.js"></script>
<button id="show-modal">Show Modal</button>
<span id="answer"></span>

<!-- template for the modal component -->
<script type="x/template" id="modal-template">
  <div class="modal-mask" v-show="show" transition="modal">
    <div class="modal-wrapper">
      <div class="modal-container">

        <div class="modal-header">
          <slot name="header">
            default header
          </slot>
        </div>
        
        <div class="modal-body">
          <slot name="body">
            default body
          </slot>
        </div>

        <div class="modal-footer">
          <slot name="footer">
            default footer
            <button class="modal-default-button"
              @click="reply('no')">
              Cancel
            </button>
            <button class="modal-default-button"
              @click="reply('yes')">
              OK
            </button>

          </slot>
        </div>
      </div>
    </div>
  </div>
</script>
  
<!-- app -->
<script type="x/template" id="modal-app-template">
<div id="modal-app">  
  <modal>
    <!--
      you can use custom content here to overwrite
      default content
    -->
    <h3 slot="header">custom header</h3>
  </modal>
</div>
</script>

<h2>Questions?</h2>
<ul>
  <li>Can I put the template in the vue instance?</li>
  <li>How can I change the content?</li>
  <li>Can I pass one single DOM object to Vue instead of a css selector? (Answer: yes)</li>
</ul>

CSS

.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .5);
  display: table;
  transition: opacity .3s ease;
}

.modal-wrapper {
  display: table-cell;
  vertical-align: middle;
}

.modal-container {
  width: 300px;
  margin: 0px auto;
  padding: 20px 30px;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
  transition: all .3s ease;
  font-family: Helvetica, Arial, sans-serif;
}

.modal-header h3 {
  margin-top: 0;
  color: #42b983;
}

.modal-body {
  margin: 20px 0;
}

.modal-default-button {
  float: right;
}

/*
 * the following styles are auto-applied to elements with
 * v-transition="modal" when their visiblity is toggled
 * by Vue.js.
 *
 * You can easily play with the modal transition by editing
 * these styles.
 */

.modal-enter, .modal-leave {
  opacity: 0;
}

.modal-enter .modal-container,
.modal-leave .modal-container {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

JavaScript

var VueFactory = {
  components: {},
  registerComponent: function(name, component) {
    this.components[name] = component;
  },
  getComponent: function(name) {
    return this.components[name];
  },
  vue: function(name, options) {
    if (typeof options !== "object") {
      options = {};
    }
    if (!options.el) {
      throw new Error('[VueFactory] options.el cannot be missing');
    }
    if (typeof options.data != "object") {
      options.data = {};
    }
    if ((typeof options.resolve !== "function") ||
        (typeof options.resolve !== "function")) {
      console.info('[VueFactory] You can pass options.resolve and options.reject functions in case you want the Vue instance to return a value.');
    }
    options.components = {};
    options.components[name] = this.getComponent(name);
    options.events = {
      resolve: function(value) {
        console.log('this is this', this);
        this.$emit('resolved', value);
        if (typeof options.resolve === "function") {
          options.resolve(value);
        }
      },
      reject: function(value) {
        this.$emit('rejected', value);
        if (typeof options.reject === "function") {
          options.reject(value);
        }
      }
    };
    return new Vue(options);
  }
};

var Modal = Vue.extend({
  name: 'modal',
  
  template: '#modal-template',

  data: function() {
    return {
      answers: [
        {message: 'Yes', value: true},
        {message: 'No', value: false}
      ],
      body: 'This could kill a unicorn.',
      header: 'Are you sure?',
      show: true
    }
  },

  methods: {
    reply: function(answer) {
      this.show = false
      this.$dispatch('resolve', answer)
    }
  }
});

// How register components.
VueFactory.registerComponent('Modal', Modal);

$(document).on('click', '#show-modal', function(e) {
  e.preventDefault();
  var $el = $('<div id="modal-app"><modal></modal></div>');
  $('body').append($el);
  new Promise(function(resolve, reject) {
   ...