Slab
Slab

Form

Password Field

A password input with a button that shows and hides what was typed.

Element
<slab-password-field>
Built on
<input type=password>

Example

Here's a styled version, <slab-styled-password-field>. 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
At least 8 characters. Press Show password to check it.
HTML
<slab-styled-password-field label="Site gate code" autocomplete="current-password" minlength="8" required>
  <span slot="description">At least 8 characters. Press Show password to check it.</span>
</slab-styled-password-field>
At least 8 characters. Press Show password to check it.
HTML
<slab-password-field label="Site gate code" autocomplete="current-password" minlength="8" required>
  <span slot="description">At least 8 characters. Press Show password to check it.</span>
</slab-password-field>

Usage

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

export function MyPasswordField() {
  return (
    <slab-password-field label="Password" name="password" autocomplete="current-password" required></slab-password-field>
  );
}

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

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

@customElement('my-password-field')
export class MyPasswordField extends LitElement {
  render() {
    return html`
      <slab-password-field label="Password" name="password" autocomplete="current-password" required></slab-password-field>
    `;
  }
}

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

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

<template>
  <slab-password-field label="Password" name="password" autocomplete="current-password" required></slab-password-field>
</template>

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

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

<slab-password-field label="Password" name="password" autocomplete="current-password" required></slab-password-field>

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

import 'slab-elements/password-field.js';

export const MyPasswordField = <template>
  <slab-password-field label="Password" name="password" autocomplete="current-password" required></slab-password-field>
</template>;

MyPasswordField.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/password-field.js';
</script>

<slab-password-field label="Password" name="password" autocomplete="current-password" required></slab-password-field>

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

The button is a toggle button, so its name stays “Show password” (or your toggle-label) and aria-pressed says whether the password shows. A screenreader reads “Show password, toggle button, pressed” instead of a name that changes under the user. While it shows, the input is a text input, with spellcheck, autocorrect and autocapitalize off so the password doesn’t go to a spellchecker.

revealed works like value: the attribute sets the starting state and the property tracks the current one. Pressing the button dispatches “reveal-change”, so read revealed there for which way it went. Resetting the form hides the password again. Edge’s own reveal button is hidden so there’s only one. Set autocomplete to “current-password” on a sign-in form or “new-password” where a password is chosen, so password managers fill and save it. selectionStart, selectionEnd, selectionDirection, select(), setSelectionRange() and setRangeText() work as they do on a native password input. Like a native input’s, setRangeText() dispatches no “input” or “change”.

Keyboard

Key What it does
Tab, Shift+Tab Moves from the input to the button, and out of the field.
Space, Enter On the button, shows or hides the password.
Enter In the input, submits the form.
Anything else What it does on a native password input.

Behavior

A password field with no visual styles, around a native password input, and a button beside it that shows and hides what's been typed.

The button is a toggle button: its aria-pressed is "true" while the password shows, and its name, toggle-label, doesn't change, so a screenreader reads "Show password, toggle button, pressed" rather than a name that flips under the user. It controls the input, which becomes a text input while the password shows and goes back to a password input when the button is pressed again or the form is reset. Spellcheck, autocorrect and autocapitalize are off, so revealing the password doesn't hand it to a spellchecker.

The label and the input are both in the shadow root, so a plain for reference names it without crossing out of one. The host delegates focus, so clicking anywhere on it, or a <label for> pointing at it, focuses the input.

Validity is the native input's. required, minlength, maxlength and pattern are passed through to it, and whatever it decides is mirrored onto the host, so valueMissing, tooShort and patternMismatch all mean what they mean on an <input>. Nothing is computed twice.

Style it through its parts and through :state(disabled), :state(error), :state(readonly), :state(required) and :state(revealed). Where the label sits relative to the input, and the button relative to it, is layout, so it's left to the styles around it.

API

<slab-password-field>

Attributes
Name Type Default Description
label Required string Names it. It's the accessible name, and the visible label where there is one.
autocomplete HTMLInputElement['autocomplete'] "current-password" on a sign-in form and "new-password" where one is chosen, so a password manager knows which it is.
disabled boolean false Disables it, as disabled does a native control: the user can't focus or use it, and a form control's value isn't submitted.
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.
maxlength number The most characters the user can type, as a native input's maxlength.
minlength number The fewest characters a valid value has, as a native input's minlength.
name string '' The name its value is submitted under, as a native control's name.
pattern string A regular expression a valid value matches, as a native input's pattern.
placeholder string Shown while it's empty, as a native input's placeholder. It's a hint, not a label.
readonly boolean false Lets the user select the value but not change it, as readonly does a native input. The value is still submitted.
required boolean false Makes it invalid while it has no value, as required does a native control.
revealed boolean false Whether the password shows. Like value, the attribute is the default and the property is the current one, so the attribute doesn't change as the button is pressed.
toggle-label string The button's text and so its accessible name. "Show password" in English when unset.
value string '' Like a native input, the attribute is the default value and the property is the current one, so the attribute doesn't change as the field is typed into.
custom-validity string Makes it invalid with this message, as setCustomValidity() does, and shows the message at once rather than once validity is reported, for an error the page already knows about, such as one from a server. Removing it, or setting it to an empty string, makes it valid again, unless something else makes it invalid.
Properties
Name Type Description
defaultValue string The value it starts with, and goes back to when its form resets. It's the value attribute, as a native input's defaultValue is, so setting it sets the attribute, and value with it.
selectionDirection HTMLInputElement['selectionDirection'] Which way the selection was made, "forward", "backward" or "none", as a native input's selectionDirection is.
selectionEnd number | null Where the selection ends, or where the caret is when nothing is selected, as a native input's selectionEnd is.
selectionStart number | null Where the selection starts, or where the caret is when nothing is selected, as a native input's selectionStart is.
form Read-only HTMLFormElement | null The form it belongs to, or null.
labels Read-only NodeList The <label> elements associated with it, as a native control's labels: each <label for> that names its id, and a <label> around it.
validationMessage Read-only string What's wrong, as the validity message says it: the message given to setCustomValidity() or setValidity(), or else the browser's own. Empty while it's valid.
validity Read-only ValidityState Its ValidityState, as a native control's.
willValidate Read-only boolean Whether its validity is checked when its form submits. It isn't while it's disabled or read-only.
Methods
Signature Description
checkValidity(): boolean Whether its value is valid. Dispatches "invalid" when it isn't, as a native control's does.
click(): void Clicks it, as a native element's click() does.
focus(options?: FocusOptions): void Focuses it, or the part of it that takes focus.
reportValidity(): boolean Whether its value is valid. When it isn't, dispatches "invalid" and shows the validity message, as a native control's does.
resetValidityFeedback(): void Clears the validity feedback without changing validity.
select(): void Selects its text, as a native input's select() does.
setCustomValidity(message: string): void Makes it invalid with a message of your own, or valid again with an empty one, as a native control's does.
setRangeText(replacement: string, start?: number, end?: number, selectionMode?: SelectionMode): void Replaces part of its text, or the selection when start and end are left out, as a native input's setRangeText() does. selectionMode says what's selected afterward: "select", "start", "end" or "preserve", the default. Like a native input's, it dispatches no "input" or "change".
setSelectionRange(start: number, end: number, direction?: 'forward' | 'backward' | 'none'): void Selects part of its text, as a native input's setSelectionRange() does.
setValidity(flags?: ValidityStateFlags, message?: string): void Sets its validity and message, as ElementInternals.setValidity() does, for checks of your own beyond setCustomValidity().
Events
Name Type Description
input Event As the user changes its value, as a native control's does. Setting it from code doesn't dispatch it.
change Event When the user changes its value, as a native control's does. Setting it from code doesn't dispatch it.
reveal-change Event When the user shows or hides the password. Read revealed for which.
invalid Event When a check finds its value invalid, as a native control's does. Cancel it to leave the validity message unshown, after a blur, a submit or reportValidity(), and focus where it is.
Slots
Name Type Description
description Element | string Text that describes it, such as a hint or what the validity message means. It's its accessible description.
validity-message Element | string Shown in place of the validity message's text while it's invalid and the message shows, for wording of your own, a translation, or a link. It's part of its accessible description, and validationMessage stays the browser's or setCustomValidity()'s.
CSS parts
Name Description
control The input and the button, together
description Text about the field, and its validity message when it has one
input The input
label The label
toggle-button The button that shows and hides the password
validity-message The validity message, which says why the value is invalid
CSS states
Name Description
:state(disabled) While it's disabled.
:state(error) While it's invalid and its validity message shows, which is once its validity is reported, as when its form is submitted.
:state(readonly) While it's read-only.
:state(required) While it's required, for styles such as an asterisk.
:state(revealed) While the password shows as text.

Copy and own it

<slab-styled-password-field> 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-password-field> 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.

PasswordField.jsx
import 'slab-elements/password-field.js';
import './password-field.css';

export function PasswordField(props) {
  return <slab-password-field {...props} />;
}
password-field.css
slab-password-field {
  color: var(--ink);
  display: grid;
  font-family: var(--font-body);
}

slab-password-field::part(label) {
  font-family: var(--font-display);
  font-size: 1.0625rem;
  font-weight: 600;
  margin-block-end: 0.375rem;
}

slab-password-field:state(required)::part(label)::after {
  color: var(--ember-deep);
  content: ' *' / '';
}

slab-password-field::part(input),
slab-password-field::part(textarea) {
  background-color: var(--paper);
  block-size: 3rem;
  border: var(--border);
  border-radius: 0;
  box-sizing: border-box;
  color: inherit;
  font: inherit;
  /* A fixed height, shared with Select, so fields line up in a row. */
  padding: 0 0.75rem;
  transition:
    background-color 100ms,
    border-color 100ms,
    box-shadow 100ms,
    translate 100ms;
}

slab-password-field::part(input)::placeholder,
slab-password-field::part(textarea)::placeholder {
  color: var(--ash);
}

/*
  The outline is transparent rather than gone, so it still shows in forced
  colors, where box shadows don't.
*/
slab-password-field::part(input):focus-visible,
slab-password-field::part(textarea):focus-visible {
  box-shadow: 4px 4px 0 var(--ember);
  outline: 2px solid transparent;
  translate: -2px -2px;
}

slab-password-field::part(input):disabled,
slab-password-field::part(textarea):disabled {
  background-color: var(--chalk);
  border-style: dashed;
  color: var(--ash);
  cursor: not-allowed;
}

slab-password-field::part(textarea) {
  block-size: auto;
  min-block-size: 7rem;
  padding: 0.625rem 0.75rem;
  resize: vertical;
}

slab-password-field:state(readonly)::part(input),
slab-password-field:state(readonly)::part(textarea) {
  background-color: var(--chalk);
}

slab-password-field:state(error)::part(input),
slab-password-field:state(error)::part(textarea) {
  border-color: var(--ember-deep);
}

/*
  Spaced by what's in it rather than by a gap, so a description with
  nothing in it takes no room, and a control beside the field lines up
  with its bottom edge.
*/
slab-password-field::part(description) {
  color: var(--ash);
  display: grid;
  font-size: 0.875rem;
}

slab-password-field > [slot='description'] {
  margin-block-start: 0.375rem;
}

slab-password-field::part(validity-message) {
  color: var(--ember-deep);
  font-weight: 600;
  margin-block-start: 0.375rem;
}

slab-password-field::part(validity-message)::before {
  content: '■ ' / '';
}

@media (prefers-reduced-motion: reduce) {
  slab-password-field::part(input),
  slab-password-field::part(textarea) {
    transition: none;
  }
}

slab-password-field::part(control) {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

/*
  Butted against the input, sharing its border, at the same fixed height.
  Pressed, it's filled in, so the state shows without the text changing.
*/
slab-password-field::part(toggle-button) {
  background-color: var(--paper);
  block-size: 3rem;
  border: var(--border);
  border-radius: 0;
  color: inherit;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  letter-spacing: 0.06em;
  margin-inline-start: -2px;
  padding: 0 0.875rem;
  text-transform: uppercase;
  transition:
    background-color 100ms,
    box-shadow 100ms,
    color 100ms,
    translate 100ms;
}

slab-password-field::part(toggle-button):hover {
  background-color: var(--chalk);
}

/*
  The outline is transparent rather than gone, so it still shows in forced
  colors, where box shadows don't.
*/
slab-password-field::part(toggle-button):focus-visible {
  box-shadow: 4px 4px 0 var(--ember);
  outline: 2px solid transparent;
  translate: -2px -2px;
}

slab-password-field::part(toggle-button):disabled {
  background-color: var(--chalk);
  border-style: dashed;
  color: var(--ash);
  cursor: not-allowed;
}

slab-password-field:state(revealed)::part(toggle-button) {
  background-color: var(--ink);
  color: var(--paper);
}

slab-password-field:state(error)::part(toggle-button) {
  border-color: var(--ember-deep);
}

@media (prefers-reduced-motion: reduce) {
  slab-password-field::part(toggle-button) {
    transition: none;
  }
}
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.

password-field.ts
import { customElement } from 'lit/decorators.js';
import PasswordField from 'slab-elements/password-field.js';
import styles from './password-field.styles.js';
import textInput from './text-input.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-password-field': StyledPasswordField;
  }
}

/**
 * Password Field, styled: Text Field's input with its show button butted
 * against it, filled in with ink while the password shows.
 */
@customElement('slab-styled-password-field')
export default class StyledPasswordField extends PasswordField {
  static override styles = [tokens, textInput, styles];
}
password-field.styles.ts
import { css } from 'lit';

export default css`
  [part~='control'] {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
  }

  /*
    Butted against the input, sharing its border, at the same fixed height.
    Pressed, it's filled in, so the state shows without the text changing.
  */
  [part~='toggle-button'] {
    background-color: var(--paper);
    block-size: 3rem;
    border: var(--border);
    border-radius: 0;
    color: inherit;
    cursor: pointer;
    font-family: var(--font-mono);
    font-size: 0.8125rem;
    letter-spacing: 0.06em;
    margin-inline-start: -2px;
    padding: 0 0.875rem;
    text-transform: uppercase;
    transition:
      background-color 100ms,
      box-shadow 100ms,
      color 100ms,
      translate 100ms;

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

    /*
      The outline is transparent rather than gone, so it still shows in forced
      colors, where box shadows don't.
    */
    &:focus-visible {
      box-shadow: 4px 4px 0 var(--ember);
      outline: 2px solid transparent;
      translate: -2px -2px;
    }

    &:disabled {
      background-color: var(--chalk);
      border-style: dashed;
      color: var(--ash);
      cursor: not-allowed;
    }
  }

  :host(:state(revealed)) [part~='toggle-button'] {
    background-color: var(--ink);
    color: var(--paper);
  }

  :host(:state(error)) [part~='toggle-button'] {
    border-color: var(--ember-deep);
  }

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

/*
  The look shared by everything typed into: Text Field, Password Field,
  Search Field, Textarea, Number, Date Field, Time Field, Combobox and Tags
  Input, whose fields are "::part(input)", or "::part(textarea)" for Textarea.
  File Drop starts from it too, for its label and description.
*/
export default css`
  :host {
    color: var(--ink);
    display: grid;
    font-family: var(--font-body);
  }

  [part~='label'] {
    font-family: var(--font-display);
    font-size: 1.0625rem;
    font-weight: 600;
    margin-block-end: 0.375rem;
  }

  :host(:state(required)) [part~='label']::after {
    color: var(--ember-deep);
    content: ' *' / '';
  }

  [part~='input'],
  [part~='textarea'] {
    background-color: var(--paper);
    block-size: 3rem;
    border: var(--border);
    border-radius: 0;
    box-sizing: border-box;
    color: inherit;
    font: inherit;

    /* A fixed height, shared with Select, so fields line up in a row. */
    padding: 0 0.75rem;
    transition:
      background-color 100ms,
      border-color 100ms,
      box-shadow 100ms,
      translate 100ms;

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

    /*
      The outline is transparent rather than gone, so it still shows in forced
      colors, where box shadows don't.
    */
    &:focus-visible {
      box-shadow: 4px 4px 0 var(--ember);
      outline: 2px solid transparent;
      translate: -2px -2px;
    }

    &:disabled {
      background-color: var(--chalk);
      border-style: dashed;
      color: var(--ash);
      cursor: not-allowed;
    }
  }

  [part~='textarea'] {
    block-size: auto;
    min-block-size: 7rem;
    padding: 0.625rem 0.75rem;
    resize: vertical;
  }

  :host(:state(readonly)) [part~='input'],
  :host(:state(readonly)) [part~='textarea'] {
    background-color: var(--chalk);
  }

  :host(:state(error)) [part~='input'],
  :host(:state(error)) [part~='textarea'] {
    border-color: var(--ember-deep);
  }

  /*
    Spaced by what's in it rather than by a gap, so a description with
    nothing in it takes no room, and a control beside the field lines up
    with its bottom edge.
  */
  [part~='description'] {
    color: var(--ash);
    display: grid;
    font-size: 0.875rem;
  }

  ::slotted([slot='description']) {
    margin-block-start: 0.375rem;
  }

  [part~='validity-message'] {
    color: var(--ember-deep);
    font-weight: 600;
    margin-block-start: 0.375rem;

    &::before {
      content: '■ ' / '';
    }
  }

  @media (prefers-reduced-motion: reduce) {
    [part~='input'],
    [part~='textarea'] {
      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);
  }

  /*
    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-password-field> 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.

PasswordField.vue
<script setup>
import 'slab-elements/password-field.js';
import './password-field.css';
</script>

<template>
  <slab-password-field><slot /></slab-password-field>
</template>
password-field.css
slab-password-field {
  color: var(--ink);
  display: grid;
  font-family: var(--font-body);
}

slab-password-field::part(label) {
  font-family: var(--font-display);
  font-size: 1.0625rem;
  font-weight: 600;
  margin-block-end: 0.375rem;
}

slab-password-field:state(required)::part(label)::after {
  color: var(--ember-deep);
  content: ' *' / '';
}

slab-password-field::part(input),
slab-password-field::part(textarea) {
  background-color: var(--paper);
  block-size: 3rem;
  border: var(--border);
  border-radius: 0;
  box-sizing: border-box;
  color: inherit;
  font: inherit;
  /* A fixed height, shared with Select, so fields line up in a row. */
  padding: 0 0.75rem;
  transition:
    background-color 100ms,
    border-color 100ms,
    box-shadow 100ms,
    translate 100ms;
}

slab-password-field::part(input)::placeholder,
slab-password-field::part(textarea)::placeholder {
  color: var(--ash);
}

/*
  The outline is transparent rather than gone, so it still shows in forced
  colors, where box shadows don't.
*/
slab-password-field::part(input):focus-visible,
slab-password-field::part(textarea):focus-visible {
  box-shadow: 4px 4px 0 var(--ember);
  outline: 2px solid transparent;
  translate: -2px -2px;
}

slab-password-field::part(input):disabled,
slab-password-field::part(textarea):disabled {
  background-color: var(--chalk);
  border-style: dashed;
  color: var(--ash);
  cursor: not-allowed;
}

slab-password-field::part(textarea) {
  block-size: auto;
  min-block-size: 7rem;
  padding: 0.625rem 0.75rem;
  resize: vertical;
}

slab-password-field:state(readonly)::part(input),
slab-password-field:state(readonly)::part(textarea) {
  background-color: var(--chalk);
}

slab-password-field:state(error)::part(input),
slab-password-field:state(error)::part(textarea) {
  border-color: var(--ember-deep);
}

/*
  Spaced by what's in it rather than by a gap, so a description with
  nothing in it takes no room, and a control beside the field lines up
  with its bottom edge.
*/
slab-password-field::part(description) {
  color: var(--ash);
  display: grid;
  font-size: 0.875rem;
}

slab-password-field > [slot='description'] {
  margin-block-start: 0.375rem;
}

slab-password-field::part(validity-message) {
  color: var(--ember-deep);
  font-weight: 600;
  margin-block-start: 0.375rem;
}

slab-password-field::part(validity-message)::before {
  content: '■ ' / '';
}

@media (prefers-reduced-motion: reduce) {
  slab-password-field::part(input),
  slab-password-field::part(textarea) {
    transition: none;
  }
}

slab-password-field::part(control) {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

/*
  Butted against the input, sharing its border, at the same fixed height.
  Pressed, it's filled in, so the state shows without the text changing.
*/
slab-password-field::part(toggle-button) {
  background-color: var(--paper);
  block-size: 3rem;
  border: var(--border);
  border-radius: 0;
  color: inherit;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  letter-spacing: 0.06em;
  margin-inline-start: -2px;
  padding: 0 0.875rem;
  text-transform: uppercase;
  transition:
    background-color 100ms,
    box-shadow 100ms,
    color 100ms,
    translate 100ms;
}

slab-password-field::part(toggle-button):hover {
  background-color: var(--chalk);
}

/*
  The outline is transparent rather than gone, so it still shows in forced
  colors, where box shadows don't.
*/
slab-password-field::part(toggle-button):focus-visible {
  box-shadow: 4px 4px 0 var(--ember);
  outline: 2px solid transparent;
  translate: -2px -2px;
}

slab-password-field::part(toggle-button):disabled {
  background-color: var(--chalk);
  border-style: dashed;
  color: var(--ash);
  cursor: not-allowed;
}

slab-password-field:state(revealed)::part(toggle-button) {
  background-color: var(--ink);
  color: var(--paper);
}

slab-password-field:state(error)::part(toggle-button) {
  border-color: var(--ember-deep);
}

@media (prefers-reduced-motion: reduce) {
  slab-password-field::part(toggle-button) {
    transition: none;
  }
}
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-password-field> on the page. Import tokens.css, the colors and fonts every styled example shares, once, wherever your global styles go.

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

password-field.js
import 'slab-elements/password-field.js';
import './password-field.css';
password-field.css
slab-password-field {
  color: var(--ink);
  display: grid;
  font-family: var(--font-body);
}

slab-password-field::part(label) {
  font-family: var(--font-display);
  font-size: 1.0625rem;
  font-weight: 600;
  margin-block-end: 0.375rem;
}

slab-password-field:state(required)::part(label)::after {
  color: var(--ember-deep);
  content: ' *' / '';
}

slab-password-field::part(input),
slab-password-field::part(textarea) {
  background-color: var(--paper);
  block-size: 3rem;
  border: var(--border);
  border-radius: 0;
  box-sizing: border-box;
  color: inherit;
  font: inherit;
  /* A fixed height, shared with Select, so fields line up in a row. */
  padding: 0 0.75rem;
  transition:
    background-color 100ms,
    border-color 100ms,
    box-shadow 100ms,
    translate 100ms;
}

slab-password-field::part(input)::placeholder,
slab-password-field::part(textarea)::placeholder {
  color: var(--ash);
}

/*
  The outline is transparent rather than gone, so it still shows in forced
  colors, where box shadows don't.
*/
slab-password-field::part(input):focus-visible,
slab-password-field::part(textarea):focus-visible {
  box-shadow: 4px 4px 0 var(--ember);
  outline: 2px solid transparent;
  translate: -2px -2px;
}

slab-password-field::part(input):disabled,
slab-password-field::part(textarea):disabled {
  background-color: var(--chalk);
  border-style: dashed;
  color: var(--ash);
  cursor: not-allowed;
}

slab-password-field::part(textarea) {
  block-size: auto;
  min-block-size: 7rem;
  padding: 0.625rem 0.75rem;
  resize: vertical;
}

slab-password-field:state(readonly)::part(input),
slab-password-field:state(readonly)::part(textarea) {
  background-color: var(--chalk);
}

slab-password-field:state(error)::part(input),
slab-password-field:state(error)::part(textarea) {
  border-color: var(--ember-deep);
}

/*
  Spaced by what's in it rather than by a gap, so a description with
  nothing in it takes no room, and a control beside the field lines up
  with its bottom edge.
*/
slab-password-field::part(description) {
  color: var(--ash);
  display: grid;
  font-size: 0.875rem;
}

slab-password-field > [slot='description'] {
  margin-block-start: 0.375rem;
}

slab-password-field::part(validity-message) {
  color: var(--ember-deep);
  font-weight: 600;
  margin-block-start: 0.375rem;
}

slab-password-field::part(validity-message)::before {
  content: '■ ' / '';
}

@media (prefers-reduced-motion: reduce) {
  slab-password-field::part(input),
  slab-password-field::part(textarea) {
    transition: none;
  }
}

slab-password-field::part(control) {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

/*
  Butted against the input, sharing its border, at the same fixed height.
  Pressed, it's filled in, so the state shows without the text changing.
*/
slab-password-field::part(toggle-button) {
  background-color: var(--paper);
  block-size: 3rem;
  border: var(--border);
  border-radius: 0;
  color: inherit;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  letter-spacing: 0.06em;
  margin-inline-start: -2px;
  padding: 0 0.875rem;
  text-transform: uppercase;
  transition:
    background-color 100ms,
    box-shadow 100ms,
    color 100ms,
    translate 100ms;
}

slab-password-field::part(toggle-button):hover {
  background-color: var(--chalk);
}

/*
  The outline is transparent rather than gone, so it still shows in forced
  colors, where box shadows don't.
*/
slab-password-field::part(toggle-button):focus-visible {
  box-shadow: 4px 4px 0 var(--ember);
  outline: 2px solid transparent;
  translate: -2px -2px;
}

slab-password-field::part(toggle-button):disabled {
  background-color: var(--chalk);
  border-style: dashed;
  color: var(--ash);
  cursor: not-allowed;
}

slab-password-field:state(revealed)::part(toggle-button) {
  background-color: var(--ink);
  color: var(--paper);
}

slab-password-field:state(error)::part(toggle-button) {
  border-color: var(--ember-deep);
}

@media (prefers-reduced-motion: reduce) {
  slab-password-field::part(toggle-button) {
    transition: none;
  }
}
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-password-field> 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.

PasswordField.gjs
import 'slab-elements/password-field.js';
import './password-field.css';

export const PasswordField = <template>
  <slab-password-field ...attributes>{{yield}}</slab-password-field>
</template>;
password-field.css
slab-password-field {
  color: var(--ink);
  display: grid;
  font-family: var(--font-body);
}

slab-password-field::part(label) {
  font-family: var(--font-display);
  font-size: 1.0625rem;
  font-weight: 600;
  margin-block-end: 0.375rem;
}

slab-password-field:state(required)::part(label)::after {
  color: var(--ember-deep);
  content: ' *' / '';
}

slab-password-field::part(input),
slab-password-field::part(textarea) {
  background-color: var(--paper);
  block-size: 3rem;
  border: var(--border);
  border-radius: 0;
  box-sizing: border-box;
  color: inherit;
  font: inherit;
  /* A fixed height, shared with Select, so fields line up in a row. */
  padding: 0 0.75rem;
  transition:
    background-color 100ms,
    border-color 100ms,
    box-shadow 100ms,
    translate 100ms;
}

slab-password-field::part(input)::placeholder,
slab-password-field::part(textarea)::placeholder {
  color: var(--ash);
}

/*
  The outline is transparent rather than gone, so it still shows in forced
  colors, where box shadows don't.
*/
slab-password-field::part(input):focus-visible,
slab-password-field::part(textarea):focus-visible {
  box-shadow: 4px 4px 0 var(--ember);
  outline: 2px solid transparent;
  translate: -2px -2px;
}

slab-password-field::part(input):disabled,
slab-password-field::part(textarea):disabled {
  background-color: var(--chalk);
  border-style: dashed;
  color: var(--ash);
  cursor: not-allowed;
}

slab-password-field::part(textarea) {
  block-size: auto;
  min-block-size: 7rem;
  padding: 0.625rem 0.75rem;
  resize: vertical;
}

slab-password-field:state(readonly)::part(input),
slab-password-field:state(readonly)::part(textarea) {
  background-color: var(--chalk);
}

slab-password-field:state(error)::part(input),
slab-password-field:state(error)::part(textarea) {
  border-color: var(--ember-deep);
}

/*
  Spaced by what's in it rather than by a gap, so a description with
  nothing in it takes no room, and a control beside the field lines up
  with its bottom edge.
*/
slab-password-field::part(description) {
  color: var(--ash);
  display: grid;
  font-size: 0.875rem;
}

slab-password-field > [slot='description'] {
  margin-block-start: 0.375rem;
}

slab-password-field::part(validity-message) {
  color: var(--ember-deep);
  font-weight: 600;
  margin-block-start: 0.375rem;
}

slab-password-field::part(validity-message)::before {
  content: '■ ' / '';
}

@media (prefers-reduced-motion: reduce) {
  slab-password-field::part(input),
  slab-password-field::part(textarea) {
    transition: none;
  }
}

slab-password-field::part(control) {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

/*
  Butted against the input, sharing its border, at the same fixed height.
  Pressed, it's filled in, so the state shows without the text changing.
*/
slab-password-field::part(toggle-button) {
  background-color: var(--paper);
  block-size: 3rem;
  border: var(--border);
  border-radius: 0;
  color: inherit;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  letter-spacing: 0.06em;
  margin-inline-start: -2px;
  padding: 0 0.875rem;
  text-transform: uppercase;
  transition:
    background-color 100ms,
    box-shadow 100ms,
    color 100ms,
    translate 100ms;
}

slab-password-field::part(toggle-button):hover {
  background-color: var(--chalk);
}

/*
  The outline is transparent rather than gone, so it still shows in forced
  colors, where box shadows don't.
*/
slab-password-field::part(toggle-button):focus-visible {
  box-shadow: 4px 4px 0 var(--ember);
  outline: 2px solid transparent;
  translate: -2px -2px;
}

slab-password-field::part(toggle-button):disabled {
  background-color: var(--chalk);
  border-style: dashed;
  color: var(--ash);
  cursor: not-allowed;
}

slab-password-field:state(revealed)::part(toggle-button) {
  background-color: var(--ink);
  color: var(--paper);
}

slab-password-field:state(error)::part(toggle-button) {
  border-color: var(--ember-deep);
}

@media (prefers-reduced-motion: reduce) {
  slab-password-field::part(toggle-button) {
    transition: none;
  }
}
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;
  }
}