Skip to Content
CodeUI

TextField

@codeui/solid v0.3.0

A single-line text input with a label, a description, and an error message.

When to use

Use TextField for one line of free text, such as a snippet name, a share link, or an email address. It renders the label, the input, the hint, and the error message as one field, and links them for assistive technology.

When to choose another component

  • Use TextArea when the value can span several lines, such as a caption or a code snippet.
  • Use NumberField for a number. It parses, clamps, and steps the value for you.
  • Use Select when the value comes from a fixed list, and Combobox when the user types to filter a list.

Live demo

Used as the file name of the export.
text-field/basic.tsx
import { TextField } from "@codeui/solid";

export default function TextFieldBasic() {
	return (
		<div style={{ width: "320px" }}>
			<TextField
				label="Snippet name"
				placeholder="my-snippet"
				description="Used as the file name of the export."
			/>
		</div>
	);
}

Anatomy

Anyone with the link can view the snippet.

Tap a part to outline it in the demo.

  1. 01TextFieldtextfieldclasses.root
  2. 02Labeltextfield-labelclasses.label
  3. 03Inputtextfield-inputclasses.input
  4. 04Descriptiontextfield-descriptionclasses.description
  5. 05Error messagetextfield-error-messageclasses.errorMessage
ExportRendersclasses keydata-cuiRole
TextFieldRoot divroottextfieldgroup
label, when label is setlabeltextfield-labelnone
inputinputtextfield-inputtextbox
Hint div, when description is setdescriptiontextfield-descriptionnone
Error div, when invalid and errorMessage is seterrorMessagetextfield-error-messagenone

The root, the label, the input, the hint, and the error carry Kobalte's state attributes: data-invalid, data-valid, data-required, data-disabled, and data-readonly. The root also carries data-field-size when you set size.

Variants

Size

text-field/sizes.tsx
import { Stack, TextField } from "@codeui/solid";
import { For } from "solid-js";

const sizes = ["xs", "sm", "md", "lg"] as const;

export default function TextFieldSizes() {
	return (
		<Stack gap="4" style={{ width: "320px" }}>
			<For each={sizes}>
				{size => <TextField size={size} label={`Size ${size}`} placeholder="my-snippet" />}
			</For>
		</Stack>
	);
}

size sets the input height, the corner radius, and the font size of every part.

sizeInput heightInput font size
xs28px12px
sm36px14px
md44px16px
lg52px16px

The default is sm with a 9px radius, the size CodeImage’s sidebar fields use. Density adjusts the heights in the table. Touch density and touch devices use at least 16px for input text.

Filled and inline

text-field/themes.tsx
import { Stack, TextField } from "@codeui/solid";

export default function TextFieldThemes() {
	return (
		<Stack gap="4" style={{ width: "320px" }}>
			<TextField variant="filled" label="Filled" defaultValue="hello-codeimage.ts" />
			<TextField variant="inline" label="Inline" defaultValue="hello-codeimage.ts" />
		</Stack>
	);
}

filled draws the field fill with a transparent border. inline drops the fill and the radius and keeps a 2px bottom border, for a field that sits inside a row of text. These are the only two variants the source defines.

Description

Saved as snippet.png. Letters, numbers and dashes only.
text-field/description.tsx
import { TextField } from "@codeui/solid";

export default function TextFieldDescription() {
	return (
		<div style={{ width: "320px" }}>
			<TextField
				label="Export file name"
				defaultValue="snippet"
				description={
					<>
						Saved as <code>snippet.png</code>. Letters, numbers and dashes only.
					</>
				}
			/>
		</div>
	);
}

label, description, and errorMessage accept JSX, so a hint can hold code, a link, or a formatted value.

States

Required, read-only, and disabled

text-field/states.tsx
import { Stack, TextField } from "@codeui/solid";

export default function TextFieldStates() {
	return (
		<Stack gap="4" style={{ width: "320px" }}>
			<TextField label="Snippet name" required placeholder="Required" />
			<TextField label="Snippet id" readOnly defaultValue="snp_8f2a91" />
			<TextField label="Workspace" disabled defaultValue="Personal" />
		</Stack>
	);
}

required sets required and aria-required on the input. The kit adds no asterisk, so say "required" or "optional" in the label or the hint when it matters. readOnly keeps the value selectable and focusable and blocks edits. disabled removes the input from the tab order and dims it to 40% opacity.

Invalid

text-field/invalid.tsx
import { TextField } from "@codeui/solid";
import { createSignal } from "solid-js";

export default function TextFieldInvalid() {
	const [link, setLink] = createSignal("codeimage");
	const invalid = () => !/^https?:\/\/\S+$/.test(link());
	return (
		<div style={{ width: "320px" }}>
			<TextField
				label="Share link"
				value={link()}
				onChange={setLink}
				validationState={invalid() ? "invalid" : "valid"}
				errorMessage="Enter a full URL, starting with https://."
			/>
		</div>
	);
}

Set validationState="invalid" to show errorMessage, color the border and the text with --cui-text-danger, and set aria-invalid on the input. The error message renders only while the field is invalid. The kit runs no validation of its own. You decide when the field is invalid.

Controlled

Exports as untitled.png
text-field/controlled.tsx
import { Stack, Text, TextField } from "@codeui/solid";
import { createSignal } from "solid-js";

export default function TextFieldControlled() {
	const [name, setName] = createSignal("untitled");
	return (
		<Stack gap="2" style={{ width: "320px" }}>
			<TextField label="Snippet name" value={name()} onChange={setName} />
			<Text size="xs" tone="muted">
				Exports as {name() || "untitled"}.png
			</Text>
		</Stack>
	);
}

Pass value and onChange to own the value. onChange receives the string, not the event. Pass defaultValue instead to leave the value to the field. A form reset restores defaultValue.

Ready API

TextField

Renders the root, the label, the input, the hint, and the error message.

Prop

Type

Inherited from Kobalte's text field root:

Prop

Type

TextField splits its props in two. These native attributes go to the input element, and every other prop goes to the root:

Prop

Type

text-field/input-attributes.tsx
import { TextField } from "@codeui/solid";

export default function TextFieldInputAttributes() {
	return (
		<div style={{ width: "320px" }}>
			<TextField
				label="Notification email"
				type="email"
				autocomplete="email"
				inputmode="email"
				spellcheck={false}
				maxlength={120}
				placeholder="you@example.com"
			/>
		</div>
	);
}

Use classes to style one part. Each class is merged after the kit styles, so it wins on equal specificity. slotClasses is a deprecated alias that stays until 1.0. The root accepts no class prop. See Classes for the shared contract.

Shown in the window title.
text-field/classes.tsx
import { TextField } from "@codeui/solid";
import "./classes.css";

export default function TextFieldClasses() {
	return (
		<TextField
			label="Snippet name"
			defaultValue="hello-codeimage.ts"
			description="Shown in the window title."
			classes={{
				root: "snippet-name-field",
				input: "snippet-name-input",
			}}
		/>
	);
}

TextFieldLabel

Renders Kobalte's TextField.Label with the kit label style. Use it only inside a Kobalte TextField root that you compose yourself. TextField already renders its label.

Prop

Type

Styled parts API

Use @codeui/solid/text-field/parts to place an input inside a custom field layout. Put Label, Input, Description, and ErrorMessage under Root. Put value, name, and validation state on Root. Put native input attributes such as autocomplete and placeholder on Input.

import * as F from "@codeui/solid/text-field/parts";

<F.Root name="fileName" defaultValue="untitled">
	<F.Label>File name</F.Label>
	<F.Description>Use the name without an extension.</F.Description>
	<F.Input autocomplete="off" />
</F.Root>

Accessibility

Keyboard

KeyAction
TabMove focus to the input.
Shift+TabMove focus to the previous control.
EnterSubmit the parent form, as a native input does.

ARIA

  • The root is a div with role="group".
  • The label is a label element whose for points at the input id.
  • The input gets aria-labelledby for the label and aria-describedby for the hint and, while invalid, the error message.
  • The input gets aria-invalid, aria-required, aria-disabled, and aria-readonly from the matching props.
  • Without a visible label, pass aria-label or aria-labelledby. They go to the input.

Focus

TextField moves no focus on its own. On focus, the border takes --cui-focus-color, and :focus-visible adds the 2px Inkstone ring with a 2px offset, as CodeImage's fields do. Text inputs match :focus-visible for mouse focus too, so the ring shows whenever the field takes focus.

SSR notes

Every part renders on the server, and none uses a portal. TextField sets the input id to ${id}-input and points the label for at it during render, so the server HTML already links the label to the input. Without an id, the kit generates one with createUniqueId, which matches between the server and the client while the component tree is the same. Kobalte adds aria-labelledby and aria-describedby after hydration.

text-field/ssr-ids.tsx
import { TextField } from "@codeui/solid";

export default function TextFieldSsrIds() {
	return (
		<div style={{ width: "320px" }}>
			<TextField id="snippet-title" name="title" label="Title" />
		</div>
	);
}

Pass id when another element or a test needs a fixed input id. Pass name for form data. Without it, the input submits under the root id.

Theming hooks

TextField reads these variables, through the shared field style:

  • Fill, text, and border: --cui-surface-input, --cui-text-input, --cui-border-input, --cui-border-width, and --cui-border-width-strong for inline.
  • Hint and placeholder: --cui-text-hint.
  • Label: --cui-text-label and --cui-font-weight-medium.
  • Focus and errors: --cui-focus-color and --cui-text-danger.
  • Size: --cui-control-height-xs to --cui-control-height-lg, --cui-font-size-xs to --cui-font-size-lg, and --cui-radius-xs to --cui-radius-xl.
  • Spacing and motion: --cui-space-3, --cui-motion-duration-fast, and --cui-motion-ease-out.

See Tokens for the defaults.

Composition examples

  • Build forms puts TextField, TextArea, and NumberField in one form with validation and submission.
  • Field helpers builds a custom field that matches TextField.

On this page