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.
When to use
When not to use
To use modal in a standalone component, import DAFF_MODAL_COMPONENTS directly into your custom 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 {}
To use modal in a module, import DaffModalModule into your custom module:
import { NgModule } from '@angular/core';
import { DaffModalModule } from '@daffodil/design/modal';
import { CustomComponent } from './custom.component';
@NgModule({
declarations: [
CustomComponent,
],
exports: [
CustomComponent,
],
imports: [
DaffModalModule,
],
})
export class CustomComponentModule { }
This method is deprecated. It's recommended to update all custom components to standalone.
A modal consists of the following contents, displayed in the order listed:
<daff-modal-header>: The header section containing the title and optional close button.
[daffModalTitle]: The primary text summarizing the modal.
The close button in the header is shown by default but can be hidden by setting the dismissible property to false on the header.
<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 the left and the secondary button on the right.
<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>
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. |