Navigation
Menu
A button that opens a list of actions. It's the APG's menu button, keyboard and all.
- Element
<slab-menu>- Built on
[role=menu] + [popover]
Example
Here's a styled version, <slab-styled-menu>. 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.
<slab-styled-menu label="Job actions">
<button class="styled-button" slot="anchor" type="button">Actions</button>
<slab-styled-menu-item>Schedule a pour</slab-styled-menu-item>
<slab-styled-menu-item>Duplicate job</slab-styled-menu-item>
<slab-styled-menu label="Export report">
<slab-styled-menu-item slot="anchor">Export report</slab-styled-menu-item>
<slab-styled-menu-item>PDF</slab-styled-menu-item>
<slab-styled-menu-item>Spreadsheet</slab-styled-menu-item>
</slab-styled-menu>
<slab-styled-menu-item disabled>Call the inspector</slab-styled-menu-item>
<slab-styled-menu-group label="Crew">
<slab-styled-menu-item type="radio" checked>Day shift</slab-styled-menu-item>
<slab-styled-menu-item type="radio">Night shift</slab-styled-menu-item>
</slab-styled-menu-group>
</slab-styled-menu>
<slab-menu label="Job actions">
<button class="styled-button" slot="anchor" type="button">Actions</button>
<slab-menu-item>Schedule a pour</slab-menu-item>
<slab-menu-item>Duplicate job</slab-menu-item>
<slab-menu label="Export report">
<slab-menu-item slot="anchor">Export report</slab-menu-item>
<slab-menu-item>PDF</slab-menu-item>
<slab-menu-item>Spreadsheet</slab-menu-item>
</slab-menu>
<slab-menu-item disabled>Call the inspector</slab-menu-item>
<slab-menu-group label="Crew">
<slab-menu-item type="radio" checked>Day shift</slab-menu-item>
<slab-menu-item type="radio">Night shift</slab-menu-item>
</slab-menu-group>
</slab-menu>
Usage
pnpm add slab-elements lit
import 'slab-elements/menu.js';
import 'slab-elements/menu.item.js';
import 'slab-elements/menu.group.js';
export function MyMenu() {
return (
<slab-menu label="File">
<button slot="anchor">File</button>
<slab-menu-item>New</slab-menu-item>
<slab-menu-item>Open…</slab-menu-item>
<slab-menu-item disabled>Save</slab-menu-item>
</slab-menu>
);
}
MyMenu.jsx React 19 or later, which sets props on custom elements as properties.
import 'slab-elements/menu.js';
import 'slab-elements/menu.item.js';
import 'slab-elements/menu.group.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-menu')
export class MyMenu extends LitElement {
render() {
return html`
<slab-menu label="File">
<button slot="anchor">File</button>
<slab-menu-item>New</slab-menu-item>
<slab-menu-item>Open…</slab-menu-item>
<slab-menu-item disabled>Save</slab-menu-item>
</slab-menu>
`;
}
}
my-menu.ts Slab is built on Lit, so its elements are at home in Lit's templates.
<script setup>
import 'slab-elements/menu.js';
import 'slab-elements/menu.item.js';
import 'slab-elements/menu.group.js';
</script>
<template>
<slab-menu label="File">
<button slot="anchor">File</button>
<slab-menu-item>New</slab-menu-item>
<slab-menu-item>Open…</slab-menu-item>
<slab-menu-item disabled>Save</slab-menu-item>
</slab-menu>
</template>
MyMenu.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.
<script>
import 'slab-elements/menu.js';
import 'slab-elements/menu.item.js';
import 'slab-elements/menu.group.js';
</script>
<slab-menu label="File">
<button slot="anchor">File</button>
<slab-menu-item>New</slab-menu-item>
<slab-menu-item>Open…</slab-menu-item>
<slab-menu-item disabled>Save</slab-menu-item>
</slab-menu>
MyMenu.svelte Svelte binds properties and events to custom elements like any other element.
import 'slab-elements/menu.js';
import 'slab-elements/menu.item.js';
import 'slab-elements/menu.group.js';
export const MyMenu = <template>
<slab-menu label="File">
<button slot="anchor">File</button>
<slab-menu-item>New</slab-menu-item>
<slab-menu-item>Open…</slab-menu-item>
<slab-menu-item disabled>Save</slab-menu-item>
</slab-menu>
</template>;
MyMenu.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/menu.js';
import 'slab-elements/menu.item.js';
import 'slab-elements/menu.group.js';
</script>
<slab-menu label="File">
<button slot="anchor">File</button>
<slab-menu-item>New</slab-menu-item>
<slab-menu-item>Open…</slab-menu-item>
<slab-menu-item disabled>Save</slab-menu-item>
</slab-menu>
index.html Any bundler (or an import map) takes care of the imports.
To do something when a Menu Item is chosen, listen for its “click”.
A Menu Item with type="checkbox" or type="radio" is a menuitemcheckbox or menuitemradio, and a click checks it before your “click” listener hears it. Put each set of radios in a slab-menu-group with a label, so they’re named together and a click unchecks only the radios in that group.
<slab-menu label="View">
<button slot="anchor">View</button>
<slab-menu-item type="checkbox" checked>Show grid</slab-menu-item>
<slab-menu-group label="Units">
<slab-menu-item type="radio" checked>Metric</slab-menu-item>
<slab-menu-item type="radio">Imperial</slab-menu-item>
</slab-menu-group>
</slab-menu>
Submenus
Put a slab-menu inside another, with a slab-menu-item in its anchor slot, to make a submenu. Its Menu Item is one of its parent’s, and opens it beside the parent. Give the submenu a label, usually the same as its Menu Item’s.
<slab-menu label="File">
<button slot="anchor">File</button>
<slab-menu-item>New</slab-menu-item>
<slab-menu label="Share">
<slab-menu-item slot="anchor">Share</slab-menu-item>
<slab-menu-item>Email</slab-menu-item>
<slab-menu-item>Message</slab-menu-item>
</slab-menu>
</slab-menu>
Resting the pointer on a submenu’s Menu Item for a moment opens it, and moving on to another of its parent’s Menu Items closes it after the same moment, so the pointer can cut across on its way in. A click on a Menu Item in a submenu closes every Menu it’s in.
Keyboard
| Key | What it does |
|---|---|
| Enter, Space, Down | On the anchor, opens the menu at the first Menu Item. |
| Up | On the anchor, opens the menu at the last Menu Item. |
| Up, Down | Moves to the previous or next Menu Item, wrapping and skipping disabled ones. |
| Home, End | Moves to the first or last Menu Item. |
| A character | Moves to the next Menu Item whose label starts with what’s typed. |
| Enter, Space | Clicks the active Menu Item and closes the menu, or opens its submenu. |
| Escape | Closes the menu and returns focus to the anchor. |
| Tab | Closes the menu and moves focus on. |
| Right | On a Menu Item with a submenu, opens it at its first Menu Item. |
| Left | In a submenu, closes it and returns focus to its Menu Item. |
| Escape | In a submenu, closes only the submenu and returns focus to its Menu Item. |
Behavior
<slab-menu>
A menu with no visual styles: a button that opens a list of Menu Items, to act on one.
It's the APG's menu button. There's no native menu, so the Menu Items are in a [role=menu] element in a native [popover="auto"], opened from whatever is slotted into anchor. A click on the anchor, or Enter or Space, opens it at the first Menu Item, and the Up arrow opens it at the last. The anchor gets aria-haspopup and aria-expanded, which a Slab anchor, such as a Copy Button, passes on to its own button.
Focus moves to the menu while it's open, and its aria-activedescendant points at the active Menu Item. The Up and Down arrows move through the Menu Items, wrapping and skipping disabled ones, Home and End go to the first and last, and typing moves to the next one whose label starts with what was typed. Hovering a Menu Item makes it the active one, as in the APG's examples. Enter, Space and a click click the Menu Item and close the menu. Escape and Tab close it, Escape returning focus to the anchor.
A Menu inside a Menu, with a Menu Item as its anchor, is a submenu. It's one of its parent's Menu Items, so the arrow keys and typeahead land on it, and it opens beside it rather than below. Enter, Space, a click and the Right arrow open it at its first Menu Item, and so does resting the pointer on its Menu Item for a moment. Left and Escape close it and return focus to its parent, and the pointer moving off to another of its parent's Menu Items closes it after the same moment. Clicking a Menu Item in a submenu closes every Menu it's in, as does Tab. In a right-to-left page, Left and Right swap.
The popover is placed beside the anchor with CSS anchor positioning, the way Popover's is. placement picks the side and nothing else.
Style it through its parts and :state(open), and each Menu Item through the states Menu Item sets.
<slab-menu-item>
A Menu Item with no visual styles. The host is the menu item, so style it directly.
Listen for "click" on it to act on it. Its Menu clicks it for Enter and Space, the way a button is clicked from the keyboard, and closes once it has. A disabled Menu Item isn't clicked, again like a button.
type="checkbox" makes it a menuitemcheckbox, which a click checks or unchecks. type="radio" makes it a menuitemradio, which a click checks, unchecking the other radio Menu Items beside it. Put each set of radios in a Menu Group, so they're named together and kept apart from the rest. Either way, it's checked or unchecked before any "click" listener hears the click, as a native checkbox is, so a listener reads the new checked.
It's never focused. Focus stays on its Menu, which points at the active Menu Item with aria-activedescendant.
Its Menu sets :state(active). It sets :state(checked) and :state(disabled) itself. Nothing is drawn for a check: that's a mark a design makes.
<slab-menu-group>
A Menu Group with no visual styles: a named [role=group] of Menu Items, such as a set of radio Menu Items, inside a Menu or a Context Menu.
The host is the group, so style it directly. Its Menu Items are still its Menu's, so the arrow keys and typeahead move through them as through the rest, and a radio Menu Item unchecks only the other radios in its group.
Its label is rendered, for sighted users, and hidden from screenreaders, which hear it as the group's name from aria-label instead. hide-label hides it visually too.
API
<slab-menu>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
Names the menu. | |
open
|
boolean |
false |
Whether it's open. Set it to open or close it. |
placement
|
'bottom' | 'left' | 'right' | 'top' |
'bottom' |
The side of its anchor it opens on, when there's room. It flips when there isn't. A submenu ignores it and opens beside its Menu Item. |
| Name | Type | Description |
|---|---|---|
items
Read-only
|
MenuItem[] |
Its Menu Items, in order. |
| Signature | Description |
|---|---|
hide(): void
|
Closes it. |
show(): void
|
Opens it. |
toggle(force?: boolean): boolean
|
Opens it when it's closed, and closes it when it's open, as a native popover's togglePopover() does. true only opens it and false only closes it. Returns whether it's open. |
| Name | Type | Description |
|---|---|---|
toggle
|
ToggleEvent |
When it opens or closes, as a ToggleEvent with oldState and newState that doesn't bubble, as a popover's does. |
beforetoggle
|
ToggleEvent |
Before it opens or closes, as a ToggleEvent that doesn't bubble. Cancel it while it's opening to keep it closed. |
| Name | Type | Description |
|---|---|---|
anchor
|
Element |
What opens the menu, usually a \<button>\, or a Menu Item for a submenu |
(default)
|
MenuItem | MenuGroup | Menu |
| Name | Description |
|---|---|
menu
|
The Menu Items |
popover
|
What the menu opens in |
| Name | Description |
|---|---|
:state(open)
|
While it's open. |
:state(submenu)
|
While it's a submenu, opened from an item of another Menu. |
<slab-menu-item>
| Name | Type | Default | Description |
|---|---|---|---|
label
|
string |
Names the Menu Item in place of its text, and is what typeahead matches against. | |
checked
|
boolean |
false |
Whether a checkbox or radio Menu Item is checked. Ignored for any other. |
disabled
|
boolean |
false |
Disables it, as disabled does a native control: the user can't focus or use it, and a form control's value isn't submitted. |
type
|
'checkbox' | 'item' | 'radio' |
'item' |
"checkbox" for an item that's checked and unchecked, "radio" for one that's checked within its Menu Group, or "item". |
| Name | Type | Description |
|---|---|---|
text
Read-only
|
string |
What the user reads as the Menu Item's name: its label, or its text when it has no label. |
| Name | Type | Description |
|---|---|---|
(default)
|
Element | string |
The label, plus anything else a design puts in a Menu Item |
| Name | Description |
|---|---|
:state(active)
|
While it's the active one, which the keyboard moves through while focus stays on its container. |
:state(checked)
|
While it's checked. |
:state(disabled)
|
While it's disabled. |
<slab-menu-group>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
Names it. It's the accessible name, and the visible label where there is one. | |
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. |
| Name | Type | Description |
|---|---|---|
(default)
|
MenuItem |
| Name | Description |
|---|---|
label
|
The label |
Copy and own it
<slab-styled-menu> 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-menu> 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.
Menu.jsx
import 'slab-elements/menu.js';
import './menu.css';
import 'slab-elements/menu.item.js';
import './menu.item.css';
import 'slab-elements/menu.group.js';
import './menu.group.css';
export function Menu(props) {
return <slab-menu {...props} />;
}
export function MenuItem(props) {
return <slab-menu-item {...props} />;
}
export function MenuGroup(props) {
return <slab-menu-group {...props} />;
}
menu.css
/* A closed popover isn't displayed, so this runs each time it opens. */
slab-menu::part(popover) {
animation: slab-menu-drop 150ms ease-out;
background-color: var(--paper);
border: var(--border);
box-shadow: var(--shadow);
color: var(--ink);
font-family: var(--font-body);
margin: 0.5rem 0;
min-inline-size: 12rem;
padding: 0.25rem 0;
}
/*
A submenu opens beside its Menu Item, overlapping its Menu's edge a little,
with its first Menu Item level with the one that opened it.
*/
slab-menu:state(submenu)::part(popover) {
margin: calc(-0.25rem - 2px) -0.25rem;
}
/*
The menu is focused for as long as it's open, and the active Menu Item is
what shows where the keyboard is, so the menu itself has no outline.
*/
slab-menu::part(menu):focus {
outline: none;
}
@keyframes slab-menu-drop {
from {
opacity: 0;
translate: 0 -0.375rem;
}
}
@media (prefers-reduced-motion: reduce) {
slab-menu::part(popover) {
animation: none;
}
}
menu.item.css
slab-menu-item {
border-inline-start: 4px solid transparent;
cursor: pointer;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
slab-menu-item:state(active) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
/*
Checkbox and radio Menu Items keep room for their mark whether or not
they're checked, so their labels line up. The mark is decoration: the
state is read from aria-checked.
*/
slab-menu-item[type='checkbox'],
slab-menu-item[type='radio'] {
padding-inline-start: 2rem;
position: relative;
}
slab-menu-item:state(checked)::before {
content: '✓' / '';
inset-inline-start: 0.75rem;
position: absolute;
}
slab-menu-item[type='radio']:state(checked)::before {
content: '●' / '';
}
/*
A Menu Item that opens a submenu points the way it opens. The arrow is
decoration: that it opens one is read from aria-haspopup. While its
submenu is open, it stays marked, even once the pointer has moved on.
*/
slab-menu-item[aria-haspopup] {
padding-inline-end: 2rem;
position: relative;
}
slab-menu-item[aria-haspopup]::after {
content: '›' / '';
inset-inline-end: 0.75rem;
position: absolute;
}
slab-menu-item[aria-haspopup]:dir(rtl)::after {
content: '‹' / '';
}
slab-menu-item[aria-expanded='true'] {
border-inline-start-color: var(--ember);
}
slab-menu-item:state(disabled) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
slab-menu-item:state(disabled):state(active) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
slab-menu-item {
transition: none;
}
}
menu.group.css
slab-menu-group {
border-block-start: var(--border);
}
slab-menu-group::part(label) {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
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.
menu.ts
import { customElement } from 'lit/decorators.js';
import Menu from 'slab-elements/menu.js';
import styles from './menu.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-menu': StyledMenu;
}
}
/**
* Menu, styled: its Menu Items in a paper panel with an ink border and a hard
* shadow.
*/
@customElement('slab-styled-menu')
export default class StyledMenu extends Menu {
static override styles = [tokens, styles];
}
menu.styles.ts
import { css } from 'lit';
export default css`
/* A closed popover isn't displayed, so this runs each time it opens. */
[part~='popover'] {
animation: drop 150ms ease-out;
background-color: var(--paper);
border: var(--border);
box-shadow: var(--shadow);
color: var(--ink);
font-family: var(--font-body);
margin: 0.5rem 0;
min-inline-size: 12rem;
padding: 0.25rem 0;
}
/*
A submenu opens beside its Menu Item, overlapping its Menu's edge a little,
with its first Menu Item level with the one that opened it.
*/
:host(:state(submenu)) [part~='popover'] {
margin: calc(-0.25rem - 2px) -0.25rem;
}
/*
The menu is focused for as long as it's open, and the active Menu Item is
what shows where the keyboard is, so the menu itself has no outline.
*/
[part~='menu']:focus {
outline: none;
}
@keyframes drop {
from {
opacity: 0;
translate: 0 -0.375rem;
}
}
@media (prefers-reduced-motion: reduce) {
[part~='popover'] {
animation: none;
}
}
`;
menu.item.ts
import { customElement } from 'lit/decorators.js';
import MenuItem from 'slab-elements/menu.item.js';
import styles from './menu.item.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-menu-item': StyledMenuItem;
}
}
/**
* Menu Item, styled: a row that inverts to paper on ink while it's active,
* with an ember bar on its edge, as Option does. Hovering makes it active.
*/
@customElement('slab-styled-menu-item')
export default class StyledMenuItem extends MenuItem {
static override styles = [tokens, styles];
}
menu.item.styles.ts
import { css } from 'lit';
export default css`
:host {
border-inline-start: 4px solid transparent;
cursor: pointer;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
:host(:state(active)) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
/*
Checkbox and radio Menu Items keep room for their mark whether or not
they're checked, so their labels line up. The mark is decoration: the
state is read from aria-checked.
*/
:host([type='checkbox']),
:host([type='radio']) {
padding-inline-start: 2rem;
position: relative;
}
:host(:state(checked))::before {
content: '✓' / '';
inset-inline-start: 0.75rem;
position: absolute;
}
:host([type='radio']:state(checked))::before {
content: '●' / '';
}
/*
A Menu Item that opens a submenu points the way it opens. The arrow is
decoration: that it opens one is read from aria-haspopup. While its
submenu is open, it stays marked, even once the pointer has moved on.
*/
:host([aria-haspopup]) {
padding-inline-end: 2rem;
position: relative;
}
:host([aria-haspopup])::after {
content: '›' / '';
inset-inline-end: 0.75rem;
position: absolute;
}
:host([aria-haspopup]:dir(rtl))::after {
content: '‹' / '';
}
:host([aria-expanded='true']) {
border-inline-start-color: var(--ember);
}
:host(:state(disabled)) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
:host(:state(disabled):state(active)) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
:host {
transition: none;
}
}
`;
menu.group.ts
import { customElement } from 'lit/decorators.js';
import MenuGroup from 'slab-elements/menu.group.js';
import styles from './menu.group.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-menu-group': StyledMenuGroup;
}
}
/**
* Menu Group, styled: a small ash label over its Menu Items, ruled off from
* the rows before it.
*/
@customElement('slab-styled-menu-group')
export default class StyledMenuGroup extends MenuGroup {
static override styles = [tokens, styles];
}
menu.group.styles.ts
import { css } from 'lit';
export default css`
:host {
border-block-start: var(--border);
}
[part~='label'] {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
`;
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-menu> 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.
Menu.vue
<script setup>
import 'slab-elements/menu.js';
import './menu.css';
</script>
<template>
<slab-menu><slot /></slab-menu>
</template>
MenuItem.vue
<script setup>
import 'slab-elements/menu.item.js';
import './menu.item.css';
</script>
<template>
<slab-menu-item><slot /></slab-menu-item>
</template>
MenuGroup.vue
<script setup>
import 'slab-elements/menu.group.js';
import './menu.group.css';
</script>
<template>
<slab-menu-group><slot /></slab-menu-group>
</template>
menu.css
/* A closed popover isn't displayed, so this runs each time it opens. */
slab-menu::part(popover) {
animation: slab-menu-drop 150ms ease-out;
background-color: var(--paper);
border: var(--border);
box-shadow: var(--shadow);
color: var(--ink);
font-family: var(--font-body);
margin: 0.5rem 0;
min-inline-size: 12rem;
padding: 0.25rem 0;
}
/*
A submenu opens beside its Menu Item, overlapping its Menu's edge a little,
with its first Menu Item level with the one that opened it.
*/
slab-menu:state(submenu)::part(popover) {
margin: calc(-0.25rem - 2px) -0.25rem;
}
/*
The menu is focused for as long as it's open, and the active Menu Item is
what shows where the keyboard is, so the menu itself has no outline.
*/
slab-menu::part(menu):focus {
outline: none;
}
@keyframes slab-menu-drop {
from {
opacity: 0;
translate: 0 -0.375rem;
}
}
@media (prefers-reduced-motion: reduce) {
slab-menu::part(popover) {
animation: none;
}
}
menu.item.css
slab-menu-item {
border-inline-start: 4px solid transparent;
cursor: pointer;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
slab-menu-item:state(active) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
/*
Checkbox and radio Menu Items keep room for their mark whether or not
they're checked, so their labels line up. The mark is decoration: the
state is read from aria-checked.
*/
slab-menu-item[type='checkbox'],
slab-menu-item[type='radio'] {
padding-inline-start: 2rem;
position: relative;
}
slab-menu-item:state(checked)::before {
content: '✓' / '';
inset-inline-start: 0.75rem;
position: absolute;
}
slab-menu-item[type='radio']:state(checked)::before {
content: '●' / '';
}
/*
A Menu Item that opens a submenu points the way it opens. The arrow is
decoration: that it opens one is read from aria-haspopup. While its
submenu is open, it stays marked, even once the pointer has moved on.
*/
slab-menu-item[aria-haspopup] {
padding-inline-end: 2rem;
position: relative;
}
slab-menu-item[aria-haspopup]::after {
content: '›' / '';
inset-inline-end: 0.75rem;
position: absolute;
}
slab-menu-item[aria-haspopup]:dir(rtl)::after {
content: '‹' / '';
}
slab-menu-item[aria-expanded='true'] {
border-inline-start-color: var(--ember);
}
slab-menu-item:state(disabled) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
slab-menu-item:state(disabled):state(active) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
slab-menu-item {
transition: none;
}
}
menu.group.css
slab-menu-group {
border-block-start: var(--border);
}
slab-menu-group::part(label) {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
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-menu> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Import menu.js once, such
as in your root layout, and use
<slab-menu> as is. A Svelte component
around it would take slot="…" content as its
own slot and drop it.
menu.js
import 'slab-elements/menu.js';
import './menu.css';
import 'slab-elements/menu.item.js';
import './menu.item.css';
import 'slab-elements/menu.group.js';
import './menu.group.css';
menu.css
/* A closed popover isn't displayed, so this runs each time it opens. */
slab-menu::part(popover) {
animation: slab-menu-drop 150ms ease-out;
background-color: var(--paper);
border: var(--border);
box-shadow: var(--shadow);
color: var(--ink);
font-family: var(--font-body);
margin: 0.5rem 0;
min-inline-size: 12rem;
padding: 0.25rem 0;
}
/*
A submenu opens beside its Menu Item, overlapping its Menu's edge a little,
with its first Menu Item level with the one that opened it.
*/
slab-menu:state(submenu)::part(popover) {
margin: calc(-0.25rem - 2px) -0.25rem;
}
/*
The menu is focused for as long as it's open, and the active Menu Item is
what shows where the keyboard is, so the menu itself has no outline.
*/
slab-menu::part(menu):focus {
outline: none;
}
@keyframes slab-menu-drop {
from {
opacity: 0;
translate: 0 -0.375rem;
}
}
@media (prefers-reduced-motion: reduce) {
slab-menu::part(popover) {
animation: none;
}
}
menu.item.css
slab-menu-item {
border-inline-start: 4px solid transparent;
cursor: pointer;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
slab-menu-item:state(active) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
/*
Checkbox and radio Menu Items keep room for their mark whether or not
they're checked, so their labels line up. The mark is decoration: the
state is read from aria-checked.
*/
slab-menu-item[type='checkbox'],
slab-menu-item[type='radio'] {
padding-inline-start: 2rem;
position: relative;
}
slab-menu-item:state(checked)::before {
content: '✓' / '';
inset-inline-start: 0.75rem;
position: absolute;
}
slab-menu-item[type='radio']:state(checked)::before {
content: '●' / '';
}
/*
A Menu Item that opens a submenu points the way it opens. The arrow is
decoration: that it opens one is read from aria-haspopup. While its
submenu is open, it stays marked, even once the pointer has moved on.
*/
slab-menu-item[aria-haspopup] {
padding-inline-end: 2rem;
position: relative;
}
slab-menu-item[aria-haspopup]::after {
content: '›' / '';
inset-inline-end: 0.75rem;
position: absolute;
}
slab-menu-item[aria-haspopup]:dir(rtl)::after {
content: '‹' / '';
}
slab-menu-item[aria-expanded='true'] {
border-inline-start-color: var(--ember);
}
slab-menu-item:state(disabled) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
slab-menu-item:state(disabled):state(active) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
slab-menu-item {
transition: none;
}
}
menu.group.css
slab-menu-group {
border-block-start: var(--border);
}
slab-menu-group::part(label) {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
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-menu> 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.
Menu.gjs
import 'slab-elements/menu.js';
import './menu.css';
export const Menu = <template>
<slab-menu ...attributes>{{yield}}</slab-menu>
</template>;
MenuItem.gjs
import 'slab-elements/menu.item.js';
import './menu.item.css';
export const MenuItem = <template>
<slab-menu-item ...attributes>{{yield}}</slab-menu-item>
</template>;
MenuGroup.gjs
import 'slab-elements/menu.group.js';
import './menu.group.css';
export const MenuGroup = <template>
<slab-menu-group ...attributes>{{yield}}</slab-menu-group>
</template>;
menu.css
/* A closed popover isn't displayed, so this runs each time it opens. */
slab-menu::part(popover) {
animation: slab-menu-drop 150ms ease-out;
background-color: var(--paper);
border: var(--border);
box-shadow: var(--shadow);
color: var(--ink);
font-family: var(--font-body);
margin: 0.5rem 0;
min-inline-size: 12rem;
padding: 0.25rem 0;
}
/*
A submenu opens beside its Menu Item, overlapping its Menu's edge a little,
with its first Menu Item level with the one that opened it.
*/
slab-menu:state(submenu)::part(popover) {
margin: calc(-0.25rem - 2px) -0.25rem;
}
/*
The menu is focused for as long as it's open, and the active Menu Item is
what shows where the keyboard is, so the menu itself has no outline.
*/
slab-menu::part(menu):focus {
outline: none;
}
@keyframes slab-menu-drop {
from {
opacity: 0;
translate: 0 -0.375rem;
}
}
@media (prefers-reduced-motion: reduce) {
slab-menu::part(popover) {
animation: none;
}
}
menu.item.css
slab-menu-item {
border-inline-start: 4px solid transparent;
cursor: pointer;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
slab-menu-item:state(active) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
/*
Checkbox and radio Menu Items keep room for their mark whether or not
they're checked, so their labels line up. The mark is decoration: the
state is read from aria-checked.
*/
slab-menu-item[type='checkbox'],
slab-menu-item[type='radio'] {
padding-inline-start: 2rem;
position: relative;
}
slab-menu-item:state(checked)::before {
content: '✓' / '';
inset-inline-start: 0.75rem;
position: absolute;
}
slab-menu-item[type='radio']:state(checked)::before {
content: '●' / '';
}
/*
A Menu Item that opens a submenu points the way it opens. The arrow is
decoration: that it opens one is read from aria-haspopup. While its
submenu is open, it stays marked, even once the pointer has moved on.
*/
slab-menu-item[aria-haspopup] {
padding-inline-end: 2rem;
position: relative;
}
slab-menu-item[aria-haspopup]::after {
content: '›' / '';
inset-inline-end: 0.75rem;
position: absolute;
}
slab-menu-item[aria-haspopup]:dir(rtl)::after {
content: '‹' / '';
}
slab-menu-item[aria-expanded='true'] {
border-inline-start-color: var(--ember);
}
slab-menu-item:state(disabled) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
slab-menu-item:state(disabled):state(active) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
slab-menu-item {
transition: none;
}
}
menu.group.css
slab-menu-group {
border-block-start: var(--border);
}
slab-menu-group::part(label) {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
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;
}
}