Drawer·续

Responsive

Responsive drawer classes hide content outside the viewport from a specified breakpoint and down. Above that breakpoint, the contents within will behave as usual. For example, .lg:drawer hides content in a drawer below the lg breakpoint, but shows the content above the lg breakpoint. Responsive drawer classes are available for each breakpoint.

To make a responsive drawer, replace the .drawer base class with a responsive variant. Close buttons inside the drawer work without an explicit data-bs-target.

<Example code={`<button class="btn-solid theme-primary lg:d-none" type="button" data-bs-toggle="drawer" data-bs-target="#drawerResponsive" aria-controls="drawerResponsive">Toggle drawer</button>

<div class="alert theme-info d-none lg:d-block mb-3"><p>Resize your browser to show the responsive drawer toggle.</p></div>

<dialog class="lg:drawer drawer-end" tabindex="-1" id="drawerResponsive" aria-labelledby="drawerResponsiveLabel"> <div class="drawer-header"> <h5 class="drawer-title" id="drawerResponsiveLabel">Responsive drawer</h5> </div> <div class="drawer-body"> <p class="mb-0">This is content within an <code>.lg:drawer</code>.</p> </div> </dialog>`} />

Placement

There’s no default placement for drawer components, so you must add one of the modifier classes below.

  • .drawer-start places drawer on the left of the viewport (shown above)
  • .drawer-end places drawer on the right of the viewport
  • .drawer-top places drawer on the top of the viewport
  • .drawer-bottom places drawer on the bottom of the viewport
  • .drawer-fullscreen covers the entire viewport

Try the top, right, bottom, and fullscreen examples out below.

<Example code={`<button class="btn-solid theme-primary" type="button" data-bs-toggle="drawer" data-bs-target="#drawerTop" aria-controls="drawerTop">Toggle top drawer</button>

<dialog class="drawer drawer-top" tabindex="-1" id="drawerTop" aria-labelledby="drawerTopLabel"> <div class="drawer-header"> <h5 class="drawer-title" id="drawerTopLabel">Drawer top</h5> </div> <div class="drawer-body"> ... </div> </dialog>`} />

<Example code={`<button class="btn-solid theme-primary" type="button" data-bs-toggle="drawer" data-bs-target="#drawerRight" aria-controls="drawerRight">Toggle right drawer</button>

<dialog class="drawer drawer-end" tabindex="-1" id="drawerRight" aria-labelledby="drawerRightLabel"> <div class="drawer-header"> <h5 class="drawer-title" id="drawerRightLabel">Drawer right</h5> </div> <div class="drawer-body"> ... </div> </dialog>`} />

<Example code={`<button class="btn-solid theme-primary" type="button" data-bs-toggle="drawer" data-bs-target="#drawerBottom" aria-controls="drawerBottom">Toggle bottom drawer</button>

<dialog class="drawer drawer-bottom" tabindex="-1" id="drawerBottom" aria-labelledby="drawerBottomLabel"> <div class="drawer-header"> <h5 class="drawer-title" id="drawerBottomLabel">Drawer bottom</h5> </div> <div class="drawer-body small"> ... </div> </dialog>`} />

<Example code={`<button class="btn-solid theme-primary" type="button" data-bs-toggle="drawer" data-bs-target="#drawerFullscreen" aria-controls="drawerFullscreen">Toggle fullscreen drawer</button>

<dialog class="drawer drawer-fullscreen" tabindex="-1" id="drawerFullscreen" aria-labelledby="drawerFullscreenLabel"> <div class="drawer-header"> <h5 class="drawer-title" id="drawerFullscreenLabel">Fullscreen drawer</h5> </div> <div class="drawer-body"> <p>This drawer covers the entire viewport, useful for full-page menus or modal-like experiences.</p> </div> </dialog>`} />

Add an optional .drawer-footer for action buttons or other content at the bottom of the drawer.

<div class="drawer-header"> <h5 class="drawer-title" id="drawerFooterLabel">Drawer with footer</h5> </div> <div class="drawer-body"> <p>Content for the drawer goes here. The footer will stick to the bottom.</p> </div> <div class="drawer-footer"> <button type="button" class="btn-solid theme-secondary" data-bs-dismiss="drawer">Close</button> <button type="button" class="btn-solid theme-primary">Save changes</button> </div> </dialog>`} />

Accessibility

Since the drawer is a native <dialog>, it gets the correct dialog role. Modal drawers also get modal semantics and focus trapping from showModal(). Non-modal drawers (scroll: true and backdrop: false) use show() instead, so they do not trap focus or mark the rest of the page as inert. Add aria-labelledby that points at the drawer title.

CSS

Variables

Tokens for the drawer backdrop are available in a separate map as they style the native ::backdrop pseudo-element.

Usage

The drawer plugin is built on the native <dialog> element and utilizes a few classes and attributes to handle the heavy lifting:

  • .drawer on a <dialog> element enables drawer behavior
  • .drawer-start places the drawer on the left
  • .drawer-end places the drawer on the right
  • .drawer-top places the drawer on the top
  • .drawer-bottom places the drawer on the bottom

Add a dismiss button with the data-bs-dismiss="drawer" attribute, which triggers the JavaScript functionality. Be sure to use the <button> element with it for proper behavior across all devices.

Via data attributes

Attribute Description
data-bs-toggle="drawer" Initializes drawer behavior on the trigger element.
data-bs-target CSS selector for the <dialog> drawer element to open (or use href on a link).
data-bs-dismiss="drawer" On a dismiss control, closes the drawer when activated.

Toggle

Add data-bs-toggle="drawer" and a data-bs-target or href to the element to automatically assign control of one drawer element. The data-bs-target attribute accepts a CSS selector to apply the drawer to. Be sure to use a <dialog> element with the class drawer.

Dismiss

While both ways to dismiss a drawer are supported, keep in mind that dismissing from outside a drawer does not match the ARIA Authoring Practices Guide dialog (modal) pattern. Do this at your own risk.

Via JavaScript

Enable manually with:

const drawerElementList = document.querySelectorAll('dialog.drawer')
const drawerList = [...drawerElementList].map(drawerEl => new bootstrap.Drawer(drawerEl))

Dependencies

Options

Name Type Default Description
backdrop boolean or the string static true Apply a backdrop on body while drawer is open. Alternatively, specify static for a backdrop which doesn’t close the drawer when clicked.
keyboard boolean true Closes the drawer when escape key is pressed.
scroll boolean false Allow body scrolling while the drawer is open. Combined with backdrop: false, opens the drawer as non-modal (no focus trap).

Methods

Activates your content as a drawer element. Accepts an optional options object.

You can create a drawer instance with the constructor, for example:

const bsDrawer = new bootstrap.Drawer('#myDrawer')
Method Description
dispose Destroys an element’s drawer.
getInstance Static method which allows you to get the drawer instance associated with a DOM element.
getOrCreateInstance Static method which allows you to get the drawer instance associated with a DOM element, or create a new one in case it wasn’t initialized.
hide Hides a drawer element. Returns a promise that resolves once the drawer element is hidden (i.e. after the hidden.bs.drawer event fires).
show Shows a drawer element. Returns a promise that resolves once the drawer element is shown (i.e. after the shown.bs.drawer event fires).
toggle Toggles a drawer element to shown or hidden. Returns a promise that resolves once the drawer element is shown or hidden (i.e. after the shown.bs.drawer or hidden.bs.drawer event fires).

Events

Bootstrap’s drawer class exposes a few events for hooking into drawer functionality.

Event type Description
hide.bs.drawer This event is fired immediately when the hide method has been called.
hidden.bs.drawer This event is fired when a drawer element has been hidden from the user (will wait for CSS transitions to complete).
hidePrevented.bs.drawer This event is fired when the drawer is shown, its backdrop is static and a click outside of the drawer is performed. The event is also fired when the escape key is pressed and the keyboard option is set to false.
show.bs.drawer This event fires immediately when the show instance method is called.
shown.bs.drawer This event is fired when a drawer element has been made visible to the user (will wait for CSS transitions to complete).
const myDrawer = document.getElementById('myDrawer')
myDrawer.addEventListener('hidden.bs.drawer', event => {
  // do something...
})