Slab
Slab

Form

Form

A <form> that Slab's controls submit, reset, and validate with — just like native ones.

Element
<slab-form>
Built on
<form>

Example

Here's a styled version, <slab-styled-form>. 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
HTML
<slab-styled-form label="Schedule a pour">
  <slab-styled-text-field label="Job name" name="job" value="14 Alder Lane foundation" required></slab-styled-text-field>
  <slab-styled-date-field label="Pour date" name="date" required></slab-styled-date-field>
  <slab-styled-number label="Cubic yards" min="1" name="yards" value="38"></slab-styled-number>
  <div>
    <button class="styled-button styled-button--primary">Schedule</button>
  </div>
</slab-styled-form>
HTML
<slab-form label="Schedule a pour">
  <slab-text-field label="Job name" name="job" value="14 Alder Lane foundation" required></slab-text-field>
  <slab-date-field label="Pour date" name="date" required></slab-date-field>
  <slab-number label="Cubic yards" min="1" name="yards" value="38"></slab-number>
  <div>
    <button class="styled-button styled-button--primary">Schedule</button>
  </div>
</slab-form>

Usage

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

export function MyForm() {
  return (
    <slab-form label="Sign up" action="/sign-up" method="post">
      <slab-text-field label="Email" name="email" type="email" required></slab-text-field>
      <button>Sign up</button>
    </slab-form>
  );
}

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

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

@customElement('my-form')
export class MyForm extends LitElement {
  render() {
    return html`
      <slab-form label="Sign up" action="/sign-up" method="post">
        <slab-text-field label="Email" name="email" type="email" required></slab-text-field>
        <button>Sign up</button>
      </slab-form>
    `;
  }
}

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

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

<template>
  <slab-form label="Sign up" action="/sign-up" method="post">
    <slab-text-field label="Email" name="email" type="email" required></slab-text-field>
    <button>Sign up</button>
  </slab-form>
</template>

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

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

<slab-form label="Sign up" action="/sign-up" method="post">
  <slab-text-field label="Email" name="email" type="email" required></slab-text-field>
  <button>Sign up</button>
</slab-form>

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

import 'slab-elements/form.js';

export const MyForm = <template>
  <slab-form label="Sign up" action="/sign-up" method="post">
    <slab-text-field label="Email" name="email" type="email" required></slab-text-field>
    <button>Sign up</button>
  </slab-form>
</template>;

MyForm.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/form.js';
</script>

<slab-form label="Sign up" action="/sign-up" method="post">
  <slab-text-field label="Email" name="email" type="email" required></slab-text-field>
  <button>Sign up</button>
</slab-form>

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

A plain <form> works with Slab’s controls too. Reach for Form when you want what it adds: a label that makes the form a landmark, and a checkValidity() that doesn’t show errors, just like a native one.

Keyboard

Key What it does
Enter In a field, submits the form, as in a native one.

Resetting

A reset, from a <button type="reset"> or reset(), puts each control back to the value it started with, as it does a native one. That’s its value attribute, or checked on a Checkbox, Switch or Radio, and selected on an Option. Read or change it from code with defaultValue, defaultChecked or defaultSelected, just like a native input’s. Range Slider and Date Range Field have defaultStartValue and defaultEndValue.

const field = document.querySelector('slab-text-field');

field.defaultValue = '14 Alder Lane foundation';
field.form.reset();

Labels

Each control has labels, as a native one does: the <label> elements associated with it, by a for that names its id or by being around it. Slab’s controls are named by their own label attribute, so it’s often empty.

Clicking one of those labels does what it does for a native control. A Checkbox or Switch toggles, a File Drop opens its file picker, and any other control takes focus. The label doesn’t name the control, though, so keep its label attribute.

const field = document.querySelector('slab-text-field');

for (const label of field.labels) {
  label.classList.add('is-labelling');
}

Validation

Each control checks itself the way a native one does. A required field that’s empty is invalid, and so is one whose value breaks its pattern. You don’t have to write any of that yourself.

When errors show

Nobody likes an error before they’ve had a chance to type. So a control’s validity message shows once its validity is reported:

  • when focus leaves it
  • when its form is submitted, which also focuses the first invalid control
  • when you call reportValidity()

While it shows, the control has aria-invalid, the message is part of its accessible description, and it matches :state(error). Style the message through ::part(validity-message).

checkValidity() asks without showing anything, on a control or a whole Form. resetValidityFeedback() hides the message again, and a form reset does it for you.

Your own words

The browser’s messages are generic, and they’re in the browser’s language, not your page’s. Slot your own into validity-message. You don’t need to make the control invalid yourself: the slot shows whenever it’s invalid, for any reason, and its message shows.

React Lit Vue Svelte Ember HTML
export function JobName() {
  return (
    <slab-text-field label="Job name" name="job" required>
      <span slot="validity-message">
        Give the job a name so the crew can find it.
      </span>
    </slab-text-field>
  );
}
render() {
  return html`
    <slab-text-field label="Job name" name="job" required>
      <span slot="validity-message">
        Give the job a name so the crew can find it.
      </span>
    </slab-text-field>
  `;
}
<template>
  <slab-text-field label="Job name" name="job" required>
    <span slot="validity-message">
      Give the job a name so the crew can find it.
    </span>
  </slab-text-field>
</template>
<slab-text-field label="Job name" name="job" required>
  <span slot="validity-message">
    Give the job a name so the crew can find it.
  </span>
</slab-text-field>
<template>
  <slab-text-field label="Job name" name="job" required>
    <span slot="validity-message">
      Give the job a name so the crew can find it.
    </span>
  </slab-text-field>
</template>
<slab-text-field label="Job name" name="job" required>
  <span slot="validity-message">
    Give the job a name so the crew can find it.
  </span>
</slab-text-field>

It’s markup, so a link works too. validationMessage stays the browser’s message, or the one you set.

A different message for each problem

The slot shows the same thing whatever went wrong. Can a field fail more than one way? Pick the words on “invalid”, which fires just before the message shows. validity says what’s wrong.

React Lit Vue Svelte Ember HTML
import { useEffect, useRef, useState } from 'react';

export function Email() {
  const field = useRef(null);
  const [message, setMessage] = useState('');

  useEffect(() => {
    const element = field.current;

    function onInvalid() {
      setMessage(
        element.validity.valueMissing
          ? 'Enter an email so we can send the schedule.'
          : "That doesn't look like an email. Check for typos?",
      );
    }

    element.addEventListener('invalid', onInvalid);

    return () => element.removeEventListener('invalid', onInvalid);
  }, []);

  return (
    <slab-text-field
      label="Email"
      name="email"
      ref={field}
      required
      type="email"
    >
      <span slot="validity-message">{message}</span>
    </slab-text-field>
  );
}
@state()
private message = '';

#onInvalid(event: Event) {
  const field = event.target as HTMLElementTagNameMap['slab-text-field'];

  this.message = field.validity.valueMissing
    ? 'Enter an email so we can send the schedule.'
    : "That doesn't look like an email. Check for typos?";
}

render() {
  return html`
    <slab-text-field
      label="Email"
      name="email"
      required
      type="email"
      @invalid=${this.#onInvalid}
    >
      <span slot="validity-message">${this.message}</span>
    </slab-text-field>
  `;
}
<script setup>
import { ref } from 'vue';

const message = ref('');

function onInvalid(event) {
  message.value = event.target.validity.valueMissing
    ? 'Enter an email so we can send the schedule.'
    : "That doesn't look like an email. Check for typos?";
}
</script>

<template>
  <slab-text-field
    label="Email"
    name="email"
    required
    type="email"
    @invalid="onInvalid"
  >
    <span slot="validity-message">{{ message }}</span>
  </slab-text-field>
</template>
<script>
  let message = $state('');

  function oninvalid(event) {
    message = event.target.validity.valueMissing
      ? 'Enter an email so we can send the schedule.'
      : "That doesn't look like an email. Check for typos?";
  }
</script>

<slab-text-field label="Email" name="email" required type="email" {oninvalid}>
  <span slot="validity-message">{message}</span>
</slab-text-field>
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { on } from '@ember/modifier';

export class Email extends Component {
  @tracked message = '';

  onInvalid = (event) => {
    this.message = event.target.validity.valueMissing
      ? 'Enter an email so we can send the schedule.'
      : "That doesn't look like an email. Check for typos?";
  };

  <template>
    <slab-text-field label="Email" name="email" required type="email" {{on "invalid" this.onInvalid}}>
      <span slot="validity-message">{{this.message}}</span>
    </slab-text-field>
  </template>
}
const field = document.querySelector('slab-text-field[name="email"]');
const message = field.querySelector('[slot="validity-message"]');

field.addEventListener('invalid', () => {
  message.textContent = field.validity.valueMissing
    ? 'Enter an email so we can send the schedule.'
    : "That doesn't look like an email. Check for typos?";
});

Errors from your server

Some errors only your server knows about, like an email that’s already taken. Put the message in custom-validity. It’s setCustomValidity() from markup: the control is invalid with that message, and it shows right away, without waiting for a blur or a submit.

The error stays until you take it away, just like setCustomValidity(), and it stops the form from submitting. Clear it once the user changes the value. An empty string, or no attribute at all, makes the control valid again.

React Lit Vue Svelte Ember HTML
import { useState } from 'react';

// `error` is what your server said, such as "That email is already taken".
export function Email({ error: serverError }) {
  const [error, setError] = useState(serverError);

  return (
    <slab-text-field
      custom-validity={error}
      label="Email"
      name="email"
      onInput={() => setError('')}
      type="email"
    ></slab-text-field>
  );
}
// What your server said, such as "That email is already taken".
@property()
error = '';

render() {
  return html`
    <slab-text-field
      custom-validity=${this.error}
      label="Email"
      name="email"
      type="email"
      @input=${() => (this.error = '')}
    ></slab-text-field>
  `;
}
<script setup>
import { ref } from 'vue';

// What your server said, such as "That email is already taken".
const props = defineProps({ error: String });
const error = ref(props.error);
</script>

<template>
  <slab-text-field
    :custom-validity="error"
    label="Email"
    name="email"
    type="email"
    @input="error = ''"
  ></slab-text-field>
</template>
<script>
  // What your server said, such as "That email is already taken".
  let { error: serverError } = $props();
  let error = $state(serverError);
</script>

<slab-text-field
  custom-validity={error}
  label="Email"
  name="email"
  oninput={() => (error = '')}
  type="email"
></slab-text-field>
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { on } from '@ember/modifier';

// `@error` is what your server said, such as "That email is already taken".
export class Email extends Component {
  @tracked error = this.args.error;

  clear = () => (this.error = '');

  <template>
    <slab-text-field
      custom-validity={{this.error}}
      label="Email"
      name="email"
      type="email"
      {{on "input" this.clear}}
    ></slab-text-field>
  </template>
}
<slab-text-field
  custom-validity="That email is already taken"
  label="Email"
  name="email"
  type="email"
></slab-text-field>

<script type="module">
  const field = document.querySelector('slab-text-field[name="email"]');

  field.addEventListener('input', () => {
    field.removeAttribute('custom-validity');
  });
</script>

Rules of your own

For a rule only your page knows, like two passwords that have to match, call setCustomValidity() whenever the value changes. An empty string makes it valid again.

React Lit Vue Svelte Ember HTML
import { useRef } from 'react';

export function Passwords() {
  const password = useRef(null);
  const confirm = useRef(null);

  function onInput() {
    confirm.current.setCustomValidity(
      confirm.current.value === password.current.value
        ? ''
        : "The passwords don't match.",
    );
  }

  return (
    <>
      <slab-password-field
        label="Password"
        name="password"
        onInput={onInput}
        ref={password}
      />
      <slab-password-field
        label="Confirm password"
        name="confirm"
        onInput={onInput}
        ref={confirm}
      />
    </>
  );
}
@query('[name="password"]')
private password?: HTMLElementTagNameMap['slab-password-field'];

@query('[name="confirm"]')
private confirm?: HTMLElementTagNameMap['slab-password-field'];

#onInput() {
  this.confirm?.setCustomValidity(
    this.confirm.value === this.password?.value
      ? ''
      : "The passwords don't match.",
  );
}

render() {
  return html`
    <slab-password-field label="Password" name="password" @input=${this.#onInput}></slab-password-field>
    <slab-password-field label="Confirm password" name="confirm" @input=${this.#onInput}></slab-password-field>
  `;
}
<script setup>
import { ref } from 'vue';

const password = ref(null);
const confirm = ref(null);

function onInput() {
  confirm.value.setCustomValidity(
    confirm.value.value === password.value.value
      ? ''
      : "The passwords don't match.",
  );
}
</script>

<template>
  <slab-password-field
    ref="password"
    label="Password"
    name="password"
    @input="onInput"
  ></slab-password-field>
  <slab-password-field
    ref="confirm"
    label="Confirm password"
    name="confirm"
    @input="onInput"
  ></slab-password-field>
</template>
<script>
  let password = $state();
  let confirm = $state();

  function oninput() {
    confirm.setCustomValidity(
      confirm.value === password.value ? '' : "The passwords don't match.",
    );
  }
</script>

<slab-password-field bind:this={password} label="Password" name="password" {oninput}></slab-password-field>
<slab-password-field bind:this={confirm} label="Confirm password" name="confirm" {oninput}></slab-password-field>
import Component from '@glimmer/component';
import { on } from '@ember/modifier';
import { modifier } from 'ember-modifier';

export class Passwords extends Component {
  #password;
  #confirm;

  password = modifier((element) => (this.#password = element));
  confirm = modifier((element) => (this.#confirm = element));

  onInput = () => {
    this.#confirm.setCustomValidity(
      this.#confirm.value === this.#password.value
        ? ''
        : "The passwords don't match.",
    );
  };

  <template>
    <slab-password-field label="Password" name="password" {{this.password}} {{on "input" this.onInput}}></slab-password-field>
    <slab-password-field label="Confirm password" name="confirm" {{this.confirm}} {{on "input" this.onInput}}></slab-password-field>
  </template>
}
const password = document.querySelector('[name="password"]');
const confirm = document.querySelector('[name="confirm"]');

function onInput() {
  confirm.setCustomValidity(
    confirm.value === password.value ? '' : "The passwords don't match.",
  );
}

password.addEventListener('input', onInput);
confirm.addEventListener('input', onInput);

setValidity() does the same with the flags of your choice, as ElementInternals.setValidity() does, when a rule should read as rangeUnderflow or patternMismatch rather than customError.

Keeping errors hidden

Would you rather show errors your own way, or only after a submit? Cancel “invalid”, as you would on a native control. The message stays hidden, the control doesn’t match :state(error), and on a submit it isn’t focused. That goes for a blur, a submit and reportValidity() alike. The control is still invalid, so validity, :invalid and a blocked submit work as usual.

React Lit Vue Svelte Ember HTML
import { useEffect, useRef } from 'react';

export function Email() {
  const field = useRef(null);

  useEffect(() => {
    const element = field.current;
    const onInvalid = (event) => event.preventDefault();

    element.addEventListener('invalid', onInvalid);

    return () => element.removeEventListener('invalid', onInvalid);
  }, []);

  return (
    <slab-text-field
      label="Email"
      name="email"
      ref={field}
      required
      type="email"
    />
  );
}
render() {
  return html`
    <slab-text-field
      label="Email"
      name="email"
      required
      type="email"
      @invalid=${(event: Event) => event.preventDefault()}
    ></slab-text-field>
  `;
}
<template>
  <slab-text-field
    label="Email"
    name="email"
    required
    type="email"
    @invalid.prevent
  ></slab-text-field>
</template>
<slab-text-field
  label="Email"
  name="email"
  oninvalid={(event) => event.preventDefault()}
  required
  type="email"
></slab-text-field>
import { on } from '@ember/modifier';

const preventDefault = (event) => event.preventDefault();

<template>
  <slab-text-field label="Email" name="email" required type="email" {{on "invalid" preventDefault}}></slab-text-field>
</template>
const field = document.querySelector('slab-text-field[name="email"]');

field.addEventListener('invalid', (event) => {
  event.preventDefault();
});

Behavior

A form with no visual styles, around a native <form>, so it's submitted, reset and validated the way one is.

The <form> is in the light DOM, not the shadow root. A control's form owner is its nearest <form> ancestor in its own tree, and that doesn't cross a shadow boundary: a <form> in the shadow root with a <slot> inside it would own nothing slotted into it, and every control would submit to no form at all. So Form appends a <form> as its only child and moves its other children into it, on connect and whenever one is added. The shadow root holds a single <slot>, which the <form> is slotted into.

Everything else is the <form>'s. Slab's controls are form-associated through ElementInternals, so they submit, reset, and report their validity to it the way a native <input> does, and nothing here walks them. checkValidity(), reportValidity(), requestSubmit(), reset() and submit() are the <form>'s, and "submit" and "reset" bubble from it through the host.

A native <form>'s checkValidity() fires "invalid" at each invalid control but shows nothing, and so does this one's: Form marks its own <form> while its checkValidity() runs, whether it's called on Form or on form, and Slab's controls stay out of their error state. Only that <form> is marked. HTMLFormElement.prototype is left alone, so a consumer's own plain <form> gets no such marking, and its checkValidity() puts Slab's invalid controls into their error state as reportValidity() does.

label, when set, names the <form>, which is what makes it a form landmark. The <form> is reachable as form and styled as any light-DOM element is.

API

<slab-form>

Attributes
Name Type Default Description
action string Where it submits to, as a native form's action.
autocomplete 'on' | 'off' Whether the browser may fill its controls, as a native form's autocomplete.
enctype string How it encodes what it submits, as a native form's enctype.
label string Names the form, which makes it a landmark.
method 'get' | 'post' | 'dialog' How it submits, as a native form's method.
name string Its name, as a native form's name.
novalidate boolean false Submits without checking validity, as a native form's novalidate.
target string Where the response opens, as a native form's target.
Properties
Name Type Description
elements Read-only HTMLFormControlsCollection Its controls, as a native form's elements.
form Read-only HTMLFormElement The native form, in the light DOM
Methods
Signature Description
checkValidity(): boolean Whether its value is valid. Dispatches "invalid" when it isn't, as a native control's does.
reportValidity(): boolean Whether its value is valid. When it isn't, dispatches "invalid" and shows the validity message, as a native control's does.
requestSubmit(submitter?: HTMLElement | null): void Submits it as a submit button would, checking validity and dispatching "submit", as a native form's requestSubmit().
reset(): void Resets its controls, as a native form's reset().
submit(): void Submits it without checking validity or dispatching "submit", as a native form's submit().
Slots
Name Type Description
(default) Element | string Moved into the form

Copy and own it

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

Form.jsx
import 'slab-elements/form.js';
import './form.css';

export function Form(props) {
  return <slab-form {...props} />;
}
form.css
slab-form > form {
  display: grid;
  gap: 1.5rem;
}
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.

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

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-form': StyledForm;
  }
}

/**
 * Form, styled: its fields stacked, with room between them.
 */
@customElement('slab-styled-form')
export default class StyledForm extends Form {
  static override styles = [tokens, styles];
}
form.styles.ts
import { css } from 'lit';

/*
  Form moves its children into a "<form>" of its own, in the light DOM, and
  slots that in. So the form is what's laid out.
*/
export default css`
  ::slotted(form) {
    display: grid;
    gap: 1.5rem;
  }
`;
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-form> 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.

Form.vue
<script setup>
import 'slab-elements/form.js';
import './form.css';
</script>

<template>
  <slab-form><slot /></slab-form>
</template>
form.css
slab-form > form {
  display: grid;
  gap: 1.5rem;
}
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-form> on the page. Import tokens.css, the colors and fonts every styled example shares, once, wherever your global styles go.

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

form.js
import 'slab-elements/form.js';
import './form.css';
form.css
slab-form > form {
  display: grid;
  gap: 1.5rem;
}
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-form> 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.

Form.gjs
import 'slab-elements/form.js';
import './form.css';

export const Form = <template>
  <slab-form ...attributes>{{yield}}</slab-form>
</template>;
form.css
slab-form > form {
  display: grid;
  gap: 1.5rem;
}
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;
  }
}