Examples
Basic Modal
A simple modal dialog with form fields.
<button onclick="openModal('basicModal')">Sign up…</button><div class="modal-overlay" id="basicModal"> <div class="modal"> <div class="modal-header"> <h3 class="modal-title">Sign up</h3> </div> <div class="modal-content"> <form> <div class="form-group"> <label class="form-label">First Name:</label> <input class="form-input" type="text" autofocus> </div> <div class="form-group"> <label class="form-label">Last Name:</label> <input class="form-input" type="text"> </div> </form> </div> <div class="modal-footer"> <button class="btn btn-secondary" onclick="closeModal('basicModal')">Cancel</button> <button class="btn btn-primary" onclick="submitForm()">Submit</button> </div> </div></div>
Dismissable Modal
A modal that can be closed by clicking outside or pressing Escape.
<button onclick="openModal('dismissableModal')">Open dialog</button><div class="modal-overlay" id="dismissableModal" onclick="closeOnOutsideClick(event, 'dismissableModal')"> <div class="modal"> <div class="modal-content"> <h3>Notice</h3> <p>Click outside to close this dialog.</p> </div> </div></div>
Non-dismissable Modal
A modal that requires explicit user action to close.
<button onclick="openModal('nonDismissableModal')">Open dialog</button><div class="modal-overlay" id="nonDismissableModal"> <div class="modal"> <div class="modal-content"> <h3>Notice</h3> <p>You must close this dialog using the button below.</p> <button class="btn btn-primary" onclick="closeModal('nonDismissableModal')">Close</button> </div> </div></div>
Custom Drawer Modal
A side drawer implementation using modal overlay.
<button onclick="openDrawer()">Open drawer</button><div class="drawer-overlay" id="drawerModal"> <div class="drawer"> <div class="modal-header"> <h3 class="modal-title">Settings</h3> </div> <div class="modal-content"> <p>This is a custom drawer modal.</p> <button class="btn btn-primary" onclick="closeDrawer()">Close</button> </div> </div></div>
Programmatic Modal
A modal controlled programmatically with state management.
let isModalOpen = false;function toggleProgrammaticModal() { isModalOpen = !isModalOpen; if (isModalOpen) { openModal('programmaticModal'); } else { closeModal('programmaticModal'); }}
Features
The Modal component provides the following features:
- Styleable – States for entry and exit animations are included for easy styling. Both the underlay and overlay elements can be customized.
- Accessible – Content outside the modal is hidden from assistive technologies while it is open. The modal optionally closes when interacting outside, or pressing the Escape key.
- Focus management – Focus is moved into the modal on mount, and restored to the trigger element on unmount. While open, focus is contained within the modal.
- Scroll locking – Scrolling the page behind the modal is prevented while it is open, including in mobile browsers.
- Responsive – Adapts to different screen sizes and orientations.
- Customizable – Supports custom animations, positioning, and styling.
Anatomy
A modal consists of several key components:
Content
<div class="modal-overlay"> <div class="modal"> <div class="modal-header"> <h3 class="modal-title">Title</h3> </div> <div class="modal-content"> Content </div> <div class="modal-footer"> Actions </div> </div></div>
Interactions
Keyboard Interactions
- Escape – Closes the modal (if dismissable)
- Tab – Moves focus to the next focusable element within the modal
- Shift + Tab – Moves focus to the previous focusable element within the modal
Mouse Interactions
- Click outside modal – Closes the modal (if dismissable)
- Click close button – Closes the modal
- Click trigger button – Opens the modal
Touch Interactions
- Tap outside modal – Closes the modal (if dismissable)
- Tap close button – Closes the modal
- Tap trigger button – Opens the modal
Component List
All interactive components demonstrated:
- Modal Overlay
- Modal Container
- Dialog
- Button (Primary, Secondary, Danger)
- Text Input
- Form Elements
- Drawer
- Notification
- Tabs
Notification sent successfully!