Form
Checkbox
A checkbox built on the native one, with an indeterminate state and real form validation.
- Element
<slab-checkbox>- Built on
<input type=checkbox>
Example
Here's a styled version, <slab-styled-checkbox>. 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.
<div class="stack">
<slab-styled-checkbox label="Call 811 before digging" checked></slab-styled-checkbox>
<slab-styled-checkbox label="Order the pump truck"></slab-styled-checkbox>
<slab-styled-checkbox label="Some of the crew is confirmed" data-indeterminate></slab-styled-checkbox>
</div>
<div class="stack">
<slab-checkbox label="Call 811 before digging" checked></slab-checkbox>
<slab-checkbox label="Order the pump truck"></slab-checkbox>
<slab-checkbox label="Some of the crew is confirmed" data-indeterminate></slab-checkbox>
</div>
Usage
pnpm add slab-elements lit
import 'slab-elements/checkbox.js';
export function MyCheckbox() {
return (
<slab-checkbox label="I agree to the terms" name="terms" required></slab-checkbox>
);
}
MyCheckbox.jsx React 19 or later, which sets props on custom elements as properties.
import 'slab-elements/checkbox.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-checkbox')
export class MyCheckbox extends LitElement {
render() {
return html`
<slab-checkbox label="I agree to the terms" name="terms" required></slab-checkbox>
`;
}
}
my-checkbox.ts Slab is built on Lit, so its elements are at home in Lit's templates.
<script setup>
import 'slab-elements/checkbox.js';
</script>
<template>
<slab-checkbox label="I agree to the terms" name="terms" required></slab-checkbox>
</template>
MyCheckbox.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.
<script>
import 'slab-elements/checkbox.js';
</script>
<slab-checkbox label="I agree to the terms" name="terms" required></slab-checkbox>
MyCheckbox.svelte Svelte binds properties and events to custom elements like any other element.
import 'slab-elements/checkbox.js';
export const MyCheckbox = <template>
<slab-checkbox label="I agree to the terms" name="terms" required></slab-checkbox>
</template>;
MyCheckbox.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/checkbox.js';
</script>
<slab-checkbox label="I agree to the terms" name="terms" required></slab-checkbox>
index.html Any bundler (or an import map) takes care of the imports.
Keyboard
| Key | What it does |
|---|---|
| Space | Checks or unchecks it. |
Behavior
A checkbox with no visual styles, around a native one, so it is checked, focused, and read the way one is.
The label and the checkbox 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 checkbox.
Style it through its parts and through :state(checked), :state(disabled), :state(error), :state(indeterminate), and :state(required). Nothing is drawn for a check or a dash: those are marks a design makes, from ::part(checkbox). Where the label sits relative to the checkbox is layout, so it's left to the styles around it.
API
<slab-checkbox>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
Names it. It's the accessible name, and the visible label where there is one. | |
checked
|
boolean |
false |
Like a native checkbox, the attribute is the default state and the property is the current one, so the attribute doesn't change as the checkbox is checked. Style :state(checked) rather than [checked]. |
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. |
name
|
string |
'' |
The name its value is submitted under, as a native control's name. |
required
|
boolean |
false |
Makes it invalid while it has no value, as required does a native control. |
value
|
string |
'' |
An empty value is submitted as nothing, and counts for nothing in a Checkbox Group's value. |
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. |
| Name | Type | Description |
|---|---|---|
indeterminate
|
boolean |
Shows it as neither checked nor unchecked, as a native checkbox's indeterminate does. Checking or unchecking it clears it. |
defaultChecked
|
boolean |
Whether it starts checked, and is checked again when its form resets. It's the checked attribute, as a native input's defaultChecked is, so setting it sets the attribute, and checked with it. |
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. |
| 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. |
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. |
setValidity(flags?: ValidityStateFlags, message?: string): void
|
Sets its validity and message, as ElementInternals.setValidity() does, for checks of your own beyond setCustomValidity(). |
| 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. |
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. |
| 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. |
| Name | Description |
|---|---|
checkbox
|
Where a design draws its check or dash |
description
|
Text about the checkbox, and its validity message when it has one |
input
|
The input |
label
|
The label |
validity-message
|
The validity message, which says why the value is invalid |
| Name | Description |
|---|---|
:state(checked)
|
While it's checked. |
: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(indeterminate)
|
While it's indeterminate, neither checked nor unchecked. |
:state(required)
|
While it's required, for styles such as an asterisk. |
Copy and own it
<slab-styled-checkbox> 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.
The same styles, written from outside the element, so
there's nothing to subclass. The CSS styles every
<slab-checkbox> 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.
Checkbox.jsx
import 'slab-elements/checkbox.js';
import './checkbox.css';
export function Checkbox(props) {
return <slab-checkbox {...props} />;
}
checkbox.css
slab-checkbox {
align-items: center;
color: var(--ink);
column-gap: 0.75rem;
display: grid;
font-family: var(--font-body);
grid-template-columns: auto 1fr;
}
slab-checkbox:state(disabled) {
color: var(--ash);
}
slab-checkbox::part(checkbox) {
display: grid;
position: relative;
}
/*
24 pixels square, the smallest target WCAG 2.5.8 allows without counting
the space around it.
*/
slab-checkbox::part(input) {
appearance: none;
background-color: var(--paper);
block-size: 1.5rem;
border: var(--border);
cursor: pointer;
inline-size: 1.5rem;
margin: 0;
transition:
background-color 100ms,
box-shadow 100ms;
}
slab-checkbox::part(input):hover {
box-shadow: 2px 2px 0 var(--ink);
}
slab-checkbox::part(input):focus-visible {
outline: 3px solid var(--ember);
outline-offset: 2px;
}
slab-checkbox::part(input):disabled {
border-style: dashed;
box-shadow: none;
cursor: not-allowed;
opacity: 0.5;
}
slab-checkbox:state(checked)::part(input),
slab-checkbox:state(indeterminate)::part(input) {
background-color: var(--ember);
}
slab-checkbox:state(error)::part(input) {
border-color: var(--ember-deep);
box-shadow: 2px 2px 0 var(--ember-deep);
}
/*
The mark is a shape cut from paper with a mask, so it takes the theme's
color. It's drawn on rather than shown: a clip slides off it from left to
right, the way Glide Core's stroke draws on. Unchecking takes it away at
once. It doesn't take clicks, so the input under it still does.
*/
slab-checkbox::part(checkbox)::after {
background-color: var(--paper);
clip-path: inset(0 100% 0 0);
content: '';
inset: 2px;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 8.25l3.25 3.25L13 4.5' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E")
center / contain no-repeat;
pointer-events: none;
position: absolute;
}
slab-checkbox:state(checked)::part(checkbox)::after,
slab-checkbox:state(indeterminate)::part(checkbox)::after {
clip-path: inset(0);
transition: clip-path 300ms cubic-bezier(0.32, 0, 0.67, 0);
}
slab-checkbox:state(indeterminate)::part(checkbox)::after {
mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8h9' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E");
}
slab-checkbox::part(label) {
cursor: pointer;
}
slab-checkbox:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
slab-checkbox::part(description) {
color: var(--ash);
font-size: 0.875rem;
grid-column: 2;
}
slab-checkbox::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
}
@media (prefers-reduced-motion: reduce) {
slab-checkbox::part(input),
slab-checkbox:state(checked)::part(checkbox)::after,
slab-checkbox:state(indeterminate)::part(checkbox)::after {
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.
checkbox.ts
import { customElement } from 'lit/decorators.js';
import Checkbox from 'slab-elements/checkbox.js';
import styles from './checkbox.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-checkbox': StyledCheckbox;
}
}
/**
* Checkbox, styled: a square box that fills with ember when checked, with a
* check or a dash drawn on it in paper.
*/
@customElement('slab-styled-checkbox')
export default class StyledCheckbox extends Checkbox {
static override styles = [tokens, styles];
}
checkbox.styles.ts
import { css } from 'lit';
export default css`
:host {
align-items: center;
color: var(--ink);
column-gap: 0.75rem;
display: grid;
font-family: var(--font-body);
grid-template-columns: auto 1fr;
}
:host(:state(disabled)) {
color: var(--ash);
}
[part~='checkbox'] {
display: grid;
position: relative;
}
/*
24 pixels square, the smallest target WCAG 2.5.8 allows without counting
the space around it.
*/
[part~='input'] {
appearance: none;
background-color: var(--paper);
block-size: 1.5rem;
border: var(--border);
cursor: pointer;
inline-size: 1.5rem;
margin: 0;
transition:
background-color 100ms,
box-shadow 100ms;
&:hover {
box-shadow: 2px 2px 0 var(--ink);
}
&:focus-visible {
outline: 3px solid var(--ember);
outline-offset: 2px;
}
&:disabled {
border-style: dashed;
box-shadow: none;
cursor: not-allowed;
opacity: 0.5;
}
}
:host(:state(checked)) [part~='input'],
:host(:state(indeterminate)) [part~='input'] {
background-color: var(--ember);
}
:host(:state(error)) [part~='input'] {
border-color: var(--ember-deep);
box-shadow: 2px 2px 0 var(--ember-deep);
}
/*
The mark is a shape cut from paper with a mask, so it takes the theme's
color. It's drawn on rather than shown: a clip slides off it from left to
right, the way Glide Core's stroke draws on. Unchecking takes it away at
once. It doesn't take clicks, so the input under it still does.
*/
[part~='checkbox']::after {
background-color: var(--paper);
clip-path: inset(0 100% 0 0);
content: '';
inset: 2px;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 8.25l3.25 3.25L13 4.5' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E")
center / contain no-repeat;
pointer-events: none;
position: absolute;
}
:host(:state(checked)) [part~='checkbox']::after,
:host(:state(indeterminate)) [part~='checkbox']::after {
clip-path: inset(0);
transition: clip-path 300ms cubic-bezier(0.32, 0, 0.67, 0);
}
:host(:state(indeterminate)) [part~='checkbox']::after {
mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8h9' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E");
}
[part~='label'] {
cursor: pointer;
}
:host(:state(required)) [part~='label']::after {
color: var(--ember-deep);
content: ' *' / '';
}
[part~='description'] {
color: var(--ash);
font-size: 0.875rem;
grid-column: 2;
}
[part~='validity-message'] {
color: var(--ember-deep);
font-weight: 600;
}
@media (prefers-reduced-motion: reduce) {
[part~='input'],
:host(:state(checked)) [part~='checkbox']::after,
:host(:state(indeterminate)) [part~='checkbox']::after {
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-checkbox> 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.
Checkbox.vue
<script setup>
import 'slab-elements/checkbox.js';
import './checkbox.css';
</script>
<template>
<slab-checkbox><slot /></slab-checkbox>
</template>
checkbox.css
slab-checkbox {
align-items: center;
color: var(--ink);
column-gap: 0.75rem;
display: grid;
font-family: var(--font-body);
grid-template-columns: auto 1fr;
}
slab-checkbox:state(disabled) {
color: var(--ash);
}
slab-checkbox::part(checkbox) {
display: grid;
position: relative;
}
/*
24 pixels square, the smallest target WCAG 2.5.8 allows without counting
the space around it.
*/
slab-checkbox::part(input) {
appearance: none;
background-color: var(--paper);
block-size: 1.5rem;
border: var(--border);
cursor: pointer;
inline-size: 1.5rem;
margin: 0;
transition:
background-color 100ms,
box-shadow 100ms;
}
slab-checkbox::part(input):hover {
box-shadow: 2px 2px 0 var(--ink);
}
slab-checkbox::part(input):focus-visible {
outline: 3px solid var(--ember);
outline-offset: 2px;
}
slab-checkbox::part(input):disabled {
border-style: dashed;
box-shadow: none;
cursor: not-allowed;
opacity: 0.5;
}
slab-checkbox:state(checked)::part(input),
slab-checkbox:state(indeterminate)::part(input) {
background-color: var(--ember);
}
slab-checkbox:state(error)::part(input) {
border-color: var(--ember-deep);
box-shadow: 2px 2px 0 var(--ember-deep);
}
/*
The mark is a shape cut from paper with a mask, so it takes the theme's
color. It's drawn on rather than shown: a clip slides off it from left to
right, the way Glide Core's stroke draws on. Unchecking takes it away at
once. It doesn't take clicks, so the input under it still does.
*/
slab-checkbox::part(checkbox)::after {
background-color: var(--paper);
clip-path: inset(0 100% 0 0);
content: '';
inset: 2px;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 8.25l3.25 3.25L13 4.5' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E")
center / contain no-repeat;
pointer-events: none;
position: absolute;
}
slab-checkbox:state(checked)::part(checkbox)::after,
slab-checkbox:state(indeterminate)::part(checkbox)::after {
clip-path: inset(0);
transition: clip-path 300ms cubic-bezier(0.32, 0, 0.67, 0);
}
slab-checkbox:state(indeterminate)::part(checkbox)::after {
mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8h9' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E");
}
slab-checkbox::part(label) {
cursor: pointer;
}
slab-checkbox:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
slab-checkbox::part(description) {
color: var(--ash);
font-size: 0.875rem;
grid-column: 2;
}
slab-checkbox::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
}
@media (prefers-reduced-motion: reduce) {
slab-checkbox::part(input),
slab-checkbox:state(checked)::part(checkbox)::after,
slab-checkbox:state(indeterminate)::part(checkbox)::after {
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-checkbox> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Import checkbox.js once, such
as in your root layout, and use
<slab-checkbox> as is. A Svelte component
around it would take slot="…" content as its
own slot and drop it.
checkbox.js
import 'slab-elements/checkbox.js';
import './checkbox.css';
checkbox.css
slab-checkbox {
align-items: center;
color: var(--ink);
column-gap: 0.75rem;
display: grid;
font-family: var(--font-body);
grid-template-columns: auto 1fr;
}
slab-checkbox:state(disabled) {
color: var(--ash);
}
slab-checkbox::part(checkbox) {
display: grid;
position: relative;
}
/*
24 pixels square, the smallest target WCAG 2.5.8 allows without counting
the space around it.
*/
slab-checkbox::part(input) {
appearance: none;
background-color: var(--paper);
block-size: 1.5rem;
border: var(--border);
cursor: pointer;
inline-size: 1.5rem;
margin: 0;
transition:
background-color 100ms,
box-shadow 100ms;
}
slab-checkbox::part(input):hover {
box-shadow: 2px 2px 0 var(--ink);
}
slab-checkbox::part(input):focus-visible {
outline: 3px solid var(--ember);
outline-offset: 2px;
}
slab-checkbox::part(input):disabled {
border-style: dashed;
box-shadow: none;
cursor: not-allowed;
opacity: 0.5;
}
slab-checkbox:state(checked)::part(input),
slab-checkbox:state(indeterminate)::part(input) {
background-color: var(--ember);
}
slab-checkbox:state(error)::part(input) {
border-color: var(--ember-deep);
box-shadow: 2px 2px 0 var(--ember-deep);
}
/*
The mark is a shape cut from paper with a mask, so it takes the theme's
color. It's drawn on rather than shown: a clip slides off it from left to
right, the way Glide Core's stroke draws on. Unchecking takes it away at
once. It doesn't take clicks, so the input under it still does.
*/
slab-checkbox::part(checkbox)::after {
background-color: var(--paper);
clip-path: inset(0 100% 0 0);
content: '';
inset: 2px;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 8.25l3.25 3.25L13 4.5' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E")
center / contain no-repeat;
pointer-events: none;
position: absolute;
}
slab-checkbox:state(checked)::part(checkbox)::after,
slab-checkbox:state(indeterminate)::part(checkbox)::after {
clip-path: inset(0);
transition: clip-path 300ms cubic-bezier(0.32, 0, 0.67, 0);
}
slab-checkbox:state(indeterminate)::part(checkbox)::after {
mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8h9' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E");
}
slab-checkbox::part(label) {
cursor: pointer;
}
slab-checkbox:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
slab-checkbox::part(description) {
color: var(--ash);
font-size: 0.875rem;
grid-column: 2;
}
slab-checkbox::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
}
@media (prefers-reduced-motion: reduce) {
slab-checkbox::part(input),
slab-checkbox:state(checked)::part(checkbox)::after,
slab-checkbox:state(indeterminate)::part(checkbox)::after {
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-checkbox> 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.
Checkbox.gjs
import 'slab-elements/checkbox.js';
import './checkbox.css';
export const Checkbox = <template>
<slab-checkbox ...attributes>{{yield}}</slab-checkbox>
</template>;
checkbox.css
slab-checkbox {
align-items: center;
color: var(--ink);
column-gap: 0.75rem;
display: grid;
font-family: var(--font-body);
grid-template-columns: auto 1fr;
}
slab-checkbox:state(disabled) {
color: var(--ash);
}
slab-checkbox::part(checkbox) {
display: grid;
position: relative;
}
/*
24 pixels square, the smallest target WCAG 2.5.8 allows without counting
the space around it.
*/
slab-checkbox::part(input) {
appearance: none;
background-color: var(--paper);
block-size: 1.5rem;
border: var(--border);
cursor: pointer;
inline-size: 1.5rem;
margin: 0;
transition:
background-color 100ms,
box-shadow 100ms;
}
slab-checkbox::part(input):hover {
box-shadow: 2px 2px 0 var(--ink);
}
slab-checkbox::part(input):focus-visible {
outline: 3px solid var(--ember);
outline-offset: 2px;
}
slab-checkbox::part(input):disabled {
border-style: dashed;
box-shadow: none;
cursor: not-allowed;
opacity: 0.5;
}
slab-checkbox:state(checked)::part(input),
slab-checkbox:state(indeterminate)::part(input) {
background-color: var(--ember);
}
slab-checkbox:state(error)::part(input) {
border-color: var(--ember-deep);
box-shadow: 2px 2px 0 var(--ember-deep);
}
/*
The mark is a shape cut from paper with a mask, so it takes the theme's
color. It's drawn on rather than shown: a clip slides off it from left to
right, the way Glide Core's stroke draws on. Unchecking takes it away at
once. It doesn't take clicks, so the input under it still does.
*/
slab-checkbox::part(checkbox)::after {
background-color: var(--paper);
clip-path: inset(0 100% 0 0);
content: '';
inset: 2px;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 8.25l3.25 3.25L13 4.5' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E")
center / contain no-repeat;
pointer-events: none;
position: absolute;
}
slab-checkbox:state(checked)::part(checkbox)::after,
slab-checkbox:state(indeterminate)::part(checkbox)::after {
clip-path: inset(0);
transition: clip-path 300ms cubic-bezier(0.32, 0, 0.67, 0);
}
slab-checkbox:state(indeterminate)::part(checkbox)::after {
mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8h9' fill='none' stroke='black' stroke-width='2.5'/%3E%3C/svg%3E");
}
slab-checkbox::part(label) {
cursor: pointer;
}
slab-checkbox:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
slab-checkbox::part(description) {
color: var(--ash);
font-size: 0.875rem;
grid-column: 2;
}
slab-checkbox::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
}
@media (prefers-reduced-motion: reduce) {
slab-checkbox::part(input),
slab-checkbox:state(checked)::part(checkbox)::after,
slab-checkbox:state(indeterminate)::part(checkbox)::after {
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;
}
}