Dialog
<p-dialog> | PDialog
Dialogs, sometimes called “modals”, appear above the page and require the user’s immediate attention.
<p-dialog label="Dialog" class="dialog-overview"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <p-button slot="footer" variant="primary">Close</p-button> </p-dialog> <p-button>Open Dialog</p-button> <script> const dialog = document.querySelector('.dialog-overview'); const openButton = dialog.nextElementSibling; const closeButton = dialog.querySelector('p-button[slot="footer"]'); openButton.addEventListener('click', () => dialog.show()); closeButton.addEventListener('click', () => dialog.hide()); </script>
import { useState } from 'react'; import PButton from 'pure-uikit/dist/react/button'; import PDialog from 'pure-uikit/dist/react/dialog'; const App = () => { const [open, setOpen] = useState(false); return ( <> <PDialog label="Dialog" open={open} onPAfterHide={() => setOpen(false)}> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <PButton slot="footer" variant="primary" onClick={() => setOpen(false)}> Close </PButton> </PDialog> <PButton onClick={() => setOpen(true)}>Open Dialog</PButton> </> ); };
Examples
Custom Width
Use the --width
custom property to set the dialog’s width.
<p-dialog label="Dialog" class="dialog-width" style="--width: 50vw;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <p-button slot="footer" variant="primary">Close</p-button> </p-dialog> <p-button>Open Dialog</p-button> <script> const dialog = document.querySelector('.dialog-width'); const openButton = dialog.nextElementSibling; const closeButton = dialog.querySelector('p-button[slot="footer"]'); openButton.addEventListener('click', () => dialog.show()); closeButton.addEventListener('click', () => dialog.hide()); </script>
import { useState } from 'react'; import PButton from 'pure-uikit/dist/react/button'; import PDialog from 'pure-uikit/dist/react/dialog'; const App = () => { const [open, setOpen] = useState(false); return ( <> <PDialog label="Dialog" open={open} style={{ '--width': '50vw' }} onPAfterHide={() => setOpen(false)}> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <PButton slot="footer" variant="primary" onClick={() => setOpen(false)}> Close </PButton> </PDialog> <PButton onClick={() => setOpen(true)}>Open Dialog</PButton> </> ); };
Scrolling
By design, a dialog’s height will never exceed that of the viewport. As such, dialogs will not scroll with the page ensuring the header and footer are always accessible to the user.
Scroll down and give it a try! 👇
<p-dialog label="Dialog" class="dialog-scrolling"> <div style="height: 150vh; border: dashed 2px var(--p-color-neutral-200); padding: 0 1rem;"> <p>Scroll down and give it a try! 👇</p> </div> <p-button slot="footer" variant="primary">Close</p-button> </p-dialog> <p-button>Open Dialog</p-button> <script> const dialog = document.querySelector('.dialog-scrolling'); const openButton = dialog.nextElementSibling; const closeButton = dialog.querySelector('p-button[slot="footer"]'); openButton.addEventListener('click', () => dialog.show()); closeButton.addEventListener('click', () => dialog.hide()); </script>
import { useState } from 'react'; import PButton from 'pure-uikit/dist/react/button'; import PDialog from 'pure-uikit/dist/react/dialog'; const App = () => { const [open, setOpen] = useState(false); return ( <> <PDialog label="Dialog" open={open} onPAfterHide={() => setOpen(false)}> <div style={{ height: '150vh', border: 'dashed 2px var(--p-color-neutral-200)', padding: '0 1rem' }} > <p>Scroll down and give it a try! 👇</p> </div> <PButton slot="footer" variant="primary" onClick={() => setOpen(false)}> Close </PButton> </PDialog> <PButton onClick={() => setOpen(true)}>Open Dialog</PButton> </> ); };
Header Actions
The header shows a functional close button by default. You can use the header-actions
slot to
add additional icon buttons if needed.
<p-dialog label="Dialog" class="dialog-header-actions"> <p-icon-button class="new-window" slot="header-actions" name="box-arrow-up-right"></p-icon-button> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <p-button slot="footer" variant="primary">Close</p-button> </p-dialog> <p-button>Open Dialog</p-button> <script> const dialog = document.querySelector('.dialog-header-actions'); const openButton = dialog.nextElementSibling; const closeButton = dialog.querySelector('p-button[slot="footer"]'); const newWindowButton = dialog.querySelector('.new-window'); openButton.addEventListener('click', () => dialog.show()); closeButton.addEventListener('click', () => dialog.hide()); newWindowButton.addEventListener('click', () => window.open(location.href)); </script>
import { useState } from 'react'; import PButton from 'pure-uikit/dist/react/button'; import PDialog from 'pure-uikit/dist/react/dialog'; import PIconButton from 'pure-uikit/dist/react/icon-button'; const App = () => { const [open, setOpen] = useState(false); return ( <> <PDialog label="Dialog" open={open} onPAfterHide={() => setOpen(false)}> <PIconButton class="new-window" slot="header-actions" name="box-arrow-up-right" onClick={() => window.open(location.href)} /> Lorem ipsum dolor sit amet, consectetur adipiscing elit. <PButton slot="footer" variant="primary" onClick={() => setOpen(false)}> Close </PButton> </PDialog> <PButton onClick={() => setOpen(true)}>Open Dialog</PButton> </> ); };
Preventing the Dialog from Closing
By default, dialogs will close when the user clicks the close button, clicks the overlay, or presses the Escape key. In most cases, the default behavior is the best behavior in terms of UX. However, there are situations where this may be undesirable, such as when data loss will occur.
To keep the dialog open in such cases, you can cancel the p-request-close
event. When canceled,
the dialog will remain open and pulse briefly to draw the user’s attention to it.
You can use event.detail.source
to determine what triggered the request to close. This example
prevents the dialog from closing when the overlay is clicked, but allows the close button or
Escape to dismiss it.
<p-dialog label="Dialog" class="dialog-deny-close"> This dialog will not close when you click on the overlay. <p-button slot="footer" variant="primary">Close</p-button> </p-dialog> <p-button>Open Dialog</p-button> <script> const dialog = document.querySelector('.dialog-deny-close'); const openButton = dialog.nextElementSibling; const closeButton = dialog.querySelector('p-button[slot="footer"]'); openButton.addEventListener('click', () => dialog.show()); closeButton.addEventListener('click', () => dialog.hide()); // Prevent the dialog from closing when the user clicks on the overlay dialog.addEventListener('p-request-close', event => { if (event.detail.source === 'overlay') { event.preventDefault(); } }); </script>
import { useState } from 'react'; import PButton from 'pure-uikit/dist/react/button'; import PDialog from 'pure-uikit/dist/react/dialog'; const App = () => { const [open, setOpen] = useState(false); // Prevent the dialog from closing when the user clicks on the overlay function handleRequestClose(event) { if (event.detail.source === 'overlay') { event.preventDefault(); } } return ( <> <PDialog label="Dialog" open={open} onPRequestClose={handleRequestClose} onPAfterHide={() => setOpen(false)}> This dialog will not close when you click on the overlay. <PButton slot="footer" variant="primary" onClick={() => setOpen(false)}> Close </PButton> </PDialog> <PButton onClick={() => setOpen(true)}>Open Dialog</PButton> </> ); };
Customizing Initial Focus
By default, the dialog’s panel will gain focus when opened. This allows a subsequent tab press to focus on
the first tabbable element in the dialog. If you want a different element to have focus, add the
autofocus
attribute to it as shown below.
<p-dialog label="Dialog" class="dialog-focus"> <p-input autofocus placeholder="I will have focus when the dialog is opened"></p-input> <p-button slot="footer" variant="primary">Close</p-button> </p-dialog> <p-button>Open Dialog</p-button> <script> const dialog = document.querySelector('.dialog-focus'); const input = dialog.querySelector('p-input'); const openButton = dialog.nextElementSibling; const closeButton = dialog.querySelector('p-button[slot="footer"]'); openButton.addEventListener('click', () => dialog.show()); closeButton.addEventListener('click', () => dialog.hide()); </script>
import { useState } from 'react'; import PButton from 'pure-uikit/dist/react/button'; import PDialog from 'pure-uikit/dist/react/dialog'; import PInput from 'pure-uikit/dist/react/input'; const App = () => { const [open, setOpen] = useState(false); return ( <> <PDialog label="Dialog" open={open} onPAfterHide={() => setOpen(false)}> <PInput autofocus placeholder="I will have focus when the dialog is opened" /> <PButton slot="footer" variant="primary" onClick={() => setOpen(false)}> Close </PButton> </PDialog> <PButton onClick={() => setOpen(true)}>Open Dialog</PButton> </> ); };
You can further customize initial focus behavior by canceling the p-initial-focus
event and
setting focus yourself inside the event handler.
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/dialog/dialog.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/dialog/dialog.js';
To import this component using a bundler:
import 'pure-uikit/dist/components/dialog/dialog.js';
To import this component as a React component:
import PDialog from 'pure-uikit/dist/react/dialog';
Slots
Name | Description |
---|---|
(default) | The dialog’s main content. |
label
|
The dialog’s label. Alternatively, you can use the label attribute. |
header-actions
|
Optional actions to add to the header. Works best with <p-icon-button> . |
footer
|
The dialog’s footer, usually one or more buttons representing various options. |
Learn more about using slots.
Properties
Name | Description | Reflects | Type | Default |
---|---|---|---|---|
modal
|
Exposes the internal modal utility that controls focus trapping. To temporarily disable focus
trapping and allow third-party modals spawned from an active Pure UI modal, call
modal.activateExternal() when the third-party modal opens. Upon closing, call
modal.deactivateExternal() to restore pure ui ’s focus trapping.
|
- |
new Modal(this)
|
|
open
|
Indicates whether or not the dialog is open. You can toggle this attribute to show and hide the
dialog, or you can use the show() and hide() methods and this attribute
will reflect the dialog’s open state.
|
|
boolean
|
false
|
label
|
The dialog’s label as displayed in the header. You should always include a relevant label even when
using
no-header , as it is required for proper accessibility. If you need to display HTML, use
the label slot instead.
|
|
string
|
""
|
noHeader
no-header
|
Disables the header. This will also remove the default close button, so please ensure you provide an easy, accessible way for users to dismiss the dialog. |
|
boolean
|
false
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Events
Name | React Event | Description | Event Detail |
---|---|---|---|
p-show |
onPShow |
Emitted when the dialog opens. | - |
p-after-show |
onPAfterShow |
Emitted after the dialog opens and all animations are complete. | - |
p-hide |
onPHide |
Emitted when the dialog closes. | - |
p-after-hide |
onPAfterHide |
Emitted after the dialog closes and all animations are complete. | - |
p-initial-focus |
onPInitialFocus |
Emitted when the dialog opens and is ready to receive focus. Calling
event.preventDefault() will prevent focusing and allow you to set it on a different
element, such as an input.
|
- |
p-request-close |
onPRequestClose |
Emitted when the user attempts to close the dialog by clicking the close button, clicking the
overlay, or pressing escape. Calling event.preventDefault() will keep the dialog open.
Avoid using this unless closing the dialog will result in destructive behavior such as data loss.
|
{ source: 'close-button' | 'keyboard' | 'overlay' }
|
Learn more about events.
Methods
Name | Description | Arguments |
---|---|---|
show() |
Shows the dialog. | - |
hide() |
Hides the dialog | - |
Learn more about methods.
Custom Properties
Name | Description | Default |
---|---|---|
--width |
The preferred width of the dialog. Note that the dialog will shrink to accommodate smaller screens. | |
--header-spacing |
The amount of padding to use for the header. | |
--body-spacing |
The amount of padding to use for the body. | |
--footer-spacing |
The amount of padding to use for the footer. |
Learn more about customizing CSS custom properties.
Parts
Name | Description |
---|---|
base |
The component’s base wrapper. |
overlay |
The overlay that covers the screen behind the dialog. |
panel |
The dialog’s panel (where the dialog and its content are rendered). |
header |
The dialog’s header. This element wraps the title and header actions. |
header-actions |
Optional actions to add to the header. Works best with <p-icon-button> . |
title |
The dialog’s title. |
close-button |
The close button, an <p-icon-button> . |
close-button__base |
The close button’s exported base part. |
body |
The dialog’s body. |
footer |
The dialog’s footer. |
Learn more about customizing CSS parts.
Animations
Name | Description |
---|---|
dialog.show |
The animation to use when showing the dialog. |
dialog.hide |
The animation to use when hiding the dialog. |
dialog.denyClose |
The animation to use when a request to close the dialog is denied. |
dialog.overlay.show |
The animation to use when showing the dialog’s overlay. |
dialog.overlay.hide |
The animation to use when hiding the dialog’s overlay. |
Learn more about customizing animations.
Dependencies
This component automatically imports the following dependencies.
<p-icon>
<p-icon-button>