Overlays
Drawer
A dialog for a side panel. Non-modal by default, so the page behind it stays usable, or modal through showModal().
- Element
<slab-drawer>- Built on
<dialog>
Example
Here's a styled version, <slab-styled-drawer>. It's not
in the package — it's an example of what you can build with the
headless element. Copy it and make it yours at the
bottom of this page. Switch to "As it ships" to see the same markup
with the headless element and no CSS at all. That's your starting
point.
14 Alder Lane. A 1,600 square foot slab, 4 inches thick, on a gravel base.
Poured Friday. Curing until the 23rd.
<button class="styled-button" type="button">Site details</button>
<button class="styled-button" type="button" data-show-modal="drawer-example">Site details, as a modal</button>
<slab-styled-drawer id="drawer-example" label="Lot 14">
<p>14 Alder Lane. A 1,600 square foot slab, 4 inches thick, on a gravel base.</p>
<p>Poured Friday. Curing until the 23rd.</p>
<form method="dialog">
<button class="styled-button">Close</button>
</form>
</slab-styled-drawer>
14 Alder Lane. A 1,600 square foot slab, 4 inches thick, on a gravel base.
Poured Friday. Curing until the 23rd.
<button class="styled-button" type="button">Site details</button>
<button class="styled-button" type="button" data-show-modal="drawer-example-as-it-ships">Site details, as a modal</button>
<slab-drawer id="drawer-example-as-it-ships" label="Lot 14">
<p>14 Alder Lane. A 1,600 square foot slab, 4 inches thick, on a gravel base.</p>
<p>Poured Friday. Curing until the 23rd.</p>
<form method="dialog">
<button class="styled-button">Close</button>
</form>
</slab-drawer>
Usage
pnpm add slab-elements lit
import 'slab-elements/drawer.js';
export function MyDrawer() {
return (
<slab-drawer label="Filters">
…
<form method="dialog">
<button>Done</button>
</form>
</slab-drawer>
);
}
MyDrawer.jsx React 19 or later, which sets props on custom elements as properties.
import 'slab-elements/drawer.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-drawer')
export class MyDrawer extends LitElement {
render() {
return html`
<slab-drawer label="Filters">
…
<form method="dialog">
<button>Done</button>
</form>
</slab-drawer>
`;
}
}
my-drawer.ts Slab is built on Lit, so its elements are at home in Lit's templates.
<script setup>
import 'slab-elements/drawer.js';
</script>
<template>
<slab-drawer label="Filters">
…
<form method="dialog">
<button>Done</button>
</form>
</slab-drawer>
</template>
MyDrawer.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.
<script>
import 'slab-elements/drawer.js';
</script>
<slab-drawer label="Filters">
…
<form method="dialog">
<button>Done</button>
</form>
</slab-drawer>
MyDrawer.svelte Svelte binds properties and events to custom elements like any other element.
import 'slab-elements/drawer.js';
export const MyDrawer = <template>
<slab-drawer label="Filters">
…
<form method="dialog">
<button>Done</button>
</form>
</slab-drawer>
</template>;
MyDrawer.gjs Ember sets attributes, and a dynamic value like {{this.max}} sets the property when the element has one. {{on}} listens for any event by its name.
<script type="module">
import 'slab-elements/drawer.js';
</script>
<slab-drawer label="Filters">
…
<form method="dialog">
<button>Done</button>
</form>
</slab-drawer>
index.html Any bundler (or an import map) takes care of the imports.
Modal or not
Setting open, or calling show(), opens it non-modally, and the page behind
it stays usable. Calling showModal() opens it as a modal, the way it opens a
<dialog>: there’s a backdrop, focus stays inside, and the rest of the page is
inert until it closes. Try both above.
const drawer = document.querySelector('slab-drawer');
drawer.show(); // The page behind it stays usable.
drawer.showModal(); // The rest of the page goes inert.
With modal, setting open opens it as a modal too, for a framework that
binds open.
<slab-drawer label="Filters" modal open>…</slab-drawer>
Style a modal one’s backdrop through ::part(dialog)::backdrop, and match one
or the other with ::part(dialog):modal and ::part(dialog):not(:modal).
Keyboard
| Key | What it does |
|---|---|
| Escape | Closes the drawer, unless closedby is “none”. |
| Tab | Moves focus on through the drawer and out to the page. When it’s modal, focus stays in the drawer. |
Behavior
A dialog with no visual styles for a side panel. It's Dialog, so setting open or calling show() opens it non-modally and the page behind it stays interactive: there's no focus trap, no ::backdrop, and nothing is made inert. showModal() opens it as a modal, with a backdrop, the way it opens a <dialog>.
Everything else is Dialog's: open, returnValue, close(returnValue), a slotted <form method="dialog">, the label, focus moving into it when it opens, and "cancel" and "close" from the host. Focus goes back to where it was when it closes, which the <dialog> does itself.
A non-modal <dialog> ignores Escape unless it has closedby, so closedby defaults to "closerequest" here. Escape closes it, and "cancel" can still be canceled to keep it open. Set "none" to turn that off, or "any" to also close it on a click outside it.
Which edge it's anchored to, and how it gets there, is how it looks. Position it through ::part(dialog).
API
<slab-drawer>
| Name | Type | Default | Description |
|---|---|---|---|
closedby
|
'any' | 'closerequest' | 'none' |
Passed to the <dialog>. "any" closes it on a click outside it where the browser supports that. Browsers that don't ignore it. |
|
hide-label
|
boolean |
false |
Hides the label visually and leaves it as the accessible name, for a design that shows the label some other way. |
label
|
string |
Names it. It's the accessible name, and the visible label where there is one. | |
modal
|
boolean |
false |
Opens it as a modal when open is set, as showModal() does, for a modal opened declaratively. Not on a native <dialog>, where open always opens a non-modal one. show() and showModal() open it as they say whatever it is, and a change while it's open waits for it to open again. |
open
|
boolean |
false |
Whether it's open. Setting it opens it non-modally, as open does on a <dialog>, unless modal is set. |
| Name | Type | Description |
|---|---|---|
returnValue
|
string |
The <dialog>'s, so close(returnValue) and a <form method="dialog"> set it the way they do natively. |
| Signature | Description |
|---|---|
close(returnValue?: string): void
|
Closes it, setting returnValue to the value given, as a native dialog's close() does. |
requestClose(returnValue?: string): void
|
Asks it to close, as a native dialog's requestClose() does: "cancel" first, then a close that sets returnValue to the value given, unless "cancel" is canceled. |
show(): void
|
Opens it non-modally, as a native dialog's show() does. |
showModal(): void
|
Opens it as a modal, as a native dialog's showModal() does, with a backdrop and the rest of the page inert. |
| Name | Type | Description |
|---|---|---|
cancel
|
Event |
When something asks it to close: Escape, a click outside with closedby="any", or requestClose(). Cancel it to keep it open, as a native dialog's. |
toggle
|
ToggleEvent |
When it opens or closes, as a ToggleEvent with oldState and newState that doesn't bubble, as a popover's does. |
close
|
Event |
When it closes, as a native dialog's does. |
| Name | Type | Description |
|---|---|---|
label
|
Element | string |
Its heading, in place of label |
(default)
|
Element | string |
| Name | Description |
|---|---|
dialog
|
The native dialog. A modal one's backdrop is its ::backdrop |
label
|
The label |
| Name | Description |
|---|---|
:state(open)
|
While it's open. |
Copy and own it
<slab-styled-drawer> isn't in the package. It's an example —
copy it into your project and change whatever you want. Keyboard,
focus, ARIA, forms, and events all come from the headless element,
and the styles only reach in through ::part() and
:state(), same as yours would.
The same styles, written from outside the element, so
there's nothing to subclass. The CSS styles every
<slab-drawer> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Each component registers its headless element and imports its CSS. Use it like the element: its props are the element's attributes and properties.
Drawer.jsx
import 'slab-elements/drawer.js';
import './drawer.css';
export function Drawer(props) {
return <slab-drawer {...props} />;
}
drawer.css
/*
A closed dialog isn't displayed, so the animation runs again each time it
opens.
*/
slab-drawer::part(dialog) {
animation: slab-drawer-rise 200ms ease-out;
background-color: var(--paper);
border: 3px solid var(--ink);
box-shadow: 10px 10px 0 var(--ember);
box-sizing: border-box;
color: var(--ink);
font-family: var(--font-body);
inline-size: min(32rem, 100% - 2rem);
/*
Where a modal one is already, in the top layer. A non-modal one isn't
there, so it's fixed in the middle of the viewport too, rather than left
where it is in the page, and raised over what's around it. Drawer's
styles, which come after, put it at an edge instead.
*/
inset: 0;
line-height: 1.5;
margin: auto;
padding: 0 1.5rem 1.5rem;
position: fixed;
z-index: 1;
}
slab-drawer::part(dialog)::backdrop {
animation: slab-drawer-fade 200ms ease-out;
/*
Not a token. A backdrop doesn't inherit custom properties from the
dialog in every browser, but it does take its color scheme.
*/
background-color: light-dark(rgb(28 28 28 / 70%), rgb(0 0 0 / 75%));
}
/*
The header. It's pulled out to the dialog's edges, which its padding would
otherwise keep it from.
*/
/*
Ink in the light. In the dark, ink is the light color, so the header is
the raised surface instead, rather than a bright bar.
*/
slab-drawer::part(label) {
background-color: light-dark(var(--ink), var(--chalk));
border-block-end: 6px solid var(--ember);
color: light-dark(var(--paper), var(--ink));
font-family: var(--font-display);
font-size: 1.5rem;
font-weight: 600;
margin-block-end: 1.25rem;
margin-inline: -1.5rem;
padding: 1rem 1.5rem;
}
@keyframes slab-drawer-rise {
from {
opacity: 0;
translate: 0 1rem;
}
}
@keyframes slab-drawer-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-drawer::part(dialog),
slab-drawer::part(dialog)::backdrop {
animation: none;
}
}
slab-drawer::part(dialog) {
animation: slab-drawer-slide 200ms ease-out;
block-size: 100dvh;
border-block: 0;
border-inline-end: 0;
box-shadow: -8px 0 0 var(--ember);
inline-size: min(26rem, 100% - 2rem);
inset-block: 0;
inset-inline: auto 0;
margin: 0;
max-block-size: none;
position: fixed;
}
@keyframes slab-drawer-slide {
from {
opacity: 0;
translate: 1.5rem 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-drawer::part(dialog) {
animation: none;
}
}
tokens.css
:root {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}
Under the hood, it's just the headless element, subclassed,
with styles set. That's it. To use it, copy
these files, swap the tag in @customElement()
for one of your own, and import it.
tokens.styles.ts holds the colors and fonts
every styled example shares, so you only need it once.
drawer.ts
import { customElement } from 'lit/decorators.js';
import Drawer from 'slab-elements/drawer.js';
import dialog from './dialog.styles.js';
import styles from './drawer.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-drawer': StyledDrawer;
}
}
/**
* Drawer, styled: Dialog's header and edges, as a full-height panel on the
* page's end edge.
*/
@customElement('slab-styled-drawer')
export default class StyledDrawer extends Drawer {
static override styles = [tokens, dialog, styles];
}
dialog.styles.ts
import { css } from 'lit';
export default css`
/*
A closed dialog isn't displayed, so the animation runs again each time it
opens.
*/
[part~='dialog'] {
animation: rise 200ms ease-out;
background-color: var(--paper);
border: 3px solid var(--ink);
box-shadow: 10px 10px 0 var(--ember);
box-sizing: border-box;
color: var(--ink);
font-family: var(--font-body);
inline-size: min(32rem, 100% - 2rem);
/*
Where a modal one is already, in the top layer. A non-modal one isn't
there, so it's fixed in the middle of the viewport too, rather than left
where it is in the page, and raised over what's around it. Drawer's
styles, which come after, put it at an edge instead.
*/
inset: 0;
line-height: 1.5;
margin: auto;
padding: 0 1.5rem 1.5rem;
position: fixed;
z-index: 1;
&::backdrop {
animation: fade 200ms ease-out;
/*
Not a token. A backdrop doesn't inherit custom properties from the
dialog in every browser, but it does take its color scheme.
*/
background-color: light-dark(rgb(28 28 28 / 70%), rgb(0 0 0 / 75%));
}
}
/*
The header. It's pulled out to the dialog's edges, which its padding would
otherwise keep it from.
*/
[part~='label'] {
/*
Ink in the light. In the dark, ink is the light color, so the header is
the raised surface instead, rather than a bright bar.
*/
background-color: light-dark(var(--ink), var(--chalk));
border-block-end: 6px solid var(--ember);
color: light-dark(var(--paper), var(--ink));
font-family: var(--font-display);
font-size: 1.5rem;
font-weight: 600;
margin-block-end: 1.25rem;
margin-inline: -1.5rem;
padding: 1rem 1.5rem;
}
@keyframes rise {
from {
opacity: 0;
translate: 0 1rem;
}
}
@keyframes fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
[part~='dialog'],
[part~='dialog']::backdrop {
animation: none;
}
}
`;
drawer.styles.ts
import { css } from 'lit';
/*
Dialog's styles, then these, which move it to the page's end edge at full
height. A non-modal one has no backdrop, so the shadow falls across the page
instead. A modal one, opened with showModal(), keeps Dialog's backdrop.
*/
export default css`
[part~='dialog'] {
animation: slide 200ms ease-out;
block-size: 100dvh;
border-block: 0;
border-inline-end: 0;
box-shadow: -8px 0 0 var(--ember);
inline-size: min(26rem, 100% - 2rem);
inset-block: 0;
inset-inline: auto 0;
margin: 0;
max-block-size: none;
position: fixed;
}
@keyframes slide {
from {
opacity: 0;
translate: 1.5rem 0;
}
}
@media (prefers-reduced-motion: reduce) {
[part~='dialog'] {
animation: none;
}
}
`;
tokens.styles.ts
import { css } from 'lit';
/*
The styled components' palette and type, taken from the logo: ink for the
courses, ember for the base, paper behind them.
Each is a short alias for a "--slab-styled-*" property, with the default as
its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
which every shadow root inherits. Were the defaults declared under those
names here instead, each host's own declaration would shadow the page's.
Each default is "light-dark()", so the components follow the page's
"color-scheme": "dark" for dark, "light" for light, and "light dark" to
follow the operating system. Ink and paper trade places in the dark, so
ink is always the text and paper always what's behind it. Contrast is given
as light, then dark.
*/
export default css`
:host {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}
`;
The same styles, written from outside the element, so
there's nothing to subclass. The CSS styles every
<slab-drawer> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Each component registers its headless element and imports its CSS. Use it like the element: its props are the element's attributes and properties.
Drawer.vue
<script setup>
import 'slab-elements/drawer.js';
import './drawer.css';
</script>
<template>
<slab-drawer><slot /></slab-drawer>
</template>
drawer.css
/*
A closed dialog isn't displayed, so the animation runs again each time it
opens.
*/
slab-drawer::part(dialog) {
animation: slab-drawer-rise 200ms ease-out;
background-color: var(--paper);
border: 3px solid var(--ink);
box-shadow: 10px 10px 0 var(--ember);
box-sizing: border-box;
color: var(--ink);
font-family: var(--font-body);
inline-size: min(32rem, 100% - 2rem);
/*
Where a modal one is already, in the top layer. A non-modal one isn't
there, so it's fixed in the middle of the viewport too, rather than left
where it is in the page, and raised over what's around it. Drawer's
styles, which come after, put it at an edge instead.
*/
inset: 0;
line-height: 1.5;
margin: auto;
padding: 0 1.5rem 1.5rem;
position: fixed;
z-index: 1;
}
slab-drawer::part(dialog)::backdrop {
animation: slab-drawer-fade 200ms ease-out;
/*
Not a token. A backdrop doesn't inherit custom properties from the
dialog in every browser, but it does take its color scheme.
*/
background-color: light-dark(rgb(28 28 28 / 70%), rgb(0 0 0 / 75%));
}
/*
The header. It's pulled out to the dialog's edges, which its padding would
otherwise keep it from.
*/
/*
Ink in the light. In the dark, ink is the light color, so the header is
the raised surface instead, rather than a bright bar.
*/
slab-drawer::part(label) {
background-color: light-dark(var(--ink), var(--chalk));
border-block-end: 6px solid var(--ember);
color: light-dark(var(--paper), var(--ink));
font-family: var(--font-display);
font-size: 1.5rem;
font-weight: 600;
margin-block-end: 1.25rem;
margin-inline: -1.5rem;
padding: 1rem 1.5rem;
}
@keyframes slab-drawer-rise {
from {
opacity: 0;
translate: 0 1rem;
}
}
@keyframes slab-drawer-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-drawer::part(dialog),
slab-drawer::part(dialog)::backdrop {
animation: none;
}
}
slab-drawer::part(dialog) {
animation: slab-drawer-slide 200ms ease-out;
block-size: 100dvh;
border-block: 0;
border-inline-end: 0;
box-shadow: -8px 0 0 var(--ember);
inline-size: min(26rem, 100% - 2rem);
inset-block: 0;
inset-inline: auto 0;
margin: 0;
max-block-size: none;
position: fixed;
}
@keyframes slab-drawer-slide {
from {
opacity: 0;
translate: 1.5rem 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-drawer::part(dialog) {
animation: none;
}
}
tokens.css
:root {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}
The same styles, written from outside the element, so
there's nothing to subclass. The CSS styles every
<slab-drawer> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Import drawer.js once, such
as in your root layout, and use
<slab-drawer> as is. A Svelte component
around it would take slot="…" content as its
own slot and drop it.
drawer.js
import 'slab-elements/drawer.js';
import './drawer.css';
drawer.css
/*
A closed dialog isn't displayed, so the animation runs again each time it
opens.
*/
slab-drawer::part(dialog) {
animation: slab-drawer-rise 200ms ease-out;
background-color: var(--paper);
border: 3px solid var(--ink);
box-shadow: 10px 10px 0 var(--ember);
box-sizing: border-box;
color: var(--ink);
font-family: var(--font-body);
inline-size: min(32rem, 100% - 2rem);
/*
Where a modal one is already, in the top layer. A non-modal one isn't
there, so it's fixed in the middle of the viewport too, rather than left
where it is in the page, and raised over what's around it. Drawer's
styles, which come after, put it at an edge instead.
*/
inset: 0;
line-height: 1.5;
margin: auto;
padding: 0 1.5rem 1.5rem;
position: fixed;
z-index: 1;
}
slab-drawer::part(dialog)::backdrop {
animation: slab-drawer-fade 200ms ease-out;
/*
Not a token. A backdrop doesn't inherit custom properties from the
dialog in every browser, but it does take its color scheme.
*/
background-color: light-dark(rgb(28 28 28 / 70%), rgb(0 0 0 / 75%));
}
/*
The header. It's pulled out to the dialog's edges, which its padding would
otherwise keep it from.
*/
/*
Ink in the light. In the dark, ink is the light color, so the header is
the raised surface instead, rather than a bright bar.
*/
slab-drawer::part(label) {
background-color: light-dark(var(--ink), var(--chalk));
border-block-end: 6px solid var(--ember);
color: light-dark(var(--paper), var(--ink));
font-family: var(--font-display);
font-size: 1.5rem;
font-weight: 600;
margin-block-end: 1.25rem;
margin-inline: -1.5rem;
padding: 1rem 1.5rem;
}
@keyframes slab-drawer-rise {
from {
opacity: 0;
translate: 0 1rem;
}
}
@keyframes slab-drawer-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-drawer::part(dialog),
slab-drawer::part(dialog)::backdrop {
animation: none;
}
}
slab-drawer::part(dialog) {
animation: slab-drawer-slide 200ms ease-out;
block-size: 100dvh;
border-block: 0;
border-inline-end: 0;
box-shadow: -8px 0 0 var(--ember);
inline-size: min(26rem, 100% - 2rem);
inset-block: 0;
inset-inline: auto 0;
margin: 0;
max-block-size: none;
position: fixed;
}
@keyframes slab-drawer-slide {
from {
opacity: 0;
translate: 1.5rem 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-drawer::part(dialog) {
animation: none;
}
}
tokens.css
:root {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}
The same styles, written from outside the element, so
there's nothing to subclass. The CSS styles every
<slab-drawer> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Each component registers its headless element and imports its CSS. Use it like the element: its attributes and its content go to the element.
Drawer.gjs
import 'slab-elements/drawer.js';
import './drawer.css';
export const Drawer = <template>
<slab-drawer ...attributes>{{yield}}</slab-drawer>
</template>;
drawer.css
/*
A closed dialog isn't displayed, so the animation runs again each time it
opens.
*/
slab-drawer::part(dialog) {
animation: slab-drawer-rise 200ms ease-out;
background-color: var(--paper);
border: 3px solid var(--ink);
box-shadow: 10px 10px 0 var(--ember);
box-sizing: border-box;
color: var(--ink);
font-family: var(--font-body);
inline-size: min(32rem, 100% - 2rem);
/*
Where a modal one is already, in the top layer. A non-modal one isn't
there, so it's fixed in the middle of the viewport too, rather than left
where it is in the page, and raised over what's around it. Drawer's
styles, which come after, put it at an edge instead.
*/
inset: 0;
line-height: 1.5;
margin: auto;
padding: 0 1.5rem 1.5rem;
position: fixed;
z-index: 1;
}
slab-drawer::part(dialog)::backdrop {
animation: slab-drawer-fade 200ms ease-out;
/*
Not a token. A backdrop doesn't inherit custom properties from the
dialog in every browser, but it does take its color scheme.
*/
background-color: light-dark(rgb(28 28 28 / 70%), rgb(0 0 0 / 75%));
}
/*
The header. It's pulled out to the dialog's edges, which its padding would
otherwise keep it from.
*/
/*
Ink in the light. In the dark, ink is the light color, so the header is
the raised surface instead, rather than a bright bar.
*/
slab-drawer::part(label) {
background-color: light-dark(var(--ink), var(--chalk));
border-block-end: 6px solid var(--ember);
color: light-dark(var(--paper), var(--ink));
font-family: var(--font-display);
font-size: 1.5rem;
font-weight: 600;
margin-block-end: 1.25rem;
margin-inline: -1.5rem;
padding: 1rem 1.5rem;
}
@keyframes slab-drawer-rise {
from {
opacity: 0;
translate: 0 1rem;
}
}
@keyframes slab-drawer-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-drawer::part(dialog),
slab-drawer::part(dialog)::backdrop {
animation: none;
}
}
slab-drawer::part(dialog) {
animation: slab-drawer-slide 200ms ease-out;
block-size: 100dvh;
border-block: 0;
border-inline-end: 0;
box-shadow: -8px 0 0 var(--ember);
inline-size: min(26rem, 100% - 2rem);
inset-block: 0;
inset-inline: auto 0;
margin: 0;
max-block-size: none;
position: fixed;
}
@keyframes slab-drawer-slide {
from {
opacity: 0;
translate: 1.5rem 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-drawer::part(dialog) {
animation: none;
}
}
tokens.css
:root {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}