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
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
Tap a part to outline it in the demo.
- 01TextField
textfieldclasses.root - 02Label
textfield-labelclasses.label - 03Input
textfield-inputclasses.input - 04Description
textfield-descriptionclasses.description - 05Error message
textfield-error-messageclasses.errorMessage
| Export | Renders | classes key | data-cui | Role |
|---|---|---|---|---|
TextField | Root div | root | textfield | group |
label, when label is set | label | textfield-label | none | |
input | input | textfield-input | textbox | |
Hint div, when description is set | description | textfield-description | none | |
Error div, when invalid and errorMessage is set | errorMessage | textfield-error-message | none |
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
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.
size | Input height | Input font size |
|---|---|---|
xs | 28px | 12px |
sm | 36px | 14px |
md | 44px | 16px |
lg | 52px | 16px |
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
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
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
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
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
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
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.
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
| Key | Action |
|---|---|
| Tab | Move focus to the input. |
| Shift+Tab | Move focus to the previous control. |
| Enter | Submit the parent form, as a native input does. |
ARIA
- The root is a
divwithrole="group". - The label is a
labelelement whoseforpoints at the inputid. - The input gets
aria-labelledbyfor the label andaria-describedbyfor the hint and, while invalid, the error message. - The input gets
aria-invalid,aria-required,aria-disabled, andaria-readonlyfrom the matching props. - Without a visible label, pass
aria-labeloraria-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.
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-strongforinline. - Hint and placeholder:
--cui-text-hint. - Label:
--cui-text-labeland--cui-font-weight-medium. - Focus and errors:
--cui-focus-colorand--cui-text-danger. - Size:
--cui-control-height-xsto--cui-control-height-lg,--cui-font-size-xsto--cui-font-size-lg, and--cui-radius-xsto--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.
Related
- TextArea takes the same props for multi-line text.
- NumberField parses and steps a number.
- Field helpers give your own control the field style.