opnform/resources/js/components/Modal.vue

155 lines
4.5 KiB
Vue
Raw Normal View History

2022-09-20 19:59:52 +00:00
<template>
2023-10-19 08:46:04 +00:00
<Teleport to="body">
2022-09-20 19:59:52 +00:00
<transition leave-active-class="duration-200" name="fade" appear>
<div v-if="show" class="fixed z-30 top-0 inset-x-0 px-4 pt-6 sm:px-0 sm:flex sm:items-top sm:justify-center">
<transition enter-active-class="transition-all delay-75 linear duration-300"
2023-10-14 15:31:30 +00:00
enter-from-class="opacity-0"
2022-09-20 19:59:52 +00:00
enter-to-class="opacity-100"
leave-active-class="transition-all linear duration-100"
2023-10-14 15:31:30 +00:00
leave-from-class="opacity-100"
2022-09-20 19:59:52 +00:00
leave-to-class="opacity-0"
appear @after-leave="leaveCallback"
>
<div v-if="show" class="fixed inset-0 transform" @click="close">
2023-10-14 15:31:30 +00:00
<div class="absolute inset-0 bg-gray-500 opacity-75" />
2022-09-20 19:59:52 +00:00
</div>
</transition>
<transition enter-active-class="delay-75 linear duration-300"
2023-10-14 15:31:30 +00:00
enter-from-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
2022-09-20 19:59:52 +00:00
enter-to-class="opacity-100 translate-y-0 sm:scale-100"
leave-active-class="linear duration-200" appear
2023-10-14 15:31:30 +00:00
leave-from-class="opacity-100 translate-y-0 sm:scale-100"
2022-09-20 19:59:52 +00:00
leave-to-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
2022-10-18 14:55:08 +00:00
<div v-if="show"
class="modal-content bg-white dark:bg-notion-dark rounded-lg overflow-y-auto shadow-xl transform transition-all sm:w-full"
2022-09-20 19:59:52 +00:00
:class="maxWidthClass"
>
2022-10-18 14:55:08 +00:00
<div class="bg-white relative dark:bg-notion-dark p-4 md:p-6">
2023-10-14 15:31:30 +00:00
<div v-if="closeable" class="absolute top-4 right-4">
2022-10-18 14:55:08 +00:00
<button class="text-gray-500 hover:text-gray-900 cursor-pointer" @click.prevent="close">
<svg class="h-6 w-6" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M18 6L6 18M6 6L18 18" stroke="currentColor" stroke-width="2" stroke-linecap="round"
2023-10-14 15:31:30 +00:00
stroke-linejoin="round"
/>
2022-10-18 14:55:08 +00:00
</svg>
</button>
</div>
<div class="sm:flex sm:flex-col sm:items-start">
2023-10-24 09:00:54 +00:00
<div v-if="$slots.hasOwnProperty('icon')" class="flex w-full justify-center mb-4">
2022-10-18 14:55:08 +00:00
<div class="w-14 h-14 rounded-full flex justify-center items-center"
2023-10-14 15:31:30 +00:00
:class="'bg-'+iconColor+'-100 text-'+iconColor+'-600'"
>
<slot name="icon" />
2022-10-18 14:55:08 +00:00
</div>
</div>
<div class="mt-3 text-center sm:mt-0 w-full">
2023-10-24 09:00:54 +00:00
<h2 v-if="$slots.hasOwnProperty('title')"
2023-10-14 15:31:30 +00:00
class="text-2xl font-semibold text-center text-gray-900"
>
<slot name="title" />
2022-10-18 14:55:08 +00:00
</h2>
2022-09-20 19:59:52 +00:00
</div>
</div>
<div class="mt-2 w-full">
2023-10-14 15:31:30 +00:00
<slot />
2022-09-20 19:59:52 +00:00
</div>
</div>
2023-10-24 09:00:54 +00:00
<div v-if="$slots.hasOwnProperty('footer')" class="px-6 py-4 bg-gray-100 text-right">
2023-10-14 15:31:30 +00:00
<slot name="footer" />
2022-09-20 19:59:52 +00:00
</div>
</div>
</transition>
</div>
</transition>
2023-10-19 08:46:04 +00:00
</Teleport>
2022-09-20 19:59:52 +00:00
</template>
<script>
export default {
name: 'Modal',
props: {
show: {
default: false
},
2022-10-18 14:55:08 +00:00
iconColor: {
default: 'blue'
},
2022-09-20 19:59:52 +00:00
maxWidth: {
default: '2xl'
},
closeable: {
default: true
},
portalOrder: {
default: 1
},
afterLeave: {
type: Function,
required: false
}
},
computed: {
2023-10-14 15:31:30 +00:00
maxWidthClass () {
2022-09-20 19:59:52 +00:00
return {
sm: 'sm:max-w-sm',
md: 'sm:max-w-md',
lg: 'sm:max-w-lg',
xl: 'sm:max-w-xl',
'2xl': 'sm:max-w-2xl'
}[this.maxWidth]
}
},
watch: {
show: {
immediate: true,
handler: (show) => {
if (show) {
document.body.style.overflow = 'hidden'
} else {
document.body.style.overflow = null
}
}
}
},
2023-10-14 15:31:30 +00:00
created () {
2023-10-24 09:00:54 +00:00
document.addEventListener('keydown', this.closeOnEscape)
},
2022-09-20 19:59:52 +00:00
2023-10-24 09:00:54 +00:00
beforeUnmount () {
document.removeEventListener('keydown', this.closeOnEscape)
2022-09-20 19:59:52 +00:00
},
methods: {
2023-10-14 15:31:30 +00:00
close () {
2022-09-20 19:59:52 +00:00
if (this.closeable) {
this.$emit('close')
}
},
2023-10-14 15:31:30 +00:00
leaveCallback () {
2022-09-20 19:59:52 +00:00
if (this.afterLeave) {
this.afterLeave()
}
2023-10-24 09:00:54 +00:00
},
closeOnEscape (e) {
if (e.key === 'Escape' && this.show) {
this.close()
}
2022-09-20 19:59:52 +00:00
}
}
}
</script>
<style lang="scss" scoped>
.modal-content {
max-height: calc(100vh - 40px);
}
</style>