Slab

Disclosure

Splitter

Two panes with a separator between them that resizes them, dragged or moved with the keyboard.

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

Example

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

Footing, slab, wall.

Slab

4,000 psi, a 4-inch slump, broom finish. Cure for seven days before it takes a load.

HTML
<slab-styled-splitter label="Mixes" min="20" max="80" value="35" style="block-size: 14rem">
  <div slot="primary">
    <strong>Mixes</strong>
    <p>Footing, slab, wall.</p>
  </div>
  <div slot="secondary">
    <strong>Slab</strong>
    <p>4,000 psi, a 4-inch slump, broom finish. Cure for seven days before it takes a load.</p>
  </div>
</slab-styled-splitter>
Mixes

Footing, slab, wall.

Slab

4,000 psi, a 4-inch slump, broom finish. Cure for seven days before it takes a load.

HTML
<slab-splitter label="Mixes" min="20" max="80" value="35" style="block-size: 14rem">
  <div slot="primary">
    <strong>Mixes</strong>
    <p>Footing, slab, wall.</p>
  </div>
  <div slot="secondary">
    <strong>Slab</strong>
    <p>4,000 psi, a 4-inch slump, broom finish. Cure for seven days before it takes a load.</p>
  </div>
</slab-splitter>

Usage

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

<slab-splitter label="Sidebar" min="15" max="50" value="25">
  <nav slot="primary">…</nav>
  <main slot="secondary">…</main>
</slab-splitter>

The separator is named by label, which should name the primary pane, since that’s the one value sizes. Give the splitter a size, such as a block-size, or it’s as tall as its tallest pane.

Keyboard

Key What it does
Left, Right Moves the separator by step, the way the arrow points, so Left shrinks the primary pane unless it’s on the right. Up and Down when orientation is “horizontal”.
Home, End Moves the separator to min or max.
Enter Collapses the primary pane to min, or restores it to where it was.

Behavior

A window splitter with no visual styles: two panes and a [role=separator] between them that's dragged, or moved with the keyboard, to resize them.

value is how much of the splitter the primary pane takes up, as a percentage, and the secondary pane takes the rest. The separator keeps its own size, whatever your styles make it, and the panes share what's left. min and max bound it, so a sidebar can't be squeezed to nothing or take over.

The separator is named by label, which should name the primary pane, and reports value as its aria-valuenow. Left and Right move it, or Up and Down when orientation is "horizontal", by step. Home and End move it to min and max. Enter collapses the primary pane to min, and restores it to where it was before.

Laying out the panes and sizing them is what a splitter does, so that's functional and done here: the host is a grid of the two panes and the separator. Give the host a size, or it'll be as tall as its tallest pane. Style the rest through its parts and through :state(collapsed), :state(disabled) and :state(dragging).

API

<slab-splitter>

Attributes
Name Type Default Description
label Required string Names the separator. Name it after the primary pane, as the separator resizes it.
disabled boolean false
max number 100 The most of the splitter the primary pane can take up, as a percentage.
min number 0 The least of the splitter the primary pane can take up, as a percentage. Enter collapses the primary pane to it.
orientation 'horizontal' | 'vertical' 'vertical' The separator's. A vertical one sits between panes side by side, and a horizontal one between panes stacked on top of each other.
step number 5 How far each arrow key moves the separator, as a percentage.
value number 50 How much of the splitter the primary pane takes up, as a percentage. It's always between min and max.
version string
Methods
Signature Description
focus(options?: FocusOptions): void
Events
Name Type Description
input Event
change Event
Slots
Name Type Description
primary Element
secondary Element
CSS parts
Name Description
container The panes and the separator
primary The primary pane
secondary The secondary pane
separator The separator between the panes

Copy and own it

<slab-styled-splitter> 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.

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

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-splitter': StyledSplitter;
  }
}

/**
 * Splitter, styled: two panes in a raised box, split by an ink bar that turns
 * ember while it's hovered, focused or dragged.
 */
@customElement('slab-styled-splitter')
export default class StyledSplitter extends Splitter {
  static override styles = [tokens, styles];
}
splitter.styles.ts
import { css } from 'lit';

/*
  The separator is an ink bar with a grip at its middle, drawn with a
  pseudo-element so it needs no markup. Its hit area is wider than the bar,
  so it's easy to grab.
*/
export default css`
  :host {
    background-color: var(--paper);
    border: var(--border);
    box-shadow: var(--shadow);
    color: var(--ink);
    font-family: var(--font-body);
  }

  [part~='primary'],
  [part~='secondary'] {
    padding: 1rem;
  }

  [part~='separator'] {
    align-items: center;
    background-color: var(--ink);
    cursor: col-resize;
    display: flex;
    inline-size: 0.5rem;
    justify-content: center;
    transition: background-color 100ms;

    &::before {
      background-color: var(--paper);
      block-size: 2rem;
      content: '';
      inline-size: 2px;
    }

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

    &:focus-visible {
      background-color: var(--ember);
      outline: 3px solid var(--ember);
      outline-offset: 2px;
    }
  }

  :host([orientation='horizontal']) [part~='separator'] {
    block-size: 0.5rem;
    cursor: row-resize;
    inline-size: auto;

    &::before {
      block-size: 2px;
      inline-size: 2rem;
    }
  }

  :host(:state(dragging)) [part~='separator'] {
    background-color: var(--ember);
  }

  :host(:state(disabled)) [part~='separator'] {
    background-color: var(--ash);
    cursor: not-allowed;
  }

  @media (prefers-reduced-motion: reduce) {
    [part~='separator'] {
      transition: none;
    }
  }
`;
tokens.styles.ts
import { css } from 'lit';

/*
  The styled components' palette and type, taken from the logo: ink for the
  courses, ember for the base, paper behind them.

  Each is a short alias for a "--slab-styled-*" property, with the default as
  its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
  which every shadow root inherits. Were the defaults declared under those
  names here instead, each host's own declaration would shadow the page's.

  Each default is "light-dark()", so the components follow the page's
  "color-scheme": "dark" for dark, "light" for light, and "light dark" to
  follow the operating system. Ink and paper trade places in the dark, so
  ink is always the text and paper always what's behind it. Contrast is given
  as light, then dark.
*/
export default css`
  :host {
    /* Secondary text. 6.8:1 and 6.9:1 on paper. */
    --ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
    --border: 2px solid var(--ink);

    /* Behind disabled and read-only controls, and hovered rows. */
    --chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));

    /*
      The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
      enough for borders, fills and large text, but not always for small text,
      which takes "--ember-deep".
    */
    --ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));

    /*
      Orange for small text on paper, and for paper text on it. 5.7:1 and
      7.4:1.
    */
    --ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
    --font-body: var(
      --slab-styled-font-body,
      'IBM Plex Sans',
      system-ui,
      sans-serif
    );
    --font-display: var(
      --slab-styled-font-display,
      'Zilla Slab',
      rockwell,
      'Roboto Slab',
      serif
    );
    --font-mono: var(
      --slab-styled-font-mono,
      'IBM Plex Mono',
      ui-monospace,
      monospace
    );
    --ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
    --paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));

    /* The hard, offset shadow a raised surface casts. */
    --shadow: 4px 4px 0 var(--ink);
  }
`;