3.0.0-alpha.4 Alpha
Light Dark Light Dark System Search /

Drawer

<wa-drawer> Since 2.0 stable

Drawers slide in from a container to expose additional options and information.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Close Open Drawer
<wa-drawer label="Drawer" with-header with-footer class="drawer-overview">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-overview');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

Examples

Drawer with Header

Headers can be used to display titles and more. Use the with-header attribute to add a header to the drawer.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Open Drawer
<wa-drawer label="Drawer" with-header class="drawer-header">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-header');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

Footers can be used to display titles and more. Use the with-footer attribute to add a footer to the drawer.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Close Open Drawer
<wa-drawer label="Drawer" with-footer class="drawer-footer">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-footer');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

Dismissing Drawers

You can add the special data-drawer="close" attribute to a button inside the drawer to tell it to close without additional JavaScript. Alternatively, you can set the open property to false to close the drawer programmatically.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Close Open Drawer
<wa-drawer label="Drawer" with-header with-footer class="drawer-dismiss">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-dismiss');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

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.

This drawer slides in from the start. Close Open Drawer
<wa-drawer label="Drawer" with-header with-footer placement="start" class="drawer-placement-start">
  This drawer slides in from the start.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-placement-start');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

Slide in From Top

To make the drawer slide in from the top, set the placement attribute to top.

This drawer slides in from the top. Close Open Drawer
<wa-drawer label="Drawer" with-header with-footer placement="top" class="drawer-placement-top">
  This drawer slides in from the top.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-placement-top');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

Slide in From Bottom

To make the drawer slide in from the bottom, set the placement attribute to bottom.

This drawer slides in from the bottom. Close Open Drawer
<wa-drawer label="Drawer" with-header with-footer placement="bottom" class="drawer-placement-bottom">
  This drawer slides in from the bottom.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-placement-bottom');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

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.

This drawer is always 50% of the viewport. Close Open Drawer
<wa-drawer label="Drawer" with-header with-footer class="drawer-custom-size" style="--size: 50vw;">
  This drawer is always 50% of the viewport.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-custom-size');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

Scrolling

By design, a drawer's height will never exceed 100% of its container. As such, drawers will not scroll with the page to ensure the header and footer are always accessible to the user.

Scroll down and give it a try! 👇

Close
Open Drawer
<wa-drawer label="Drawer" with-header with-footer class="drawer-scrolling">
  <div style="height: 150vh; border: dashed 2px var(--wa-color-surface-border); padding: 0 1rem;">
    <p>Scroll down and give it a try! 👇</p>
  </div>
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-scrolling');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

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.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Close Open Drawer
<wa-drawer label="Drawer" with-header with-footer class="drawer-header-actions">
  <wa-icon-button class="new-window" slot="header-actions" name="arrow-up-right-from-square" variant="solid"></wa-icon-button>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-header-actions');
  const openButton = drawer.nextElementSibling;
  const newWindowButton = drawer.querySelector('.new-window');

  openButton.addEventListener('click', () => drawer.open = true);
  newWindowButton.addEventListener('click', () => window.open(location.href));
</script>

Light Dismissal

If you want the drawer to close when the user clicks on the overlay, add the light-dismiss attribute.

This drawer will close when you click on the overlay. Close Open Drawer
<wa-drawer label="Drawer" light-dismiss with-header with-footer class="drawer-light-dismiss">
  This drawer will close when you click on the overlay.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-light-dismiss');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

Preventing the Drawer from Closing

By default, drawers 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 drawer open in such cases, you can cancel the wa-hide event. When canceled, the drawer 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 drawer from closing when the overlay is clicked, but allows the close button or Escape to dismiss it.

This drawer will only close when you click the button below. Close Open Drawer
<wa-drawer label="Drawer" with-header with-footer class="drawer-deny-close">
  This drawer will only close when you click the button below.
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-deny-close');
  const openButton = drawer.nextElementSibling;
  const closeButton = drawer.querySelector('wa-button[slot="footer"]');

  openButton.addEventListener('click', () => drawer.open = true);

  // Prevent the drawer from closing unless the close button is clicked
  drawer.addEventListener('wa-hide', event => {
    if (event.detail.source !== closeButton) {
      event.preventDefault();
    }
  });
</script>

Customizing 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.

Close Open Drawer
<wa-drawer label="Drawer" with-header with-footer class="drawer-focus">
  <wa-input autofocus placeholder="I will have focus when the drawer is opened"></wa-input>
  <wa-button slot="footer" variant="brand" data-drawer="close">Close</wa-button>
</wa-drawer>

<wa-button>Open Drawer</wa-button>

<script>
  const drawer = document.querySelector('.drawer-focus');
  const input = drawer.querySelector('wa-input');
  const openButton = drawer.nextElementSibling;

  openButton.addEventListener('click', () => drawer.open = true);
</script>

Slots

Name Description
(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 <wa-icon-button>.
footer The drawer's footer, usually one or more buttons representing various options.

Properties

Name Description Reflects
open
open
Indicates whether or not the drawer is open. You can toggle this attribute to show and hide the drawer, or you can use the show() and hide() methods and this attribute will reflect the drawer's open state.
Type boolean
Default false
label
label
The drawer's label as displayed in the header. You should always include a relevant label, as it is required for proper accessibility. If you need to display HTML, use the label slot instead.
Type string
Default ''
placement
placement
The direction from which the drawer will open.
Type 'top' | 'end' | 'bottom' | 'start'
Default 'end'
withHeader
with-header
Renders the drawer with a header.
Type boolean
Default false
withFooter
with-footer
Renders the drawer with a footer.
Type boolean
Default false
lightDismiss
light-dismiss
When enabled, the drawer will be closed when the user clicks outside of it.
Type boolean
Default false
modal
Exposes the internal modal utility that controls focus trapping. To temporarily disable focus trapping and allow third-party modals spawned from an active Shoelace modal, call modal.activateExternal() when the third-party modal opens. Upon closing, call modal.deactivateExternal() to restore Shoelace's focus trapping.

Events

Name Description
wa-show Emitted when the drawer opens.
wa-after-show Emitted after the drawer opens and all animations are complete.
wa-hide Emitted when the drawer is requesting to close. Calling event.preventDefault() will prevent the dialog from closing. You can inspect event.detail.source to see which element caused the dialog to close. If the source is the dialog element itself, the user has pressed Escape or the dialog has been closed programmatically. Avoid using this unless closing the dialog will result in destructive behavior such as data loss.
wa-after-hide Emitted after the drawer closes and all animations are complete.

CSS custom properties

Name Description
--background-color
The drawer's background color.
--box-shadow
The shadow effects around the edges of the drawer.
--spacing
The amount of space around and between the drawer's content.
--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.
--show-duration
The animation duration when showing the drawer.
Default 200ms
--hide-duration
The animation duration when hiding the drawer.
Default 200ms

CSS parts

Name Description
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 <wa-icon-button>.
title The drawer's title.
close-button The close button, a <wa-icon-button>.
close-button__base The close button's exported base part.
body The drawer's body.
footer The drawer's footer.

Dependencies

This component automatically imports the following elements. Subdependencies, if any exist, will also be included in this list.

Importing

The autoloader is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.

CDN npm React

To manually import this component from the CDN, use the following code.

import 'https://early.webawesome.com/webawesome@3.0.0-alpha.4/dist/components/drawer/drawer.js';
Coming soon! Coming soon!
    No results