SlideOver.vue 1.2 KB

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