Modal
Display a modal dialog box, providing a title, content area, and action buttons.
When To Use
When requiring users to interact with the application without jumping to a new page to interrupt the user's workflow, you can use Modal to create a new overlay layer over the current page for user-getting feedback or information purposes.
Basic
Basic modal.
---
import Modal from '@/components/feedback/Modal.astro';
import Button from '@/components/general/Button.astro';
---
<Button onclick="window.Modal.open('example-modal')">
Open Modal
</Button>
<Modal id="example-modal" title="Edit Profile">
<p class="text-sm text-zinc-600 dark:text-zinc-400 mb-4">
Make changes to your profile here. Click save when you're done.
</p>
<Fragment slot="footer">
<Button variant="secondary" onclick="window.Modal.close('example-modal')">Cancel</Button>
<Button variant="primary" onclick="window.Modal.close('example-modal')">Save changes</Button>
</Fragment>
</Modal> Information modal dialog
In the various types of information modal dialog, only one button to close dialog is provided.
---
import Modal from '@/components/feedback/Modal.astro';
import Button from '@/components/general/Button.astro';
---
<Button onclick="window.Modal.open('modal-success')" variant="secondary">Success</Button>
<Button onclick="window.Modal.open('modal-error')" variant="secondary">Error</Button>
<Button onclick="window.Modal.open('modal-info')" variant="secondary">Info</Button>
<Button onclick="window.Modal.open('modal-warning')" variant="secondary">Warning</Button>
<Modal id="modal-success" variant="success" title="Payment Successful" description="Your payment has been processed successfully.">
<Fragment slot="footer"><Button onclick="window.Modal.close('modal-success')">Done</Button></Fragment>
</Modal>
<Modal id="modal-error" variant="error" title="Action Failed" description="There was a problem processing your request. Please try again.">
<Fragment slot="footer"><Button variant="danger" onclick="window.Modal.close('modal-error')">Close</Button></Fragment>
</Modal>
<Modal id="modal-info" variant="info" title="System Update" description="A new system update is available.">
<Fragment slot="footer"><Button onclick="window.Modal.close('modal-info')">Got it</Button></Fragment>
</Modal>
<Modal id="modal-warning" variant="warning" title="Are you sure?" description="This action cannot be undone.">
<Fragment slot="footer">
<Button variant="secondary" onclick="window.Modal.close('modal-warning')">Cancel</Button>
<Button variant="primary" onclick="window.Modal.close('modal-warning')">Confirm</Button>
</Fragment>
</Modal> Customized Parts
A more complex example which define a customized footer button bar or header.
---
import Modal from '@/components/feedback/Modal.astro';
import Button from '@/components/general/Button.astro';
---
<Button onclick="window.Modal.open('modal-custom')">Open Custom Modal</Button>
<Modal id="modal-custom" closable={false} width="600px">
<Fragment slot="header">
<div class="flex items-center gap-2 pb-4 border-b border-zinc-200 dark:border-zinc-800">
<div class="w-8 h-8 rounded bg-blue-500 flex items-center justify-center text-white font-bold">A</div>
<h2 id="custom-header" class="text-xl font-bold">Custom Header</h2>
</div>
</Fragment>
<p class="py-4 text-zinc-600 dark:text-zinc-400">
This modal cannot be closed by clicking outside or pressing Escape
because `closable={false}`. You must provide a button to close it.
</p>
<Fragment slot="footer">
<Button onclick="window.Modal.close('modal-custom')" class="w-full">
Understood
</Button>
</Fragment>
</Modal> Utility Methods
The Modal component automatically exposes window.Modal.open(id) and window.Modal.close(id) global utilities to make it easy
to trigger modals from anywhere in your client-side JavaScript.
<script>
// Open a modal programmatically
window.Modal.open('my-modal');
// Close a modal programmatically
window.Modal.close('my-modal');
</script> API Reference
| Property | Description | Type | Default |
|---|---|---|---|
| id | A unique identifier for the modal, required for opening/closing. | string | - |
| title | Optional title text for the modal header. | string | - |
| description | Optional description text displayed below the title. | string | - |
| variant | Semantic style variant determining the icon and border color. | 'default' | 'success' | 'error' | 'info' | 'warning' | 'default' |
| closable | If true, shows a close button and allows closing via backdrop or ESC. | boolean | true |
| draggable | If true, allows the user to drag the modal by its header. | boolean | false |
| resizable | If true, allows the user to resize the modal from the bottom right. | boolean | false |
| width | Custom width (e.g., "600px", "80vw"). | string | - |
| height | Custom height (e.g., "400px", "50vh"). | string | - |