Modal dialog API
Import
Section titled “Import”Cimpress UI exports four modal dialog-related components:
DialogRoot: component that connects a trigger button with its associated dialog.ModalDialog: component that displays an overlay element which blocks interaction with outside elements.ModalDialogBody: component that contains the main content of the modal dialog.ModalDialogActions: component that contains the actions of the modal dialog.
import { DialogRoot, ModalDialog, ModalDialogActions, ModalDialogBody } from '@cimpress-ui/react';Accessibility notes
Section titled “Accessibility notes”While a modal dialog is open, all content outside of it is hidden from assistive technologies.
By default, modal dialogs can be closed by interacting outside or by pressing the Escape key. This can be disabled by setting the isDismissible prop to false. If you do this, you must provide a way for the user to close the modal dialog.
If your modal dialog contains form fields, we recommend to automatically focus one of the fields when the modal dialog is opened. This can be achieved by setting the autoFocus prop on the field that you want focused.
ModalDialog requires a textual label to remain accessible to assistive technologies. See our accessibility guide for more details.
Triggering a modal dialog
Section titled “Triggering a modal dialog”To open a modal dialog when a trigger button is pressed, wrap both the trigger button and the modal dialog component with a DialogRoot component. This will automatically associate the button with the modal dialog, without any manual setup.
It’s also possible to display a modal dialog programmatically (not as a result of a user action), or render it in a different part of the component tree than its trigger. In this case, you’ll have to manage the modal dialog’s open state manually.
Automatic trigger
Manual trigger
import { Button, DialogRoot, ModalDialog, ModalDialogBody, Stack, Text } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [isOpen, setIsOpen] = useState(false);
return ( <Stack gap={32}> <Stack gap={16}> <Text as="h2" variant="title-4"> Automatic trigger </Text>
<DialogRoot> <Button>Open modal dialog</Button> <ModalDialog title="Modal dialog"> <ModalDialogBody> This modal dialog can be opened by an automatically attached trigger button. </ModalDialogBody> </ModalDialog> </DialogRoot> </Stack>
<Stack gap={16}> <Text as="h2" variant="title-4"> Manual trigger </Text>
<Button onPress={() => setIsOpen(true)}>Open modal dialog</Button>
<ModalDialog title="Modal dialog" isOpen={isOpen} onOpenChange={setIsOpen}> <ModalDialogBody>This modal dialog's open state is managed manually.</ModalDialogBody> </ModalDialog> </Stack> </Stack> );}Closing programmatically
Section titled “Closing programmatically”Modal dialogs can be closed programmatically by passing a function as children to the ModalDialog component, and using the close function provided as an argument to that function.
import { Button, DialogRoot, ModalDialog, ModalDialogActions, ModalDialogBody } from '@cimpress-ui/react';
export default function Demo() { return ( <DialogRoot> <Button>Open dialog</Button> <ModalDialog title="Closing example"> {({ close }) => ( <> <ModalDialogBody>Pressing the button below will close this dialog.</ModalDialogBody> <ModalDialogActions> <Button onPress={close}>Close dialog</Button> </ModalDialogActions> </> )} </ModalDialog> </DialogRoot> );}API reference
Section titled “API reference”DialogRoot
Section titled “DialogRoot”Encapsulates a dialog trigger and its associated dialog. The trigger can be any Cimpress UI button, and the dialog will be displayed when the trigger is activated.
DialogRootProps
- ReactNode
children *
Section titled “ children * ” -
The dialog trigger with its associated dialog. Provide the trigger as the first child, and the dialog as the second child.
- boolean
isOpen
Section titled “ isOpen ” -
Whether the dialog is open (controlled).
- boolean
defaultOpen
Section titled “ defaultOpen ” -
Whether the dialog is open by default (uncontrolled).
- (isOpen: boolean) => void
onOpenChange
Section titled “ onOpenChange ” -
Handler that is called when the dialog's open state changes.
ModalDialog
Section titled “ModalDialog”Displays an overlay element which blocks interaction with outside elements.
- Ref<HTMLElement>
-
The
reftype for this component.
ModalDialogProps
- ReactNode | ((renderProps: ModalDialogRenderProps) => ReactNode)
children *
Section titled “ children * ” -
The contents of the modal dialog. A function may be provided to access a function to close the modal dialog.
- string
title *
Section titled “ title * ” -
The title of the modal dialog.
- 'small' | 'medium' | 'large'
- Defaults to 'medium' .
The size of the modal dialog.
- boolean
isDismissible
Section titled “ isDismissible ” - Defaults to true .
Whether the modal dialog can be closed by clicking the close button, clicking outside of the modal dialog, or pressing the Escape key. When
false, the modal dialog can only be closed programmatically. - boolean
isOpen
Section titled “ isOpen ” -
Whether the modal dialog is open (controlled). If using
DialogRoot, this prop has no effect - provideisOpentoDialogRootinstead. - boolean
defaultOpen
Section titled “ defaultOpen ” -
Whether the modal dialog is open by default (uncontrolled). If using
DialogRoot, this prop has no effect - providedefaultOpentoDialogRootinstead. - (isOpen: boolean) => void
onOpenChange
Section titled “ onOpenChange ” -
Handler that is called when the modal dialog's open state changes. If using
DialogRoot, this prop has no effect - provideonOpenChangetoDialogRootinstead. - string
-
The element's unique identifier. See MDN.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
aria-label
Section titled “ aria-label ” -
Defines a string value that labels the current element.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
ModalDialogBody
Section titled “ModalDialogBody”Renders content within ModalDialog.
ModalDialogBodyProps
- ReactNode
children *
Section titled “ children * ” -
The content to render within the modal dialog.
ModalDialogActions
Section titled “ModalDialogActions”Renders actions within ModalDialog.
ModalDialogActionsProps
- ReactNode
children *
Section titled “ children * ” -
Actions that should be available in the modal dialog.