Input Group
Adds icons, units, text and buttons to the edges of an input or a textarea.
Installation
pnpm dlx shadcn@latest add @silkware/input-groupTo copy the component by hand, install its dependencies:
pnpm add react-aria-components class-variance-authority cnInput Group also uses Button, Input and Textarea:
pnpm dlx shadcn@latest add @silkware/button @silkware/input @silkware/textareaThen copy the source into components/ui/input-group.tsx:
"use client"
import * as React from "react"import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"import { Group, type GroupProps } from "react-aria-components"
import { Button } from "@/components/ui/button"import { Input } from "@/components/ui/input"import { Textarea } from "@/components/ui/textarea"
function InputGroup({ className, ...props }: GroupProps) { return ( <Group data-slot="input-group" className={cn( "group/input-group relative flex h-9 w-full min-w-0 items-center rounded-md border border-input shadow-xs transition-[color,box-shadow] outline-none in-data-[slot=combobox-content]:focus-within:border-inherit in-data-[slot=combobox-content]:focus-within:ring-0 has-[[data-slot=input-group-control]:focus-visible]:border-ring has-[[data-slot=input-group-control]:focus-visible]:ring-3 has-[[data-slot=input-group-control]:focus-visible]:ring-ring/50 has-[[data-slot][aria-invalid=true]]:border-destructive has-[[data-slot][aria-invalid=true]]:ring-3 has-[[data-slot][aria-invalid=true]]:ring-destructive/20 has-[>[data-align=block-end]]:h-auto has-[>[data-align=block-end]]:flex-col has-[>[data-align=block-start]]:h-auto has-[>[data-align=block-start]]:flex-col has-[>textarea]:h-auto dark:bg-input/30 dark:has-[[data-slot][aria-invalid=true]]:ring-destructive/40 has-[>[data-align=block-end]]:[&>input]:pt-3 has-[>[data-align=block-start]]:[&>input]:pb-3 has-[>[data-align=inline-end]]:[&>input]:pr-1.5 has-[>[data-align=inline-start]]:[&>input]:pl-1.5", className )} {...props} /> )}
const inputGroupAddonVariants = cva( "flex h-auto cursor-text items-center justify-center gap-2 py-1.5 text-sm font-medium text-muted-foreground select-none group-data-[disabled=true]/input-group:opacity-50 [&>kbd]:rounded-[calc(var(--radius)-5px)] [&>svg:not([class*='size-'])]:size-4", { variants: { align: { "inline-start": "order-first pl-2 has-[>button]:-ml-1 has-[>kbd]:ml-[-0.15rem]", "inline-end": "order-last pr-2 has-[>button]:-mr-1 has-[>kbd]:mr-[-0.15rem]", "block-start": "order-first w-full justify-start px-2.5 pt-2 group-has-[>input]/input-group:pt-2 [.border-b]:pb-2", "block-end": "order-last w-full justify-start px-2.5 pb-2 group-has-[>input]/input-group:pb-2 [.border-t]:pt-2", }, }, defaultVariants: { align: "inline-start", }, })
function InputGroupAddon({ className, align = "inline-start", ...props}: React.ComponentProps<"div"> & VariantProps<typeof inputGroupAddonVariants>) { return ( <div role="group" data-slot="input-group-addon" data-align={align} className={cn(inputGroupAddonVariants({ align }), className)} onClick={(e) => { if ((e.target as HTMLElement).closest("button")) { return } e.currentTarget.parentElement?.querySelector("input")?.focus() }} {...props} /> )}
const inputGroupButtonVariants = cva( "flex items-center gap-2 text-sm shadow-none", { variants: { size: { xs: "h-6 gap-1 rounded-[calc(var(--radius)-5px)] px-1.5 [&>svg:not([class*='size-'])]:size-3.5", sm: "", "icon-xs": "size-6 rounded-[calc(var(--radius)-5px)] p-0 has-[>svg]:p-0", "icon-sm": "size-8 p-0 has-[>svg]:p-0", }, }, defaultVariants: { size: "xs", }, })
function InputGroupButton({ className, type = "button", variant = "ghost", size = "xs", ...props}: Omit<React.ComponentProps<typeof Button>, "size" | "type"> & VariantProps<typeof inputGroupButtonVariants> & { type?: "button" | "submit" | "reset" }) { return ( <Button type={type} data-size={size} variant={variant} className={cn(inputGroupButtonVariants({ size }), className)} {...props} /> )}
function InputGroupText({ className, ...props }: React.ComponentProps<"span">) { return ( <span className={cn( "flex items-center gap-2 text-sm text-muted-foreground [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4", className )} {...props} /> )}
function InputGroupInput({ className, ...props}: React.ComponentProps<"input">) { return ( <Input data-slot="input-group-control" className={cn( "flex-1 rounded-none border-0 bg-transparent shadow-none ring-0 focus-visible:ring-0 aria-invalid:ring-0 dark:bg-transparent", className )} {...props} /> )}
function InputGroupTextarea({ className, ...props}: React.ComponentProps<"textarea">) { return ( <Textarea data-slot="input-group-control" className={cn( "flex-1 resize-none rounded-none border-0 bg-transparent py-2 shadow-none ring-0 focus-visible:ring-0 aria-invalid:ring-0 dark:bg-transparent", className )} {...props} /> )}
export { InputGroup, InputGroupAddon, InputGroupButton, InputGroupText, InputGroupInput, InputGroupTextarea,}Usage
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, InputGroupText, InputGroupTextarea,} from "@/components/ui/input-group"<InputGroup> <InputGroupInput aria-label="Search suppliers" placeholder="Search suppliers" /> <InputGroupAddon> <SearchIcon /> </InputGroupAddon></InputGroup>The group draws the border and the focus ring, and the input inside has none. Always put InputGroupAddon after the input in the markup, and use align to place it. The input stays first in the tab order and the click on an addon moves focus to it.
Give the input an id and a FieldLabel, as for a plain input. Addon text is not a label.
Examples
Inline
The default addon sits at the start. Set align="inline-end" for the end. Use an icon to hint at the kind of value.
Order confirmations go to this address.
Text
InputGroupText shows a currency symbol, a unit or a fixed prefix. Use the start for what comes before the value and the end for the unit.
Button
InputGroupButton is a ghost button sized for the group. Its sizes are xs, sm, icon-xs and icon-sm. An icon button needs an aria-label.
Block addons
block-start and block-end put the addon above or below the input, across the full width. The group grows to hold it.
Textarea
InputGroupTextarea takes the place of the input. Put the actions in a block-end addon.
Keyboard hint
Show a shortcut at the end of a search box with Kbd.
Loading
A Spinner in an end addon shows that the value is being checked. Say it in text too, in a status description.
Checking that the code is free.
Invalid
Set aria-invalid on the input. The group reads it and draws the border and ring in the destructive colour.
Disabled
Set isDisabled on the group and disabled on the input. The group dims its addons, and the input is removed from the tab order.
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus to the input, then to each button in an addon. |
| ShiftTab | Moves focus to the previous control. |
| Space | Presses a focused addon button. |
| Enter | Presses a focused addon button. |
React Aria provides:
- A
Groupwith thegrouprole around the input and its addons, and press events forInputGroupButton. - The keyboard behaviour of a button and a focus ring for keyboard focus only.
You provide:
- A
FieldLabelfor the input, or anaria-label. Text in an addon is not an accessible label. - An
aria-labelon every icon-onlyInputGroupButton. - Text alternatives for icons that carry meaning. Icons that only decorate need none.
aria-invalidon the input and a visible error message.
Guidelines
When to use
- To give a value its unit or symbol: euro, kilograms, a domain.
- To add one small action to a field: copy, clear, show password, search.
- For a search box with an icon and a shortcut hint.
- For a textarea with its send button inside the border.
When not to use
- For a plain input. Use Input.
- For a row of unrelated buttons. Use Button Group.
- For a long explanation. Use a
FieldDescription, see Field. - To replace the label. Keep a visible label.
Content
- Keep addon text to a symbol or a unit: "€", "kg", "https://".
- Use one addon on each side at most. A third element crowds the input.
- Keep the button action a verb or a familiar icon with an
aria-label: "Copy link".
Addons and labels
Use the addon for a unit and keep the label above the group.
Don’t use the addon as the label. It looks like one but it is not linked to the input.
API reference
InputGroup
Renders a React Aria Group. It accepts every React Aria Group prop. It draws the border, the focus ring and the invalid ring for the controls inside.
| Prop | Type | Default | Description |
|---|---|---|---|
isDisabled? | boolean | false | Marks the group as disabled, which dims the addons. Disable the input as well. |
className? | string | None | Classes merged after the base classes. |
InputGroupInput
Renders the Input without its own border or ring. It accepts every Input prop.
InputGroupTextarea
Renders the Textarea without its own border or ring, and without resizing. It accepts every Textarea prop.
InputGroupAddon
Renders a div with role="group". A click on it, away from a button, focuses the input. Place it after the input in the markup.
| Prop | Type | Default | Description |
|---|---|---|---|
align? | "inline-start" | "inline-end" | "block-start" | "block-end" | "inline-start" | Where the addon sits. The block values put it above or below the input across the full width. |
className? | string | None | Classes merged after the alignment classes. |
InputGroupButton
Renders a Button sized for the group, with the ghost variant and type="button" by default. It accepts every Button prop except size, plus the one below.
| Prop | Type | Default | Description |
|---|---|---|---|
size? | "xs" | "sm" | "icon-xs" | "icon-sm" | "xs" | The height and padding of the button. |
variant? | ButtonProps variant | "ghost" | Any Button variant. |
type? | "button" | "submit" | "reset" | "button" | The behaviour of the button in a form. |
InputGroupText
Renders a span of muted text for a symbol or a unit. It accepts every span prop.