Slab
Slab

Guide

Get started

Install Slab, import the elements you use, and make them yours. Everything you need, in order.

01 Install it

Slab is one package, slab-elements, with Lit as a peer dependency.

Terminal
pnpm add slab-elements lit

02 Import what you use

Each element is its own module, and importing it registers its tag. Import the ones you use, and nothing else ships. An element made of parts, like Tabs, has a module for each part.

main.js
import 'slab-elements/switch.js';

import 'slab-elements/tabs.js';
import 'slab-elements/tabs.tab.js';
import 'slab-elements/tabs.panel.js';

Any bundler resolves the imports, or an import map does without one.

03 Use it like a native element

Attributes in, events out, the same as every built-in element. The events are the ones its native element fires, like "change", "input" and "toggle", and they bubble, so you can listen higher up too.

index.html
<slab-switch label="Notifications" name="notifications"></slab-switch>

<script type="module">
  import 'slab-elements/switch.js';

  const toggle = document.querySelector('slab-switch');

  toggle.addEventListener('change', () => {
    console.log(toggle.checked);
  });
</script>

Every control needs a label. While you develop on localhost, one without throws, so a missing one shows up early. hide-label hides it from view and keeps it for screenreaders. Like a native control, the checked and value attributes are where it starts, and the properties are where it is now.

04 In your framework

There's no wrapper package. The tag is the component, in React, Vue, Svelte, Ember, Rails, or plain HTML. React 19 and Svelte listen for an event through on and its exact name, such as onchange or oncopy-error, Ember's {{on}} takes the exact name too, and Vue needs to know which tags are custom elements. The home page shows the same Disclosure in each.

vite.config.js
import vue from '@vitejs/plugin-vue';

export default {
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement: (tag) => tag.startsWith('slab-'),
        },
      },
    }),
  ],
};

05 On the server

Using Next.js, Nuxt, SvelteKit, Astro, Ember or Rails? You're good. Importing an element on the server is safe. Slab doesn't render there, though. The tag is sent as you wrote it, and the element takes over in the browser once its module loads. One thing to watch: that module has to reach the browser. With server components, import it from a client one.

notifications.jsx
'use client';

import 'slab-elements/switch.js';

export default function Notifications() {
  return <slab-switch label="Notifications" name="notifications" />;
}

Before an element is defined, there's nothing of it to see. Use :not(:defined) to hold its space, or to hide what's slotted into it, until it's ready.

06 Put it in a form

Slab's controls are form-associated, so they work in a plain <form> the way an <input> does. They submit their name and value, reset with the form, and come back when the browser restores the page. Validation is the native element's too: required, pattern and the rest mean what they mean on an <input>, and checkValidity(), reportValidity() and setCustomValidity() work the same.

index.html
<form>
  <slab-text-field
    label="Email"
    name="email"
    type="email"
    required
  ></slab-text-field>

  <slab-checkbox
    label="Send me updates"
    name="updates"
    value="yes"
  ></slab-checkbox>

  <button>Sign up</button>
</form>

There's a <slab-form> too, if you'd like the form itself to be a Slab element.

07 Style it

Nothing ships styled, so an element has no size and nothing to see until you give it one. Reach its pieces with ::part() and its conditions with :state(). Every component's page lists both.

styles.css
slab-checkbox::part(checkbox) {
  block-size: 1rem;
  border: 1px solid currentcolor;
  inline-size: 1rem;
}

slab-checkbox:state(checked)::part(checkbox) {
  background-color: currentcolor;
}

08 Make it your own

To go further, subclass it, set styles, and register it under a tag of your own. Slab's functional styles, like a label that's hidden but still read, survive yours.

acme-checkbox.js
import Checkbox from 'slab-elements/checkbox.js';
import { css } from 'lit';

customElements.define(
  'acme-checkbox',
  class extends Checkbox {
    static styles = css`
      /* ... */
    `;
  },
);

Every component's page has a styled version to start from, with its source to copy and change.

09 Your words, in any language

Slab brings little text of its own. Where an element has to make some, mostly names for screenreaders, it's English until you translate it. Register your language's words once, and every element whose nearest lang is that language uses them. "fr-CA" takes "fr" for anything it leaves out, and English after that. Text built from a value, like Pagination's "Page 3", is a function, so you can word and order it for your language.

main.js
import { registerStrings } from 'slab-elements/strings.js';

registerStrings('de', {
  alertDismiss: 'Schließen',
  paginationPage: (page) => `Seite ${page}`,
});

Strings lists every one. An attribute or property on an element, like Alert's dismiss-label, still wins, for the one place you want different words.

10 TypeScript

The types come with the package. Each element adds its tag to HTMLElementTagNameMap, so document.querySelector('slab-switch') is typed as Slab's Switch. For your templates, import your framework's types once, and every tag's properties are checked, so a misspelled hideLabel or a wrong orientation is a type error.

slab.d.ts
// React. Or 'slab-elements/svelte.js' for Svelte 5,
// 'slab-elements/vue.js' for Vue, or 'slab-elements/ember.js'
// for Ember with Glint.
import type {} from 'slab-elements/react.js';

Events are typed too: a handler such as oncopy-error in React and Svelte, @copy-error in Vue, or {{on "copy-error"}} in Ember, gets the event with the element as its currentTarget. In Ember, Glint checks the attributes of each tag, by their names in HTML, such as hide-label. For any other framework, slab-elements/jsx.js has SlabElementProps, every tag and its properties, and SlabElementEvents, every tag and its events.

11 In your editor

Slab comes with what editors need to autocomplete its tags in plain HTML. Type <slab- and you get every tag, its attributes, and the values one such as placement takes, with what each does on hover. In CSS, you get its ::part() and :state() names. WebStorm and the other JetBrains editors find it on their own. For VS Code, and editors built on it such as Cursor, point your workspace settings at it.

.vscode/settings.json
{
  "html.customData": [
    "./node_modules/slab-elements/dist/vscode.html-custom-data.json"
  ],
  "css.customData": [
    "./node_modules/slab-elements/dist/vscode.css-custom-data.json"
  ]
}

Both are built from custom-elements.json each release, so they match the version you've installed.

12 Test it in a browser

Test what you build with Slab in a real browser. Playwright, Cypress, WebdriverIO and Vitest's browser mode all work great. Why not jsdom or happy-dom? Slab leans on the platform: popovers, form association and custom states. They don't have enough of it for an element to render.

The good news: you find a Slab element the same way you find a native one. Playwright's getByRole() and getByLabel() reach through shadow roots, so getByRole('switch', { name: 'Notifications' }) finds a Switch. From there, assert on checked, value and its events. It's how Slab tests itself!

13 Browsers

Slab needs Chrome and Edge 125, Firefox 126, or Safari 17.4, on desktop or mobile, or anything newer. Those are the first versions with both popovers and custom states, which every element depends on, and from 1.0, raising them waits for a major version. Slab is tested in the latest Chromium, Firefox and WebKit.

Where a browser hasn't caught up on a newer feature, Slab uses it where it's there and leaves the platform's behavior where it isn't. Without CSS anchor positioning, for example, a popover is centered rather than placed beside its anchor, and without ARIA element reflection, an aria-labelledby or aria-describedby on the host doesn't reach the element inside. Every element still opens, closes, submits and is announced, everywhere.

14 What it weighs

You only ship what you import. Here's what a few elements cost, minified and gzipped, each on its own without Lit. Elements share code too, so two cost less than their sum.

  • 1.9 kB Dialog
  • 3.9 kB Checkbox
  • 10.2 kB Select
  • 14 kB Combobox
  • 6.8 kB Lit, once
  • 87 kB Every element, with Lit

Tooltip is about 3 kB, Calendar 5 kB and Menu 7 kB.

15 What you can count on

Everything a component's page lists is public: its tag, attributes, properties, methods, events, slots, parts, states and custom properties. So are registerStrings() and the framework types. From 1.0, changing any of them waits for a major version. A minor version adds to them, and a patch fixes them without changing what they are.

Everything else is Slab's own and can change in any release: a member or event whose name starts with private, anything marked @internal, slab-elements/library/*, and whatever is in a shadow root other than its parts. Coming from 0.x? Upgrade to 1.0 lists what changed.

16 How things go away

From 1.0, nothing public is removed or renamed without warning. It's deprecated in a minor release first, and keeps working, unchanged, until the next major version removes it. A deprecated attribute, property or method says so on its component's page, with what to use instead, and its @deprecated tag has your editor strike it through. Each deprecation is in its release's notes.

Every major version lists what it removes, and what to do about each, in its upgrade guide, as Upgrade to 1.0 does for 0.x.

17 The same everywhere

Elements that do the same thing do it the same way. multiple lets more than one be chosen, and value is the first of values. A number is a number where the native element's is, as on Meter and Progress, and a string where it isn't, as on Slider and Number, which add valueAsNumber. Anything with open dispatches a ToggleEvent named "toggle" that doesn't bubble, as a popover does. Those that open a popup, such as Popover, Select, Combobox, Menu, Tooltip and the date pickers, dispatch "beforetoggle" first, and cancelling it keeps one from opening. readonly is on the fields that take typing, Combobox and Tags Input among them, and on Rating, and leaves the value focusable and submitted. Controls a native readonly does nothing to, like Checkbox, Select and Slider, don't have it. And the form controls for a set of choices are Checkbox Group and Radio Group, since toggle buttons aren't form controls.

18 How it compares

There are a lot of great headless libraries out there. React Aria, Base UI and Radix are built for React. Headless UI is for React and Vue, and Ark UI reaches a few more frameworks with a package for each. Web Awesome and Shoelace are custom elements like Slab, and they come with a design of their own to theme. Slab comes with none, so there's nothing to override.

  • Any framework, or none

    One package works in React, Vue, Svelte, Ember, a server-rendered template or a plain HTML file. Change frameworks and Slab comes with you.

  • Forms without glue

    Slab's controls are form-associated. A <form> submits, validates and resets them. No extra wiring needed.

  • What you already know

    Events, methods and validation work the way native elements do. If you know <input> and <dialog>, you know Slab.

  • Only what you import

    Each element is its own module and only a few kilobytes. See What it weighs for the numbers.

It's not all free, though. An element is empty markup until its script runs, where a React library can render on the server. Slab needs the browsers above, with no fallbacks for older ones. And you style through ::part() and :state() rather than class names, so utility classes reach an element's host and what you slot into it, but not its insides. For more, see Slab vs Web Awesome and Shoelace and Slab vs React Aria, Radix and Base UI.