Dialog

A window overlaid on either the primary window or another dialog window, rendering the content underneath inert.

Features

  • Focus is automatically trapped.
  • Works in an uncontrolled way by default.
  • Can also be controlled.

Install the component from your command line.

npm install @radix-ui/react-dialog

Import the components and piece the parts together.

import * as Dialog from '@radix-ui/react-dialog';
export default () => (
<Dialog.Root>
<Dialog.Trigger />
<Dialog.Overlay />
<Dialog.Content>
<Dialog.Close />
</Dialog.Content>
</Dialog.Root>
);

Create your styled dialog component from the dialog primitives.

import { styled } from 'path-to/stitches.config';
import * as Dialog from '@radix-ui/react-dialog';
const StyledOverlay = styled(Dialog.Overlay, {
backgroundColor: 'rgba(0, 0, 0, .15)',
position: 'fixed',
top: 0,
right: 0,
bottom: 0,
left: 0,
});
const StyledContent = styled(Dialog.Content, {
position: 'fixed',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
minWidth: 200,
maxWidth: 'fit-content',
maxHeight: '85vh',
padding: 20,
marginTop: '-5vh',
backgroundColor: 'white',
borderRadius: 6,
'&:focus': {
outline: 'none',
},
});
export default () => (
<Dialog.Root>
<Dialog.Trigger>Open</Dialog.Trigger>
<StyledOverlay />
<StyledContent>
<p>Dialog content</p>
<Dialog.Close>Close</Dialog.Close>
</StyledContent>
</Dialog.Root>
);

Contains all the parts of a dialog.

PropTypeDefault
defaultOpenbooleanNo default value
openbooleanNo default value
onOpenChangefunctionNo default value
idstringNo default value

The button that opens the dialog.

PropTypeDefault
asenumbutton

A layer that covers the inert portion of the view when the dialog is open.

PropTypeDefault
asenumdiv
forceMountbooleanNo default value

Contains content to be rendered in the open dialog.

PropTypeDefault
asenumdiv
forceMountbooleanNo default value
onOpenAutoFocusfunctionNo default value
onCloseAutoFocusfunctionNo default value
onEscapeKeyDownfunctionNo default value
onPointerDownOutsidefunctionNo default value

The button that closes the dialog.

PropTypeDefault
asenumbutton

Adheres to the Dialog WAI-ARIA design pattern.

KeyDescription
SpaceOpens/closes the dialog.
EnterOpens/closes the dialog.
TabMoves focus to the next focusable element.
Shift + TabMoves focus to the previous focusable element.
EscCloses the dialog and moves focus to Dialog.Trigger.

Create your own API by abstracting the primitive parts into your own component.

This example abstracts the Dialog.Overlay and Dialog.Close parts.

Usage

import { Dialog, DialogTrigger, DialogContent } from './your-dialog';
export default () => (
<Dialog>
<DialogTrigger>Dialog trigger</DialogTrigger>
<DialogContent>Dialog Content</DialogContent>
</Dialog>
);

Implementation

// your-dialog.js
import React from 'react';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import { Cross1Icon } from '@radix-ui/react-icons';
export function Dialog({ children, ...props }) {
return (
<DialogPrimitive.Root {...props}>
<DialogPrimitive.Overlay />
{children}
</DialogPrimitive.Root>
);
}
export const DialogContent = React.forwardRef(
({ children, ...props }, forwardedRef) => (
<DialogPrimitive.Content {...props} ref={forwardedRef}>
{children}
<DialogPrimitive.Close>
<Cross1Icon />
</DialogPrimitive.Close>
</DialogPrimitive.Content>
)
);
export const DialogTrigger = DialogPrimitive.Trigger;
export const DialogClose = DialogPrimitive.Close;