Input group
An input with what belongs to it inside the same border: a unit or a prefix, an icon, a button that clears or copies. The group draws the border and the focus ring, so the parts read as one field. For a plain field use the input.
Install
npx shadcn@latest add https://thrumui.dev/r/input-group.jsonAdds components/ui/input-group.tsx. Nothing else in your project changes.
import { InputGroup } from "@/components/ui/input-group";Props
| Prop | Type | Default | Description |
|---|---|---|---|
InputGroup: size | "sm" | "md" | "lg" | Default "md" | sm is 28 pixels tall, md 32, lg 40. |
InputGroup: className | string | – | Extra classes for the group, merged last. It fills its container; set a width here. |
InputGroupAddon | ComponentProps<"div"> | – | Text, an icon or an InputGroupButton. Put it before the input to lead and after it to trail. A press on its text or icon moves the caret into the input. |
InputGroupInput | ComponentProps<"input"> | – | The input, with no border of its own. It takes every input attribute: value, onChange, name, type, placeholder, disabled, aria-invalid. |
InputGroupButton | ComponentProps<"button"> & { sound?: boolean } | – | A ghost button 24 pixels tall for an action on the field. With only an icon it needs an aria-label. Plays a soft tick on press by default. |
className | string | – | On every part: extra classes, merged last. |
Motion and sound
Motion
- The border colour changes over 150 ms when the field becomes invalid or valid again.
- The button's background fades in over 150 ms on hover and a press scales it to 97%.
- Reduced motion: these changes happen at once.
Sound
- A subtle tick when an InputGroupButton is pressed.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- The input is a native input element and the buttons are native buttons. The group itself adds no role.
- Name the input with a label element, as the example does. An addon's text is not part of that name: when it changes the meaning, as a unit does, say it in the label too.
- While the input has the focus, a 2 pixel ring in the theme's ring colour is drawn around the whole group. A focused button shows its own ring.
- An invalid input turns the group's border to the destructive colour; a disabled one dims the whole group to 50%.
- Buttons are 24 pixels tall and at least 24 wide, the minimum for a pointer target.
- Addons keep their width and the input takes the rest, so keep addons short. Nothing overflows at 160 pixels wide.
| Keys | Action |
|---|---|
| Tab | Focus the input, then each button in the group, in the order they are written. |
| Enter / Space | On a button: press it. |
Source
"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { buttonClass } from "@/components/ui/button";
export type InputGroupSize = "sm" | "md" | "lg";
export type InputGroupProps = ComponentProps<"div"> & {
/** `sm` is 28 pixels tall, `md` 32, `lg` 40. */
size?: InputGroupSize;
};
const SIZE: Record<InputGroupSize, string> = { sm: "h-7", md: "h-8", lg: "h-10" };
/**
* One field made of an input and what belongs to it: a unit, an icon, a button. The border, the focus ring and the
* invalid and disabled looks are drawn on the group, so the parts read as one control.
*/
export function InputGroup({ size = "md", className, ...props }: InputGroupProps) {
return (
<div
data-slot="input-group"
data-surface="control"
className={cn(
"flex w-full min-w-0 items-center gap-2 rounded-md border border-input bg-background px-2.5 text-foreground",
"transition-[border-color] duration-150 ease-out motion-reduce:transition-none",
// The ring belongs to the input. A button inside the group shows its own.
"has-[input:focus-visible]:outline-2 has-[input:focus-visible]:-outline-offset-1 has-[input:focus-visible]:outline-ring",
"has-[input:disabled]:cursor-not-allowed has-[input:disabled]:opacity-50 has-[input[aria-invalid=true]]:border-destructive",
// The whole group is dimmed already: a disabled button inside is not dimmed a second time.
"[&:has(input:disabled)_button:disabled]:opacity-100",
SIZE[size],
className,
)}
{...props}
/>
);
}
/**
* Text, an icon or an `InputGroupButton` before or after the input: its place in the markup is its place in the group.
* A press on its text or icon puts the caret in the input.
*/
export function InputGroupAddon({ className, onClick, ...props }: ComponentProps<"div">) {
return (
<div
data-slot="input-group-addon"
className={cn(
"flex flex-none items-center gap-1.5 text-sm whitespace-nowrap text-muted-foreground select-none [&_svg]:size-4 [&_svg]:flex-none",
// A button sits closer to the edge than text does, so its own padding lines up with the field's.
"first:has-[button]:-ms-1.5 last:has-[button]:-me-1.5",
className,
)}
onClick={(event) => {
onClick?.(event);
if (!(event.target as HTMLElement).closest("button, a, input")) event.currentTarget.parentElement?.querySelector("input")?.focus();
}}
{...props}
/>
);
}
/** The input itself. It has no border and no ring of its own: the group draws both. */
export function InputGroupInput({ type = "text", className, ...props }: ComponentProps<"input">) {
return (
<input
data-slot="input-group-input"
type={type}
className={cn(
// 16px text on phones: anything smaller makes iOS zoom the page when the field takes focus.
"h-full min-w-0 flex-1 bg-transparent text-base text-foreground outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed md:text-sm",
className,
)}
{...props}
/>
);
}
export type InputGroupButtonProps = ComponentProps<"button"> & {
/** Play a subtle tick on press, if the visitor has sound on. */
sound?: boolean;
};
/** A small button inside the group: clear, copy, show. With only an icon it needs an `aria-label`. */
export function InputGroupButton({ type = "button", sound = true, onClick, className, ...props }: InputGroupButtonProps) {
return (
<button
data-slot="input-group-button"
type={type}
onClick={(event) => {
if (sound) play("tick");
onClick?.(event);
}}
className={cn(buttonClass({ variant: "ghost", size: "sm" }), "h-6 min-w-6 gap-1 rounded-sm px-1.5 [&_svg]:size-4", className)}
{...props}
/>
);
}Files and dependencies
- Adds
components/ui/input-group.tsx- Packages
- None
- Brings along
- ButtonSound engine
Changelog
- 1.0.0 · 2026-12-03First release.