Text Control

updated

Same 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.

Source

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.

VariantFrame
outlinea border in --input
softa --muted surface, no border
filleda --muted surface with a line underneath, rounded at the top only
ghostinvisible until hover or focus
subtlea --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:

StateFrame
: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
:disabledfaded 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

ConstantGoes onCarries
textControlSizethe fieldthe side padding of each size, --control-padding-xs to -xl; from 768px up, xs sets 12px text and xl 16px
textControlRadiusthe field or the frame--frame-radius, from --radius-control-xs to -xl, which every variant rounds with
textControlTextthe frame--control-text and --control-text-leading: 12px at xs, 14px from sm to lg, 16px at xl
textControlInnerTextthe field in a framereads --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

Changelog

  1. registry 0.21.0

    e5495ef — size works the same way everywhere. Every component exports its size type (AlertSize, BadgeSize, CardSize, CheckboxSize, RadioSize, SwitchSize, EmptySize, InputFloatingSize, ItemSize, ListboxSize, SeparatorSize, SliderSize, StepperSize, TabsSize, TextareaSize, TimelineSize) and the props take it without null; Button-based parts take ButtonSize, Input TextControlSize, SelectTrigger SelectSize and Tree TreeSize. ToggleGroup and ToolbarToggleGroup carry data-size on the group as well. Table's row heights moved into tableSize, so a class on DataTable's ui.table overrides --table-row-h at every size, not only at md.

    95a0fa1 — Parts follow their root's size through CSS variables instead of the context, so a class on a part wins over the size. PinInput sets --pin-input-slot, TagsInput --tags-input-line and --tags-input-delete, and PinInput, InputNumber, TagsInput and InputGroup set --control-text and --control-text-leading for the field inside (textControlText and textControlInnerText in @/ui/input). NavigationMenu's triggers and links read the --nav-* variables of the menu instead of setting their own, and DataTable's sort buttons and row toggles shrink at xs through the table's variables. PinInput's slots now sit --control-gap-* apart (2px wider at sm, lg and xl); nothing else moves. inputNumberInputVariants, tagsInputInputVariants and tagsInputItemDeleteVariants became the strings inputNumberInput, tagsInputInput and tagsInputItemDelete, inputGroupControlText is gone, and pinInputSlotVariants lost its size key.

  2. registry 0.9.0

    0c326fd — New InputNumber: a number field over Reka UI's NumberField with InputNumberInput, InputNumberIncrement and InputNumberDecrement parts inside one frame, in Input's variants and sizes, horizontal or vertical, with min, max, step, formatOptions and locale, wired to a surrounding Field.

    Input now exports textControlFrameVariant, the text control variants for a frame around a control, read from the control's focus, invalid and disabled state. InputGroup draws its frame from it; its radius variable is renamed from --input-group-radius to --control-radius.