Skip to content

Modal

Modal is a component for rendering modal dialogs. It supports an optional container layout, vertical centering, and emits an event when the modal becomes visible. The modal content is wrapped in a card layout with a close button.

Note: This component relies on the following external components to function properly:


<script setup>
import Container from './Container.vue';
import Card from './Card.vue';
import { Icon } from '@iconify/vue';
import { watch } from 'vue';
defineProps({
withContainer: {
type: Boolean,
default: true,
},
classes: {
type: Object,
default: () => ({
container: '',
}),
},
cardProps: null,
containerProps: null,
title: String,
verticalCenter: Boolean,
});
const emit = defineEmits(['opened']);
const visible = defineModel('visible');
function onClose() {
visible.value = false;
}
watch(visible, (newValue) => {
if (newValue) {
emit('opened');
document.body.classList.add('overflow-y-hidden');
} else {
document.body.classList.remove('overflow-y-hidden');
}
});
</script>
<template>
<div
v-if="visible"
:class="[
'fixed inset-0 bg-black/30 py-20 z-10 overflow-y-auto',
verticalCenter ? 'flex items-center' : '',
]"
>
<component
:is="withContainer ? Container : 'div'"
v-motion-slide-top
:class="classes.container"
v-bind="containerProps"
>
<Card
v-click-outside="onClose"
:bordered="false"
:title="title"
v-bind="cardProps"
>
<template #action>
<button
class="cursor-pointer text-gray-900"
@click="onClose"
>
<Icon icon="tabler:x" />
</button>
</template>
<slot />
</Card>
</component>
</div>
</template>

PropTypeDefaultDescription
withContainerBooleantrueWraps the modal content with Container if true.
classesObject{ container: '' }Custom class object. The container key is used to apply classes to the container wrapper.
cardPropsObjectnullProps forwarded to the Card component.
containerPropsObjectnullProps forwarded to the container element (Container or div).
titleStringTitle text displayed in the modal card header.
verticalCenterBooleanfalseVertically centers the modal content if true.
PropTypeDescription
visibleBooleanControls the visibility of the modal dialog.
EventDescription
openedEmitted when the modal is opened
SlotDescription
defaultThe main content displayed inside the modal.