Dialog
A Dialog is a component that focuses the user's attention exclusively on an information via a window that is overlaid on primary content. It is also known as a modal or modal dialog.
Overview
QUI exports 6 components to help you create any dialog:
QDialog
: provides context and state for the dialog.QDialogCloseButton
: the component that closes the dialog, typically a button.QDialogContent
: the container for the dialog's content.QDialogHeader
: the header that labels the dialog.QDialogBody
: the container that houses the dialog's main content.QDialogFooter
: the footer that houses the dialog's actions.
Examples
Our dialog is based on the ARIA Dialog pattern. Accessibility is baked into the components.
QDialogContent
hasrole
set todialog
by default.QDialogHeader
provides the dialog's accessible name (aria-labelledby).QDialogBody
provides the dialog's accessible description (aria-describedby).
Accessibility
Showcase
import {ReactNode} from "react"import {QButton,QDialog,QDialogBody,QDialogCloseButton,QDialogContent,QDialogFooter,QDialogHeader,QDialogTrigger,} from "@qui/react"export default function Showcase(): ReactNode {return (<QDialog dismissAction={<QDialogCloseButton />}><QDialogTrigger><QButton color="primary" variant="fill">Show Dialog</QButton></QDialogTrigger><QDialogContent>{({setOpen}) => (<><QDialogHeader>Dialog Title</QDialogHeader><QDialogBody>Sit nulla est ex deserunt exercitation anim occaecat. Nostrudullamco deserunt aute id consequat veniam incididunt duis in sintirure nisi. Mollit officia cillum Lorem ullamco minim nostrud elitofficia tempor esse quis.</QDialogBody><QDialogFooter><QButton onClick={() => setOpen(false)} variant="fill">Confirm</QButton></QDialogFooter></>)}</QDialogContent></QDialog>)}
Close Action
The dialog features a closeAction prop that you can use to render a close action. The exported QDialogCloseButton
renders a clickable close icon and is typically sufficient. Should you need more customization, provide a render function to the dismissAction prop instead.
import {ReactNode} from "react"import {QButton,QDialog,QDialogBody,QDialogCloseButton,QDialogContent,QDialogContextValue,QDialogHeader,QDialogTrigger,} from "@qui/react"export default function CloseElement(): ReactNode {return (<QDialogdismissAction={(context: QDialogContextValue) => (<QDialogCloseButtononClick={() => {if (window.confirm("Are you sure you want to exit?")) {context.setOpen(false)}}}/>)}><QDialogTrigger><QButton color="primary" variant="fill">Show Dialog</QButton></QDialogTrigger><QDialogContent><QDialogHeader>Dialog Title</QDialogHeader><QDialogBody>Sit nulla est ex deserunt exercitation anim occaecat. Nostrud ullamcodeserunt aute id consequat veniam incididunt duis in sint irure nisi.Mollit officia cillum Lorem ullamco minim nostrud elit officia temporesse quis.</QDialogBody></QDialogContent></QDialog>)}
Animations
Customize the enter and exit animations using the animation, animationDuration and animationEasing props.
import {ReactNode, useState} from "react"import {QButton,QDialog,QDialogAnimation,QDialogBody,QDialogContent,QDialogFooter,QDialogHeader,QRadio,QRadioGroup,QTextInput,} from "@qui/react"const animations: QDialogAnimation[] = ["fade","slide-up","slide-right","slide-down","slide-left","none",]export default function Animations(): ReactNode {const [currentAnimation, setCurrentAnimation] =useState<QDialogAnimation>("fade")const [open, setOpen] = useState(false)const [animationDuration, setAnimationDuration] = useState<number>(250)const handleChange = (animation: QDialogAnimation) => {setCurrentAnimation(animation)setOpen(true)}return (<div className="flex flex-col gap-6 sm:flex-row sm:items-center"><QRadioGroupclassName="grid justify-center gap-2"value={currentAnimation}>{animations.map((animation) => {return (<QRadiokey={animation}label={animation}onClick={() => handleChange(animation)}value={animation}/>)})}</QRadioGroup><QDialoganimation={currentAnimation}animationDuration={animationDuration}onOpenChange={(value: boolean) => {// respond to the emitted close events from Escape, outside click, etc...setOpen(value)}}open={open}><QDialogContent><QDialogHeader>Dialog Title</QDialogHeader><QDialogBody>Sit nulla est ex deserunt exercitation anim occaecat. Nostrudullamco deserunt aute id consequat veniam incididunt duis in sintirure nisi. Mollit officia cillum Lorem ullamco minim nostrud elitofficia tempor esse quis.</QDialogBody><QDialogFooter><QButton onClick={() => setOpen(false)} variant="fill">Confirm</QButton></QDialogFooter></QDialogContent></QDialog><QTextInputclassName="min-w-[225px]"inputProps={{max: 10000, min: 0, step: 10, type: "number"}}label="Transition Duration (ms)"onChange={(event, value) => {setAnimationDuration(value)}}value={animationDuration}/></div>)}
Height and Width
Use the fullWidth and fullHeight properties in combination with maxWidth and maxHeight to enable dialogs of any size. Here's an example of a fullscreen dialog:
import {ReactNode} from "react"import {QButton,QDialog,QDialogBody,QDialogCloseButton,QDialogContent,QDialogFooter,QDialogHeader,QDialogTrigger,} from "@qui/react"export default function FullScreen(): ReactNode {return (<QDialogdismissAction={<QDialogCloseButton />}fullHeightfullWidthmaxHeight="100vh"maxWidth="100vw"size="l"><QDialogTrigger><QButton color="primary" variant="fill">Show Dialog</QButton></QDialogTrigger><QDialogContent>{({setOpen}) => (<><QDialogHeader>Dialog Title</QDialogHeader><QDialogBody>Sit nulla est ex deserunt exercitation anim occaecat. Nostrudullamco deserunt aute id consequat veniam incididunt duis in sintirure nisi. Mollit officia cillum Lorem ullamco minim nostrud elitofficia tempor esse quis.</QDialogBody><QDialogFooter><QButton onClick={() => setOpen(false)} variant="fill">Confirm</QButton></QDialogFooter></>)}</QDialogContent></QDialog>)}
API
QDialog
Name & Description | Option(s) | Default |
---|---|---|
ReactNode | ||
The animation to play when the dialog is entering and exiting. |
| 'fade' |
The CSS transition-duration of the enter and exit animations. Supply as a
number to configure both transitions at once, or supply an object to configure
the enter and exit transitions independently. |
| 250 |
The transition timing function of the enter and exit animations. Supply as a
string to configure both timings at once, or supply an object to configure the
enter and exit timings independently. |
| 'ease' |
Close the dialog when the escape key is pressed. |
| true |
Close the dialog when the user clicks the content behind it. |
| true |
The default value when uncontrolled. |
| |
On open, the dialog focuses the first focusable element by default. Set this
input to true to disable this behavior. |
| false |
If true, the component is disabled and hidden. |
| false |
If true, disable the scroll lock behavior. |
| false |
Element to be used for the dismiss action. |
| |
Box shadow severity of the dialog, based on the five elevation styles provided
by QDS. A higher number means a more severe box shadow. Supply 0 to disable
the box shadow. |
| 5 |
If true, the dialog will take up the full height of its container. When this
prop is true , the dialog will adapt based on maxHeight. |
| false |
| false | |
If true, the backdrop is hidden. |
| false |
Unique identifier for this dialog |
| |
Max height of the dialog. |
| 'none' |
Max width of the dialog. |
| '500px' |
Callback fired when panel visibility changes.
|
| |
Override the outside click event listener. If this returns false , the close
action will be prevented.
|
| |
The controlled state for the component's visibility. |
| |
CSS position property of the component. Can be "absolute" or "fixed". |
| 'absolute' |
If true , focus will be restored to the dialog's trigger element when the
dialog is closed. |
| true |
Adjusts the amount of padding applied to the content container as well as font
properties of the content. |
| 'm' |
| 'var(--q-zindex-modal)' |
Type
| 'fade'| 'slide-up'| 'slide-down'| 'slide-left'| 'slide-right'| 'none'
Default
'fade'
Description
The animation to play when the dialog is entering and exiting.
Type
| number| {enter: numberexit: number}
Default
250
Description
The CSS transition-duration of the enter and exit animations. Supply as a
number to configure both transitions at once, or supply an object to configure
the enter and exit transitions independently.
Type
| string| {enter: stringexit: string}
Default
'ease'
Description
The transition timing function of the enter and exit animations. Supply as a
string to configure both timings at once, or supply an object to configure the
enter and exit timings independently.
Type
boolean
Default
true
Description
Close the dialog when the escape key is pressed.
Type
boolean
Default
true
Description
Close the dialog when the user clicks the content behind it.
Type
boolean
Description
The default value when uncontrolled.
Type
boolean
Default
false
Description
On open, the dialog focuses the first focusable element by default. Set this
input to
true
to disable this behavior.Type
boolean
Default
false
Description
If true, the component is disabled and hidden.
Type
boolean
Default
false
Description
If true, disable the scroll lock behavior.
Type
| ReactElement| ((props: {open: booleansetOpen: (open: boolean,...events: any[]) => void}) => ReactElement)
Description
Element to be used for the dismiss action.
Type
| 0| 1| 2| 3| 4| 5| '0'| '1'| '2'| '3'| '4'| '5'
Default
5
Description
Box shadow severity of the dialog, based on the five elevation styles provided
by QDS. A higher number means a more severe box shadow. Supply 0 to disable
the box shadow.
Type
boolean
Default
false
Description
If true, the dialog will take up the full height of its container. When this
prop is
true
, the dialog will adapt based on maxHeight.Type
boolean
Default
false
Description
If true, the dialog will take up the full width of its container. When this
prop is true, the dialog will adapt based on maxWidth.
Type
boolean
Default
false
Description
If true, the backdrop is hidden.
Type
string
Description
Unique identifier for this dialog
Type
| string| number
Default
'none'
Description
Max height of the dialog.
Type
| string| number
Default
'500px'
Description
Max width of the dialog.
Type
(open: boolean,event?: Event,reason?:| 'ancestor-scroll'| 'click'| 'escape-key'| 'focus'| 'hover'| 'list-navigation'| 'outside-press'| 'reference-press'| 'safe-polygon',) => void
Description
Callback fired when panel visibility changes.
- openThe updated state.
- eventThe event that triggered the change.
- reasonThe cause of the change.
Type
(event?: MouseEvent,) => boolean
Description
Override the outside click event listener. If this returns
false
, the close
action will be prevented.- eventthe DOM event associated with the click.
Type
boolean
Description
The controlled state for the component's visibility.
Type
| 'absolute'| 'fixed'
Default
'absolute'
Description
CSS position property of the component. Can be "absolute" or "fixed".
Type
boolean
Default
true
Description
If
true
, focus will be restored to the dialog's trigger element when the
dialog is closed.Type
| 's'| 'm'| 'l'
Default
'm'
Description
Adjusts the amount of padding applied to the content container as well as font
properties of the content.
QDialogContextValue
Several of the dialog's props and child elements expose the dialog's visibility as a context object. For examples of this pattern in use, refer to the Showcase and Close Action demos.
QDialogTrigger
QDialogCloseButton
This component inherits its props from the Icon Button component.
QDialogContent
This component's children
prop is a RenderProp
. It accepts either a ReactNode or a function that returns a ReactNode.
When supplied as a function, the QDialogContextValue object is provided.
Refer to the above demos for examples.
Name & Description | Option(s) | Default |
---|---|---|
RenderProp, the child of this component. |
| |
The component used for the root node. It can be a React component or
element. |
| 'section' |
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element. |
| |
Props applied to the dialog content container. |
| |
HTML role, applied to the root element. Don't change this unless you know
what you're doing. |
| 'dialog' |
The style global attribute
contains CSS styling
declarations to be applied to the element. | CSSProperties | |
Props applied to the dialog content wrapper. |
|
Type
| ReactNode| ((props: {open: booleansetOpen: (open: boolean,...events: any[]) => void}) => ReactNode)
Description
RenderProp, the child of this component.
Type
| ElementType| ComponentType
Default
'section'
Description
The component used for the root node. It can be a React component or
element.
Type
string
Description
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element.
Type
HTMLAttributes<HTMLDivElement>
Description
Props applied to the dialog content container.
Type
string
Default
'dialog'
Description
HTML role, applied to the root element. Don't change this unless you know
what you're doing.
Type
CSSProperties
Description
The style global attribute
contains CSS styling
declarations to be applied to the element.
Type
HTMLAttributes<HTMLDivElement>
Description
Props applied to the dialog content wrapper.
QDialogHeader
Name & Description | Option(s) | Default |
---|---|---|
ReactNode | ||
The component used for the root node. It can be a React component or
element. |
| 'h2' |
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element. |
| |
| ||
The style global attribute
contains CSS styling
declarations to be applied to the element. | CSSProperties |
Type
| ElementType| ComponentType
Default
'h2'
Description
The component used for the root node. It can be a React component or
element.
Type
string
Description
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element.
Type
string
Description
HTML id attribute. If omitted,
a random value will be randomly generated for optimal accessibility.
Type
CSSProperties
Description
The style global attribute
contains CSS styling
declarations to be applied to the element.
QDialogBody
Name & Description | Option(s) | Default |
---|---|---|
ReactNode | ||
The component used for the root node. It can be a React component or
element. |
| 'div' |
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element. |
| |
| ||
The style global attribute
contains CSS styling
declarations to be applied to the element. | CSSProperties |
Type
| ElementType| ComponentType
Default
'div'
Description
The component used for the root node. It can be a React component or
element.
Type
string
Description
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element.
Type
string
Description
HTML id attribute. If omitted,
a random value will be randomly generated for optimal accessibility.
Type
CSSProperties
Description
The style global attribute
contains CSS styling
declarations to be applied to the element.
QDialogFooter
Name & Description | Option(s) | Default |
---|---|---|
ReactNode | ||
The component used for the root node. It can be a React component or
element. |
| 'div' |
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element. |
| |
The style global attribute
contains CSS styling
declarations to be applied to the element. | CSSProperties |
Type
| ElementType| ComponentType
Default
'div'
Description
The component used for the root node. It can be a React component or
element.
Type
string
Description
The className property of the Element
interface gets and sets the value of the
class attribute
of the specified element.
Type
CSSProperties
Description
The style global attribute
contains CSS styling
declarations to be applied to the element.