Overlays
Dialog
A dialog built on the native <dialog>, modal or not. A modal one keeps focus inside, closes on Escape, and makes the rest of the page inert — the platform does the heavy lifting.
- Element
<slab-dialog>- Built on
<dialog>
Example
Here's a styled version, <slab-styled-dialog>. 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.
It's day 2 of 7. Concrete reaches about 70% of its strength in a week, and stripping the forms now can crack the edges.
<button class="styled-button styled-button--danger" type="button" data-show-modal="dialog-example">
Strip forms
</button>
<slab-styled-dialog id="dialog-example" label="Strip the forms?" closedby="any">
<p>
It's day 2 of 7. Concrete reaches about 70% of its strength in a week,
and stripping the forms now can crack the edges.
</p>
<form class="actions" method="dialog">
<button class="styled-button" value="cancel">Wait</button>
<button class="styled-button styled-button--danger" value="strip">Strip anyway</button>
</form>
</slab-styled-dialog>
It's day 2 of 7. Concrete reaches about 70% of its strength in a week, and stripping the forms now can crack the edges.
<button class="styled-button styled-button--danger" type="button" data-show-modal="dialog-example-as-it-ships">
Strip forms
</button>
<slab-dialog id="dialog-example-as-it-ships" label="Strip the forms?" closedby="any">
<p>
It's day 2 of 7. Concrete reaches about 70% of its strength in a week,
and stripping the forms now can crack the edges.
</p>
<form class="actions" method="dialog">
<button class="styled-button" value="cancel">Wait</button>
<button class="styled-button styled-button--danger" value="strip">Strip anyway</button>
</form>
</slab-dialog>
Usage
pnpm add slab-elements lit
import 'slab-elements/dialog.js';
export function MyDialog() {
return (
<slab-dialog label="Delete this file?">
<p>It can't be recovered.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</slab-dialog>
);
}
MyDialog.jsx React 19 or later, which sets props on custom elements as properties.
import 'slab-elements/dialog.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-dialog')
export class MyDialog extends LitElement {
render() {
return html`
<slab-dialog label="Delete this file?">
<p>It can't be recovered.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</slab-dialog>
`;
}
}
my-dialog.ts Slab is built on Lit, so its elements are at home in Lit's templates.
<script setup>
import 'slab-elements/dialog.js';
</script>
<template>
<slab-dialog label="Delete this file?">
<p>It can't be recovered.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</slab-dialog>
</template>
MyDialog.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.
<script>
import 'slab-elements/dialog.js';
</script>
<slab-dialog label="Delete this file?">
<p>It can't be recovered.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</slab-dialog>
MyDialog.svelte Svelte binds properties and events to custom elements like any other element.
import 'slab-elements/dialog.js';
export const MyDialog = <template>
<slab-dialog label="Delete this file?">
<p>It can't be recovered.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</slab-dialog>
</template>;
MyDialog.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/dialog.js';
</script>
<slab-dialog label="Delete this file?">
<p>It can't be recovered.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</slab-dialog>
index.html Any bundler (or an import map) takes care of the imports.
Open it with showModal(). When it closes, returnValue tells you which
button did it — same as a <dialog>.
Modal or not
Calling showModal() opens it as a modal. Calling show(), or setting open,
opens it non-modally, the way they open a <dialog>: there’s no backdrop,
focus can leave it, and the rest of the page stays usable while it’s open.
const dialog = document.querySelector('slab-dialog');
dialog.showModal(); // The rest of the page goes inert.
dialog.show(); // The rest of the page stays usable.
A <dialog> can’t be opened as a modal without script, so Slab adds modal.
With it, setting open opens it as showModal() does, which is what a
framework that binds open needs. show() and showModal() still open it as
they say.
<slab-dialog label="Delete this file?" modal open>…</slab-dialog>
As with a <dialog>, Escape only closes a non-modal one with closedby. Set
closedby="closerequest" for Escape, or closedby="any" for a click outside
it too. Style one or the other with ::part(dialog):modal and
::part(dialog):not(:modal).
Keyboard
| Key | What it does |
|---|---|
| Tab, Shift+Tab | Moves focus between the dialog’s controls. When it’s modal, the rest of the page is inert. |
| Escape | Closes the dialog and dispatches “cancel”, which can be canceled to keep it open. A non-modal one needs closedby. |
Behavior
A dialog with no visual styles, around a native <dialog>, opened modally or not the way one is.
showModal() opens it as a modal: it traps focus, closes on Escape, has a ::backdrop, and makes the rest of the page inert. show(), or setting open, opens it non-modally, as they open a <dialog>: the rest of the page stays interactive, there's no backdrop, and, as natively, Escape only closes it with closedby. Calling either method while it's open leaves it as it is. Match a modal one with ::part(dialog):modal.
modal is Slab's, for a modal opened declaratively, as a framework binding open does: with it, setting open opens it as showModal() does.
open reflects, and it stays true to the dialog: when the dialog closes itself, on Escape or through a <form method="dialog">, open goes false along with it. close(returnValue) and a <form method="dialog">'s submitter both set returnValue, as they do on a <dialog>.
The dialog is named by its label: the label attribute or the label slot. The label isn't a heading, because a heading's level is the page's to choose. Plain label text names the dialog without being one. For a heading, slot your own at whatever level fits: <h2 slot="label">.
Light dismiss is the platform's or nothing. closedby="any" is passed through to the <dialog>, which closes on a backdrop click where the browser supports it and ignores the attribute where it doesn't. Nothing here listens for clicks on the backdrop to fake it.
Style it through its parts, and the backdrop through ::part(dialog)::backdrop.
API
<slab-dialog>
| 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, whether or not modal is set. The rest of the page stays interactive. |
showModal(): void
|
Opens it as a modal, as a native dialog's showModal() does, whether or not modal is set. |
| 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-dialog> 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-dialog> 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.
Dialog.jsx
import 'slab-elements/dialog.js';
import './dialog.css';
export function Dialog(props) {
return <slab-dialog {...props} />;
}
dialog.css
/*
A closed dialog isn't displayed, so the animation runs again each time it
opens.
*/
slab-dialog::part(dialog) {
animation: slab-dialog-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-dialog::part(dialog)::backdrop {
animation: slab-dialog-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-dialog::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-dialog-rise {
from {
opacity: 0;
translate: 0 1rem;
}
}
@keyframes slab-dialog-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-dialog::part(dialog),
slab-dialog::part(dialog)::backdrop {
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.
dialog.ts
import { customElement } from 'lit/decorators.js';
import Dialog from 'slab-elements/dialog.js';
import styles from './dialog.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-dialog': StyledDialog;
}
}
/**
* Dialog, styled: a paper slab under an ink header with an ember base, casting
* an ember shadow over a dimmed page.
*/
@customElement('slab-styled-dialog')
export default class StyledDialog extends Dialog {
static override styles = [tokens, 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;
}
}
`;
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-dialog> 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.
Dialog.vue
<script setup>
import 'slab-elements/dialog.js';
import './dialog.css';
</script>
<template>
<slab-dialog><slot /></slab-dialog>
</template>
dialog.css
/*
A closed dialog isn't displayed, so the animation runs again each time it
opens.
*/
slab-dialog::part(dialog) {
animation: slab-dialog-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-dialog::part(dialog)::backdrop {
animation: slab-dialog-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-dialog::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-dialog-rise {
from {
opacity: 0;
translate: 0 1rem;
}
}
@keyframes slab-dialog-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-dialog::part(dialog),
slab-dialog::part(dialog)::backdrop {
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-dialog> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Import dialog.js once, such
as in your root layout, and use
<slab-dialog> as is. A Svelte component
around it would take slot="…" content as its
own slot and drop it.
dialog.js
import 'slab-elements/dialog.js';
import './dialog.css';
dialog.css
/*
A closed dialog isn't displayed, so the animation runs again each time it
opens.
*/
slab-dialog::part(dialog) {
animation: slab-dialog-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-dialog::part(dialog)::backdrop {
animation: slab-dialog-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-dialog::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-dialog-rise {
from {
opacity: 0;
translate: 0 1rem;
}
}
@keyframes slab-dialog-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-dialog::part(dialog),
slab-dialog::part(dialog)::backdrop {
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-dialog> 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.
Dialog.gjs
import 'slab-elements/dialog.js';
import './dialog.css';
export const Dialog = <template>
<slab-dialog ...attributes>{{yield}}</slab-dialog>
</template>;
dialog.css
/*
A closed dialog isn't displayed, so the animation runs again each time it
opens.
*/
slab-dialog::part(dialog) {
animation: slab-dialog-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-dialog::part(dialog)::backdrop {
animation: slab-dialog-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-dialog::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-dialog-rise {
from {
opacity: 0;
translate: 0 1rem;
}
}
@keyframes slab-dialog-fade {
from {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
slab-dialog::part(dialog),
slab-dialog::part(dialog)::backdrop {
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;
}
}