role textbox
Text field draft v0.1.0
Lets people enter or edit a single line of text.
A text field collects one line of free text: a name, an email address, a search term, a quantity typed as text. It has a visible label above, an optional description and, when its value fails validation, an error message under it. Several lines of text are a Textarea; a choice among known values is a Select.
Web implementation
Root .ds-text-field on a <div>. Properties map to attributes: size → data-sizedisabled → data-disabledinvalid → data-invalidreadonly → data-readonly
packages/components/src/components/text-field/text-field.css
/*
* Text field — vanilla CSS reference implementation.
* Spec: packages/components/specs/components/text-field.spec.json
* Tokens: packages/tokens/dist/css/components/text-field.css (generated from packages/tokens/src/components/text-field.tokens.json)
*
* Rules:
* - Only --text-field-* custom properties may appear here. No raw values, no semantic tokens.
* - The root is a wrapper (label, control, description, error message, stacked); the native
* single-line text input is the control, drawn by the component.
* - Size is a data attribute on the root; states are native pseudo-classes or attributes of the
* control, and the root's [data-state="<state>"] forces them for review. Precedence follows the
* spec order: default < hover < focus-visible < invalid < readonly < disabled: hover only tints the
* background, invalid only recolors the border, so they combine as the token rule combines them.
* - Private properties (--_*) are the single place an option or a state writes to.
*/
.ds-text-field {
/* Defaults = size "medium", state "default". */
--_control-background: var(--text-field-control-background-default);
--_control-border-color: var(--text-field-control-border-color-default);
--_control-color: var(--text-field-control-color-default);
--_placeholder-color: var(--text-field-control-placeholder-color-default);
--_label-color: var(--text-field-label-color-default);
--_description-color: var(--text-field-description-color-default);
--_min-height: var(--text-field-control-min-height-medium);
--_padding-inline: var(--text-field-control-padding-inline-medium);
--_font-family: var(--text-field-control-typography-medium-font-family);
--_font-size: var(--text-field-control-typography-medium-font-size);
--_font-weight: var(--text-field-control-typography-medium-font-weight);
--_line-height: var(--text-field-control-typography-medium-line-height);
--_letter-spacing: var(--text-field-control-typography-medium-letter-spacing);
box-sizing: border-box;
display: inline-flex;
flex-direction: column;
align-items: stretch;
gap: var(--text-field-container-gap);
min-inline-size: 0;
vertical-align: middle;
}
/* ---- Size ---------------------------------------------------------------- */
.ds-text-field[data-size="small"] {
--_min-height: var(--text-field-control-min-height-small);
--_padding-inline: var(--text-field-control-padding-inline-small);
--_font-family: var(--text-field-control-typography-small-font-family);
--_font-size: var(--text-field-control-typography-small-font-size);
--_font-weight: var(--text-field-control-typography-small-font-weight);
--_line-height: var(--text-field-control-typography-small-line-height);
--_letter-spacing: var(--text-field-control-typography-small-letter-spacing);
}
.ds-text-field[data-size="large"] {
--_min-height: var(--text-field-control-min-height-large);
--_padding-inline: var(--text-field-control-padding-inline-large);
--_font-family: var(--text-field-control-typography-large-font-family);
--_font-size: var(--text-field-control-typography-large-font-size);
--_font-weight: var(--text-field-control-typography-large-font-weight);
--_line-height: var(--text-field-control-typography-large-line-height);
--_letter-spacing: var(--text-field-control-typography-large-letter-spacing);
}
/* ---- Parts --------------------------------------------------------------- */
.ds-text-field__label {
color: var(--_label-color);
font-family: var(--text-field-label-typography-font-family);
font-size: var(--text-field-label-typography-font-size);
font-weight: var(--text-field-label-typography-font-weight);
line-height: var(--text-field-label-typography-line-height);
letter-spacing: var(--text-field-label-typography-letter-spacing);
}
.ds-text-field__control {
box-sizing: border-box;
inline-size: 100%;
min-height: var(--_min-height);
margin: 0;
padding-block: 0;
padding-inline: var(--_padding-inline);
border: var(--text-field-control-border-width) solid var(--_control-border-color);
border-radius: var(--text-field-control-border-radius);
background-color: var(--_control-background);
color: var(--_control-color);
font-family: var(--_font-family);
font-size: var(--_font-size);
font-weight: var(--_font-weight);
line-height: var(--_line-height);
letter-spacing: var(--_letter-spacing);
appearance: none;
-webkit-appearance: none;
transition:
background-color var(--text-field-control-transition-duration) var(--text-field-control-transition-easing),
border-color var(--text-field-control-transition-duration) var(--text-field-control-transition-easing);
}
.ds-text-field__control::placeholder {
color: var(--_placeholder-color);
opacity: 1;
}
.ds-text-field__description {
margin: 0;
color: var(--_description-color);
font-family: var(--text-field-description-typography-font-family);
font-size: var(--text-field-description-typography-font-size);
font-weight: var(--text-field-description-typography-font-weight);
line-height: var(--text-field-description-typography-line-height);
letter-spacing: var(--text-field-description-typography-letter-spacing);
}
.ds-text-field__error-message {
margin: 0;
color: var(--text-field-error-message-color);
font-family: var(--text-field-error-message-typography-font-family);
font-size: var(--text-field-error-message-typography-font-size);
font-weight: var(--text-field-error-message-typography-font-weight);
line-height: var(--text-field-error-message-typography-line-height);
letter-spacing: var(--text-field-error-message-typography-letter-spacing);
}
/* ---- States -------------------------------------------------------------- */
.ds-text-field:where([data-state="hover"]) .ds-text-field__control:not(:disabled, [readonly]),
.ds-text-field:not([data-state]) .ds-text-field__control:hover:not(:disabled, [readonly]) {
--_control-background: var(--text-field-control-background-hover);
}
.ds-text-field:where([data-state="invalid"]) .ds-text-field__control,
.ds-text-field__control[aria-invalid="true"] {
--_control-border-color: var(--text-field-control-border-color-invalid);
}
.ds-text-field:where([data-state="readonly"]) .ds-text-field__control,
.ds-text-field__control[readonly] {
--_control-background: var(--text-field-control-background-readonly);
}
.ds-text-field:where([data-state="disabled"]),
.ds-text-field:has(.ds-text-field__control:disabled) {
--_label-color: var(--text-field-label-color-disabled);
--_description-color: var(--text-field-description-color-disabled);
}
.ds-text-field:where([data-state="disabled"]) .ds-text-field__control,
.ds-text-field__control:disabled {
--_control-background: var(--text-field-control-background-disabled);
--_control-border-color: var(--text-field-control-border-color-disabled);
--_control-color: var(--text-field-control-color-disabled);
--_placeholder-color: var(--text-field-control-placeholder-color-disabled);
cursor: not-allowed;
}
.ds-text-field__control:focus {
outline: none;
}
.ds-text-field:where([data-state="focus-visible"]) .ds-text-field__control,
.ds-text-field__control:focus-visible {
outline: var(--text-field-control-outline-width-focus-visible) solid var(--text-field-control-outline-color-focus-visible);
outline-offset: var(--text-field-control-outline-offset-focus-visible);
}
@media (prefers-reduced-motion: reduce) {
.ds-text-field__control {
transition: none;
}
}
packages/components/src/components/text-field/text-field.template.mjs
/**
* Text field — HTML template for the web implementation.
* Pure function of the spec properties (packages/components/specs/components/text-field.spec.json) -> HTML string.
* Used by the docs site (server and client side). No DOM, no framework, no script: a native text
* input named by its label, described by its description and, when invalid, by its error message.
*
* Extra props: `id` names the input (unique in the page; without it instances are numbered in
* render order); `name` is the form field's; `attrs` adds HTML attributes to the input (autocomplete,
* inputmode, maxlength…).
* Extra sandbox-only prop: `state` forces a visual state through data-state on the root.
*/
import { escapeHtml, escapeAttr } from '../../lib/template-utils.mjs';
export const meta = { id: 'text-field', selector: '.ds-text-field' };
let count = 0;
export function render(props = {}) {
const {
label = 'Label',
value = '',
placeholder = null,
description = null,
errorMessage = null,
size = 'medium',
required = false,
disabled = false,
readonly = false,
invalid = false,
state = null,
id = null,
name = null,
attrs: extra = {},
} = props;
const inputId = id ?? `ds-text-field-${++count}`;
const descriptionId = `${inputId}-description`;
const errorId = `${inputId}-error`;
const rootAttrs = ['class="ds-text-field"', `data-size="${escapeAttr(size)}"`];
if (disabled) rootAttrs.push('data-disabled');
if (readonly) rootAttrs.push('data-readonly');
if (invalid) rootAttrs.push('data-invalid');
if (state && state !== 'default') rootAttrs.push(`data-state="${escapeAttr(state)}"`);
const inputAttrs = ['type="text"', 'class="ds-text-field__control"', 'data-part="control"', `id="${escapeAttr(inputId)}"`];
if (name) inputAttrs.push(`name="${escapeAttr(name)}"`);
if (value !== '' && value != null) inputAttrs.push(`value="${escapeAttr(value)}"`);
if (placeholder) inputAttrs.push(`placeholder="${escapeAttr(placeholder)}"`);
if (required) inputAttrs.push('required');
if (disabled) inputAttrs.push('disabled');
if (readonly) inputAttrs.push('readonly');
if (invalid) inputAttrs.push('aria-invalid="true"');
const describedBy = [description ? descriptionId : null, invalid && errorMessage ? errorId : null].filter(Boolean);
if (describedBy.length) inputAttrs.push(`aria-describedby="${escapeAttr(describedBy.join(' '))}"`);
for (const [k, v] of Object.entries(extra)) {
if (v === false || v == null) continue;
inputAttrs.push(v === true ? escapeAttr(k) : `${escapeAttr(k)}="${escapeAttr(v)}"`);
}
const parts = [
`<label class="ds-text-field__label" data-part="label" for="${escapeAttr(inputId)}">${escapeHtml(label)}</label>`,
`<input ${inputAttrs.join(' ')}>`,
];
if (description) parts.push(`<p class="ds-text-field__description" data-part="description" id="${escapeAttr(descriptionId)}">${escapeHtml(description)}</p>`);
if (invalid && errorMessage) parts.push(`<p class="ds-text-field__error-message" data-part="error-message" id="${escapeAttr(errorId)}">${escapeHtml(errorMessage)}</p>`);
return `<div ${rootAttrs.join(' ')}>${parts.join('')}</div>`;
}
No other platform implementation recorded yet. Platform token outputs live under Platforms.