Slab
Slab

Overlays

Command Palette

A search for commands, opened as a modal with showModal(): type to filter a list of Options, then Enter runs the best match. It's a Combobox in a Dialog.

Element
<slab-command-palette>
Built on
<dialog> + <input role=combobox>

Example

Here's a styled version, <slab-styled-command-palette>. 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
Schedule a pour Order ready-mix Assign a crew Strip the forms Log a cure test Send an invoice No commands match.
HTML
<button class="styled-button" type="button" data-show-modal="command-palette-example">
  Search commands
</button>

<slab-styled-command-palette
  id="command-palette-example"
  label="Commands"
  placeholder="Type a command"
  closedby="any"
  hide-label
>
  <slab-styled-option value="schedule">Schedule a pour</slab-styled-option>
  <slab-styled-option value="order">Order ready-mix</slab-styled-option>
  <slab-styled-option value="crew">Assign a crew</slab-styled-option>
  <slab-styled-option value="forms">Strip the forms</slab-styled-option>
  <slab-styled-option value="cure" disabled>Log a cure test</slab-styled-option>
  <slab-styled-option value="invoice">Send an invoice</slab-styled-option>
  <span slot="empty">No commands match.</span>
</slab-styled-command-palette>
Schedule a pour Order ready-mix Assign a crew Strip the forms Log a cure test Send an invoice No commands match.
HTML
<button class="styled-button" type="button" data-show-modal="command-palette-example-as-it-ships">
  Search commands
</button>

<slab-command-palette
  id="command-palette-example-as-it-ships"
  label="Commands"
  placeholder="Type a command"
  closedby="any"
  hide-label
>
  <slab-option value="schedule">Schedule a pour</slab-option>
  <slab-option value="order">Order ready-mix</slab-option>
  <slab-option value="crew">Assign a crew</slab-option>
  <slab-option value="forms">Strip the forms</slab-option>
  <slab-option value="cure" disabled>Log a cure test</slab-option>
  <slab-option value="invoice">Send an invoice</slab-option>
  <span slot="empty">No commands match.</span>
</slab-command-palette>

Usage

Install
pnpm add slab-elements lit
ReactLitVueSvelteEmberHTML
import 'slab-elements/command-palette.js';

export function MyCommandPalette() {
  return (
    <slab-command-palette label="Commands" placeholder="Type a command">
      <slab-option value="new-file">New file</slab-option>
      <slab-option value="open-file">Open file</slab-option>
      <slab-option value="settings">Settings</slab-option>
      <span slot="empty">No results</span>
    </slab-command-palette>
  );
}

MyCommandPalette.jsx React 19 or later, which sets props on custom elements as properties.

import 'slab-elements/command-palette.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';

@customElement('my-command-palette')
export class MyCommandPalette extends LitElement {
  render() {
    return html`
      <slab-command-palette label="Commands" placeholder="Type a command">
        <slab-option value="new-file">New file</slab-option>
        <slab-option value="open-file">Open file</slab-option>
        <slab-option value="settings">Settings</slab-option>
        <span slot="empty">No results</span>
      </slab-command-palette>
    `;
  }
}

my-command-palette.ts Slab is built on Lit, so its elements are at home in Lit's templates.

<script setup>
import 'slab-elements/command-palette.js';
</script>

<template>
  <slab-command-palette label="Commands" placeholder="Type a command">
    <slab-option value="new-file">New file</slab-option>
    <slab-option value="open-file">Open file</slab-option>
    <slab-option value="settings">Settings</slab-option>
    <span slot="empty">No results</span>
  </slab-command-palette>
</template>

MyCommandPalette.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.

<script>
  import 'slab-elements/command-palette.js';
</script>

<slab-command-palette label="Commands" placeholder="Type a command">
  <slab-option value="new-file">New file</slab-option>
  <slab-option value="open-file">Open file</slab-option>
  <slab-option value="settings">Settings</slab-option>
  <span slot="empty">No results</span>
</slab-command-palette>

MyCommandPalette.svelte Svelte binds properties and events to custom elements like any other element.

import 'slab-elements/command-palette.js';

export const MyCommandPalette = <template>
  <slab-command-palette label="Commands" placeholder="Type a command">
    <slab-option value="new-file">New file</slab-option>
    <slab-option value="open-file">Open file</slab-option>
    <slab-option value="settings">Settings</slab-option>
    <span slot="empty">No results</span>
  </slab-command-palette>
</template>;

MyCommandPalette.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/command-palette.js';
</script>

<slab-command-palette label="Commands" placeholder="Type a command">
  <slab-option value="new-file">New file</slab-option>
  <slab-option value="open-file">Open file</slab-option>
  <slab-option value="settings">Settings</slab-option>
  <span slot="empty">No results</span>
</slab-command-palette>

index.html Any bundler (or an import map) takes care of the imports.

It’s Dialog with a Combobox’s input and listbox inside, and its Options are the same ones Combobox uses. Open it as a modal with showModal(), or non-modally by setting open or calling show(). Choosing an Option closes it with that Option’s value as returnValue, so a “close” handler knows what to run. Its Close button, Escape, or a click outside with closedby="any" closes it with an empty returnValue. The Close button is there because not every browser supports closedby (iOS Safari doesn’t), so on a phone it’s the only way out short of choosing something. Slot your own text or icon into close to change what it says.

const palette = document.querySelector('slab-command-palette');

document.addEventListener('keydown', (event) => {
  if (event.key === 'k' && (event.metaKey || event.ctrlKey)) {
    event.preventDefault();
    palette.showModal();
  }
});

palette.addEventListener('close', () => {
  if (palette.returnValue) {
    run(palette.returnValue);
  }
});

Typing filters the Options by their text. Replace filter to match on more, like keywords, or listen for “input” and read query to fetch results as the user types. What’s slotted into empty is shown, and announced, when nothing matches. Once typing pauses, screenreaders are also told how many Options are left, such as “3 results”. Set resultsLabel to a function of the count to change what they’re told.

Keyboard

Key What it does
Up, Down Move the active Option, stopping at each end and skipping disabled ones.
Enter Chooses the active Option and closes the palette. Typing makes the first match active.
Tab Moves between the input and the Close button. Focus stays in the palette.
Escape Closes the palette and dispatches “cancel”, which can be canceled to keep it open.
Home, End Move the caret, as in any input.

Behavior

A command palette with no visual styles: a Combobox's input and listbox in a Dialog, to find a command by typing its name and run it.

It's Dialog, so everything a Dialog does it does too: showModal() opening it as a modal, open and show() opening it non-modally, close(returnValue), closedby, the label, a modal's focus trap, Escape closing it, and "cancel" and "close" from the host. Focus goes back to where it was when it closes, which the <dialog> does itself. Opening it from a shortcut such as Ctrl+K is left to you, since which keys are free is the page's to know.

Inside is the APG's editable combobox with list autocomplete, the way Combobox has it, except that the listbox is always shown rather than in a popover. The input is a <input role="combobox">, and the slotted Options are in a <div role="listbox"> below it. Both are named by the label. Focus stays in the input, which points at the active Option with aria-activedescendant. Read or set it with activeOption.

Opening it empties the input, and the first Option is the active one. Typing filters the Options by filter, a case-insensitive substring of each Option's text unless it's replaced, and makes the first one left the active one, so Enter runs the best match. An Option that doesn't match is hidden. With none left, the listbox is hidden too, the host matches :state(empty), and whatever is slotted into empty is shown. Once typing pauses, screenreaders are told how many Options are left, by resultsLabel, or with none left, the text slotted into empty when there is some.

ArrowDown and ArrowUp move the active Option, stopping at each end and skipping disabled Options. Enter and a click choose the active Option: the palette closes with that Option's value as its returnValue, so a "close" handler reads returnValue to know what to run. Closing any other way leaves returnValue empty. Home, End and the other caret keys stay the input's.

A Close button follows the input, so it can be closed without a keyboard. A click on the backdrop closes it only with closedby="any", which iOS Safari doesn't support. The button says "Close" unless something is slotted into close.

The input's "input" leaves the host as it is, and query is what's been typed, so results can be fetched as the user types. Set filter to one that always returns true to show the Options you give it as they are.

Style it through its parts and :state(empty), the backdrop through ::part(dialog)::backdrop, and each Option through :state(active) and :state(disabled).

API

<slab-command-palette>

Attributes
Name Type Default Description
placeholder string Shown while it's empty, as a native input's placeholder. It's a hint, not a label.
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.
Properties
Name Type Description
filter (option: Option, query: string) => boolean Whether an Option matches what's been typed. A case-insensitive substring of its text by default.
resultsLabel (count: number) => string What screenreaders are told once typing pauses, from how many Options are left. "No results", "1 result" and so on in English when unset. With none left, whatever is slotted into empty is told instead, if anything is.
activeOption Option | null The Option the user is on, which Enter chooses. It's pointed at rather than focused, so focus stays in the input.
options Read-only Option[] Its Options, in order.
query Read-only string What's been typed into the input.
returnValue string The <dialog>'s, so close(returnValue) and a <form method="dialog"> set it the way they do natively.
Methods
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.
Events
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.
Slots
Name Type Description
close Element | string What the close button says. "Close" when nothing is slotted.
(default) Option
empty Element | string Shown when no Option matches what's been typed, such as "No results"
label Element | string Its heading, in place of label
CSS parts
Name Description
close Closes the palette with an empty returnValue
dialog The native dialog. A modal one's backdrop is its ::backdrop
empty Shows the "empty" slot when no Option matches
input The input
label The label
listbox The Options, below the input
CSS states
Name Description
:state(empty) While no command matches what the user typed, for a "no results" message of your own.
:state(open) While it's open.

Copy and own it

<slab-styled-command-palette> 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.

ReactLitVueSvelteEmber

The same styles, written from outside the element, so there's nothing to subclass. The CSS styles every <slab-command-palette> 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.

CommandPalette.jsx
import 'slab-elements/command-palette.js';
import './command-palette.css';

export function CommandPalette(props) {
  return <slab-command-palette {...props} />;
}
command-palette.css
/*
  A closed dialog isn't displayed, so the animation runs again each time it
  opens.
*/
slab-command-palette::part(dialog) {
  animation: slab-command-palette-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-command-palette::part(dialog)::backdrop {
  animation: slab-command-palette-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-command-palette::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-command-palette-rise {
  from {
    opacity: 0;
    translate: 0 1rem;
  }
}

@keyframes slab-command-palette-fade {
  from {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-command-palette::part(dialog),
  slab-command-palette::part(dialog)::backdrop {
    animation: none;
  }
}

slab-command-palette::part(dialog) {
  inline-size: min(36rem, 100% - 2rem);
  margin-block-start: 12vh;
  padding: 0;
}

/*
  On the open host only. A closed dialog isn't displayed, and "display: grid"
  would show it.
*/
slab-command-palette[open]::part(dialog) {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

slab-command-palette::part(label),
slab-command-palette::part(listbox),
slab-command-palette::part(empty) {
  grid-column: 1 / -1;
}

slab-command-palette::part(label) {
  margin: 0;
}

slab-command-palette::part(close) {
  background-color: var(--paper);
  border: 0;
  border-block-end: var(--border);
  border-inline-start: var(--border);
  border-radius: 0;
  color: var(--ink);
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0 1rem;
  transition:
    background-color 100ms,
    color 100ms;
}

slab-command-palette::part(close):hover {
  background-color: var(--chalk);
}

slab-command-palette::part(close):focus-visible {
  background-color: var(--ember-deep);
  color: var(--paper);
  outline: 2px solid transparent;
}

@media (prefers-reduced-motion: reduce) {
  slab-command-palette::part(close) {
    transition: none;
  }
}

slab-command-palette::part(input) {
  background-color: var(--paper);
  block-size: 3.5rem;
  border: 0;
  border-block-end: var(--border);
  border-radius: 0;
  box-sizing: border-box;
  color: inherit;
  display: block;
  font: inherit;
  font-size: 1.125rem;
  inline-size: 100%;
  padding: 0 1.25rem;
}

slab-command-palette::part(input)::placeholder {
  color: var(--ash);
}

/*
  The ember bar under it is the focus ring, since the input is always
  focused while the palette is open. The outline is transparent rather
  than gone, so it still shows in forced colors.
*/
slab-command-palette::part(input):focus-visible {
  border-block-end-color: var(--ember);
  outline: 2px solid transparent;
}

slab-command-palette::part(listbox) {
  max-block-size: min(20rem, 50vh);
  overflow-y: auto;
  padding: 0.25rem 0;
}

slab-command-palette:state(empty)::part(empty) {
  color: var(--ash);
  padding: 1rem 1.25rem;
}
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.

command-palette.ts
import { customElement } from 'lit/decorators.js';
import CommandPalette from 'slab-elements/command-palette.js';
import dialog from './dialog.styles.js';
import styles from './command-palette.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-command-palette': StyledCommandPalette;
  }
}

/**
 * Command Palette, styled: Dialog's slab, high on the page, with Text Field's
 * input across its top and the Options in a scrolling list below it.
 */
@customElement('slab-styled-command-palette')
export default class StyledCommandPalette extends CommandPalette {
  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;
    }
  }
`;
command-palette.styles.ts
import { css } from 'lit';

/*
  Dialog's styles, then these. It sits in the upper part of the page rather
  than its middle, so the list grows down without the input moving, and the
  input and list run to the slab's edges. The Close button sits beside the
  input, in a row of its own, and everything else spans the row.
*/
export default css`
  [part~='dialog'] {
    inline-size: min(36rem, 100% - 2rem);
    margin-block-start: 12vh;
    padding: 0;
  }

  /*
    On the open host only. A closed dialog isn't displayed, and "display: grid"
    would show it.
  */
  :host([open]) [part~='dialog'] {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
  }

  [part~='label'],
  [part~='listbox'],
  [part~='empty'] {
    grid-column: 1 / -1;
  }

  [part~='label'] {
    margin: 0;
  }

  [part~='close'] {
    background-color: var(--paper);
    border: 0;
    border-block-end: var(--border);
    border-inline-start: var(--border);
    border-radius: 0;
    color: var(--ink);
    cursor: pointer;
    font-family: var(--font-body);
    font-size: 0.875rem;
    font-weight: 600;
    padding: 0 1rem;
    transition:
      background-color 100ms,
      color 100ms;

    &:hover {
      background-color: var(--chalk);
    }

    &:focus-visible {
      background-color: var(--ember-deep);
      color: var(--paper);
      outline: 2px solid transparent;
    }

    @media (prefers-reduced-motion: reduce) {
      transition: none;
    }
  }

  [part~='input'] {
    background-color: var(--paper);
    block-size: 3.5rem;
    border: 0;
    border-block-end: var(--border);
    border-radius: 0;
    box-sizing: border-box;
    color: inherit;
    display: block;
    font: inherit;
    font-size: 1.125rem;
    inline-size: 100%;
    padding: 0 1.25rem;

    &::placeholder {
      color: var(--ash);
    }

    /*
      The ember bar under it is the focus ring, since the input is always
      focused while the palette is open. The outline is transparent rather
      than gone, so it still shows in forced colors.
    */
    &:focus-visible {
      border-block-end-color: var(--ember);
      outline: 2px solid transparent;
    }
  }

  [part~='listbox'] {
    max-block-size: min(20rem, 50vh);
    overflow-y: auto;
    padding: 0.25rem 0;
  }

  :host(:state(empty)) [part~='empty'] {
    color: var(--ash);
    padding: 1rem 1.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);
  }

  /*
    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-command-palette> 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.

CommandPalette.vue
<script setup>
import 'slab-elements/command-palette.js';
import './command-palette.css';
</script>

<template>
  <slab-command-palette><slot /></slab-command-palette>
</template>
command-palette.css
/*
  A closed dialog isn't displayed, so the animation runs again each time it
  opens.
*/
slab-command-palette::part(dialog) {
  animation: slab-command-palette-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-command-palette::part(dialog)::backdrop {
  animation: slab-command-palette-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-command-palette::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-command-palette-rise {
  from {
    opacity: 0;
    translate: 0 1rem;
  }
}

@keyframes slab-command-palette-fade {
  from {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-command-palette::part(dialog),
  slab-command-palette::part(dialog)::backdrop {
    animation: none;
  }
}

slab-command-palette::part(dialog) {
  inline-size: min(36rem, 100% - 2rem);
  margin-block-start: 12vh;
  padding: 0;
}

/*
  On the open host only. A closed dialog isn't displayed, and "display: grid"
  would show it.
*/
slab-command-palette[open]::part(dialog) {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

slab-command-palette::part(label),
slab-command-palette::part(listbox),
slab-command-palette::part(empty) {
  grid-column: 1 / -1;
}

slab-command-palette::part(label) {
  margin: 0;
}

slab-command-palette::part(close) {
  background-color: var(--paper);
  border: 0;
  border-block-end: var(--border);
  border-inline-start: var(--border);
  border-radius: 0;
  color: var(--ink);
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0 1rem;
  transition:
    background-color 100ms,
    color 100ms;
}

slab-command-palette::part(close):hover {
  background-color: var(--chalk);
}

slab-command-palette::part(close):focus-visible {
  background-color: var(--ember-deep);
  color: var(--paper);
  outline: 2px solid transparent;
}

@media (prefers-reduced-motion: reduce) {
  slab-command-palette::part(close) {
    transition: none;
  }
}

slab-command-palette::part(input) {
  background-color: var(--paper);
  block-size: 3.5rem;
  border: 0;
  border-block-end: var(--border);
  border-radius: 0;
  box-sizing: border-box;
  color: inherit;
  display: block;
  font: inherit;
  font-size: 1.125rem;
  inline-size: 100%;
  padding: 0 1.25rem;
}

slab-command-palette::part(input)::placeholder {
  color: var(--ash);
}

/*
  The ember bar under it is the focus ring, since the input is always
  focused while the palette is open. The outline is transparent rather
  than gone, so it still shows in forced colors.
*/
slab-command-palette::part(input):focus-visible {
  border-block-end-color: var(--ember);
  outline: 2px solid transparent;
}

slab-command-palette::part(listbox) {
  max-block-size: min(20rem, 50vh);
  overflow-y: auto;
  padding: 0.25rem 0;
}

slab-command-palette:state(empty)::part(empty) {
  color: var(--ash);
  padding: 1rem 1.25rem;
}
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-command-palette> on the page. Import tokens.css, the colors and fonts every styled example shares, once, wherever your global styles go.

Import command-palette.js once, such as in your root layout, and use <slab-command-palette> as is. A Svelte component around it would take slot="…" content as its own slot and drop it.

command-palette.js
import 'slab-elements/command-palette.js';
import './command-palette.css';
command-palette.css
/*
  A closed dialog isn't displayed, so the animation runs again each time it
  opens.
*/
slab-command-palette::part(dialog) {
  animation: slab-command-palette-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-command-palette::part(dialog)::backdrop {
  animation: slab-command-palette-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-command-palette::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-command-palette-rise {
  from {
    opacity: 0;
    translate: 0 1rem;
  }
}

@keyframes slab-command-palette-fade {
  from {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-command-palette::part(dialog),
  slab-command-palette::part(dialog)::backdrop {
    animation: none;
  }
}

slab-command-palette::part(dialog) {
  inline-size: min(36rem, 100% - 2rem);
  margin-block-start: 12vh;
  padding: 0;
}

/*
  On the open host only. A closed dialog isn't displayed, and "display: grid"
  would show it.
*/
slab-command-palette[open]::part(dialog) {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

slab-command-palette::part(label),
slab-command-palette::part(listbox),
slab-command-palette::part(empty) {
  grid-column: 1 / -1;
}

slab-command-palette::part(label) {
  margin: 0;
}

slab-command-palette::part(close) {
  background-color: var(--paper);
  border: 0;
  border-block-end: var(--border);
  border-inline-start: var(--border);
  border-radius: 0;
  color: var(--ink);
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0 1rem;
  transition:
    background-color 100ms,
    color 100ms;
}

slab-command-palette::part(close):hover {
  background-color: var(--chalk);
}

slab-command-palette::part(close):focus-visible {
  background-color: var(--ember-deep);
  color: var(--paper);
  outline: 2px solid transparent;
}

@media (prefers-reduced-motion: reduce) {
  slab-command-palette::part(close) {
    transition: none;
  }
}

slab-command-palette::part(input) {
  background-color: var(--paper);
  block-size: 3.5rem;
  border: 0;
  border-block-end: var(--border);
  border-radius: 0;
  box-sizing: border-box;
  color: inherit;
  display: block;
  font: inherit;
  font-size: 1.125rem;
  inline-size: 100%;
  padding: 0 1.25rem;
}

slab-command-palette::part(input)::placeholder {
  color: var(--ash);
}

/*
  The ember bar under it is the focus ring, since the input is always
  focused while the palette is open. The outline is transparent rather
  than gone, so it still shows in forced colors.
*/
slab-command-palette::part(input):focus-visible {
  border-block-end-color: var(--ember);
  outline: 2px solid transparent;
}

slab-command-palette::part(listbox) {
  max-block-size: min(20rem, 50vh);
  overflow-y: auto;
  padding: 0.25rem 0;
}

slab-command-palette:state(empty)::part(empty) {
  color: var(--ash);
  padding: 1rem 1.25rem;
}
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-command-palette> 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.

CommandPalette.gjs
import 'slab-elements/command-palette.js';
import './command-palette.css';

export const CommandPalette = <template>
  <slab-command-palette ...attributes>{{yield}}</slab-command-palette>
</template>;
command-palette.css
/*
  A closed dialog isn't displayed, so the animation runs again each time it
  opens.
*/
slab-command-palette::part(dialog) {
  animation: slab-command-palette-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-command-palette::part(dialog)::backdrop {
  animation: slab-command-palette-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-command-palette::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-command-palette-rise {
  from {
    opacity: 0;
    translate: 0 1rem;
  }
}

@keyframes slab-command-palette-fade {
  from {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  slab-command-palette::part(dialog),
  slab-command-palette::part(dialog)::backdrop {
    animation: none;
  }
}

slab-command-palette::part(dialog) {
  inline-size: min(36rem, 100% - 2rem);
  margin-block-start: 12vh;
  padding: 0;
}

/*
  On the open host only. A closed dialog isn't displayed, and "display: grid"
  would show it.
*/
slab-command-palette[open]::part(dialog) {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

slab-command-palette::part(label),
slab-command-palette::part(listbox),
slab-command-palette::part(empty) {
  grid-column: 1 / -1;
}

slab-command-palette::part(label) {
  margin: 0;
}

slab-command-palette::part(close) {
  background-color: var(--paper);
  border: 0;
  border-block-end: var(--border);
  border-inline-start: var(--border);
  border-radius: 0;
  color: var(--ink);
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0 1rem;
  transition:
    background-color 100ms,
    color 100ms;
}

slab-command-palette::part(close):hover {
  background-color: var(--chalk);
}

slab-command-palette::part(close):focus-visible {
  background-color: var(--ember-deep);
  color: var(--paper);
  outline: 2px solid transparent;
}

@media (prefers-reduced-motion: reduce) {
  slab-command-palette::part(close) {
    transition: none;
  }
}

slab-command-palette::part(input) {
  background-color: var(--paper);
  block-size: 3.5rem;
  border: 0;
  border-block-end: var(--border);
  border-radius: 0;
  box-sizing: border-box;
  color: inherit;
  display: block;
  font: inherit;
  font-size: 1.125rem;
  inline-size: 100%;
  padding: 0 1.25rem;
}

slab-command-palette::part(input)::placeholder {
  color: var(--ash);
}

/*
  The ember bar under it is the focus ring, since the input is always
  focused while the palette is open. The outline is transparent rather
  than gone, so it still shows in forced colors.
*/
slab-command-palette::part(input):focus-visible {
  border-block-end-color: var(--ember);
  outline: 2px solid transparent;
}

slab-command-palette::part(listbox) {
  max-block-size: min(20rem, 50vh);
  overflow-y: auto;
  padding: 0.25rem 0;
}

slab-command-palette:state(empty)::part(empty) {
  color: var(--ash);
  padding: 1rem 1.25rem;
}
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;
  }
}