Drawer
<pc-drawer> Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Drawers slide in from a container to expose additional options and information.
<pc-drawer label="Drawer" class="drawer-overview">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<pc-button variant="plain" slot="footer" data-drawer="close">
Close
</pc-button>
</pc-drawer>
<pc-button>Open drawer</pc-button>
<script>
const drawer = document.querySelector(".drawer-overview");
const openButton = drawer.nextElementSibling;
openButton.addEventListener("click", () => (drawer.open = true));
</script> Demos#
Opening and closing drawers declaratively#
You can open and close drawers with JavaScript by toggling the open attribute, but you can also do it declaratively. Add the data-drawer="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-drawer="close" to a button inside of the drawer to tell it to close it.
Slide in from start#
By default, drawers slide in from the end. To make the drawer slide in from the start, set the placement attribute to start.
Slide in from top#
To make the drawer slide in from the top, set the placement attribute to top.
Slide in from bottom#
To make the drawer slide in from the bottom, set the placement attribute to bottom.
Custom size#
Use the --size custom property to set the drawer’s size. This will be applied to the drawer’s width or height depending on its placement.
Scrolling#
By design, a drawer’s height will never exceed that of the viewport. As such, drawers will not scroll with the page to ensure 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 drawer from closing if the user clicks on the overlay, add the no-light-dismiss attribute.
Prevent drawer from closing#
By default, drawers 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 drawer open in such cases, you can cancel the pc-hide event. When cancelled, the drawer 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 drawer 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 drawer’s panel will gain focus when opened. This allows a subsequent tab press to focus on the first tabbable element in the drawer. 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 drawer is open. Toggle this attribute to show and hide the drawer. Typ: boolean |
| false |
label | The drawer’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.Typ: string |
| "" |
placement | The direction from which the drawer will open. Typ: "top" | "end" | "bottom" | "start" |
| "end" |
noHeaderno-header | This 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 drawer. Typ: boolean |
| false |
noLightDismissno-light-dismiss | Prevents clicks on the backdrop causing the drawer 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 drawer’s main content. |
label | The drawer’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 drawer’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 drawer opens. | ‐ |
pc-after-show | Emitted after the drawer opens and all animations are complete. | ‐ |
pc-hide | Emitted when the drawer closes. | { source: Element } |
pc-after-hide | Emitted after the drawer closes and all animations are complete. | ‐ |
Erfahre mehr über Events.
Benutzerdefinierte Eigenschaften#
| Name | Beschreibung | Standard |
|---|---|---|
--size | The preferred size of the drawer. This will be applied to the drawer’s width or height depending on its placement. Note that the drawer will shrink to accommodate smaller screens. | 25rem |
--header-spacing | The amount of padding 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 |
|---|---|
drawer | The drawer’s <dialog> element. |
header | The drawer’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 drawer’s title. |
close-button | The close button, a <pc-button>. |
close-button-base | The close button’s exported base part. |
body | The drawer’s body. |
footer | The drawer’s footer. |
Erfahre mehr über das Anpassen von CSS‐Parts.
Animationen#
| Name | Beschreibung |
|---|---|
drawer.showTop | The animation to use when showing a drawer with top placement. |
drawer.showEnd | The animation to use when showing a drawer with end placement. |
drawer.showBottom | The animation to use when showing a drawer with bottom placement. |
drawer.showStart | The animation to use when showing a drawer with start placement. |
drawer.hideTop | The animation to use when hiding a drawer with top placement. |
drawer.hideEnd | The animation to use when hiding a drawer with end placement. |
drawer.hideBottom | The animation to use when hiding a drawer with bottom placement. |
drawer.hideStart | The animation to use when hiding a drawer with start placement. |
drawer.denyClose | The animation to use when a request to close the drawer 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/drawer/drawer.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/drawer/drawer.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/drawer/drawer.js"; Abhängigkeiten#
Diese Komponente importiert automatisch folgende Komponenten: