ModalDemo.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <script setup lang="ts">
  2. import { ref } from 'vue'
  3. const showModal = ref(false)
  4. </script>
  5. <template>
  6. <button class="modal-button" @click="showModal = true">Show Modal</button>
  7. <Teleport to="body">
  8. <Transition name="modal">
  9. <div v-show="showModal" class="modal-mask">
  10. <div class="modal-container">
  11. <p>Hello from the modal!</p>
  12. <div class="model-footer">
  13. <button class="modal-button" @click="showModal = false">
  14. Close
  15. </button>
  16. </div>
  17. </div>
  18. </div>
  19. </Transition>
  20. </Teleport>
  21. </template>
  22. <style scoped>
  23. .modal-mask {
  24. position: fixed;
  25. z-index: 200;
  26. top: 0;
  27. left: 0;
  28. width: 100%;
  29. height: 100%;
  30. background-color: rgba(0, 0, 0, 0.5);
  31. display: flex;
  32. align-items: center;
  33. justify-content: center;
  34. transition: opacity 0.3s ease;
  35. }
  36. .modal-container {
  37. width: 300px;
  38. margin: auto;
  39. padding: 20px 30px;
  40. background-color: var(--vp-c-bg);
  41. border-radius: 2px;
  42. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
  43. transition: all 0.3s ease;
  44. }
  45. .model-footer {
  46. margin-top: 8px;
  47. text-align: right;
  48. }
  49. .modal-button {
  50. padding: 4px 8px;
  51. border-radius: 4px;
  52. border-color: var(--vp-button-alt-border);
  53. color: var(--vp-button-alt-text);
  54. background-color: var(--vp-button-alt-bg);
  55. }
  56. .modal-button:hover {
  57. border-color: var(--vp-button-alt-hover-border);
  58. color: var(--vp-button-alt-hover-text);
  59. background-color: var(--vp-button-alt-hover-bg);
  60. }
  61. .modal-enter-from,
  62. .modal-leave-to {
  63. opacity: 0;
  64. }
  65. .modal-enter-from .modal-container,
  66. .modal-leave-to .modal-container {
  67. transform: scale(1.1);
  68. }
  69. </style>