Text Control
updatedSame frame, different fields. The variants, states and sizes Input, Select, Combobox and every other text field share, so they line up and light up the same way.
Input, Textarea, Select, Combobox, the date and time fields and the rest of the text fields wear one frame: the same five variants, the same focus, invalid and disabled states, the same padding and corners at each size. That frame is a handful of class strings in @/lib/text-control. Each of those components installs it for you; install it yourself to give a control of your own the same frame.
Installation
Usage
import { cva } from 'class-variance-authority'
import { textControlBase, textControlRadius, textControlSize, textControlVariant } from '@/lib/text-control'
export const searchFieldVariants = cva(textControlBase, {
variants: {
variant: textControlVariant,
size: {
sm: `h-(--control-height-sm) ${textControlSize.sm} ${textControlRadius.sm}`,
md: `h-(--control-height-md) ${textControlSize.md} ${textControlRadius.md}`,
},
},
defaultVariants: { variant: 'outline', size: 'md' },
})
That is how Input is built. Put the result on the element you type in, and pass your component's class through cn() after it.
The three constants
textControlBase
What every field has, whatever its variant: full width, a transparent background, --foreground text at 16px on phones and 14px from 768px up, a --muted-foreground placeholder, a --primary selection, colour changes over --duration-short-3, and no outline, because the variants draw focus. A disabled field fades its text to --disabled-opacity and shows a not-allowed cursor. It goes on the element you type in, never on a frame around it.
textControlVariant
The five variants for a field that is its own frame: an <input>, a <textarea>, a Select trigger.
| Variant | Frame |
|---|---|
outline | a border in --input |
soft | a --muted surface, no border |
filled | a --muted surface with a line underneath, rounded at the top only |
ghost | invisible until hover or focus |
subtle | a --muted surface with a border |
Each reads the state of the element it sits on. Focus turns the edge --primary and adds a 1px inset ring, or thickens the line in filled; data-state="open" does the same, for a trigger whose list is open. aria-invalid="true", or :user-invalid once the browser's own validation fails, turns the edge --destructive. A disabled field fades its border.
textControlFrameVariant
The same five variants for a frame around the field: InputGroup, InputNumber, TagsInput, InputDate, InputTime and the Combobox anchor. A frame is a div; it is never focused or invalid itself, so it reads the field inside with has-[…]. A field is an input, a textarea or a [role=spinbutton] (a date or time segment), anywhere in the frame except inside an [data-slot=input-group-addon]: Reka renders a hidden <input> for a named Checkbox or Switch, and that one must not light or grey the frame. The open state comes from a direct child with data-slot="input-group-control" and data-state="open".
State order
Every state of the frame matches through one list, :where(input,textarea,[role=spinbutton]):not([data-slot=input-group-addon]_*), followed by the state, in this order:
| State | Frame |
|---|---|
:focus-visible | --primary edge and ring |
[aria-invalid=true] | --destructive edge |
[aria-invalid=true]:focus-visible | --destructive ring |
:user-invalid | --destructive edge |
:user-invalid:focus-visible | --destructive ring |
:disabled | faded edge |
One list gives every state the same specificity, so the cascade decides, and Tailwind writes the invalid rules after the focus rule. A focused field that is invalid keeps a red frame instead of a blue edge around a red ring. Give one state a list of its own, with one more kind of field or without :where(), and it outweighs the rest whatever their order. A new kind of field goes into the one list, for every state at once.
On a field itself the states are plain variants (focus-visible:, aria-invalid:, user-invalid:), and focus-visible: comes later in Tailwind's variant order than user-invalid:. So the invalid states carry a stacked user-invalid:focus-visible: rule as well, which keeps the edge red while focused.
Sizes and radius
| Constant | Goes on | Carries |
|---|---|---|
textControlSize | the field | the side padding of each size, --control-padding-xs to -xl; from 768px up, xs sets 12px text and xl 16px |
textControlRadius | the field or the frame | --frame-radius, from --radius-control-xs to -xl, which every variant rounds with |
textControlText | the frame | --control-text and --control-text-leading: 12px at xs, 14px from sm to lg, 16px at xl |
textControlInnerText | the field in a frame | reads --control-text and --control-text-leading from 768px up |
Height is not part of them: a single-line field adds h-(--control-height-*) itself, and a Textarea grows with its rows. The Sizing page has the scale.
A frame carries the size and the field inside carries the text, so textControlText turns the size into variables on the frame and textControlInnerText reads them on the field. A class on the field still wins. Below 768px the field keeps 16px, so phones don't zoom in on focus.
To round every control at once, set --control-radius on :root (see Radius). For one field or frame, set --frame-radius in its class. InputGroup's buttons and InputNumber's steppers work out their own corners from it, so they stay concentric:
<InputGroup class="[--frame-radius:--spacing(5)]">…</InputGroup>
Overriding it
Every component built on the frame merges its class prop last through cn(), so a utility there replaces the one it conflicts with: class="bg-card" replaces the --muted surface, class="px-6" the padding. A state is a variant, so override it with the same variant: class="focus-visible:border-success" on an Input. To recolour every field, change the tokens the variants read instead: --input for the edge, --primary for focus and --destructive for an error.
Built on it
- On the field: Input, Textarea, Input Floating, each slot of Pin Input and the trigger of Select.
- As a frame: Input Group, Input Number, Tags Input, Input Date, Input Time and the anchor of Combobox.
- Through them: Date Picker and Date Range Picker, on Input Group and Input Date, and the field of Color Picker, on Input's variants.