Slab

Navigation

Menubar

A row of Menus, such as File, Edit and View, with the arrow keys to move between them. It's the APG's menubar.

Element
<slab-menubar>
Built on
[role=menubar]
As Markdown
menubar.md

Example

Here's a styled version, <slab-styled-menubar>. 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.

Styled example As it ships
New job Duplicate job Archive job Schedule a pour Order a mix Export report Print report
HTML
<slab-styled-menubar label="Job">
  <slab-styled-menu label="Job">
    <button class="styled-menubar-button" slot="anchor" type="button">Job</button>

    <slab-styled-menu-item>New job</slab-styled-menu-item>
    <slab-styled-menu-item>Duplicate job</slab-styled-menu-item>
    <slab-styled-menu-item disabled>Archive job</slab-styled-menu-item>
  </slab-styled-menu>

  <slab-styled-menu label="Pour">
    <button class="styled-menubar-button" slot="anchor" type="button">Pour</button>

    <slab-styled-menu-item>Schedule a pour</slab-styled-menu-item>
    <slab-styled-menu-item>Order a mix</slab-styled-menu-item>
  </slab-styled-menu>

  <slab-styled-menu label="Report">
    <button class="styled-menubar-button" slot="anchor" type="button">Report</button>

    <slab-styled-menu-item>Export report</slab-styled-menu-item>
    <slab-styled-menu-item>Print report</slab-styled-menu-item>
  </slab-styled-menu>
</slab-styled-menubar>
New job Duplicate job Archive job Schedule a pour Order a mix Export report Print report
HTML
<slab-menubar label="Job">
  <slab-menu label="Job">
    <button class="styled-menubar-button" slot="anchor" type="button">Job</button>

    <slab-menu-item>New job</slab-menu-item>
    <slab-menu-item>Duplicate job</slab-menu-item>
    <slab-menu-item disabled>Archive job</slab-menu-item>
  </slab-menu>

  <slab-menu label="Pour">
    <button class="styled-menubar-button" slot="anchor" type="button">Pour</button>

    <slab-menu-item>Schedule a pour</slab-menu-item>
    <slab-menu-item>Order a mix</slab-menu-item>
  </slab-menu>

  <slab-menu label="Report">
    <button class="styled-menubar-button" slot="anchor" type="button">Report</button>

    <slab-menu-item>Export report</slab-menu-item>
    <slab-menu-item>Print report</slab-menu-item>
  </slab-menu>
</slab-menubar>

Usage

Install
pnpm add slab-elements lit
HTML
<script type="module">
  import 'slab-elements/menubar.js';
</script>

<slab-menubar label="Editor">
  <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>

  <slab-menu label="Edit">
    <button slot="anchor">Edit</button>

    <slab-menu-item>Undo</slab-menu-item>
    <slab-menu-item>Redo</slab-menu-item>
  </slab-menu>
</slab-menubar>

Each Menu in it is an ordinary Menu, with its own label, anchor and Menu Items. To do something when a Menu Item is chosen, listen for its “click”. Disable a Menu by disabling its anchor.

Keyboard

Key What it does
Left, Right On an anchor, moves to the previous or next anchor, wrapping and skipping disabled ones.
Home, End On an anchor, moves to the first or last anchor.
Enter, Space, Down On an anchor, opens its Menu at the first Menu Item.
Up On an anchor, opens its Menu at the last Menu Item.
Left, Right In an open Menu, closes it and opens the previous or next Menu, wrapping and skipping disabled.
Up, Down In an open Menu, moves to the previous or next Menu Item.
Escape Closes the open Menu and returns focus to its anchor.
Tab Closes the open Menu and leaves the menubar. Coming back, focus returns to the last anchor.

Behavior

A menubar with no visual styles: a row of Menus, such as File, Edit and View, each opened from its anchor, with the arrow keys to move between them.

It's the APG's menubar. Each Menu's anchor becomes a [role=menuitem] in a [role=menubar], and Tab reaches the menubar once. The rest is the Menu's own: Enter, Space and Down open it at its first Menu Item, Up at its last, and Escape closes it.

Exactly one anchor is tabbable: the one most recently focused or opened, or the first that isn't disabled. Left and Right move focus between the anchors, wrapping at each end and skipping disabled ones, and Home and End go to the first and last. While a Menu is open, Left and Right close it and open the one beside it instead, and pointing at another anchor does the same, as a desktop menubar does.

Style it through its part, each Menu through its own, and the anchors directly.

API

<slab-menubar>

Attributes
Name Type Default Description
label Required string Names the menubar.
version string
Properties
Name Type Description
menus Read-only Menu[]
Methods
Signature Description
focus(options?: FocusOptions): void
Slots
Name Type Description
(default) Menu
CSS parts
Name Description
menubar The Menus

Copy and own it

<slab-styled-menubar> isn't in the package. It's an example — copy it into your project and change whatever you want. Under the hood, it's just the headless element, subclassed, with styles set. That's it. 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.

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.

menubar.ts
import { customElement } from 'lit/decorators.js';
import Menubar from 'slab-elements/menubar.js';
import styles from './menubar.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-menubar': StyledMenubar;
  }
}

/**
 * Menubar, styled: a paper bar with an ink border and a hard shadow, holding
 * styled Menus.
 */
@customElement('slab-styled-menubar')
export default class StyledMenubar extends Menubar {
  static override styles = [tokens, styles];
}
menubar.styles.ts
import { css } from 'lit';

/*
  The anchors are in each Menu rather than slotted into the menubar, so they
  can't be reached from here. They're styled where they're written, as the
  examples' buttons are.
*/
export default css`
  [part~='menubar'] {
    background-color: var(--paper);
    border: var(--border);
    box-shadow: var(--shadow);
    display: flex;
    gap: 0.25rem;
    inline-size: fit-content;
    padding: 0.25rem;
  }
`;
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);
  }
`;