Form
Editable
Text that is edited in place, such as a title or a name: a button showing the value that becomes an input when pressed.
- Element
<slab-editable>- Built on
<button> + <input>
Example
Here's a styled version, <slab-styled-editable>. 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.
<slab-styled-editable label="Job name" value="14 Alder Lane" placeholder="Name this job">
<span slot="description">Click the name, type, and press Enter. Escape puts it back.</span>
</slab-styled-editable>
<slab-editable label="Job name" value="14 Alder Lane" placeholder="Name this job">
<span slot="description">Click the name, type, and press Enter. Escape puts it back.</span>
</slab-editable>
Usage
pnpm add slab-elements lit
import 'slab-elements/editable.js';
export function MyEditable() {
return (
<slab-editable label="Title" name="title" value="Footing pour"></slab-editable>
);
}
MyEditable.jsx React 19 or later, which sets props on custom elements as properties.
import 'slab-elements/editable.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-editable')
export class MyEditable extends LitElement {
render() {
return html`
<slab-editable label="Title" name="title" value="Footing pour"></slab-editable>
`;
}
}
my-editable.ts Slab is built on Lit, so its elements are at home in Lit's templates.
<script setup>
import 'slab-elements/editable.js';
</script>
<template>
<slab-editable label="Title" name="title" value="Footing pour"></slab-editable>
</template>
MyEditable.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.
<script>
import 'slab-elements/editable.js';
</script>
<slab-editable label="Title" name="title" value="Footing pour"></slab-editable>
MyEditable.svelte Svelte binds properties and events to custom elements like any other element.
import 'slab-elements/editable.js';
export const MyEditable = <template>
<slab-editable label="Title" name="title" value="Footing pour"></slab-editable>
</template>;
MyEditable.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/editable.js';
</script>
<slab-editable label="Title" name="title" value="Footing pour"></slab-editable>
index.html Any bundler (or an import map) takes care of the imports.
Until it’s pressed, it’s a button showing the value, named by the label and then the value, so it reads as “Job name, 14 Alder Lane”. Clicking it, or its label, starts editing, with the text selected so typing replaces it. Enter keeps what’s typed and Escape discards it, and both put focus back on the button. Moving focus away keeps it too, so a click elsewhere or a Tab doesn’t lose an edit.
value is what was last committed, and it’s what the form gets under name. It doesn’t change while the input is typed into, only when an edit is committed, and that’s when “input” and then “change” are dispatched, as a native <select> dispatches them, if the value is different. The value attribute is the default, which a form reset goes back to. edit(), commit() and cancel() do what the keys do, and editing says which is showing.
While it’s empty, the button shows placeholder, and :state(empty) styles it. A field that’s disabled while it’s being edited stops, and what was typed is discarded. For a field that’s always an input, use Text Field.
It’s never invalid on its own. For a rule of your own, such as a name that can’t be empty, call setCustomValidity() with a message from its “change” listener. It shows in ::part(validity-message), and the host matches :state(error), once validity is reported, as on Text Field: when focus leaves, when its form is submitted, or on reportValidity().
Keyboard
| Key | What it does |
|---|---|
| Enter, Space | On the button, starts editing, with the text selected. |
| Enter | In the input, keeps what’s typed and goes back to the button. |
| Escape | In the input, discards what’s typed and goes back to the button. |
| Tab | In the input, keeps what’s typed and moves on, as a click away does. |
Behavior
Text that's edited in place, such as a title or a name: a native button showing the value, that becomes a native input when it's pressed.
The button is named by the label and the value, so it reads as "Job name, 14 Alder Lane". Clicking it, or pressing Enter or Space on it, starts editing, with the text selected. Enter commits what's typed and Escape discards it, and both put focus back on the button. Moving focus away commits it too, so a click elsewhere or a Tab doesn't lose it.
value is what was last committed, and it's what the form gets under name. It doesn't change as the input is typed into, only when an edit is committed, which is when "input" and "change" are dispatched, one after the other, as a native <select> dispatches them.
It's never invalid on its own. For a rule of your own, such as a name that can't be empty, call setCustomValidity() or setValidity(), from "change" for one that depends on the value. The validity message shows in its description, as on Text Field, once validity is reported: when focus leaves it, its form is submitted, or reportValidity() is called.
Style it through its parts and through :state(disabled), :state(editing), :state(empty) and :state(error). While it's empty, the button shows placeholder.
API
<slab-editable>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
Names it. It's the accessible name, and the visible label where there is one. | |
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. |
editing
|
boolean |
false |
Whether the input is showing rather than the button. Setting it doesn't move focus. edit() does. |
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. |
|
name
|
string |
'' |
The name its value is submitted under, as a native control's name. |
placeholder
|
string |
Shown on the button while it's empty, and in the input. | |
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 edits are committed. |
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 |
|---|---|---|
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. |
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(). 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 |
|---|---|
cancel(): void
|
Stops editing and discards what's typed. |
checkValidity(): boolean
|
Whether its value is valid. Dispatches "invalid" when it isn't, as a native control's does. |
commit(): void
|
Stops editing and keeps what's typed. |
edit(): Promise<void>
|
Starts editing and focuses the input, with its text selected. |
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 |
When an edit that changes its value is committed, just before "change", as a native <select> dispatches it. Not as it's typed, since value only changes on commit. |
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 |
|---|---|
description
|
Text about the field, and its validity message when it has one |
input
|
The input |
label
|
The label |
preview
|
The button, while not editing |
validity-message
|
The validity message, which says why the value is invalid |
| Name | Description |
|---|---|
:state(disabled)
|
While it's disabled. |
:state(editing)
|
While it's being edited. |
:state(empty)
|
While it has no value. |
:state(error)
|
While it's invalid and its validity message shows, which is once its validity is reported, as when its form is submitted. |
Copy and own it
<slab-styled-editable> 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-editable> 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.
Editable.jsx
import 'slab-elements/editable.js';
import './editable.css';
export function Editable(props) {
return <slab-editable {...props} />;
}
editable.css
slab-editable {
color: var(--ink);
display: grid;
font-family: var(--font-body);
}
slab-editable::part(label) {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
margin-block-end: 0.375rem;
}
slab-editable:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
slab-editable::part(input),
slab-editable::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-editable::part(input)::placeholder,
slab-editable::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-editable::part(input):focus-visible,
slab-editable::part(textarea):focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
slab-editable::part(input):disabled,
slab-editable::part(textarea):disabled {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
cursor: not-allowed;
}
slab-editable::part(textarea) {
block-size: auto;
min-block-size: 7rem;
padding: 0.625rem 0.75rem;
resize: vertical;
}
slab-editable:state(readonly)::part(input),
slab-editable:state(readonly)::part(textarea) {
background-color: var(--chalk);
}
slab-editable:state(error)::part(input),
slab-editable: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-editable::part(description) {
color: var(--ash);
display: grid;
font-size: 0.875rem;
}
slab-editable > [slot='description'] {
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message)::before {
content: '■ ' / '';
}
@media (prefers-reduced-motion: reduce) {
slab-editable::part(input),
slab-editable::part(textarea) {
transition: none;
}
}
slab-editable::part(preview) {
align-items: center;
background-color: transparent;
block-size: 3rem;
border: 2px dashed transparent;
border-radius: 0;
box-sizing: border-box;
color: inherit;
cursor: text;
display: flex;
font: inherit;
gap: 0.5rem;
padding: 0 0.75rem;
text-align: start;
transition:
background-color 100ms,
border-color 100ms,
box-shadow 100ms,
translate 100ms;
}
/* The pencil: what a click does. */
slab-editable::part(preview)::after {
color: var(--ash);
content: '✎' / '';
margin-inline-start: auto;
}
slab-editable::part(preview):hover {
background-color: var(--chalk);
border-color: var(--ash);
}
slab-editable::part(preview):focus-visible {
border-color: var(--ink);
border-style: solid;
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
slab-editable::part(preview):disabled {
color: var(--ash);
cursor: not-allowed;
}
slab-editable::part(preview):disabled::after {
content: none;
}
slab-editable::part(preview):disabled:hover {
background-color: transparent;
border-color: transparent;
}
@media (prefers-reduced-motion: reduce) {
slab-editable::part(preview) {
transition: none;
}
}
slab-editable:state(empty)::part(preview) {
color: var(--ash);
}
slab-editable::part(validity-message) {
color: var(--ember-deep);
display: block;
font-size: 0.875rem;
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message)::before {
content: '■ ' / '';
}
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.
editable.ts
import { customElement } from 'lit/decorators.js';
import Editable from 'slab-elements/editable.js';
import textInput from './text-input.styles.js';
import styles from './editable.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-editable': StyledEditable;
}
}
/**
* Editable, styled: Text Field's input while editing, and before that, text
* in the same box, outlined with a dashed border when it's hovered.
*/
@customElement('slab-styled-editable')
export default class StyledEditable extends Editable {
static override styles = [tokens, textInput, styles];
}
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;
}
}
`;
editable.styles.ts
import { css } from 'lit';
/*
The button takes the input's box, border and padding, with the border
transparent until it's hovered, so the text doesn't move when editing
starts.
*/
export default css`
[part~='preview'] {
align-items: center;
background-color: transparent;
block-size: 3rem;
border: 2px dashed transparent;
border-radius: 0;
box-sizing: border-box;
color: inherit;
cursor: text;
display: flex;
font: inherit;
gap: 0.5rem;
padding: 0 0.75rem;
text-align: start;
transition:
background-color 100ms,
border-color 100ms,
box-shadow 100ms,
translate 100ms;
/* The pencil: what a click does. */
&::after {
color: var(--ash);
content: '✎' / '';
margin-inline-start: auto;
}
&:hover {
background-color: var(--chalk);
border-color: var(--ash);
}
&:focus-visible {
border-color: var(--ink);
border-style: solid;
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
&:disabled {
color: var(--ash);
cursor: not-allowed;
&::after {
content: none;
}
&:hover {
background-color: transparent;
border-color: transparent;
}
}
@media (prefers-reduced-motion: reduce) {
transition: none;
}
}
:host(:state(empty)) [part~='preview'] {
color: var(--ash);
}
[part~='validity-message'] {
color: var(--ember-deep);
display: block;
font-size: 0.875rem;
font-weight: 600;
margin-block-start: 0.375rem;
&::before {
content: '■ ' / '';
}
}
`;
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-editable> 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.
Editable.vue
<script setup>
import 'slab-elements/editable.js';
import './editable.css';
</script>
<template>
<slab-editable><slot /></slab-editable>
</template>
editable.css
slab-editable {
color: var(--ink);
display: grid;
font-family: var(--font-body);
}
slab-editable::part(label) {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
margin-block-end: 0.375rem;
}
slab-editable:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
slab-editable::part(input),
slab-editable::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-editable::part(input)::placeholder,
slab-editable::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-editable::part(input):focus-visible,
slab-editable::part(textarea):focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
slab-editable::part(input):disabled,
slab-editable::part(textarea):disabled {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
cursor: not-allowed;
}
slab-editable::part(textarea) {
block-size: auto;
min-block-size: 7rem;
padding: 0.625rem 0.75rem;
resize: vertical;
}
slab-editable:state(readonly)::part(input),
slab-editable:state(readonly)::part(textarea) {
background-color: var(--chalk);
}
slab-editable:state(error)::part(input),
slab-editable: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-editable::part(description) {
color: var(--ash);
display: grid;
font-size: 0.875rem;
}
slab-editable > [slot='description'] {
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message)::before {
content: '■ ' / '';
}
@media (prefers-reduced-motion: reduce) {
slab-editable::part(input),
slab-editable::part(textarea) {
transition: none;
}
}
slab-editable::part(preview) {
align-items: center;
background-color: transparent;
block-size: 3rem;
border: 2px dashed transparent;
border-radius: 0;
box-sizing: border-box;
color: inherit;
cursor: text;
display: flex;
font: inherit;
gap: 0.5rem;
padding: 0 0.75rem;
text-align: start;
transition:
background-color 100ms,
border-color 100ms,
box-shadow 100ms,
translate 100ms;
}
/* The pencil: what a click does. */
slab-editable::part(preview)::after {
color: var(--ash);
content: '✎' / '';
margin-inline-start: auto;
}
slab-editable::part(preview):hover {
background-color: var(--chalk);
border-color: var(--ash);
}
slab-editable::part(preview):focus-visible {
border-color: var(--ink);
border-style: solid;
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
slab-editable::part(preview):disabled {
color: var(--ash);
cursor: not-allowed;
}
slab-editable::part(preview):disabled::after {
content: none;
}
slab-editable::part(preview):disabled:hover {
background-color: transparent;
border-color: transparent;
}
@media (prefers-reduced-motion: reduce) {
slab-editable::part(preview) {
transition: none;
}
}
slab-editable:state(empty)::part(preview) {
color: var(--ash);
}
slab-editable::part(validity-message) {
color: var(--ember-deep);
display: block;
font-size: 0.875rem;
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message)::before {
content: '■ ' / '';
}
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-editable> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Import editable.js once, such
as in your root layout, and use
<slab-editable> as is. A Svelte component
around it would take slot="…" content as its
own slot and drop it.
editable.js
import 'slab-elements/editable.js';
import './editable.css';
editable.css
slab-editable {
color: var(--ink);
display: grid;
font-family: var(--font-body);
}
slab-editable::part(label) {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
margin-block-end: 0.375rem;
}
slab-editable:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
slab-editable::part(input),
slab-editable::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-editable::part(input)::placeholder,
slab-editable::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-editable::part(input):focus-visible,
slab-editable::part(textarea):focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
slab-editable::part(input):disabled,
slab-editable::part(textarea):disabled {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
cursor: not-allowed;
}
slab-editable::part(textarea) {
block-size: auto;
min-block-size: 7rem;
padding: 0.625rem 0.75rem;
resize: vertical;
}
slab-editable:state(readonly)::part(input),
slab-editable:state(readonly)::part(textarea) {
background-color: var(--chalk);
}
slab-editable:state(error)::part(input),
slab-editable: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-editable::part(description) {
color: var(--ash);
display: grid;
font-size: 0.875rem;
}
slab-editable > [slot='description'] {
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message)::before {
content: '■ ' / '';
}
@media (prefers-reduced-motion: reduce) {
slab-editable::part(input),
slab-editable::part(textarea) {
transition: none;
}
}
slab-editable::part(preview) {
align-items: center;
background-color: transparent;
block-size: 3rem;
border: 2px dashed transparent;
border-radius: 0;
box-sizing: border-box;
color: inherit;
cursor: text;
display: flex;
font: inherit;
gap: 0.5rem;
padding: 0 0.75rem;
text-align: start;
transition:
background-color 100ms,
border-color 100ms,
box-shadow 100ms,
translate 100ms;
}
/* The pencil: what a click does. */
slab-editable::part(preview)::after {
color: var(--ash);
content: '✎' / '';
margin-inline-start: auto;
}
slab-editable::part(preview):hover {
background-color: var(--chalk);
border-color: var(--ash);
}
slab-editable::part(preview):focus-visible {
border-color: var(--ink);
border-style: solid;
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
slab-editable::part(preview):disabled {
color: var(--ash);
cursor: not-allowed;
}
slab-editable::part(preview):disabled::after {
content: none;
}
slab-editable::part(preview):disabled:hover {
background-color: transparent;
border-color: transparent;
}
@media (prefers-reduced-motion: reduce) {
slab-editable::part(preview) {
transition: none;
}
}
slab-editable:state(empty)::part(preview) {
color: var(--ash);
}
slab-editable::part(validity-message) {
color: var(--ember-deep);
display: block;
font-size: 0.875rem;
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message)::before {
content: '■ ' / '';
}
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-editable> 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.
Editable.gjs
import 'slab-elements/editable.js';
import './editable.css';
export const Editable = <template>
<slab-editable ...attributes>{{yield}}</slab-editable>
</template>;
editable.css
slab-editable {
color: var(--ink);
display: grid;
font-family: var(--font-body);
}
slab-editable::part(label) {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
margin-block-end: 0.375rem;
}
slab-editable:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
slab-editable::part(input),
slab-editable::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-editable::part(input)::placeholder,
slab-editable::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-editable::part(input):focus-visible,
slab-editable::part(textarea):focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
slab-editable::part(input):disabled,
slab-editable::part(textarea):disabled {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
cursor: not-allowed;
}
slab-editable::part(textarea) {
block-size: auto;
min-block-size: 7rem;
padding: 0.625rem 0.75rem;
resize: vertical;
}
slab-editable:state(readonly)::part(input),
slab-editable:state(readonly)::part(textarea) {
background-color: var(--chalk);
}
slab-editable:state(error)::part(input),
slab-editable: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-editable::part(description) {
color: var(--ash);
display: grid;
font-size: 0.875rem;
}
slab-editable > [slot='description'] {
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message)::before {
content: '■ ' / '';
}
@media (prefers-reduced-motion: reduce) {
slab-editable::part(input),
slab-editable::part(textarea) {
transition: none;
}
}
slab-editable::part(preview) {
align-items: center;
background-color: transparent;
block-size: 3rem;
border: 2px dashed transparent;
border-radius: 0;
box-sizing: border-box;
color: inherit;
cursor: text;
display: flex;
font: inherit;
gap: 0.5rem;
padding: 0 0.75rem;
text-align: start;
transition:
background-color 100ms,
border-color 100ms,
box-shadow 100ms,
translate 100ms;
}
/* The pencil: what a click does. */
slab-editable::part(preview)::after {
color: var(--ash);
content: '✎' / '';
margin-inline-start: auto;
}
slab-editable::part(preview):hover {
background-color: var(--chalk);
border-color: var(--ash);
}
slab-editable::part(preview):focus-visible {
border-color: var(--ink);
border-style: solid;
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
slab-editable::part(preview):disabled {
color: var(--ash);
cursor: not-allowed;
}
slab-editable::part(preview):disabled::after {
content: none;
}
slab-editable::part(preview):disabled:hover {
background-color: transparent;
border-color: transparent;
}
@media (prefers-reduced-motion: reduce) {
slab-editable::part(preview) {
transition: none;
}
}
slab-editable:state(empty)::part(preview) {
color: var(--ash);
}
slab-editable::part(validity-message) {
color: var(--ember-deep);
display: block;
font-size: 0.875rem;
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-editable::part(validity-message)::before {
content: '■ ' / '';
}
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;
}
}