Accessibility
What the component does for you:
- The dialog box is
aria-modal="true"withrole="dialog", orrole="alertdialog"when you pass it. Usealertdialogonly for an interruption that reports a condition or demands a decision that cannot wait; it makes a screen reader read the description on entry instead of waiting to be asked. - It is named automatically by its own title when
titleor the#headerslot is used. PasslabelledBywhen the accessible name lives on an element inside your header slot, anddescribedBywhen a specific element is the description. - The box carries
tabindex="-1", so there is always somewhere legitimate for focus to sit even when every control inside is disabled. - Esc closes the topmost dialog only, and
closeOnEscapeturns it off for a wrapper that answers the key itself. - An overflowing body is a labelled, focusable
role="region", so it can be scrolled from the keyboard. busyandcloseDisabledexpress "work is in flight" in ARIA rather than only in colour.
Focus is handled
NbModal holds keyboard focus for as long as it is open. You do not wire this up, and there is nothing to copy into your application:
- Focus moves in on open. The first focusable control takes it, or the dialog box itself when the dialog has no controls at all.
- Tab and Shift+Tab cycle inside the dialog. They do not reach the page behind the scrim, which behind a delete dialog is the list the user is deleting from.
- Focus comes back to the trigger on every route out, including Esc, the close button and the scrim. A trigger that has since been unmounted (the row action of a row this dialog just deleted) is skipped rather than throwing.
- Focus that leaves by a route no keystroke explains is recovered: a click on the scrim, a
focus()from application code, or a control disabled out from under the caret. - A popup teleported out of the dialog still counts as inside it. An open
NbSelectlist,NbMenuorNbDatePickercalendar is a sibling of the dialog in the DOM, not a descendant, and the trap knows it. - Only the topmost dialog answers. A confirmation raised from inside a dialog takes the keys; the surface underneath keeps its state.
Nothing behind the scrim is marked inert, because these overlays teleport to <body> next to an application root the library does not own. The trap is what stands in for it.
Directing initial focus
The tab order is Carbon's: the body, then the footer's primary, then its secondary, and the close button last. The default landing is the first stop in that order, so a form opens on its first field and a dialog with no fields on its primary. The one exception is a danger primary: the default steps over it to the secondary, because a destructive button one Space from a stray keypress is the one landing a dialog must never pick. Point initialFocus elsewhere when the first field is not where the user starts:
<NbModal :open="open" title="Invite teammate" initial-focus="input" @close="open = false">initialFocus takes a CSS selector resolved inside the dialog, or a function returning the element, for a component ref: NbTextInput exposes its native element as nativeEl, so it is :initial-focus="() => firstFieldRef?.nativeEl".
Three rules it does not decide for you:
- Never point it at a destructive control. Focus Cancel, or leave the default. A stray Space on an autofocused Delete is a deleted record. Focus Cancel, or the dialog box itself.
- Focus a text control only when typing is the point. In a read-only detail dialog, leave it alone so a screen reader starts at the title instead of halfway down.
- A confirmation is still
NbConfirm, not anNbModalyou focused carefully. It runs its own trap with extra rules for its pending state and its type-to-confirm gate, and it sets:trap-focus="false"so the two do not pull against each other.
Upgrading from a hand-rolled trap
If your application has its own focus trap for dialogs (a copied useDialogFocus composable, for example), delete it: NbModal does all of the above. For a dialog with unusual requirements, set :trap-focus="false" on that dialog and NbModal keeps its hands off focus entirely.
The demo below is a plain NbModal with no focus code of its own. Open it from the keyboard and hold Tab: focus cycles close, name, notes, Cancel, Save and back to close, never reaching the page behind. Close it any way you like, including Esc and the scrim, and the trigger button is focused again. The readout is document.activeElement.
The general rules those come from are in Keyboard and Dialogs, rule 9.
Known gap: motion
The open and close transition is a fixed 200ms of opacity and a small scale, and it is not currently reduced under prefers-reduced-motion: reduce. NbConfirm sets its own duration to zero for that preference. Until NbModal does the same, an application that cares can neutralise it globally:
@media (prefers-reduced-motion: reduce) {
.nb-modal-enter-active,
.nb-modal-leave-active,
.nb-modal-enter-active .nb-modal--content,
.nb-modal-leave-active .nb-modal--content {
transition-duration: 0ms !important;
}
}Known gap: the close button's name
Modal.vue hardcodes aria-label="Close" on the header X. There is no prop for it, so it cannot be translated, which is an asymmetry with NbToast, where closeLabel exists and is documented. A non-English application either lives with one English word in the dialog or renders its own close control in the #header slot until a prop lands.