textbox

textbox is a text field with its label, a help line and an error message wired to it. It renders a real <input> or <textarea>.

onChange called 0 time(s)

Letters and numbers, no spaces.

On the root:ui-emptyui-requiredui-readonlyui-disabled

onChange sent: nothing yet

import { computed, state } from '@markless/core';import { textbox } from '@markless/ui';export default function Username() @{	let name = state('');	const typed = computed(() => (name === '' ? 'nothing yet' : name));	<div class="desk">		<textbox.root			class="field"			name="username"			required			onChange={(value: string) => {				name = value;			}}		>			<textbox.label class="label">Username</textbox.label>			<textbox.input class="input" placeholder="ada99" />			<textbox.description class="help">Letters and numbers, no spaces.</textbox.description>			<p class="hooks">				On the root:				<span class="hook hook-empty">ui-empty</span>				<span class="hook hook-required">ui-required</span>				<span class="hook hook-readonly">ui-readonly</span>				<span class="hook hook-disabled">ui-disabled</span>			</p>		</textbox.root>		<p class="echo">			onChange sent: <span class="echo-text">{typed}</span>		</p>	</div>;}

Click the word Username. Focus moves into the field, because the label's for points at the input. Type a letter, and ui-empty leaves the list under the field. The root drops it once the field holds text.

When to use it

Use textbox for free text, such as a name or a message. For one line, use textbox.input. For several lines, swap it for textbox.textarea, and nothing else changes. For numbers, dates or codes, look at numberbox, datebox and otp first.

Parts

textbox anatomy
Anatomy perspectiveFlat
Layers
Project nameA small, good thingGive your next idea a name.
textbox.root

The text box itself; the label, the control, and any description or error go inside it.

  • textbox.root holds the text and the rules, such as required. It renders a <div> and no control of its own.
  • textbox.input is a one-line <input>.
  • textbox.textarea is a <textarea> for several lines.
  • textbox.label names the field. Clicking it focuses the textbox.input.
  • textbox.description is a help line. The control points at it with aria-describedby.
  • textbox.error is the error message. The control points at it too, before the help line.

disabled, required and readonly work on the root or on the control. If either place sets one, it holds. A control can add a rule the root did not set, but it cannot remove one.

Styling hooks

All of these sit on textbox.root:

Attribute Present when
ui-empty The field holds no text.
ui-required required is set on the root.
ui-readonly readonly is set on the root.
ui-disabled disabled is set on the root.
ui-invalid A textbox.error is on the page.

The control itself gets aria-invalid="true" while a textbox.error is on the page.

Keyboard

The field is a native control, so the browser's own keys apply. The family's tests check these:

Key What it does
Enter in textbox.input Submits the form. The text does not change.
Enter in textbox.textarea Starts a new line. It does not submit.
Escape Leaves the text as it is.

onChange runs once per change, with the whole text so far. It does not run for the text the field starts with. With an input method editor, the family never writes over the draft. The characters the person picks are what land.

Gotchas

API reference

textbox.root

valuestring
The text the box shows. Omit it and the box starts empty. Default ''.
disabledboolean
Nobody can type in the control, and it drops out of the tab order. Default false.
requiredboolean
The control must hold text before a form submits. Default false.
readonlyboolean
The text can be read and selected but not changed. Default false.
namestring
Submitted under this name by whichever control the family renders. Default ''.
onChange(value: string) => void
Called with the new text on every change a person makes, once per change, and never for the text the box started with. Omit it and the box still works; the call site simply does nothing.

textbox.description

No props of its own.

textbox.error

No props of its own.

textbox.input

No props of its own.

textbox.label

No props of its own.

textbox.textarea

No props of its own.