Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

InputGroup

The box around an input and what sits inside it: a unit, an icon or a button such as clear. It draws the edge and the focus ring once, and the label still carries all the meaning.

Package
@kvirn-ui/react
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use an InputGroup when a short unit (km, %, kr) or an icon belongs inside the box, or when a button such as clear acts on the input.
  • Put it in a Field with a visible label that says the unit. An addon is a visual repeat, hidden from screen readers (software that reads the screen aloud).
  • Put a button directly in the box, never inside an addon. It keeps its own name and Tab stop.
  • Not for a plain input with nothing inside the box: use a TextInput or a NumberInput.
  • Not for a long unit or a sentence: use the label or a help text. Not for a button that sits outside the box: that is a separate Button next to the Field.

Example#

Use cases#

A unit before the value

Some forms write the unit first, as in “kr 4 500”. Put the addon before the input in the box. It sits at the start of the box in either reading direction. The label still says the unit, because screen readers skip the addon.

A search with a clear button

A search box has an icon at the start and a button at the end that empties it. The icon is decorative and the button is a real Button directly in the box, with its own name. It shows only while there is a text, and moves focus back to the input when it empties it. Clearing announces nothing: the focused, empty input is read as such.

Warning: Don't forget

The clear button disappears when it empties the box, so move focus to the input yourself, as the example does. Otherwise focus falls to the page (2.4.3 Focus Order).

An error on the box

Put the group in a Field with invalid and write the message in the Field. The Field marks the input with aria-invalid and tells the box too, so both agree. The box draws a thicker edge, and the message under it says what is wrong and how to fix it, so colour is not the only signal.

Error: Enter the distance to work in whole kilometres

Warning: Don't forget

invalid on the Root changes only the look of the box. Set aria-invalid on the input too, or let the Field do both. See What you need to do.

Disabled, with the reason

A disabled Field disables the input and draws the box dashed, and a click no longer focuses it. Say why in a help text under the box. Avoid a disabled box in a resident’s form where you can.

The distance was set when your application was received.

Accessibility#

What it does for you#

  • APG pattern: none. An input group is a visual wrapper around a native text input. The name, description and state come from HTML and the Field (see the Field and TextInput pages). A Button inside it follows the APG Button pattern (see the Button page).
  • Deviations: none from APG. Decisions: Addons are visual only and aria-hidden, interactive add-ons are real Buttons directly in the Root, start and end follow DOM order.
  • Native elements used: <div> (Root, no role), <span> (Addon, aria-hidden), the <input> (Input), and an optional <button> (Button) placed directly in the Root.

An InputGroup puts a unit ("kr", "%", "km"), a decorative icon or a Button inside the input's box. The Root draws the box: the edge, the radius, the invalid and disabled state and the focus ring. Addons are visual only. The label always carries the meaning, so a screen-reader user never needs the Addon: "Monthly rent in kronor", never "Monthly rent" plus a "kr" Addon.

PartElement / roleARIA / stateNotes
InputGroup.Root<div>, no rolenone. data-invalid and data-disabled from the nearest Field, data-focus-visible while the Input has keyboard focusClass kv-input-group. Props: invalid, disabled (default: the Field's). No role: it holds one control, and an unnamed group would only add noise. Holds no form state
InputGroup.Addon<span>, hidden from the accessibility treearia-hidden="true"Class kv-input-group-addon. A short unit or a decorative Icon. Never focusable (a dev warning fires when it contains focusable content), never the only place a meaning lives. At the start when it comes first in the DOM, and on the right in RTL. No side prop
the Input<input> (see TextInput)as on the TextInput page. The Addon's text is not in its name or descriptionWidth classes stay on the Input. The Input has no edge or ring of its own: the Root draws them
a Button (optional)<button> (see Button), directly in the Rootits own name and Tab stopNever inside an Addon. A text Button's name is its visible text ("Clear"). An icon-only Button has an aria-label from your translations ("Clear the search")
useInputGroupthe same attributes, for your own elementsrootProps, addonPropsOptions: invalid, disabled. Also returns isInvalid, isDisabled, isFocusVisible. Spread rootProps on the box and addonProps on each Addon

Rules:

  • Addons are aria-hidden="true" and are never in the Input's accessible name or description. A unit shown in the box is also said by the label or a help text, a Field.Prose in the Field (if the Addon were linked to the Input, users would hear a unit the label already says, twice).
  • Clicking an Addon, or the Root's padding, focuses the Input (mousedown on the box, outside any control inside it). The whole box is one target, and the keyboard reaches the Input directly, so no key is needed for it. Disabled groups don't move focus.
  • Dev warning on a mismatch: when the Root's own invalid or disabled disagrees with the Input inside it (aria-invalid, :disabled), a development warning fires once. Without own props the Root follows the Field, and nothing is compared.
  • Field state on the Root: data-invalid and data-disabled come from the nearest Field (or the Root's own props). The Input keeps aria-invalid and native disabled from the Field: the theme draws the box from the Root's attributes and from :has(> .kv-input…), so it doesn't wait for JavaScript.
  • data-focus-visible is set only while the Input has keyboard focus. Text inputs also match :focus-visible on a click, so the hook tracks whether the last interaction was a pointer or a key. Focus on a Button inside the Root doesn't set it: the Button draws its own ring.
  • A Button keeps its name and its Tab stop. Tab goes Input, then Button, in DOM order.

It announces nothing.

What you need to do#

  • The label says the unit: "Monthly rent in kronor", "Working time as a percentage of full time". An Addon is a visual repeat, never the only place a meaning lives (1.1.1 Non-text Content, level A; 3.3.2 Labels or Instructions, level A).
  • Addon text is a symbol or a widely known abbreviation, at most 4 characters (kr, €, %, km, m²). Every Addon string comes from your translations, even %.
  • One Addon per side at most. An icon Addon repeats something the label already says (a search, a date) and never looks like it does something it doesn't. A calendar icon in an Addon is decorative: clicking it focuses the Input, nothing more. An action needs a Button.
  • Interactive add-ons are Buttons placed directly in InputGroup.Root, never inside an Addon. Give each a visible text name or, if it is icon-only, an aria-label from your translations. Disable the Button when the Field is disabled.
  • invalid and disabled on the Root change only the box's look (the edge, and a disabled box ignores clicks). They don't mark the Input: set aria-invalid and native disabled on it too (3.3.1 Error Identification, level A; 4.1.2 Name, Role, Value, level A), or put the group in a Field, which sets both. A dev warning fires when they disagree.
  • A clear Button renders only while there's a value, and on activation empties the value and moves focus to the Input. Don't add a clear Button to a read-only group.
  • Don't set inputMode="numeric" on a date field with separators: the iOS number pad has no "-" or ".".
  • Everything on the TextInput and Field pages still applies: a visible label, autocomplete, errors in text, never blocking paste.

Warning: Don't forget

Write the unit in the label, as in “Monthly rent in kroner”. A screen reader skips the addon, so a unit that only the addon shows is never heard (3.3.2 Labels or Instructions).

Visual and modes#

  • Focus indicator: a ring around the whole box when the Input has keyboard focus (data-focus-visible on the Root, or the browser's :focus-visible before the script runs), and around the Button only when the Button has it. The ring is at least 2px thick with at least 3:1 contrast against its background (2.4.7 Focus Visible, level AA). How an invalid box looks on a click is a theme look: it is checked by eye, and this contract claims only the ring on keyboard focus. The Input inside has no ring of its own.
  • Target size: the default theme makes the box and a Button in it larger than the 24 by 24 CSS pixel minimum, in both densities (2.5.8 Target Size (Minimum), level AA). The whole box is the click target for the Input. An Addon makes the box larger, never smaller. If you restyle, keep the minimum.
  • Colour: the edge has at least 3:1 contrast against its background (1.4.11 Non-text Contrast, level AA). An invalid box has a thicker edge in the danger colour, and colour is never the only signal: the message under the group and the thicker edge carry it. A disabled box has a dashed edge. Addon text and icon use the normal text colour (a muted one when disabled), and the Button's divider uses the edge colour. Every pair is already in the theme's contrast check.
  • forced-colors behaviour: the edge uses the system's button-border colour. An invalid box has a thicker edge in the system's text colour, and a disabled box has a dashed edge in the system's greyed-out colour. Focus uses the system's highlight colour, and on any focus in the Input the edge takes it too. Addon text uses the system's field text colour, and the Button's label and divider use the system's button text colour.
  • reduced-motion behaviour: the Root's border and fill and the Button's fill change with a transition only when the user hasn't asked for reduced motion. The focus ring appears instantly.
  • Reflow: the Root never grows wider than its column. The Input shrinks, Addons and the Button don't. At 320px with a Finnish label there's no horizontal scrolling: the reflow sweep runs every story at 320 CSS px and fails on horizontal overflow. The text Button is one short word, or icon-only where the label is long.
  • 1.4.12 Text Spacing (AA): no fixed heights, and the Addon is centred, so it isn't clipped.
  • RTL: logical properties only. A start Addon is on the right, the divider and the Button's rounded corners swap, and icons in Addons don't mirror.

Focus management#

  • Initial focus: not moved.
  • Trap: no.
  • Restore to: not applicable. A clear Button that disappears when the box is empty is the consumer's: on activation, empty the value and move focus to the Input, so focus never lands on body.
  • Never obscured by: the focus ring is drawn around the whole box, outside it, and the Root has no overflow, so nothing clips it (2.4.11 Focus Not Obscured, level AA; 2.4.13 Focus Appearance, level AAA). On a Button the ring is on the Button only. If your page has a sticky header, set scroll-padding so the header doesn't cover the focused box.

Pointer#

A click is not a key, so it isn't in the Keyboard table. Clicking an Addon, or the box's padding, focuses the Input (the Addon isn't a control), and the keyboard reaches the Input directly with Tab. A disabled box ignores the click.

WCAG success criteria#

  • 1.1.1 Non-text Content (A): the icon Addon is decorative (aria-hidden), and the label says what it shows.
  • 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): the Input is named by its label, with no extra role or name from the wrapper.
  • 1.4.1 Use of Color (A), 1.4.11 Non-text Contrast (AA): the edge, the invalid width and the dashed disabled edge, never colour alone.
  • 2.1.1 Keyboard (A), 2.4.3 Focus Order (A): Tab goes Input, then Button. Nothing is focusable in an Addon.
  • 2.4.7 Focus Visible (AA): the ring shows on keyboard focus and is never clipped by the box. 2.4.13 Focus Appearance is AAA and not counted toward the AA bar: the ring is also at least 2px thick.
  • 2.5.3 Label in Name (A): a text Button's name is its visible text.
  • 2.5.8 Target Size (Minimum) (AA): the box and the Button are larger than 24 by 24 CSS pixels in the default theme.
  • 3.3.2 Labels or Instructions (A): the label carries the unit.

Keyboard#

Focus
native
Selection follows focus
n/a
Arrows wrap
n/a
Shortcuts
none
KeyWhereWhat happens
Tabbefore the groupMoves focus into the input. The Addons are skipped
Test: input-group.test.tsx › Tab goes to the input, then to the Button, and Shift+Tab goes back
Tabin the inputMoves focus to the Button when the group has one, otherwise out of the group
Test: input-group.test.tsx › Tab goes to the input, then to the Button, and Shift+Tab goes back, input-group.test.tsx › the Addon is never a Tab stop: Tab from the Input leaves a group without a Button
Shift+Tabon the ButtonMoves focus back to the input
Test: input-group.test.tsx › Tab goes to the input, then to the Button, and Shift+Tab goes back
Enter / Spaceon the ButtonActivates the Button (native), for example clearing the search
Test: input-group.test.tsx › Enter and Space on the Button activate it
any characterin the inputTypes it. Nothing is filtered, and a unit typed in the box is fine (the form parses it)
Test: input-group.test.tsx › any character types, and the unit can be typed too

Escape, arrow keys and Home / End keep the native text-field behaviour in the input. The Addons handle no keys and are never focusable.

API reference#

import { InputGroup, useInputGroup } from '@kvirn-ui/react'

InputGroup.Root

Renders <div> with no role. It takes every attribute of a <div> and passes ref to it. It draws the input’s box, and holds one input and its addons or buttons. Start and end follow the order you write them in: there is no side prop. The default theme is described on Theming.

PropTypeDefaultDescription
invalidbooleanthe Field’s invalidDraws the invalid edge (data-invalid). It changes only the look: the input needs aria-invalid too, which a Field sets for you.
disabledbooleanthe Field’s disabledDraws the disabled edge (data-disabled) and stops a click from focusing the input. The input needs native disabled too, which a Field sets for you.
Class or attributeValuesMeaning
kv-input-groupalwaysThe part class. It draws the edge, the radius and the focus ring of the box.
data-invalidpresent or absentThe box is invalid, from the invalid prop or the Field.
data-disabledpresent or absentThe box is disabled, from the disabled prop or the Field.
data-focus-visiblepresent or absentThe input inside has keyboard focus, so the ring is drawn around the box.

InputGroup.Addon

Renders <span aria-hidden="true">. It takes every attribute of a <span> and passes ref to it. It holds a short unit or a decorative icon, is never focusable, and warns in development if it contains something focusable. A press on it focuses the input.

Class or attributeValuesMeaning
kv-input-group-addonalwaysThe part class.
aria-hidden"true"Always set. An addon is visual only, so the label must say what it shows.

InputGroup.Input

Renders the same <input> as a TextInput, under the group’s name. It takes every TextInput prop, reads its Field the same way, and has no props of its own. A NumberInput works in the box too, as in the examples. An InputGroup.Input needs nothing from the group: the Root only draws the box.

useInputGroup

Options

PropTypeDefaultDescription
invalidbooleanthe Field’s invalidDraws the invalid edge (data-invalid). It changes only the look: the input needs aria-invalid too, which a Field sets for you.
disabledbooleanthe Field’s disabledDraws the disabled edge (data-disabled) and stops a click from focusing the input. The input needs native disabled too, which a Field sets for you.

Result

PropTypeDefaultDescription
rootPropsInputGroupRootPartProps–Spread on the box, a <div>: class, state, and the handlers that focus the input on a press and track keyboard focus.
addonPropsInputGroupAddonPartProps–Spread on a unit or an icon: class and aria-hidden.
isInvalidboolean–Whether the box is invalid.
isDisabledboolean–Whether the box is disabled.
isFocusVisibleboolean–Whether the input inside has keyboard focus.

Known issues#

  • Autofill covers only the Input. The browser's autofill fill doesn't paint behind an Addon. Known browser behaviour, accepted.
  • A Button's transparent edges show in forced colours, so it looks like a bordered button attached to the inside of the box. Accepted: it's the shape Contrast Themes users expect of a button.
  • Keyboard tests run in Chromium only. The keyboard rows run in Vitest browser mode on Chromium, not in WebKit. Test in Safari with your own assistive technology.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassinput-group.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassinput-group.test.tsx › the Addon is aria-hidden and out of the accessibility tree; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apass5 rows, each names a test (input-group.test.tsx › Tab goes to the input, then to the Button, and Shift+Tab goes back)
Focus order and managementpassn/apassinput-group.test.tsx asserts focus (toHaveFocus); first rule: Tab goes to the input, then to the Button, and Shift+Tab goes back
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

What to check#

With a screen reader and the keyboard, try:

  • Tab to the input: the label (with the unit) and the field type are read. The unit addon is not read separately.
  • Tab again reaches Clear. Its name is read, and Enter empties the box.
  • After clearing, focus is back in the input and the empty field is read.
  • Invalid: the error is read with the input, and the thicker edge shows.
  • Click the unit: the input takes focus and nothing else is read.
  • With voice control, saying Clear's visible name activates it.