When not to use
Each row is a common misuse of a modal. The right-hand column is the reason it hurts, not a matter of taste.
| What you have | Use instead | Because |
|---|---|---|
| A destructive confirmation | useConfirm() | A hand-built one loses the same five things. See below |
| More than four inputs, or a form that scrolls | A route (Forms) | A form scrolling inside a scrim is a page wearing a costume, with worse focus and no URL |
| The result of something that just finished | useToast() | A result is news, not a question. Nobody has to answer it, so nobody should have to dismiss it |
| A condition that stays true while the page is open | NbBanner variant="callout" | A standing fact that interrupts once and vanishes has been told to the user exactly once, at random |
| A failure that belongs to the page or the form | NbBanner variant="inline", in place | The user has to see the failure and the thing that failed at the same time |
| Something wrong with one field | NbMessage | The field is where the fix happens |
| Properties of a selected object, edited repeatedly | NbShellPanel (Inspectors) | Selecting the next object should not mean reopening a dialog. The object and its editor stay on screen together |
| A whole task with its own sub-steps | A route, or NbStepper inside one | If it deserves a back button, it deserves a URL |
| Marketing, onboarding, a tour, a "what's new" | Nothing, or NbWalkthrough | A dialog nobody opened is an interruption, not a conversation. Dialogs are user-initiated |
| Anything the user should be able to copy out of, or return to | A route or a panel | A dialog is dismissed by Esc, by the scrim and by a stray click. It is the wrong home for anything worth keeping |
Never assemble a confirmation out of NbModal
A confirmation built by hand at each call site varies at each call site, and it loses the same five things. None of them are decorations:
- Initial focus on cancel, so a stray Space does not commit.
role="alertdialog", so the consequence is read on entry rather than on request.- The pending lock, so a fired delete cannot be fired twice or dismissed as if it had not happened.
variant="danger"on the commit, so the destructive button is not the same button as Save.- The record's own name in the body, so "delete this environment?" becomes "delete
production?".
useConfirm() is one call and owns all five:
import { useConfirm } from '@nubisco/ui'
const confirm = useConfirm()
if (
await confirm({
title: 'Delete environment',
subjectLabel: 'Environment',
subject: environment.name,
message: 'Every entry, release and API key in it is deleted.',
confirmLabel: 'Delete environment',
})
) {
await api.deleteEnvironment(environment.id)
}The whole decision (when to confirm, when confirming is itself the bug, what the buttons say, how big a guard the blast radius earns) is settled once in Dialogs and destructive confirmation. It is not restated here.
The footer is already an action bar
Wrapping the footer buttons breaks this in a way you can see: a group squeezed into the right half of the dialog with padding fighting its edges.
Modal.vue renders the #footer slot inside a fluid NbButtonSet of xl buttons, Carbon's dialog footer:
<footer v-if="$slots.footer" class="nb-modal--footer">
<NbButtonSet fluid size="xl" class="nb-modal--actions">
<slot name="footer" />
</NbButtonSet>
</footer>Four consequences follow, and they are the whole contract:
- Every button is xl, 64px, with square corners. The set gives each
NbButtonits size, so footer buttons carry nosizeof their own, and the corners stay square under the rounded appearance because the buttons are the dialog's bottom edge. The dialog's own corner clips the outer two. - The widths are Carbon's. One button spans the right half. Two share the row equally. Three are a quarter each, gathered on the right, and the left quarter stays empty.
- The primary goes on the right, whatever order you write. The set reads each button's
variant: a filledprimaryordangeris drawn rightmost and comes first in the tab order, then the secondary, then the close button. Write Cancel first and the commit second, as it reads. - A wrapper counts as one child. Wrapping your buttons in an
NbPanelor anNbGridmakes the whole group the only child, so the group is squeezed to the right half and the nesting buys nothing that was not already there.
So the buttons go in bare: a secondary Cancel and a primary (or danger) commit, no size.
| Don't | Do |
|---|---|
#footer > NbPanel > NbGrid > buttons | #footer > buttons |
Inline style="padding: 12px" on a footer wrapper | Nothing. The bar owns its own edges |
variant="primary" on a destructive commit | variant="danger", and reach for useConfirm() before NbModal |
| "Confirm" and "Cancel" as the pair | The verb and its exit: "Delete environment" / "Cancel" |
| A third and fourth button | Two. Three only when the third is a genuinely different outcome |
Behaviour
Trigger
A dialog is opened by the user, from a control they pressed. A button, a menu item, a row action, a command palette entry, activated by click, by Enter or by Space. That control is also where focus returns when the dialog closes, which is the practical reason the rule exists: a dialog with no trigger has nowhere to give focus back to, and a keyboard user lands at the top of the document.
Three things are therefore not triggers:
- A timer or a route entering. A dialog nobody opened is an interruption. If the page has something to say on arrival, it is an
NbBannervariant="callout", which stays true for as long as the page does. - A background event finishing. A sync completing while the user is typing somewhere else is a toast, not a dialog. It stole the caret otherwise.
- A failed request from a page the user has already left. Report it where it belongs, or drop it. Do not open a dialog over whatever they are looking at now.
The one legitimate exception is a session that has ended: the application cannot continue and there is genuinely a decision to take. That is a blocking step, and it still needs the exit rule above.
Opening and closing
The modal is controlled. :open is yours, and close is a request, not an event that already happened:
<NbModal :open="open" @close="open = false" />close is emitted by the header close button, by Esc, and by a click on the scrim unless closeOnOverlay is false. There is one meaning for all three: the user asked to leave without committing. If leaving would lose work, do not silence the routes; ask, and let the answer decide (Forms, rule 17).
Esc answers the topmost dialog only
Every open instance listens on document, so before this was fixed a stack of two dialogs answered one keypress twice and the user lost both. The topmost surface is the last aria-modal element in document order, which for teleported overlays is the one painted on top. A wrapper that wants to answer the key itself sets :close-on-escape="false", which is what NbConfirm does.
The page behind does not scroll
Opening takes a counted share of a single page-scroll lock shared with NbCommandPalette and NbSpinner's page overlay. The count matters: three components writing body.style.overflow independently can leave the page permanently unscrollable with nothing on screen. The original value is read once when the first owner arrives and put back once when the last one leaves, so an application that sets overflow: hidden on the body for its own layout keeps it.
A scrollable body is reachable from the keyboard
When the body actually overflows it becomes role="region" with tabindex="0", labelled by the dialog title, so it can be scrolled without a pointer. It is marked only while it overflows, so a two-line dialog does not grow a stop in its tab order for nothing. Overflow is watched with a ResizeObserver on the box and a MutationObserver over its contents, because a body that fits at open time stops fitting when the window is resized, when slot content arrives from a fetch, or when a translated string wraps onto another line.
Validation and failure
A dialog that closes on a failed submit has thrown the user's typing away and told them about it somewhere they can no longer see. Three rules, in the order the failures arrive.
A form that fails validation keeps the dialog open. Do not close, do not clear, do not toast. Mark the offending field with NbTextInput's error prop and move focus to the first invalid one, so the fix happens where the mistake is:
<template>
<NbModal :open="open" title="Invite member" @close="close">
<NbTextInput
v-model="email"
label="Email"
:error="emailError"
@update:model-value="emailError = ''"
/>
<template #footer>
<NbButton variant="secondary" @click="close">Cancel</NbButton>
<NbButton variant="primary" @click="submit">Send invite</NbButton>
</template>
</NbModal>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const open = ref(false)
const email = ref('')
const emailError = ref('')
function submit() {
if (!email.value.includes('@')) {
emailError.value = 'Enter a full email address'
return
}
// ... commit, then close
}
</script>A server-side failure goes in an inline banner inside the body. NbBanner with variant="inline" and status="error", at the top of the body, above the fields, where the retry is one press away. Never a second dialog over the first, and never a toast: toasts paint above the scrim (see Toast, when not to use), so a failure raised from behind an open dialog covers the surface the user is reading and then removes itself.
<NbModal :open="open" title="Invite member" :busy="saving" @close="close">
<NbBanner v-if="failure" variant="inline" status="error" :title="failure">
Check the address and try again.
</NbBanner>
<NbTextInput v-model="email" label="Email" />
</NbModal>A body that is still fetching opens anyway. Delaying the open so the content can arrive gives the user a button that appears not to have worked, and they click it again. Open immediately and put NbSkeleton where the content will be, at the shape the content will have, so nothing jumps when it lands:
<NbModal :open="open" title="Run log" @close="open = false">
<NbSkeleton v-if="loading" variant="text" :lines="6" label="Loading the run log" />
<pre v-else>{{ log }}</pre>
</NbModal>If the fetch comes back with nothing, say so with NbEmptyState inside the body. A blank dialog and a loading dialog must not look the same: a single .empty-text for both makes a failed fetch read as "no contacts yet".
Work in flight
Once the commit has been fired, the dialog owes the user three things, and they are not optional.
- The pending affordance goes on the commit button, not over the body.
NbButtonhasloadingfor exactly this. An overlay spinner across the dialog hides the values the user just typed at the moment they most want to check them, and it makes the footer look pressable while it is not. - The commit disables itself while it is busy. A second press on a fired delete is a second delete.
:disabled="saving"on the commit, and:loading="saving"for the spinner. busyandcloseDisabledare set together, never one without the other.busymarks the dialogaria-busy="true", which is the half a screen reader hears;closeDisableddisables the X, which is the half everyone else sees. Set onlybusyand the user can dismiss a delete that is already running; set onlycloseDisabledand the wait is invisible to assistive technology.
<NbModal
:open="open"
title="Delete environment"
:busy="saving"
:close-disabled="saving"
:close-on-overlay="!saving"
@close="open = false"
>
<p>Every entry, release and API key in {{ name }} is deleted.</p>
<template #footer>
<NbButton variant="secondary" :disabled="saving" @click="open = false">
Cancel
</NbButton>
<NbButton variant="danger" :loading="saving" :disabled="saving" @click="commit">
Delete environment
</NbButton>
</template>
</NbModal>Two things that example is honest about. closeDisabled also switches off the Esc route, because Modal.vue's key handler returns early on it, so the dialog above has no keyboard exit at all while saving is true. That is correct for an irreversible commit already in flight and wrong for anything else, which is why it is bound to saving and not left on. And once every control in the dialog is disabled, the browser drops focus onto <body>, outside the dialog. NbModal recovers it onto the dialog box itself, which carries tabindex="-1" for exactly this: see Focus is handled.
A confirmation gets all of this without you writing any of it. This example exists to show the shape for the dialogs that are not confirmations.
Sizes
size caps both dimensions of the dialog: sm (520px wide, up to 72% of the viewport height), md (720px, 84%), lg (960px, 96%), xl (1280px, 98%) and immersive (1600px, the full height inside the margin). Height stays content-driven below the cap, so short dialogs never show empty space; long content scrolls inside the body once the cap is reached.
No size is a fixed box. The dialog is always width: 100% inside an overlay that keeps a margin all round, so every cap behaves as min(cap, viewport - margin): on a narrow screen immersive and sm are the same width, and neither can overflow the viewport or push the page into horizontal scroll.
<template>
<NbModal :open="open" size="lg" title="Large dialog" @close="open = false">
<p>Long content scrolls inside the body.</p>
</NbModal>
</template>Choosing between lg, xl and immersive
The two larger steps solve different problems, which is why both exist.
Reach for xl when a dialog is content-heavy but still a dialog: a form with two columns, a detail panel, a preview beside a description. It is a bigger box, not a different kind of surface.
Reach for immersive when the content is a comparison and the columns are the point: several candidates side by side, a diff, a wide table you actually have to read across. At lg the same content wraps into cramped columns and buries the differences in inner scrolling, which is the failure this size exists to fix.
immersive is deliberately not full-screen. It keeps its margin, so the page behind stays visible and the dialog still reads as something you are inside of rather than somewhere you navigated to. If a task genuinely owns the whole screen, it wants a route, not a modal.
Open the same four-column comparison at each size to see the difference:
<template>
<NbModal :open="open" size="immersive" title="Region comparison">
<NbGrid dir="row" gap="md" align="stretch">
<NbPanel v-for="region in regions" :key="region.id">
<!-- one column per region -->
</NbPanel>
</NbGrid>
</NbModal>
</template>Props
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | false | Whether the dialog is on screen. Controlled: you set it back to false on close |
title | string | undefined | Header title, an alternative to the #header slot |
size | 'sm' | 'md' | 'lg' | 'xl' | 'immersive' | 'md' | Caps the dialog width and height (see Sizes) |
closeOnOverlay | boolean | true | Whether a click on the scrim emits close |
closeOnEscape | boolean | true | Whether Esc emits close. Only the topmost dialog answers the key either way, and closeDisabled suppresses it independently |
role | 'dialog' | 'alertdialog' | 'dialog' | alertdialog for an interruption whose description should be announced on entry |
labelledBy | string | undefined | Id of the element naming the dialog. Defaults to the built-in title, so the common case needs nothing |
describedBy | string | undefined | Id of the element describing the dialog, usually its body |
busy | boolean | false | Marks the dialog aria-busy while work it started is in flight. Set it with closeDisabled, never alone |
closeDisabled | boolean | false | Disables the header close control (disabled and aria-disabled) and switches off the Esc route: the key handler returns early on it, so a dialog with it set and no footer exit has no keyboard way out |
The close button is NbCloseButton at lg. Its name comes from the string catalogue's modalClose, so translate it with provideNbStrings({ modalClose: '...' }).
closeAppearance is deprecated. 'corner' and 'button' used to draw a 56px square and a 32px ghost button. Both now render the same 48px NbCloseButton, so the prop still compiles and changes nothing.
Slots
| Slot | Description |
|---|---|
header | Title area. Rendering either this or title is what gives the dialog its accessible name |
default | Body. Scrolls, and becomes a focusable labelled region when it overflows |
footer | Action bar. Put buttons in bare: the slot is already inside a fluid NbButtonSet of xl buttons |
Events
| Event | Payload | Description |
|---|---|---|
close | none | The user asked to leave: the close button, Esc, or the scrim (unless closeOnOverlay is off) |
Exposed
Both are getter functions, not refs and not elements. defineExpose publishes dialogEl: () => contentRef.value, so the call form has parentheses:
const modalRef = ref<{
dialogEl: () => HTMLElement | null
bodyEl: () => HTMLElement | null
} | null>(null)
modalRef.value?.dialogEl()?.focus()| Exposed | Call | Returns | Description |
|---|---|---|---|
dialogEl | modalRef.value?.dialogEl() | HTMLElement | null | The dialog box, for a wrapper that manages focus (this is how NbConfirm finds it) |
bodyEl | modalRef.value?.bodyEl() | HTMLElement | null | The scrolling body element |
Both return null until the dialog is open and rendered. NbModal handles its own focus (see Focus is handled), so these are for a wrapper that needs the element for something else.
Z-index
The overlay uses --nb-zindex-modal. Components that render inside a dialog and must escape its DOM subtree (NbSelect's list, NbMenu, NbDatePicker's calendar) use the --nb-zindex-modal-* tier. Toasts sit above both, deliberately. See Z-index.