Input group
42 lines · 1 file · MIT
An input with an icon, a prefix or a shortcut hint fixed inside its ring.
import { forwardRef, type InputHTMLAttributes, type ReactNode } from "react";
import { cn } from "./cn";
import { NO_AUTOFILL } from "./field";
/**
* An input with something fixed on either side: an icon, a `https://` prefix,
* a ⌘K hint. The slots sit inside the field's own ring, so it reads as one
* control, and the ring takes the accent when the input inside has focus.
* A slot is text or an icon; it does not take clicks, so the whole box still
* puts the caret in the input.
*/
export const InputGroup = forwardRef<
HTMLInputElement,
Omit<InputHTMLAttributes<HTMLInputElement>, "prefix"> & {
leading?: ReactNode;
trailing?: ReactNode;
/** Set the text in the mono face, for a slug or an address. */
mono?: boolean;
className?: string;
}
>(({ leading, trailing, mono, className, ...props }, ref) => (
<label
className={cn(
"flex h-9 w-full min-w-0 cursor-text items-center gap-2 rounded-lg bg-surface px-3 text-sm text-text smooth-shadow-ring-xs transition-[box-shadow] duration-150 focus-within:shadow-[0_0_0_1.5px_var(--accent),0_0_0_4px_color-mix(in_oklab,var(--accent)_25%,transparent)] has-[input[aria-invalid=true]]:shadow-[0_0_0_1.5px_var(--danger)] has-[input:disabled]:opacity-50",
className,
)}
>
{leading && <span className="flex shrink-0 items-center text-muted">{leading}</span>}
<input
ref={ref}
{...(!props.autoComplete || props.autoComplete === "off" ? NO_AUTOFILL : undefined)}
{...props}
className={cn(
"h-full min-w-0 flex-1 bg-transparent outline-none placeholder:text-placeholder",
mono && "font-mono text-[13px]",
)}
/>
{trailing && <span className="flex shrink-0 items-center text-muted">{trailing}</span>}
</label>
));
InputGroup.displayName = "InputGroup";
Install
One command. The files land in your repo, with everything they import.
$ bunx shadcn@latest add @blocco/input-group- Category
- Forms
- Files
- input-group.tsx