Dialog Component Guide for Native Web Components
Introduction
The Dialog components (nv-dialog, nv-dialogheader, and nv-dialogfooter) are native Web Components built with Stencil.js that provide accessible modal dialogs. As standard Web Components, they work seamlessly in any modern browser and can be used with or without JavaScript frameworks.
Key Features:
- Accessible modal dialogs with proper ARIA attributes and focus management
- Customizable header and footer components
- Backdrop click and escape key handling
- Form integration support
- Controlled and uncontrolled modes
- Framework agnostic
- Standards compliant Web Component
Installation
Install Nova's Web Components via npm:
npm install @nova-design-system/native
Then import the components in your JavaScript:
import '@nova-design-system/native/dist/nova-components/nova-components.css';
import '@nova-design-system/native/dist/nova-components/nova-components.js';
Basic Usage
Simple Dialog
The most basic dialog implementation uses nv-dialog with a trigger button:
<nv-dialog>
<button slot="trigger">Open Dialog</button>
<p>This is the dialog content.</p>
</nv-dialog>
Dialog with Header and Footer
Use nv-dialogheader and nv-dialogfooter for structured dialogs:
<nv-dialog>
<button slot="trigger">Open Dialog</button>
<nv-dialogheader heading="Dialog Title" subheading="Optional subtitle">
</nv-dialogheader>
<p>Dialog body content goes here.</p>
<nv-dialogfooter
primary-label="Confirm"
cancel-label="Cancel">
</nv-dialogfooter>
</nv-dialog>
Advanced Usage
Controlled Dialog
Control the dialog's open state programmatically:
<nv-dialog id="myDialog" controlled open="false">
<button slot="trigger">Open Dialog</button>
<nv-dialogheader heading="Controlled Dialog"></nv-dialogheader>
<p>This dialog is controlled via JavaScript.</p>
<nv-dialogfooter></nv-dialogfooter>
</nv-dialog>
<script>
const dialog = document.getElementById('myDialog');
const openButton = document.getElementById('openBtn');
openButton.addEventListener('click', () => {
dialog.open = true;
});
dialog.addEventListener('nvClose', () => {
console.log('Dialog closed');
});
</script>
Dialog with Custom Footer
Create custom footer actions using the slot:
<nv-dialog>
<button slot="trigger">Open Dialog</button>
<nv-dialogheader heading="Custom Footer"></nv-dialogheader>
<p>Dialog content here.</p>
<nv-dialogfooter>
<nv-button variant="text">Later</nv-button>
<nv-button variant="secondary">Save Draft</nv-button>
<nv-button variant="primary">Publish</nv-button>
</nv-dialogfooter>
</nv-dialog>
Undismissable Dialog
Prevent users from closing the dialog except through explicit actions:
<nv-dialog undismissable>
<button slot="trigger">Open Dialog</button>
<nv-dialogheader heading="Important Action"></nv-dialogheader>
<p>This dialog cannot be closed by clicking outside or pressing Escape.</p>
<nv-dialogfooter
primary-label="I Understand"
undismissable>
</nv-dialogfooter>
</nv-dialog>
Dialog with Form
Integrate forms within dialogs:
<nv-dialog>
<button slot="trigger">Add User</button>
<nv-dialogheader
heading="Add New User"
subheading="Fill in the user details">
</nv-dialogheader>
<form>
<nv-fieldtext label="Name" required></nv-fieldtext>
<nv-fieldtext label="Email" type="email" required></nv-fieldtext>
<nv-fieldselect label="Role" required>
<option value="user">User</option>
<option value="admin">Admin</option>
</nv-fieldselect>
</form>
<nv-dialogfooter
primary-label="Add User"
primary-type="submit">
</nv-dialogfooter>
</nv-dialog>
Click Outside to Close
Allow closing the dialog by clicking the backdrop:
<nv-dialog click-outside>
<button slot="trigger">Open Dialog</button>
<nv-dialogheader heading="Click Outside"></nv-dialogheader>
<p>Click the backdrop to close this dialog.</p>
<nv-dialogfooter></nv-dialogfooter>
</nv-dialog>
Component Composition
The dialog system consists of three components that work together:
nv-dialog (Container)
The parent container that manages the dialog behavior and visibility.
Properties:
open:boolean- Controls dialog visibilityundismissable:boolean- Prevents closing via backdrop/escapeclick-outside:boolean- Allows closing by clicking backdropcontrolled:boolean- Manual control via open prop/methodssize:"sm" | "md" | "lg" | "xl" | "full"- Dialog size
Events:
nvOpen- Fired when dialog opensnvClose- Fired when dialog closes
Methods:
show()- Opens the dialoghide()- Closes the dialog
Slots:
trigger- Trigger button/elementheader- Header contentdefault- Body contentfooter- Footer content
nv-dialogheader
Provides a structured header for the dialog.
Properties:
heading:string- Main heading textsubheading:string- Optional subtitle text
Slots:
default- Custom header content (overrides heading/subheading)
nv-dialogfooter
Provides action buttons in the footer.
Properties:
primary-label:string- Primary button textprimary-type:string- Primary button type (button/submit)primary-icon:string- Icon for primary buttoncancel-label:string- Cancel button textdisabled:boolean- Disables primary buttonundismissable:boolean- Hides cancel button
Events:
nvPrimary- Fired when primary button is clickednvCancel- Fired when cancel button is clicked
Slots:
default- Custom footer content (overrides default buttons)
Complete Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nova Dialog Example</title>
<script type="module">
import '@nova-design-system/native/dist/nova-components/nova-components.css';
import '@nova-design-system/native/dist/nova-components/nova-components.js';
</script>
</head>
<body>
<main class="container">
<!-- Confirmation Dialog -->
<nv-dialog id="confirmDialog">
<nv-button slot="trigger">Delete Item</nv-button>
<nv-dialogheader
heading="Confirm Deletion"
subheading="This action cannot be undone">
</nv-dialogheader>
<p>Are you sure you want to delete this item? This will permanently remove it from your account.</p>
<nv-dialogfooter
primary-label="Delete"
cancel-label="Cancel"
leading-icon="trash">
</nv-dialogfooter>
</nv-dialog>
</main>
<script>
const dialog = document.getElementById('confirmDialog');
dialog.addEventListener('nvPrimary', () => {
console.log('Item deleted');
dialog.hide();
});
dialog.addEventListener('nvCancel', () => {
console.log('Deletion cancelled');
});
</script>
</body>
</html>
Accessibility Best Practices
-
Always provide meaningful heading text:
<nv-dialogheader heading="Delete Confirmation"></nv-dialogheader> -
Use descriptive button labels:
<nv-dialogfooter primary-label="Confirm Delete" cancel-label="Keep Item"> </nv-dialogfooter> -
Focus management is handled automatically - focus moves to the dialog when opened and returns to the trigger when closed
-
Keyboard navigation:
Escapecloses the dialog (unlessundismissable)Tabcycles through focusable elementsEnterorSpaceon trigger opens the dialog
-
Use
undismissablesparingly - only for critical actions that require user acknowledgment
Common Patterns
Confirmation Dialog
<nv-dialog>
<nv-button slot="trigger" variant="danger">Delete Account</nv-button>
<nv-dialogheader
heading="Delete Account"
subheading="This action is permanent">
</nv-dialogheader>
<p>Are you sure you want to delete your account? All your data will be permanently removed.</p>
<nv-dialogfooter
primary-label="Delete Account"
cancel-label="Keep Account">
</nv-dialogfooter>
</nv-dialog>
Form Dialog
<nv-dialog size="lg">
<nv-button slot="trigger">Create Project</nv-button>
<nv-dialogheader heading="New Project"></nv-dialogheader>
<form id="projectForm">
<nv-fieldtext label="Project Name" required></nv-fieldtext>
<nv-fieldtextarea label="Description"></nv-fieldtextarea>
<nv-fielddate label="Start Date"></nv-fielddate>
</form>
<nv-dialogfooter
primary-label="Create"
primary-type="submit">
</nv-dialogfooter>
</nv-dialog>
Information Dialog
<nv-dialog click-outside>
<nv-button slot="trigger" variant="text">About</nv-button>
<nv-dialogheader heading="About This App"></nv-dialogheader>
<p>Version 1.0.0</p>
<p>Built with Nova Design System</p>
<nv-dialogfooter undismissable>
<nv-button>Close</nv-button>
</nv-dialogfooter>
</nv-dialog>
Framework Integration
React
import '@nova-design-system/native/dist/nova-components/nova-components.css';
import { NvDialog, NvDialogheader, NvDialogfooter, NvButton } from '@nova-design-system/react';
function ConfirmDialog() {
const handlePrimary = () => {
console.log('Confirmed');
};
return (
<NvDialog>
<NvButton slot="trigger">Confirm Action</NvButton>
<NvDialogheader heading="Confirmation" />
<p>Are you sure?</p>
<NvDialogfooter
primaryLabel="Confirm"
onNvPrimary={handlePrimary}
/>
</NvDialog>
);
}
Vue
<template>
<nv-dialog>
<nv-button slot="trigger">Open Dialog</nv-button>
<nv-dialogheader heading="Vue Dialog" />
<p>Dialog content</p>
<nv-dialogfooter
primary-label="OK"
@nvPrimary="handleConfirm"
/>
</nv-dialog>
</template>
<script setup>
import '@nova-design-system/native/dist/nova-components/nova-components.css';
import '@nova-design-system/native/dist/nova-components/nova-components.js';
const handleConfirm = () => {
console.log('Confirmed');
};
</script>
Angular
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import '@nova-design-system/native/dist/nova-components/nova-components.css';
import '@nova-design-system/native/dist/nova-components/nova-components.js';
@Component({
selector: 'app-dialog',
template: `
<nv-dialog>
<nv-button slot="trigger">Open Dialog</nv-button>
<nv-dialogheader heading="Angular Dialog"></nv-dialogheader>
<p>Dialog content</p>
<nv-dialogfooter
primaryLabel="Confirm"
(nvPrimary)="handleConfirm()">
</nv-dialogfooter>
</nv-dialog>
`,
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class DialogComponent {
handleConfirm() {
console.log('Confirmed');
}
}
Troubleshooting
Dialog not opening
- Ensure the trigger element is properly slotted:
<button slot="trigger">...</button> - Check if
controlledmode is enabled - you may need to setopenproperty manually - Verify that JavaScript is loaded and components are defined
Focus not trapped in dialog
- This is automatic behavior - if not working, check for JavaScript errors
- Ensure focusable elements exist within the dialog
Form submission not working
- Set
primary-type="submit"on the dialogfooter - Wrap your form fields in a
<form>element - Listen for the
nvPrimaryevent to handle submission
API Reference
For detailed API documentation including all properties, events, methods, and slots, see the component documentation tabs:
- nv-dialog - Main dialog container
- nv-dialogheader - Dialog header component
- nv-dialogfooter - Dialog footer component