modal.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. <portal target="#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. </portal>
  50. </div>
  51. <script>
  52. const App = {
  53. components: { Modal },
  54. data: {
  55. showModal: false
  56. }
  57. }
  58. Vue.createApp().mount(App, '#app')
  59. </script>
  60. <style>
  61. .modal-mask {
  62. position: fixed;
  63. z-index: 9998;
  64. top: 0;
  65. left: 0;
  66. width: 100%;
  67. height: 100%;
  68. background-color: rgba(0, 0, 0, .5);
  69. display: table;
  70. transition: opacity .3s ease;
  71. }
  72. .modal-wrapper {
  73. display: table-cell;
  74. vertical-align: middle;
  75. }
  76. .modal-container {
  77. width: 300px;
  78. margin: 0px auto;
  79. padding: 20px 30px;
  80. background-color: #fff;
  81. border-radius: 2px;
  82. box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
  83. transition: all .3s ease;
  84. font-family: Helvetica, Arial, sans-serif;
  85. }
  86. .modal-header h3 {
  87. margin-top: 0;
  88. color: #42b983;
  89. }
  90. .modal-body {
  91. margin: 20px 0;
  92. }
  93. .modal-default-button {
  94. float: right;
  95. }
  96. /*
  97. * The following styles are auto-applied to elements with
  98. * transition="modal" when their visibility is toggled
  99. * by Vue.js.
  100. *
  101. * You can easily play with the modal transition by editing
  102. * these styles.
  103. */
  104. .modal-enter-from {
  105. opacity: 0;
  106. }
  107. .modal-leave-to {
  108. opacity: 0;
  109. }
  110. .modal-enter-from .modal-container,
  111. .modal-leave-to .modal-container {
  112. -webkit-transform: scale(1.1);
  113. transform: scale(1.1);
  114. }
  115. </style>