7.9 KiB
7.9 KiB
Dynamic Modal Component Documentation
Overview
A flexible and customizable modal component built with Vuetify's v-dialog. This component provides extensive configuration options and supports slot-based content rendering.
Basic Usage
<template>
<Modal
v-model:visible="isModalVisible"
:options="modalOptions"
@close="handleClose"
@confirm="handleConfirm"
>
<p>Your modal content goes here</p>
</Modal>
</template>
<script setup>
import { ref } from 'vue'
import Modal from './components/Modal.vue'
const isModalVisible = ref(false)
const modalOptions = {
title: 'My Modal',
maxWidth: '500px'
}
const handleClose = () => {
console.log('Modal closed')
}
const handleConfirm = () => {
console.log('Modal confirmed')
}
</script>
Props
visible (Boolean)
- Default:
false - Description: Controls the visibility state of the modal
- Usage: Use with
v-model:visiblefor two-way binding
options (Object)
- Default:
{} - Description: Configuration object for customizing modal behavior and appearance
Options Object Properties
Dialog Configuration
persistent(Boolean, default:false) - Prevents closing when clicking outside or pressing escapefullscreen(Boolean, default:false) - Makes the modal fullscreenmaxWidth(String, default:'500px') - Maximum width of the modalwidth(String) - Fixed width of the modalheight(String) - Fixed height of the modalattach(String) - Element to attach the modal totransition(String, default:'dialog-transition') - CSS transition namescrollable(Boolean, default:false) - Makes the modal content scrollableretainFocus(Boolean, default:true) - Retains focus within the modalcloseOnBack(Boolean, default:true) - Closes modal on browser back buttoncloseOnContentClick(Boolean, default:false) - Closes modal when clicking contentcloseOnOutsideClick(Boolean, default:true) - Closes modal when clicking outsidecloseOnEscape(Boolean, default:true) - Closes modal when pressing escape key
Styling Options
overlayColor(String) - Color of the backdrop overlayoverlayOpacity(Number) - Opacity of the backdrop overlayzIndex(Number) - Z-index of the modaldialogClass(String) - Additional CSS classes for the dialogcardClass(String) - Additional CSS classes for the cardcardColor(String) - Background color of the cardcardVariant(String) - Vuetify card variantelevation(Number) - Shadow elevation of the cardflat(Boolean) - Removes elevationnoRadius(Boolean) - Removes border radius
Header Configuration
title(String) - Modal title textshowHeader(Boolean, default:true) - Shows/hides the headershowHeaderDivider(Boolean) - Shows divider below headerheaderClass(String) - Additional CSS classes for headershowCloseButton(Boolean, default:true) - Shows/hides close buttoncloseButtonColor(String, default:'grey') - Color of close buttoncloseIcon(String, default:'mdi-close') - Icon for close button
Content Configuration
message(String) - Default message content (HTML supported)contentClass(String) - Additional CSS classes for content areacontentHeight(String) - Fixed height of content areacontentMaxHeight(String) - Maximum height of content areacontentMinHeight(String) - Minimum height of content areanoPadding(Boolean) - Removes padding from content area
Actions Configuration
showActions(Boolean, default:true) - Shows/hides action buttonsactionsClass(String) - Additional CSS classes for actions areaactionsAlign(String) - Alignment of action buttons ('left','center','right','space-between')
Button Configuration
showConfirmButton(Boolean, default:true) - Shows/hides confirm buttonconfirmButtonText(String, default:'Confirm') - Text for confirm buttonconfirmButtonColor(String, default:'primary') - Color of confirm buttonconfirmButtonVariant(String, default:'elevated') - Variant of confirm buttonshowCancelButton(Boolean, default:true) - Shows/hides cancel buttoncancelButtonText(String, default:'Cancel') - Text for cancel buttoncancelButtonColor(String, default:'grey') - Color of cancel buttoncancelButtonVariant(String, default:'text') - Variant of cancel buttonloading(Boolean) - Shows loading state on confirm button
Behavior Configuration
autoCloseOnConfirm(Boolean, default:true) - Auto-closes modal after confirmautoCloseOnCancel(Boolean, default:true) - Auto-closes modal after cancelonOpen(Function) - Callback function when modal opensonClose(Function) - Callback function when modal closes
Events
update:visible- Emitted when visibility state changesclose- Emitted when modal is closedconfirm- Emitted when confirm button is clickedcancel- Emitted when cancel button is clickedoutside-click- Emitted when clicking outside the modalescape-key- Emitted when escape key is pressed
Slots
Default Slot
<Modal>
<p>Your content here</p>
</Modal>
Title Slot
<Modal>
<template #title>
<v-icon class="mr-2">mdi-account</v-icon>
Custom Title
</template>
</Modal>
Actions Slot
<Modal>
<template #actions="{ close, options }">
<v-btn @click="customAction">Custom Action</v-btn>
<v-btn @click="close">Close</v-btn>
</template>
</Modal>
Usage Examples
Basic Modal
const basicOptions = {
title: 'Information',
maxWidth: '400px'
}
Confirmation Modal
const confirmOptions = {
title: 'Confirm Action',
persistent: false,
confirmButtonText: 'Delete',
confirmButtonColor: 'error',
cancelButtonText: 'Keep'
}
Form Modal
const formOptions = {
title: 'Add New Item',
maxWidth: '600px',
persistent: true,
confirmButtonText: 'Save',
loading: isLoading.value
}
Fullscreen Modal
const fullscreenOptions = {
fullscreen: true,
showActions: false,
scrollable: true
}
Custom Styled Modal
const customOptions = {
maxWidth: '500px',
cardColor: 'primary',
elevation: 12,
overlayOpacity: 0.8,
transition: 'scale-transition'
}
Advanced Usage
With Reactive Options
<script setup>
import { ref, computed } from 'vue'
const loading = ref(false)
const formValid = ref(false)
const modalOptions = computed(() => ({
title: 'Dynamic Title',
loading: loading.value,
confirmButtonText: formValid.value ? 'Save' : 'Validate First',
persistent: !formValid.value
}))
</script>
Multiple Modals
<template>
<!-- Each modal can have different configurations -->
<Modal v-model:visible="modal1" :options="options1">
Content 1
</Modal>
<Modal v-model:visible="modal2" :options="options2">
Content 2
</Modal>
</template>
Best Practices
- Use persistent modals for forms to prevent accidental data loss
- Set appropriate maxWidth for different screen sizes
- Use loading states for async operations
- Provide clear button labels that describe the action
- Use slots for complex content instead of the message option
- Handle all events to provide good user feedback
- Test keyboard navigation and accessibility features
Responsive Behavior
The modal automatically adjusts for mobile devices:
- Reduced padding on smaller screens
- Appropriate font sizes
- Touch-friendly button sizes
- Proper viewport handling
Accessibility
The component includes:
- Proper ARIA attributes
- Keyboard navigation support
- Focus management
- Screen reader compatibility
- High contrast support