SlideOver.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <div>
  3. <transition v-if="show" name="fade" appear>
  4. <div class="inset-0 transition-opacity z-20 fixed" @keydown.esc="close()">
  5. <div
  6. class="bg-primaryLight opacity-90 inset-0 absolute"
  7. tabindex="0"
  8. @click="close()"
  9. ></div>
  10. </div>
  11. </transition>
  12. <aside
  13. class="
  14. bg-primary
  15. flex flex-col
  16. h-full
  17. max-w-full
  18. transform
  19. transition
  20. top-0
  21. ease-in-out
  22. right-0
  23. w-96
  24. z-30
  25. duration-300
  26. fixed
  27. overflow-auto
  28. "
  29. :class="show ? 'shadow-xl translate-x-0' : 'translate-x-full'"
  30. >
  31. <slot name="content"></slot>
  32. </aside>
  33. </div>
  34. </template>
  35. <script setup lang="ts">
  36. import { onMounted, watch } from "@nuxtjs/composition-api"
  37. const props = defineProps<{
  38. show: Boolean
  39. }>()
  40. const emit = defineEmits<{
  41. (e: "close"): void
  42. }>()
  43. watch(
  44. () => props.show,
  45. (show) => {
  46. if (process.client) {
  47. if (show) document.body.style.setProperty("overflow", "hidden")
  48. else document.body.style.removeProperty("overflow")
  49. }
  50. }
  51. )
  52. onMounted(() => {
  53. document.addEventListener("keydown", (e) => {
  54. if (e.keyCode === 27 && props.show) close()
  55. })
  56. })
  57. const close = () => {
  58. emit("close")
  59. }
  60. </script>