Drawer
A content container that switches contexts based on a provided breakpoint.
A content container that switches contexts based on a provided breakpoint.
npm install @vrembem/drawer@use "@vrembem/drawer";Drawer provides two JavaScript entries depending on how you want to register the custom element.
// Register the <vb-drawer> tag as a side-effect
import "@vrembem/drawer/define";// Import the element class directly
import { Drawer } from "@vrembem/drawer";
// Define the element yourself under a custom namespace
customElements.define("vb-drawer", Drawer);Once registered, Drawer is available as a <vb-drawer> custom element with a small Sass module for styles. Behavior is controlled through element attributes, while output and default values are controlled through Sass config and design token CSS variables.
The custom element observes the following attributes. Changes made after the element is connected are applied automatically.
id (required)<dialog> element is given an id of [id]-modal, which is the value that triggers should target with the commandfor invoker API.breakpointmd, lg, etc), or any valid CSS length.positiontop, right, bottom, or left values that apply modal--pos-[key] modifiers.Drawer comes with the following Sass configuration options that can be set using either the config module or Sass with clause methods.
@use "@vrembem/drawer" with (
$config: (
// Sets the default breakpoint for drawers
// @type string | number
"breakpoint": "md",
// Set the default position of modal drawers
// @type string
"position": "left"
)
);Drawer uses design token CSS variables to provide default values for custom element options.
| Variable | Value |
|---|---|
Copied! | Copied! |
Copied! | Copied! |
There are no variables that match your search.
The <vb-drawer> custom element wraps any content that should render inline when the viewport is at or above the breakpoint and inside a modal dialog when it's below. Once registered, the element manages the internal <dialog> structure, authors only need to provide the children.
Every drawer requires a unique id. Triggers open and close the modal state using the native command and commandfor attributes, targeting [id]-modal — the id of the dialog the drawer creates internally.
<button command="show-modal" commandfor="[unique-id]-modal">
...
</button>
<vb-drawer id="[unique-id]">
...
</vb-drawer>The panel component is a great fit for structuring drawer content and works in both the static and modal layouts. Apply the panel class to the <vb-drawer> element and compose its elements inside.
<button command="show-modal" commandfor="custom-drawer-modal">
Show modal
</button>
<vb-drawer id="custom-drawer" class="panel">
<div class="panel__header">
<h2 class="panel__title">Drawer</h2>
<button class="link" command="close" commandfor="custom-drawer-modal">
Close
</button>
</div>
<div class="panel__body">
<p>This is the contents of a drawer...</p>
</div>
<div class="panel__footer">
<p>Drawer footer</p>
</div>
</vb-drawer>When the viewport is at or above the breakpoint, the drawer's children render inline as normal document flow. When the viewport drops below the breakpoint, the element:
is-modal class to the host element and collapses its layout box so it no longer affects surrounding flow.<dialog> element with the closedby="any" attribute and the following classes:
[id]-modal, so triggers can open and close it with the native command/commandfor attributes.Switching back above the breakpoint reverses the process. Children are moved back inline and the dialog is closed if it was open.
The breakpoint attribute defines when the drawer switches between the static and modal states of the drawer. This accepts a value in three forms:
--vb-breakpoint-[key] design tokens (e.g. md, lg, or any custom key defined).760 becomes 760px).48rem, 800px).If omitted, defaults to the value of --vb-drawer-breakpoint.
The position attribute controls which viewport edge the modal dialog anchors to when open. This maps directly to the modal--pos-[key] modifier applied to the internal dialog.
If omitted, defaults to the value of --vb-drawer-position.
Available values
toprightbottomleft