Dialog
A dialog is a temporary window that appears on top of the main content of an application, requiring user interaction before the underlying content can be accessed again.
Basic
Dialogcomponent can be controlled byopenandonCloseprops.Dialogcomponent can be self-closed by 3 reasons specified in theDialogCloseReasontype:outside-press|escape-key|close-icon.Dialogcomponent requires at least oneDialogContentand oneDialogActionto render.
Source Code
import { WarningOutline } from '@ocean-network-express/magenta-ui/icons';
import {
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
Text,
} from '@ocean-network-express/magenta-ui/react';
import { useState } from 'react';
export function Basic() {Full width
- The default width of the
Dialogcomponent is fit-content. To make it full of available width, set thefullWidthprop totrue.
Source Code
import {
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
Text,
} from '@ocean-network-express/magenta-ui/react';
import React from 'react';
export function FullWidth() {
const [open, setOpen] = React.useState(false);Max width
- You can change dialog max width by setting
maxWidthprop to predefined values:xs,sm,md,lg,xl. - The default
maxWidthvalue issm.
Max width | Value |
|---|---|
xs | 448px |
sm | 608px |
md | 924px |
lg | 1240px |
xl | 1556px |
Source Code
import {
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
DialogProps,
Text,
} from '@ocean-network-express/magenta-ui/react';
import React from 'react';
export function MaxWidth() {Close Icon
- You can custom the close icon by setting
closeIconprop. - You can hide the close icon by setting
hideCloseIconprop totrue.
Source Code
import { CloseCircleFill } from '@ocean-network-express/magenta-ui/icons';
import {
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
DialogProps,
Text,
} from '@ocean-network-express/magenta-ui/react';
import React from 'react';
Close Dialog By Escape
- By default, dialog component can be closed by clicking the escape key.
- You can disable this behavior by setting the
closeByEscapeprop tofalse.
Source Code
import {
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
Text,
} from '@ocean-network-express/magenta-ui/react';
import React from 'react';
export function CloseByEscape() {
const [open, setOpen] = React.useState(false);Close Dialog By Click Out Side
- By default, dialog component can be closed by clicking out side.
- You can disable this behavior by setting the
closeByOutsideClickprop tofalse.
Source Code
import {
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
Text,
} from '@ocean-network-express/magenta-ui/react';
import React from 'react';
export function CloseByClickOutSide() {
const [open, setOpen] = React.useState(false);Customized Close Button
- You can customize the close button by setting the
closeButtonprop.
Source Code
import { CloseCircleOutline, WarningOutline } from '@ocean-network-express/magenta-ui/icons';
import {
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
IconButton,
Text,
} from '@ocean-network-express/magenta-ui/react';
import React from 'react';
Nested Dialog
- Dialogs can be nested by using the
Dialogcomponent as a child of anotherDialogcomponent.
Source Code
import {
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
Text,
} from '@ocean-network-express/magenta-ui/react';
import React from 'react';
export function NestedDialog() {
const [open, setOpen] = React.useState(false);Initial Focus
- You can set the initial focus element of the dialog when open by using the
initialFocusprop. - The
initialFocusprop can be either a number (tabbable index as specified by the order) or a ref. - You can set this to a negative number to ignore the initial focus.
Source Code
// This is a simple example of disabling initial focus
<Dialog initialFocus={-1} {...props}>
{/* other elemUsing with from
Source Code
import {
Autocomplete,
AutocompleteOption,
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
Input,
ListItemText,
Select,
SelectOption,Scrolling long content of dialog
When dialogs become too long for the user’s viewport or device, the content of the DialogContent will be scrollable.
Source Code
import { WarningOutline } from '@ocean-network-express/magenta-ui/icons';
import {
Button,
Dialog,
DialogAction,
DialogContent,
DialogHeader,
Text,
} from '@ocean-network-express/magenta-ui/react';
import React from 'react';
export function ScrollDialog() {<body> scroll and return focus
-
By default, main content area is dimmed and scrolling is disabled. You can control scrolling behavior using the
lockscrollprop. -
When a dialog is open,
returnFocusprop determines whether the focus should be returned to the element that initiated the dialog or if the current screen position should be preserved. -
Default value for both
lockScrollandreturnFocusaretrue.Source Code
import { Button, Dialog, DialogAction, DialogContent, DialogHeader, Text, } from '@ocean-network-express/magenta-ui/react'; import React from 'react'; export function ScrollableBodyAndReturnFocus() { const [open, setOpen] = React.useState(false);
Accessibility
Dialog component follows WAI-ARIA recommendations on accessibility.
Roles, States, and Properties
Dialog is a regular div[role="dialog"] element. It has states and props as follows:
- Dialog has
aria-modalattribute set totrue. - Dialog has
aria-labelledbyattribute set to theidof theDialogHeadercomponent. - Dialog has
aria-describedbyattribute set to theidof theDialogContentcomponent.
Keyboard interactions
Key | Function | Condition |
|---|---|---|
Tab | Moves focus to next focusable element inside the dialog. | When focus is on the last focusable element in the dialog, moves focus to the first focusable element in the dialog. |
Shift + Tab | Moves focus to previous focusable element inside the dialog. | When focus is on the first focusable element in the dialog, moves focus to the last focusable element in the dialog. |
Escape | Closes the dialog. |
Was this page helpful?
Modal
Popup
Overlay