Modal
The Modal component provides a flexible way to display content in a dialog. It supports various sizes and customizable content through its child components.
Import
To use the Modal component, first import it into your project:
Usage
To use the Modal, you need to pass an id
to the component
and include the data-modal-trigger
attribute with the same
id in the trigger element (e.g., a Button).
Basic Usage
Programmatic Control
You can control the modal programmatically using the openModal
and closeModal
functions. Below is an example that combines
both:
Example: Open Modal on Form Submit and Close Automatically
This example demonstrates how to open a modal programmatically when a form is submitted and then automatically close it after a few seconds.
Sizes
Overflow scroll
The ModalBody
component is prepared to handle long content.
It includes an overflow scroll to ensure that content can be scrolled
within the modal if it exceeds the available space.
Custom Styles
The Modal component includes several customizable CSS variables, allowing you to tailor its appearance to fit your design needs:
Available Variables
Example Customization
To customize the Modal styles, you can override these variables in your CSS:
Props
Modal
Attribute | Type | Description | Default |
---|---|---|---|
id | string | The unique identifier for the modal. | - |
size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | 'full' | The size of the modal. | md |
children | ModalHeader | ModalBody | ModalFooter | [ModalHeader, ModalBody, ModalFooter] | The content of the modal, including ModalHeader , ModalBody , and ModalFooter components. | - |
class | string | CSS class for custom styling. | - |
ModalHeader
Attribute | Type | Description | Default |
---|---|---|---|
children | any | The content of the header. Typically includes a title. | - |
class | string | CSS class for custom styling. | - |
ModalBody
Attribute | Type | Description | Default |
---|---|---|---|
children | any | The content of the body. Supports scrollable content. | - |
class | string | CSS class for custom styling. | - |
ModalFooter
Attribute | Type | Description | Default |
---|---|---|---|
children | any | The content of the footer. Typically includes action buttons. | - |
class | string | CSS class for custom styling. | - |
Accessibility
-
Focus Management: Ensure that focus is trapped within the modal when it is open, and returned to the trigger element when closed.
-
Keyboard Interactions: Allow users to close the modal with the
Esc
key and navigate interactive elements within the modal using theTab
key.