Dialog
Features
- Supports modal and non-modal modes.
- Focus is automatically trapped when modal.
- Can be controlled or uncontrolled.
- Manages screen reader announcements with
TitleandDescriptioncomponents. - Esc closes the component automatically.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import all parts and piece them together.
<script setup>
import {
DialogClose,
DialogContent,
DialogDescription,
DialogOverlay,
DialogPortal,
DialogRoot,
DialogTitle,
DialogTrigger,
} from 'reka-ui'
</script>
<template>
<DialogRoot>
<DialogTrigger />
<DialogPortal>
<DialogOverlay />
<DialogContent>
<DialogTitle />
<DialogDescription />
<DialogClose />
</DialogContent>
</DialogPortal>
</DialogRoot>
</template>API Reference
Root
Contains all the parts of a dialog
Trigger
The button that opens the dialog
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
Portal
When used, portals your overlay and content parts into the body.
Overlay
A layer that covers the inert portion of the view when the dialog is open.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
Content
Contains content to be rendered in the open dialog
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
Close
The button that closes the dialog
Title
An accessible title to be announced when the dialog is opened.
If you want to hide the title, wrap it inside our Visually Hidden utility like this <VisuallyHidden asChild>.
Description
An optional accessible description to be announced when the dialog is opened.
If you want to hide the description, wrap it inside our Visually Hidden utility like this <VisuallyHidden asChild>. If you want to remove the description entirely, remove this part and pass :aria-describedby="undefined" to DialogContent.
Examples
Nested dialog
You can nest multiple layers of dialogs.
Close after asynchronous form submission
Use the controlled props to programmatically close the Dialog after an async operation has completed.
<script setup>
import { DialogContent, DialogOverlay, DialogPortal, DialogRoot, DialogTrigger } from 'reka-ui'
const wait = () => new Promise(resolve => setTimeout(resolve, 1000))
const open = ref(false)
</script>
<template>
<DialogRoot v-model:open="open">
<DialogTrigger>Open</DialogTrigger>
<DialogPortal>
<DialogOverlay />
<DialogContent>
<form
@submit.prevent="
(event) => {
wait().then(() => (open = false));
}
"
>
<!-- some inputs -->
<button type="submit">
Submit
</button>
</form>
</DialogContent>
</DialogPortal>
</DialogRoot>
</template>Scrollable overlay
Move the content inside the overlay to render a dialog with overflow.
// index.vue
<script setup>
import { DialogContent, DialogOverlay, DialogPortal, DialogRoot, DialogTrigger } from 'reka-ui'
import './styles.css'
</script>
<template>
<DialogRoot>
<DialogTrigger />
<DialogPortal>
<DialogOverlay class="DialogOverlay">
<DialogContent class="DialogContent">
...
</DialogContent>
</DialogOverlay>
</DialogPortal>
</DialogRoot>
</template>/* styles.css */
.DialogOverlay {
background: rgba(0 0 0 / 0.5);
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: grid;
place-items: center;
overflow-y: auto;
}
.DialogContent {
min-width: 300px;
background: white;
padding: 30px;
border-radius: 4px;
}However, there's a caveat to this approach, where user might click on the scrollbar and close the dialog unintentionally. There's no universal solution that would fix this issue for now, however you can add the following snippet to DialogContent to prevent closing of modal when clicking on scrollbar.
<DialogContent
@pointer-down-outside="(event) => {
const originalEvent = event.detail.originalEvent;
const target = originalEvent.target as HTMLElement;
if (originalEvent.offsetX > target.clientWidth || originalEvent.offsetY > target.clientHeight) {
event.preventDefault();
}
}"
>Custom portal container
Customise the element that your dialog portals into.
<script setup>
import { DialogContent, DialogOverlay, DialogPortal, DialogRoot, DialogTrigger } from 'reka-ui'
const container = ref(null)
</script>
<template>
<div>
<DialogRoot>
<DialogTrigger />
<DialogPortal to="container">
<DialogOverlay />
<DialogContent>...</DialogContent>
</DialogPortal>
</DialogRoot>
<div ref="container" />
</div>
</template>Disable close on Interaction outside
For example, if you have some global Toaster component that should not close the Dialog when clicking on it.
Detached triggers
A DialogTrigger does not have to be inside its DialogRoot. Create a handle with createDialogHandle() and pass it to the handle prop of the root and of any number of triggers. The triggers can live anywhere in the app, including other components and portals. Here every row of a table opens the same edit dialog with its own data, and a button opens it without a trigger through openWithPayload().
The dialog returns focus to the trigger that opened it on close. Only that trigger gets aria-expanded="true" and aria-controls. Its payload reaches the root's default slot as payload, next to activeTriggerId. While a non-modal dialog is open, pressing another of its triggers moves it to that trigger without closing it. A modal dialog covers the other triggers, so the same press closes it instead.
<script setup lang="ts">
import { createDialogHandle, DialogClose, DialogContent, DialogDescription, DialogOverlay, DialogPortal, DialogRoot, DialogTitle, DialogTrigger } from 'reka-ui'
interface Project { id: number, name: string }
const projects: Project[] = [
{ id: 1, name: 'Website' },
{ id: 2, name: 'Mobile app' },
]
const editDialog = createDialogHandle<Project>()
</script>
<template>
<table>
<tbody>
<tr
v-for="project in projects"
:key="project.id"
>
<td>{{ project.name }}</td>
<td>
<DialogTrigger
:handle="editDialog"
:payload="project"
>
Edit
</DialogTrigger>
</td>
</tr>
</tbody>
</table>
<button @click="editDialog.openWithPayload({ id: 0, name: 'Untitled' })">
New project
</button>
<DialogRoot
v-slot="{ payload }"
:handle="editDialog"
>
<DialogPortal>
<DialogOverlay />
<DialogContent>
<DialogTitle>Edit {{ payload?.name }}</DialogTitle>
<DialogDescription>Rename the project or change its settings.</DialogDescription>
<DialogClose>Save</DialogClose>
</DialogContent>
</DialogPortal>
</DialogRoot>
</template>handle.open(triggerId?) opens the dialog for the trigger with that id, or without a trigger. handle.openWithPayload(payload) opens it without a trigger and with that payload. handle.close() closes it. handle.isOpen is reactive. These calls report the reason 'imperative-action' through beforeUpdate:open and update:open, so details.cancel() can veto them. They do nothing while no root with the handle is mounted. details.trigger is the trigger's element when a trigger press opens, switches or closes the popup, or handle.open(triggerId) opens it. It is undefined for other changes, such as handle.close(), Escape or an outside press.
The handle itself holds no state. The mounted root owns it, and the root and the triggers only connect to the handle after they mount. A handle created at module scope is therefore safe under SSR: nothing from one request can reach another, and every trigger renders closed on the server. Prefer creating the handle in the <script setup> of the component that renders the root and passing it down. Use a module-scope handle when the triggers live in unrelated parts of the app. Use one mounted root per handle: a second root that mounts with the same handle takes it over and closes the first, with a dev warning. A root or trigger kept alive by <KeepAlive> disconnects from the handle while it is inactive.
Real-world examples
- Dialog with Unsaved Changes: Hold a dirty Dialog open by preventing its close events, and hand the decision to an AlertDialog.
- Responsive Dialog Drawer: Present the same content as a centred Dialog on desktop and a swipeable Drawer on small screens, driven by one media query.
- Dialog Command Menu: Combining Dialog and Combobox to enable users to search a command list in a modal.
- Command Menu with Tabs: A command palette whose results are scoped by Tabs, with Tab cycling scopes without leaving the search field.
- Dialog Gesture Driven: An iOS-style gesture driven modal sheet built with Reka UI, and Motion-v.
Accessibility
Adheres to the Dialog WAI-ARIA design pattern.
Close icon button
When providing an icon (or font icon), remember to label it correctly for screen reader users.
<template>
<DialogRoot>
<DialogTrigger />
<DialogPortal>
<DialogOverlay />
<DialogContent>
<DialogTitle />
<DialogDescription />
<DialogClose aria-label="Close">
<span aria-hidden="true">×</span>
</DialogClose>
</DialogContent>
</DialogPortal>
</DialogRoot>
</template>Close using slot props
Alternatively, you can use the close method provided by the DialogRoot slot props to programmatically close the dialog.
<script setup>
import { DialogContent, DialogOverlay, DialogPortal, DialogRoot, DialogTrigger } from 'reka-ui'
</script>
<template>
<DialogRoot v-slot="{ close }">
<DialogTrigger>Open</DialogTrigger>
<DialogPortal>
<DialogOverlay />
<DialogContent>
<form>
<!-- some inputs -->
<button type="submit" @click="close">
Submit
</button>
</form>
</DialogContent>
<DialogFooter>
<button type="submit" @click="close">
Submit
</button>
</DialogFooter>
</DialogPortal>
</DialogRoot>
</template>Keyboard Interactions
| Key | Description |
|---|---|
Space | Opens/closes the dialog |
Enter | Opens/closes the dialog |
Tab | Moves focus to the next focusable element. |
Shift + Tab | Moves focus to the previous focusable element. |
Esc | Closes the dialog and moves focus to DialogTrigger. |
Custom APIs
Create your own API by abstracting the primitive parts into your own component.
Abstract the overlay and the close button
This example abstracts the DialogOverlay and DialogClose parts.
Usage
<script setup>
import { Dialog, DialogContent, DialogTrigger } from './your-dialog'
</script>
<template>
<Dialog>
<DialogTrigger>Dialog trigger</DialogTrigger>
<DialogContent>Dialog Content</DialogContent>
</Dialog>
</template>Implementation
// your-dialog.ts
export { default as DialogContent } from 'DialogContent.vue'
export { DialogRoot as Dialog, DialogTrigger } from 'reka-ui'<!-- DialogContent.vue -->
<script setup lang="ts">
import type { DialogContentEmits, DialogContentProps } from 'reka-ui'
import { Cross2Icon } from '@radix-icons/vue'
import { DialogClose, DialogContent, DialogOverlay, DialogPortal, useForwardPropsEmits } from 'reka-ui'
const props = defineProps<DialogContentProps>()
const emits = defineEmits<DialogContentEmits>()
const forwarded = useForwardPropsEmits(props, emits)
</script>
<template>
<DialogPortal>
<DialogOverlay />
<DialogContent v-bind="forwarded">
<slot />
<DialogClose>
<Cross2Icon />
<span class="sr-only">Close</span>
</DialogClose>
</DialogContent>
</DialogPortal>
</template>Headless composable
useDialog holds the open state (open, onOpenChange(value, reason?), onOpenToggle()), the ids and the trigger, close, overlay, content, title and description part surfaces: aria-haspopup, aria-expanded, aria-controls, role="dialog", aria-labelledby, aria-describedby and data-state. With a handle it also tracks the detached trigger that opened it and its payload. Mounting and exit animations (Presence), focus trapping (FocusScope), dismissal on Escape and outside interaction, scroll locking, hiding the rest of the page from assistive technology and the portal are not part of it: compose them yourself, or use the components. It is pure, so it can be called outside setup(); pass a stable baseId when rendering on the server. The composable is experimental; see Headless composables.
import { useDialog } from 'reka-ui'
import { ref } from 'vue'
const saving = ref(false)
const dialog = useDialog({
onBeforeUpdate: (open, details) => {
// keep the dialog open while a save is in flight
if (!open && saving.value)
details.cancel()
},
})
dialog.onOpenChange(true) // details.reason === 'imperative-action'