Modal is a dynamically rendered element that floats above the rest of a page's content, requiring user interaction before returning to the main application.
Modals are used to capture information or attention. They overlay the main content and prevent interaction with the page until the modal is dismissed.
<button daff-button (click)="showModal()" aria-haspopup="dialog">
Open Modal
</button>
When to use
When not to use
Import DAFF_MODAL_COMPONENTS into your component:
import { DAFF_MODAL_COMPONENTS } from '@daffodil/design/modal';
@Component({
selector: 'custom-component',
templateUrl: './custom-component.component.html',
imports: [
DAFF_MODAL_COMPONENTS,
],
})
export class CustomComponent {}
Deprecation notice:
DaffModalModuleis deprecated. Use the standalone component imports instead.
A modal is composed of a header, content, and actions, displayed in the order listed:
<daff-modal-header>
<h2 daffModalTitle>Modal Title</h2>
</daff-modal-header>
<daff-modal-content>
<p>Modal content goes here</p>
</daff-modal-content>
<daff-modal-actions>
<button daff-button>Cancel</button>
<button daff-button color="primary">Confirm</button>
</daff-modal-actions>
<daff-modal-header>: The header section containing the title and optional close button.[daffModalTitle]: The primary text summarizing the modal.<daff-modal-content>: The scrollable container for the modal's main content. Use this once per modal to wrap all body content.<daff-modal-actions>: The container for action buttons, positioned at the bottom of the modal with right-aligned buttons. If two buttons are needed, place the primary button on the left and the secondary button on the right.A modal can be dismissed via:
ESC key[daffModalClose] directive within <daff-modal-actions>.To hide the close button, set dismissible to false on <daff-modal-header>:
<daff-modal-header [dismissible]="false">
<h2 daffModalTitle>Modal Title</h2>
</daff-modal-header>
By default, modals are horizontally and vertically centered on the screen. You can position a modal at the top of the screen by passing a position configuration when opening the modal:
constructor(private modalService: DaffModalService) {}
showModal() {
this.modal = this.modalService.open(
ModalContentComponent,
{
position: {
vertical: 'top',
},
},
);
}
You can also adjust how far from the top the modal appears by setting offsetTop:
showModal() {
this.modal = this.modalService.open(
ModalContentComponent,
{
position: {
vertical: 'top',
offsetTop: '5rem',
}
},
);
}
Note: The horizontal position is always centered and cannot be customized.
Modal implements the Dialog (Modal) WAI-ARIA design pattern.
role="dialog"aria-modal="true"aria-labelledby linked to the [daffModalTitle] elementaria-labelledby through the DafffModalService if [daffModalTitle] is not usedaria-haspopup="dialog" to the element that opens the modal<button daff-button (click)="showModal()" aria-haspopup="dialog">Open Modal</button>
constructor(private modalService: DaffModalService) {}
showModal() {
this.modal = this.modalService.open(
BasicModalContentComponent,
{ ariaLabelledBy: 'Modal Title' },
);
}
| Key | Action |
|---|---|
Tab |
Moves focus to the next focusable element within the modal. |
ESC |
Closes the modal and returns focus to the triggering element. |