Dialog
<pc-dialog> Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Dialogs, sometimes called “modals”, appear above the page and draw the user’s immediate attention.
<pc-dialog label="Dialog" class="dialog-overview">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<pc-button variant="plain" slot="footer" data-dialog="close">
Close
</pc-button>
</pc-dialog>
<pc-button>Open dialog</pc-button>
<script>
const dialog = document.querySelector(".dialog-overview");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => (dialog.open = true));
</script> Demos#
Opening and closing dialogs declaratively#
You can open and close dialogs with JavaScript by toggling the open attribute, but you can also do it declaratively. Add the data-dialog="open id" to any button on the page, where id is the id of the dialog you want to open.
Similarly, you can add data-dialog="close" to a button inside of the dialog to tell it to close it.
Custom width#
Use the --width custom property to set the dialog’s width.
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.
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.
Prevent light dismissal#
If you want to prevent the dialog from closing if the user clicks on the overlay, add the no-light-dismiss attribute.
Prevent dialog from closing#
By default, dialogs will close when the user clicks the close button, clicks the overlay or presses the Esc key. In most cases, the default behaviour is the best behaviour 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 pc-hide event. When cancelled, the dialog will remain open and pulse briefly to draw the user’s attention to it.
You can use event.detail.source to check what triggered the close request. In this example, the dialog will only close when a custom close button inside the footer is clicked. Clicking the overlay, pressing Esc or using the built‐in header close button will all be prevented.
Customise 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.
Eigenschaften#
| Name | Beschreibung | Reflektiert | Standard |
|---|---|---|---|
open | Indicates whether or not the dialog is open. Toggle this attribute to show and hide the dialog. Typ: boolean |
| false |
label | The dialog’s label displayed in the header. You should always include a relevant label even when using the no-header attribute, as it is required for proper accessibility. If you need to display HTML, use the label slot instead.Typ: string |
| "" |
noHeaderno-header | Removes 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. Typ: boolean |
| false |
noLightDismissno-light-dismiss | Prevents clicks on the backdrop causing the dialog to close. Typ: boolean |
| false |
updateComplete | Ein schreibgeschütztes Promise, das erfüllt ist, sobald die Komponente fertig aktualisiert wurde. | ‐ |
Erfahre mehr über Attribute und Eigenschaften.
Slots#
| Name | Beschreibung |
|---|---|
| (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 <pc-button>. |
footer | The dialog’s footer, usually one or more buttons representing various options. |
Erfahre mehr über die Benutzung von Slots.
Events#
| Name | Beschreibung | Event‐Detail |
|---|---|---|
pc-show | Emitted when the dialog opens. | ‐ |
pc-after-show | Emitted after the dialog opens and all animations are complete. | ‐ |
pc-hide | Emitted when the dialog closes. | { source: Element } |
pc-after-hide | Emitted after the dialog closes and all animations are complete. | ‐ |
Erfahre mehr über Events.
Benutzerdefinierte Eigenschaften#
| Name | Beschreibung | Standard |
|---|---|---|
--width | The preferred width of the dialog. Note that the dialog will shrink to accommodate smaller screens. | 31rem |
--header-spacing | The amount of spacing to use for the header. | var(--pc-spacing-xl) |
--body-spacing | The amount of padding to use for the body. | var(--pc-spacing-s) var(--pc-spacing-xl) |
--footer-spacing | The amount of padding to use for the footer. | var(--pc-spacing-s) var(--pc-spacing-xl) var(--pc-spacing-xl) |
Erfahre mehr über das Anpassen von benutzerdefinierten Eigenschaften.
Parts#
| Name | Beschreibung |
|---|---|
dialog | The dialog’s <dialog> element. |
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 <pc-button>. |
title | The dialog’s title. |
close-button | The close button. Is a <pc-button> under the hood. |
close-button-base | The close button’s base part. |
body | The dialog’s body. |
footer | The dialog’s footer. |
Erfahre mehr über das Anpassen von CSS‐Parts.
Animationen#
| Name | Beschreibung |
|---|---|
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. |
Erfahre mehr über das Anpassen von Animationen.
Importieren#
Wenn du den Autoloader oder den Standard‐Loader nutzt, kannst du diesen Abschnitt überspringen. Falls du „Cherry Picking“ betreibst, kannst du die folgenden Snippets verwenden, um diese Komponente zu importieren.
Um diese Komponente manuell vom CDN zu importieren, kopiere dieses Code‐Snippet und füge es in dein HTML ein.
<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/dialog/dialog.js"></script> Um diese Komponente manuell vom CDN zu importieren, kopiere dieses Code‐Snippet und füge es in deine JavaScript‐Datei ein.
import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/dialog/dialog.js"; Um diese Komponente manuell via npm zu importieren, kopiere dieses Code‐Snippet und füge es in deine JavaScript‐Datei ein.
import "placer-toolkit/dist/components/dialog/dialog.js"; Abhängigkeiten#
Diese Komponente importiert automatisch folgende Komponenten: