Skip to content
Slotted

Text Field

Takes one line of text in a native input, with the adornments, buttons and count that sit inside its border.

Printed on every invoice and packing slip.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { TextField, TextFieldAction, TextFieldDefaultsProvider } from '@slotted/react';

Sizes

size sets the height, the padding, the type and the icons to one step of the control scale: sm for dense tables and toolbars, md for most forms, lg for sparse forms. Inside a field, a text field that states no size takes the field’s.

Variants

variant sets how much frame the control draws. outline, the default, draws a border on the ground it sits on. subtle fills the control with a sunken ground. plain draws no border and no ground at rest; hover tints the ground and focus draws the ring inside the same box, so a table cell that holds one does not change size under the pointer. mono sets the value in the monospaced font with tabular figures, for identifiers, codes and keys.

States

A text field inside a field reads invalid, readOnly and disabled from it. invalid sets aria-invalid on the input and draws the frame in the danger tone. readOnly sets the native readonly: the value keeps full contrast, stays focusable and selectable, and is still submitted. disabled sets the native disabled, which takes the input out of the tab order and out of form submission. A state written on the text field itself wins over the field’s.

Error:A German VAT number has nine digits after DE.

Assigned when the order was placed.

Set by the customer’s price list.

Types and keyboards

type is the input’s own: email, tel, url, search and password change the on-screen keyboard and what the browser offers to fill in. autoComplete names the value for autofill, and inputMode asks for a keyboard without changing the type, so a ZIP code takes inputMode="numeric" and keeps its leading zeros. Every native attribute you write reaches the input.

Passwords

revealable on a type="password" field draws a button that shows the characters and hides them again, keeping the caret where it was. The button keeps one name and carries its state in aria-pressed. While the password has focus and Caps Lock is on, a mark appears before the button and a screen reader hears the warning once.

At least 12 characters.

Adornments

start and end hold content before and after the value in the reading direction: a currency, a unit, a protocol, an icon or a key. They are named for the reading direction, not for a side, so in a right-to-left document start stays before the value. An icon inside them takes the control’s icon size. A Kbd shows the shortcut; binding the key is up to the application.

$USD
kg
https://
/

Clear button

clearable draws a button that empties the value and returns focus to the input. It appears once there is a value, and it is withdrawn while the field is empty, read-only or disabled. The change it requests carries the reason clear.

Copy button

copyable draws a button that writes the value to the clipboard, shows a check for two seconds and announces the outcome to screen readers. It stays on a read-only value, which is where an invite link, an API key or a signing secret lives. onValueCopy in React and (valueCopy) in Angular report whether the clipboard accepted the value.

Anyone with the link joins as a viewer.

Character count

showCount publishes the length of the value to the field, which draws the count at the end of the label row. With maxLength, the browser stops input at the limit. With countMax alone, the limit is soft: the value can pass it, and the count turns to the danger tone. Type past 11 characters in the second field.

36/60
9/11

Carriers cut sender names after 11 characters.

Buttons inside the field

TextFieldAction in React and slTextFieldAction in Angular place a button of your own in start or end. It defaults to type="button"; write type="submit" to submit the form the field belongs to. A button named by its text is padded; one named by aria-label is square, like the clear and copy buttons. Edit the code and press Apply or Enter.

One code per order.

The action takes a menu trigger, so a choice that qualifies the value, such as a country code, opens a menu from inside the frame. The trigger stays pressed while the menu is open, and Enter, Space and ArrowDown open it from the keyboard.

Checking the value

loading draws a spinner before the buttons and sets aria-busy on the input while something checks the value. The input stays editable, so the reader keeps typing while the check runs. Keep loading bound to a boolean, false included, rather than removing it: the spinner keeps its place in the frame and leaves with a transition instead of disappearing. Type admin to see a handle that is taken.

@

Lowercase letters, digits and hyphens.

Error:

Validation

Validate when the form is submitted, then set invalid on the field and write the error. The React example owns the value through value and onValueChange; the Angular example binds the input to a reactive form control, because slTextFieldInput is a ControlValueAccessor. Submit the empty form, then a malformed address, then a valid one.

Error:

Defaults for a section

TextFieldDefaultsProvider in React and provideTextFieldDefaults in Angular set size, variant and mono once for every text field under them. A text field that states a value of its own keeps it, and inside a field the field’s size wins over the provider’s.

Accessibility

  • There is no WAI-ARIA Authoring Practices pattern for a text field: the APG lists thirty patterns, and none of them is a single-line text input. The sources are the HTML specification for the input element, the APG practice Providing Accessible Names and Descriptions (w3.org/WAI/ARIA/apg/practices/names-and-descriptions/) for its name, and the ARIA textbox role for what may sit inside it.
  • The frame has no role. WAI-ARIA requires the children of a textbox to be non-interactive and presentational, so the clear and reveal buttons are siblings of the input inside a box that announces nothing.
  • The input takes its name from the field’s label, whose for attribute points at the identifier the field generates. The APG naming practice records that some combinations of browser and assistive technology compute no name from a label that wraps its control.
  • Inside a required field the input carries aria-required, never the native required, because the native attribute turns on browser constraint validation and changes how the form submits. Write required on the input to opt into the platform’s validation; TextField leaves it unchanged. disabled and readonly are the native attributes.
  • The clear and reveal buttons are native buttons with names you translate once through messages. Each is at least 24 CSS pixels wide at every size and density, which meets WCAG 2.2 success criterion 2.5.8. The reveal button keeps one name and carries its state in aria-pressed, which is the APG rule for a toggle button.
  • The copy button announces its outcome through the shared live announcer, Copied or Copy failed, because the check that replaces the copy mark is only visible. The Caps Lock mark is hidden from assistive technology; its message is announced once when Caps Lock turns on while the password field has focus.
  • loading sets aria-busy on the input and leaves it enabled, so the keyboard stays on the input while an asynchronous check runs. The spinner is hidden from assistive technology.
  • A menu inside the frame follows the APG menu button pattern (w3.org/WAI/ARIA/apg/patterns/menu-button/). The menu trigger on the action supplies aria-haspopup, aria-expanded, and Enter, Space and ArrowDown to open.
  • No key is bound to Escape. Some browsers clear a search input on Escape, and a text field that consumed the key inside a dialog would leave a keyboard user unable to close the dialog. WCAG success criterion 2.1.4 addresses the defects that single-key shortcuts cause.
  • The invalid state does not rely on color alone: aria-invalid announces it, the field’s error states it in words, and the inline message writes a visually hidden “Error:” before the sentence. In the high-contrast scheme the border cannot carry the state, so nothing depends on it.

API

TextField

Props

valuestring
Makes the value controlled for the life of the component. Without it the input keeps its own value, and a native form reset restores defaultValue.
defaultValuestring
The value an uncontrolled text field starts from. It is read once and written to the input as the native attribute.
size'sm' | 'md' | 'lg'Default md
On TextField, TextFieldDefaultsProviderThe step of the control scale. Without it the text field takes the size of the field around it, then the provider’s. Height, padding, type and icon size come from the control scales, which the theme restates for each density.
variant'outline' | 'subtle' | 'plain'Default outline
On TextField, TextFieldDefaultsProviderHow much frame the control draws. plain draws no border at rest and keeps the same box, for a dense table cell.
monobooleanDefault false
On TextField, TextFieldDefaultsProviderSets the value in the monospaced font with tabular figures, for identifiers, hashes and keys.
clearablebooleanDefault false
Draws a button that empties the value with reason "clear" and returns focus to the input. The button is withdrawn while the value is empty, disabled or read-only.
revealablebooleanDefault false
Draws a toggle that shows a password as characters. It appears only on type="password" and is withdrawn while the input is disabled.
copyablebooleanDefault false
Draws a button that writes the value to the clipboard, shows a check for two seconds and announces the outcome. It is hidden while the value is empty or the input is disabled.
loadingbooleanDefault false
Draws the shared spinner before the buttons and sets aria-busy on the input; the value stays editable. Once loading is set, false included, the spinner keeps its place so it can leave with a transition.
revealedboolean
Makes the password toggle controlled. defaultRevealed sets the uncontrolled starting state.
showCountbooleanDefault false
Publishes the length of the value to the field around it, which draws the count at the end of its label row.
countMaxnumberDefault maxLength
The limit the count is measured against. Without maxLength it is a soft limit: the value can pass it, and the count turns to the danger tone before validation runs.
startReactNode
Content before the value in the reading direction. It is its own bidirectional run, and an icon inside it takes the control’s icon size.
endReactNode
Content after the value in the reading direction.
disabledbooleanDefault from the field, or false
Unset by default, so the text field takes the state of the field around it. When set, it wins, and it is the native attribute: the input leaves the tab order and form submission.
readOnlybooleanDefault from the field, or false
Unset by default, so the text field takes the state of the field around it. It is the native readonly: the input stays focusable and selectable, and its value is submitted.
invalidbooleanDefault from the field, or false
Unset by default, so the text field takes the state of the field around it. It sets aria-invalid on the input and data-invalid on the frame.
requiredboolean
The input’s own attribute, passed through unchanged; TextField never writes it. A field marks a required value with aria-required instead, because the native attribute turns on browser constraint validation.
type'text' | 'search' | 'email' | 'url' | 'tel' | 'password'Default text
The input’s own type, one of the six listed. A date, a time, a month or a number is not a TextField: use date-picker (forms.md FM-16), time-picker or number-field. Other strings still reach the element for compatibility and are not supported.
messagesPartial<TextFieldMessages>
Replaces words of the active language, key by key. clear, reveal and copy name the buttons; reveal does not change with the state, because aria-pressed carries it. copied and copyFailed announce the outcome of a copy. capsLock is announced once when Caps Lock turns on in a focused password field.

Events

NamePayloadWhen
onValueChange(value: string, request: ChangeRequest<string>)Before the value changes. The request carries the new value, the value it replaces, the reason ("keyboard" for an edit in the input, "clear" for the clear button), the originating event, and cancel(), which refuses the change before it commits.
onRevealedChange(revealed: boolean, request: ChangeRequest<boolean>)Before the password toggle changes, with reason "pointer" or "keyboard" read from the click. A form that hides the password again on blur controls revealed and listens here.
onValueCopy({ copied, reason, source, value }: TextFieldCopyDetail)After the copy button has asked the clipboard. copied is false when the clipboard refused the value, and reason is "pointer" or "keyboard". onCopy remains the input’s native clipboard event.
onChange, onInput, onFocus, onBlur, onKeyDown, onKeyUpthe element’s own eventsWhenever the input fires them. TextField composes your handler with its own instead of replacing it: yours runs first, and always runs.

Composition

render(props: TextFieldRootProps) => ReactNode
Renders the frame into another element and passes it every attribute. The props are typed against HTMLAttributes<HTMLElement>, so they can be spread onto a label or a fieldset. The input cannot be replaced, because it holds the value, the identifier and the wiring.
TextFieldActionComponentPropsWithRef<"button"> & { render? }Default type="button"
On start, endA button you place inside the frame. Every native prop reaches the button, and render replaces it. MenuTrigger, PopoverTrigger and TooltipTrigger pass their wiring to it through their own render.
refRef<HTMLInputElement>
Reaches the input. focus(), select() and setSelectionRange() are the element’s own, so TextField adds no imperative handle.
className / stylestring / CSSProperties
Reach the frame, because the frame is what a reader sees. Every other native prop reaches the input.
TextFieldDefaultsProvider{ size, variant, mono }
On a subtreeSets size, variant and mono once for every text field under it. A field outranks it for size, so a text field inside a field takes the field’s size.
useField()() => FieldContextValue | undefined
On inside a FieldWhat TextField reads from the field around it: the identifier, the description, the disabled, read-only, invalid and required states, and the channel it publishes its count on. A text field inside a field needs no wiring of its own.

Parts

PartElementRequiredPurpose
rootdiv, or the element render returnsYesThe frame. It carries data-ui="text-field", data-size, data-variant and data-mono, and data-disabled, data-invalid, data-readonly and data-loading. It has no role: WAI-ARIA allows a textbox only non-interactive children, so the buttons sit beside the input inside a box that announces nothing. className and style reach this element.
inputinputYesThe control. It holds the value and the identifier, and receives every native attribute you write: type, autoComplete, inputMode, enterKeyHint, pattern, maxLength, name, form and required. ref reaches it, so focus(), select() and setSelectionRange() are the element’s own.
startspanNoContent before the value in the reading direction, such as a currency, an icon or an @. It is its own bidirectional run, so R$ keeps its order in a right-to-left document, and an icon inside it takes the control’s icon size.
endspanNoContent after the value in the reading direction, such as a unit, a domain or a suffix.
actionbutton (TextFieldAction), inside start or endNoA button you place inside the frame: a submit, a menu trigger or a tooltip trigger. It is square when named by aria-label and padded when named by its text. aria-expanded="true" keeps it pressed while the menu it opened is open.
busyspan[data-ui="spinner"]NoThe shared spinner, before the buttons, while loading is true. It is rendered whenever loading is set and hidden while loading is false, so it can leave with a transition. It is hidden from assistive technology; aria-busy on the input carries the state.
caps-lockspanNoA mark in a password field while Caps Lock is on and the input has focus. It is hidden from assistive technology; the capsLock message is announced once when Caps Lock turns on.
clearbuttonNoEmpties the value with reason "clear" and returns focus to the input. It is rendered while clearable is set, hidden while the value is empty or cannot be changed, and never smaller than 24 CSS pixels.
copybuttonNoWrites the value to the clipboard, shows a check with data-complete for two seconds and announces the outcome. It stays on a read-only value and is hidden while the value is empty or the input is disabled.
revealbuttonNoShows the password as characters and hides it again, keeping the caret where it was. Its name does not change with its state, which is the APG rule for a toggle button; aria-pressed carries the state.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every text field inside it.

Text Field tokens

TokenDefault
--slotted-text-field-action-active-backgroundvar(--slotted-tone-secondary-subtle-active)
--slotted-text-field-action-colorvar(--slotted-text-muted, GrayText)
--slotted-text-field-action-hover-backgroundvar(--slotted-tone-secondary-subtle-hover)
--slotted-text-field-action-label-colorvar(--slotted-text, CanvasText)
--slotted-text-field-action-sizemin(calc(var(--_icon-size) + 2 * var(--slotted-space-2, 0.25rem)), var(--_inner-size))
--slotted-text-field-adornment-colorvar(--slotted-text-muted, GrayText)
--slotted-text-field-autofill-backgroundvar(--slotted-tone-accent-surface)
--slotted-text-field-backgroundvar(--slotted-control-surface, Field)
--slotted-text-field-border-colorvar(--slotted-control-border-color, ButtonBorder)
--slotted-text-field-caps-lock-colorvar(--slotted-tone-warning-text, CanvasText)
--slotted-text-field-colorvar(--slotted-text, CanvasText)
--slotted-text-field-copy-complete-colorvar(--slotted-tone-success-text, CanvasText)
--slotted-text-field-disabled-backgroundvar(--slotted-disabled-background)
--slotted-text-field-disabled-border-colorvar(--slotted-disabled-border)
--slotted-text-field-disabled-colorvar(--slotted-disabled-foreground)
--slotted-text-field-hover-border-colorvar(--slotted-tone-accent-border)
--slotted-text-field-icon-size-min14px
--slotted-text-field-invalid-border-colorvar(--slotted-tone-danger-border)
--slotted-text-field-invalid-edge-widthvar(--slotted-control-invalid-edge-width, 1px)
--slotted-text-field-invalid-focus-ring-colorvar(--slotted-focus-ring-color-invalid, Highlight)
--slotted-text-field-placeholder-colorvar(--slotted-text-muted, GrayText)
--slotted-text-field-plain-hover-backgroundvar(--slotted-tone-secondary-subtle-hover)
--slotted-text-field-radiusvar(--slotted-control-radius, 4px)
--slotted-text-field-readonly-backgroundvar(--slotted-tone-secondary-subtle-hover)
--slotted-text-field-shadowvar(--slotted-control-shadow, none)
--slotted-text-field-subtle-backgroundvar(--slotted-surface-sunken, Canvas)
--slotted-text-field-subtle-border-colorvar(--slotted-border-subtle, ButtonBorder)
--slotted-text-field-subtle-hover-backgroundvar(--slotted-tone-secondary-subtle-hover)

Theme tokens it reads

TokenDefault
--slotted-border-subtleSet per scheme by the theme
--slotted-control-border-colorSet per scheme by the theme
--slotted-control-border-widthvar(--slotted-border-width-thin)
--slotted-control-focus-ring-offsetvar(--slotted-focus-ring-offset)
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-control-font-size-lgcomfortable 14px · compact 13px · dense 12px
--slotted-control-font-size-mdcomfortable 13px · compact 12px · dense 12px
--slotted-control-font-size-smcomfortable 12px · compact 12px · dense 12px
--slotted-control-gap-lgcomfortable 8px · compact 6px · dense 6px
--slotted-control-gap-mdcomfortable 8px · compact 6px · dense 4px
--slotted-control-gap-smcomfortable 6px · compact 4px · dense 4px
--slotted-control-height-lgcomfortable 38px · compact 32px · dense 30px
--slotted-control-height-mdcomfortable 34px · compact 28px · dense 26px
--slotted-control-height-smcomfortable 30px · compact 26px · dense 24px
--slotted-control-icon-size-lgcomfortable 18px · compact 16px · dense 14px
--slotted-control-icon-size-mdcomfortable 16px · compact 14px · dense 13px
--slotted-control-icon-size-smcomfortable 14px · compact 13px · dense 12px
--slotted-control-inner-size-lgcomfortable 28px · compact 24px · dense 22px
--slotted-control-inner-size-mdcomfortable 24px · compact 20px · dense 18px
--slotted-control-inner-size-smcomfortable 22px · compact 18px · dense 16px
--slotted-control-invalid-edge-widthvar(--slotted-border-width-thin)
--slotted-control-padding-inline-lgcomfortable 16px · compact 12px · dense 10px
--slotted-control-padding-inline-mdcomfortable 13px · compact 10px · dense 8px
--slotted-control-padding-inline-smcomfortable 10px · compact 8px · dense 6px
--slotted-control-radiusvar(--slotted-radius-sm)
--slotted-control-shadowSet per scheme by the theme
--slotted-control-shadow-focus-geometry0 1px 4px -1px
--slotted-control-surfaceSet per scheme by the theme
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-disabled-backgroundSet per scheme by the theme
--slotted-disabled-borderSet per scheme by the theme
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-duration-short120ms
--slotted-easing-entercubic-bezier(0.05, 0.7, 0.1, 1)
--slotted-easing-exitcubic-bezier(0.3, 0, 0.8, 0.15)
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-color-invalidSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-family-mono'Geist Mono Variable', ui-monospace, SFMono-Regular, Menlo, monospace
--slotted-font-weight-medium500
--slotted-radius-none0px
--slotted-radius-xs2px
--slotted-space-20.25rem
--slotted-space-30.375rem
--slotted-surface-sunkenSet per scheme by the theme
--slotted-target-comfortable44px
--slotted-target-min24px
--slotted-textSet per scheme by the theme
--slotted-text-mutedSet per scheme by the theme
--slotted-tone-accent-borderSet per scheme by the theme
--slotted-tone-accent-surfaceSet per scheme by the theme
--slotted-tone-danger-borderSet per scheme by the theme
--slotted-tone-secondary-subtle-activeSet per scheme by the theme
--slotted-tone-secondary-subtle-hoverSet per scheme by the theme
--slotted-tone-success-textSet per scheme by the theme
--slotted-tone-warning-textSet per scheme by the theme