Components
Dialog
Dialog allows you to create both modal and non-modal dialogs based on the HTML dialog element.
React
const Preview = () => { return ( <Dialog.TriggerContext> <Dialog.Trigger>Open Dialog</Dialog.Trigger> <Dialog> <Heading style={{ marginBottom: 'var(--ds-size-2)' }}> Dialog header </Heading> <Paragraph style={{ marginBottom: 'var(--ds-size-2)' }}> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Blanditiis doloremque obcaecati assumenda odio ducimus sunt et. </Paragraph> <Paragraph data-size='sm'>Dialog footer</Paragraph> </Dialog> </Dialog.TriggerContext> ); }; render(<Preview />)
Use Dialog when
- the user needs to focus on a specific task
- you need to ensure that the user notices important information
Avoid Dialog when
- the content is extensive or complex
- the process contains several steps
- interrupting the user’s workflow may be problematic, use
Alert,Popoveror a non-modalDialoginstead
Example
With blocks
Dialog can be divided with visual separators to keep the content clearly structured.
React
const WithBlocks = () => { return ( <Dialog.TriggerContext> <Dialog.Trigger>Open Dialog</Dialog.Trigger> <Dialog> <Dialog.Block> <Paragraph data-size='sm'>Dialog subtitle</Paragraph> <Heading>Dialog with dividers</Heading> </Dialog.Block> <Dialog.Block> <Paragraph> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sodales eros justo. </Paragraph> </Dialog.Block> <Dialog.Block> <Button variant='secondary' data-command='close'> Lukk </Button> </Dialog.Block> </Dialog> </Dialog.TriggerContext> ); }; render(<WithBlocks />)
With form
When a Dialog contains form elements, it is helpful to move focus directly to the first field, unless other important content should be read first.
React
const WithFormEn = () => { const dialogRef = useRef<HTMLDialogElement>(null); const [input, setInput] = useState(''); return ( <Dialog.TriggerContext> <Dialog.Trigger>Open dialog</Dialog.Trigger> <Dialog ref={dialogRef} onClose={() => setInput('')} closedby='any'> <Heading style={{ marginBottom: 'var(--ds-size-2)' }}> Dialog with form </Heading> <Textfield label='Name' value={input} onChange={(e) => setInput(e.target.value)} // @ts-expect-error We want the native "autofocus" and Reacts onMount smartness (see https://react.dev/reference/react-dom/components/input#input) autofocus='true' /> <div style={{ display: 'flex', gap: 'var(--ds-size-4)', marginTop: 'var(--ds-size-4)', }} > <Button onClick={() => { window.alert(`You submitted the form with the name: ${input}`); dialogRef.current?.close(); }} > Submit form </Button> <Button variant='secondary' data-color='danger' data-command='close'> Cancel </Button> </div> </Dialog> </Dialog.TriggerContext> ); }; render(<WithFormEn />)
Guidelines
Use Dialog to present important content that requires the user’s attention or action. We avoid calling the component “modal”, since it can also function as non-modal.
Dialog can be used as both modal and non-modal.
Modal Dialog
A modal Dialog is a temporary window that opens above the rest of the page. It captures the user’s attention and prevents interaction with other content, while still preserving the context of the page.
Use of modal Dialog should generally be limited, but it can be helpful when the user must consider information, confirm an action, or focus on a specific task without navigating away from the page.
Non-modal Dialog
A non-modal Dialog allows the user to continue interacting with the rest of the page while the dialog is open. It should be used with care and is most suitable for providing supplementary information or extra functionality that does not require full attention.
Closing a Dialog
Ensure that users always have a clear and simple way to close a Dialog. The most common solution is a close button (X) in the top right corner. It must remain visible even when the dialog contains a lot of content and scrolling is required.
If placing a close button (X) in the top right corner is not appropriate, the dialog must include a clear close action within the content, for example a Cancel button. This gives users control and prevents them from getting stuck in the dialog.
Text
- Use a short and concise heading. The heading must make it clear to the user that the context has changed.
- Avoid long blocks of text.
- Ensure that the purpose is clear and that all necessary information is available within the dialog.