Dialog
Dialogs are modal overlays that require user interaction. They're used for critical information, confirmation, or complex tasks that require focus.
When to use
- To confirm destructive actions (delete, remove, etc.)
- To collect information through forms
- To display important information that requires acknowledgment
- For focused tasks that shouldn't be interrupted
- When you need to prevent interaction with the rest of the page
Key features
- Modal behavior - Blocks interaction with the rest of the page
- Multiple sizes - Small, medium, large, and full-screen options
- Focus management - Traps focus within the dialog
- Keyboard support - ESC to close, Tab to navigate
- Customizable content - Header, body, and footer sections
- Accessible - Full ARIA support and screen reader friendly
Anatomy
A dialog consists of the following structure:
Main Structure
- Container (
.pm7-dialog
) - Main wrapper with behavior attributes - Overlay (
.pm7-dialog-overlay
) - Semi-transparent backdrop - Content (
.pm7-dialog-content
) - The dialog box container
Content Markers (Input)
These attributes are used on the root pm7-dialog
element or its direct children to define content and behavior:
data-pm7-dialog="id"
- Unique identifier for the dialogpm7-dialog-size
- Dialog size: sm, md, lg, xl, fullpm7-dialog-icon
- Icon type: info, warning, error, successpm7-show-close
- Shows the X close button in headerpm7-header-separator
- Add line separator under headerpm7-no-escape
- Prevents closing dialog with ESC keypm7-no-overlay-close
- Prevents closing dialog when clicking backdroppm7-header
- Marks the header section. Content within this element becomes the header.pm7-dialog-title
- Text for the main dialog title (used withpm7-header
)pm7-dialog-subtitle
- Text for the dialog subtitle/description (used withpm7-header
)pm7-body
- Marks the main content areapm7-footer
- Marks the footer section for actions
Generated Elements (Output)
Based on the content markers, the following HTML elements and classes are generated:
- Header Container (
.pm7-dialog-header
) - Header Text Container (
.pm7-dialog-header-text
) - Wraps title and description - Icon (
.pm7-dialog-icon
) - SVG icon based onpm7-dialog-icon
- Title (
.pm7-dialog-title
) - H2 element forpm7-dialog-title
- Description (
.pm7-dialog-description
) - P element forpm7-dialog-subtitle
- Close Button (
.pm7-dialog-close
) - Button with X icon whenpm7-show-close
is set - Header Separator (
.pm7-dialog-header-separator
) - Line whenpm7-header-separator
is set - Body Container (
.pm7-dialog-body
) - Footer Container (
.pm7-dialog-footer
)
Size Variants
.pm7-dialog-content--sm
- Small (400px max-width).pm7-dialog-content--md
- Medium (600px max-width, default).pm7-dialog-content--lg
- Large (800px max-width).pm7-dialog-content--xl
- Extra large (1000px max-width).pm7-dialog-content--full
- Full screen
Style Variants
.pm7-dialog--alert
- Error/warning style.pm7-dialog--success
- Success style.pm7-dialog--loading
- Loading state with spinner
Basic Dialog
Dialog Sizes
Alert Dialog
Form Dialog
Scrollable Content
Confirmation Dialog
Multi-Step Dialog
Dialog with Icon
Dialog with Description
Loading Dialog (Body Only)
Cookie Bar (Footer Only)
Custom Header with Avatar
Mixed Header (Generated + Custom)
Dialog Section Combinations
Installation
npm install @pm7/core
CSS Classes
Class | Description |
---|---|
Base Classes | |
.pm7-dialog |
Root dialog container (hidden by default, controlled by `data-state` attribute) |
.pm7-dialog-open |
Applied to <body> when any dialog is open to prevent scrolling |
.pm7-dialog-overlay |
Semi-transparent backdrop that covers the page when dialog is open |
.pm7-dialog-content |
Container for the dialog box itself (header, body, footer) |
Size Modifiers | |
.pm7-dialog-content--sm |
Small dialog (max-width: 400px) |
.pm7-dialog-content--md |
Medium dialog (max-width: 600px, default) |
.pm7-dialog-content--lg |
Large dialog (max-width: 800px) |
.pm7-dialog-content--xl |
Extra large dialog (max-width: 1000px) |
.pm7-dialog-content--full |
Full screen dialog |
Dialog Variants | |
.pm7-dialog--alert |
Alert variant (red header) |
.pm7-dialog--success |
Success variant (green header) |
.pm7-dialog--loading |
Loading variant with spinner |
.pm7-dialog--shake |
Shake animation effect |
Structure Classes | |
.pm7-dialog-header |
Dialog header section (generated by `data-pm7-header` attribute) |
.pm7-dialog-header-text |
Container for dialog title and description within the header |
.pm7-dialog-header-actions |
Container for icon and close button within the header |
.pm7-dialog-header-separator |
Horizontal line separator below the header |
.pm7-dialog-title |
Main title of the dialog (generated by `data-pm7-dialog-title` attribute) |
.pm7-dialog-description |
Subtitle or description of the dialog (generated by `data-pm7-dialog-subtitle` attribute) |
.pm7-dialog-icon |
Container for the dialog icon (generated by `data-pm7-dialog-icon` attribute) |
.pm7-dialog-close |
Close button (generated by `data-pm7-show-close` attribute) |
.pm7-dialog-body |
Main content area of the dialog (generated by `data-pm7-body` attribute) |
.pm7-dialog-footer |
Footer section for actions (generated by `data-pm7-footer` attribute) |
.pm7-dialog-spinner |
Loading spinner element |
Data Attributes
PM7 Dialog supports two approaches: Traditional HTML structure and the new Content Marker system that automatically generates dialog structure.
Root Dialog Attributes
Attribute | Type | Default | Description |
---|---|---|---|
data-pm7-dialog="id" |
String | Required | Unique identifier for the dialog |
data-pm7-dialog-size |
String | "md" | Dialog size: sm (400px), md (600px), lg (800px), xl (1000px), full |
data-pm7-show-close |
Boolean | false | Shows the X close button in header |
data-pm7-no-escape |
Boolean | false | Prevents closing dialog with ESC key |
data-pm7-no-overlay-close |
Boolean | false | Prevents closing dialog when clicking backdrop |
data-state |
String | "closed" | Dialog state: "open", "closing", or "closed" (managed automatically) |
Content Marker Attributes (for sections)
Attribute | Description | Generated Structure |
---|---|---|
data-pm7-header |
Marks element as header section | Generates .pm7-dialog-header with title, subtitle, icon, and close button |
data-pm7-dialog-title |
Title text for the dialog header | Creates .pm7-dialog-title element |
data-pm7-dialog-subtitle |
Subtitle/description text for the dialog header | Creates .pm7-dialog-description element |
data-pm7-dialog-icon |
Icon type: info, warning, error, success | Creates .pm7-dialog-icon with SVG |
data-pm7-header-separator |
Add horizontal line separator under header | Creates .pm7-dialog-header-separator element |
data-pm7-body |
Marks element as body section | Wraps content in .pm7-dialog-body |
data-pm7-footer |
Marks element as footer section | Wraps content in .pm7-dialog-footer |
Legacy Content Marker Attributes
Note: These attributes (without data-
prefix) are deprecated but still supported for backward compatibility.
Attribute | Description |
---|---|
pm7-header |
Same as data-pm7-header |
pm7-subtitle |
Same as data-pm7-subtitle |
pm7-body |
Same as data-pm7-body |
pm7-footer |
Same as data-pm7-footer |
Basic Usage
<!-- Content Marker System (Recommended) -->
<div class="pm7-dialog"
data-pm7-dialog="my-dialog"
data-pm7-dialog-size="md"
data-pm7-show-close>
<div data-pm7-header
data-pm7-dialog-title="Dialog Title"
data-pm7-dialog-subtitle="Optional subtitle text"
data-pm7-header-separator>
</div>
<div data-pm7-body>
<p>Your dialog content goes here.</p>
</div>
<div data-pm7-footer>
<button class="pm7-button pm7-button--outline" onclick="closeDialog('my-dialog')">
Cancel
</button>
<button class="pm7-button pm7-button--primary">
Confirm
</button>
</div>
</div>
<!-- Dialog with Icon -->
<div class="pm7-dialog"
data-pm7-dialog="info-dialog"
data-pm7-dialog-size="sm">
<div data-pm7-header
data-pm7-dialog-title="Information"
data-pm7-dialog-icon="info"
data-pm7-header-separator>
</div>
<div data-pm7-body>
<p>This is an informational message.</p>
</div>
<div data-pm7-footer>
<button class="pm7-button pm7-button--primary" onclick="closeDialog('info-dialog')">
Got it
</button>
</div>
</div>
<!-- Traditional HTML Structure -->
<div class="pm7-dialog" data-pm7-dialog="traditional-dialog">
<div class="pm7-dialog-overlay"></div>
<div class="pm7-dialog-content pm7-dialog-content--md">
<div class="pm7-dialog-header">
<h2 class="pm7-dialog-title">Dialog Title</h2>
<button class="pm7-dialog-close" aria-label="Close">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<line x1="18" y1="6" x2="6" y2="18"/>
<line x1="6" y1="6" x2="18" y2="18"/>
</svg>
</button>
</div>
<div class="pm7-dialog-body">
<p>Dialog content goes here.</p>
</div>
<div class="pm7-dialog-footer">
<button class="pm7-button pm7-button--outline" onclick="closeDialog('traditional-dialog')">
Cancel
</button>
<button class="pm7-button pm7-button--primary">
Confirm
</button>
</div>
</div>
</div>
<!-- Trigger Button -->
<button class="pm7-button" onclick="openDialog('my-dialog')">
Open Dialog
</button>
JavaScript API
The following functions are globally available for convenience, but can also be imported from @pm7/core
:
openDialog(id)
: Opens a dialog by its `pm7-dialog` ID.closeDialog(id)
: Closes a dialog by its `pm7-dialog` ID.pm7Alert(message, options)
: Shows a simple alert dialog.pm7Confirm(message, callback, options)
: Shows a confirmation dialog.
import { PM7Dialog, createDialog, pm7Alert, pm7Confirm } from '@pm7/core';
// Initialize a dialog (for traditional structure)
const dialogElement = document.querySelector('[data-pm7-dialog="my-dialog"]');
const dialog = new PM7Dialog(dialogElement);
// Open/close programmatically
dialog.open();
dialog.close();
// Add shake animation
dialog.shake();
// Set loading state
dialog.setLoading(true);
// Listen for events
dialogElement.addEventListener('pm7-dialog-open', () => {
console.log('Dialog opened');
});
dialogElement.addEventListener('pm7-dialog-close', () => {
console.log('Dialog closed');
});
// Global functions (auto-loaded)
openDialog('my-dialog');
closeDialog('my-dialog');
// Programmatic dialog creation
const dialog = createDialog({
id: 'dynamic-dialog',
title: 'Dynamic Dialog',
subtitle: 'Created programmatically',
body: '<p>This dialog was created with JavaScript.</p>',
footer: `
<button class="pm7-button pm7-button--primary" onclick="closeDialog('dynamic-dialog')">
Close
</button>
`,
size: 'md',
showClose: true,
icon: 'info'
});
// Utility functions
pm7Alert('This is an alert message');
pm7Confirm('Are you sure?', (confirmed) => {
if (confirmed) {
console.log('User confirmed');
}
});
CSS Customization
Dialogs support dark mode and can be customized with CSS variables:
:root {
--pm7-dialog-bg: white;
--pm7-dialog-overlay-bg: rgb(0 0 0 / 0.5);
}
[data-theme="dark"] {
--pm7-dialog-bg: #1a1a1a;
--pm7-dialog-overlay-bg: rgb(0 0 0 / 0.8);
}
React Usage
Note: The React wrapper will need to be updated to support the new structure. The vanilla JavaScript version uses the new content markers as shown above.
// Future React API (to be implemented)
import { Dialog } from '@pm7/react';
function MyComponent() {
return (
<div className="pm7-dialog" data-pm7-dialog="react-dialog">
<div data-pm7-header data-pm7-dialog-title="Edit Profile"></div>
<div data-pm7-body>
<p>Dialog content goes here</p>
</div>
<div data-pm7-footer>
<button className="pm7-button pm7-button--outline">
Cancel
</button>
<button className="pm7-button pm7-button--primary">
Save Changes
</button>
</div>
</div>
);
}
Accessibility
- Focus trap: Focus stays within dialog while open
- Escape key: Closes the dialog (unless
pm7-no-escape
is set) - Click outside: Closes dialog by clicking overlay (unless
pm7-no-overlay-close
is set) - Focus restoration: Focus returns to trigger element on close
- ARIA attributes: Proper roles and labels for screen readers
- Heading hierarchy: Semantic heading structure
- Keyboard navigation: Tab through interactive elements
Copy this link that can be used as documentation for working with this component:
🤖 AI-Optimized Documentation
This link contains complete Markdown documentation that is perfectly readable by AI assistants, developers, and other automated systems. Directly accessible on GitHub - including all essential PM7 Dialog implementation guidance.
Perfect for ChatGPT, Claude, and other AI code assistants