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:

---
import { Modal, ModalHeader, ModalBody, ModalFooter } from 'free-astro-components'
---

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

Modal Title

This is the modal body. You can add any content here.

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.

Form Submitted Successfully

Thank you for submitting the form. This modal will close automatically after a few seconds.

Sizes

Modal xs

This is the modal body. You can add any content here.

Modal sm

This is the modal body. You can add any content here.

Modal md

This is the modal body. You can add any content here.

Modal lg

This is the modal body. You can add any content here.

Modal xl

This is the modal body. You can add any content here.

Modal 2xl

This is the modal body. You can add any content here.

Modal 3xl

This is the modal body. You can add any content here.

Modal 4xl

This is the modal body. You can add any content here.

Modal full

This is the modal body. You can add any content here.

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.

Modal overflow

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati tenetur voluptatibus vero similique alias ab odio commodi sint maiores facere voluptate nostrum atque magni, delectus perferendis laborum minima saepe asperiores.

Custom Styles

The Modal component includes several customizable CSS variables, allowing you to tailor its appearance to fit your design needs:

Available Variables

--ac-modal-backdrop-color
--ac-modal-backdrop-filter
--ac-modal-background-color
--ac-modal-border-radius
--ac-modal-width-xs
--ac-modal-width-sm
--ac-modal-width-md
--ac-modal-width-lg
--ac-modal-width-xl
--ac-modal-width-2xl
--ac-modal-width-3xl
--ac-modal-width-4xl

Example Customization

To customize the Modal styles, you can override these variables in your CSS:

Custom Modal

This is the modal body. You can add any content here.

Props

Modal

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

children any The content of the header. Typically includes a title. -
class string CSS class for custom styling. -

ModalBody

children any The content of the body. Supports scrollable content. -
class string CSS class for custom styling. -

ModalFooter

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 the Tab key.