modal.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <script src="../../dist/vue.global.js"></script>
  2. <!-- template for the modal component -->
  3. <script type="text/x-template" id="modal-template">
  4. <transition name="modal">
  5. <div v-if="show" class="modal-mask">
  6. <div class="modal-wrapper">
  7. <div class="modal-container">
  8. <div class="modal-header">
  9. <slot name="header">
  10. default header
  11. </slot>
  12. </div>
  13. <div class="modal-body">
  14. <slot name="body">
  15. default body
  16. </slot>
  17. </div>
  18. <div class="modal-footer">
  19. <slot name="footer">
  20. default footer
  21. <button class="modal-default-button" @click="$emit('close')">
  22. OK
  23. </button>
  24. </slot>
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </transition>
  30. </script>
  31. <script>
  32. const Modal = {
  33. template: '#modal-template',
  34. props: ['show'],
  35. }
  36. </script>
  37. <!-- modal container that lives outside of app root -->
  38. <div id="modal-container"></div>
  39. <!-- app -->
  40. <div id="app">
  41. <button id="show-modal" @click="showModal = true">Show Modal</button>
  42. <teleport to="#modal-container">
  43. <!-- use the modal component, pass in the prop -->
  44. <modal :show="showModal" @close="showModal = false">
  45. <template #header>
  46. <h3>custom header</h3>
  47. </template>
  48. </modal>
  49. </teleport>
  50. </div>
  51. <script>
  52. Vue.createApp({
  53. components: { Modal },
  54. data: () => ({
  55. showModal: false,
  56. }),
  57. }).mount('#app')
  58. </script>
  59. <style>
  60. .modal-mask {
  61. position: fixed;
  62. z-index: 9998;
  63. top: 0;
  64. left: 0;
  65. width: 100%;
  66. height: 100%;
  67. background-color: rgba(0, 0, 0, 0.5);
  68. display: table;
  69. transition: opacity 0.3s ease;
  70. }
  71. .modal-wrapper {
  72. display: table-cell;
  73. vertical-align: middle;
  74. }
  75. .modal-container {
  76. width: 300px;
  77. margin: 0px auto;
  78. padding: 20px 30px;
  79. background-color: #fff;
  80. border-radius: 2px;
  81. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
  82. transition: all 0.3s ease;
  83. font-family: Helvetica, Arial, sans-serif;
  84. }
  85. .modal-header h3 {
  86. margin-top: 0;
  87. color: #42b983;
  88. }
  89. .modal-body {
  90. margin: 20px 0;
  91. }
  92. .modal-default-button {
  93. float: right;
  94. }
  95. /*
  96. * The following styles are auto-applied to elements with
  97. * transition="modal" when their visibility is toggled
  98. * by Vue.js.
  99. *
  100. * You can easily play with the modal transition by editing
  101. * these styles.
  102. */
  103. .modal-enter-from {
  104. opacity: 0;
  105. }
  106. .modal-leave-to {
  107. opacity: 0;
  108. }
  109. .modal-enter-from .modal-container,
  110. .modal-leave-to .modal-container {
  111. -webkit-transform: scale(1.1);
  112. transform: scale(1.1);
  113. }
  114. </style>