Input group
An input or textarea with leading and trailing addons, buttons and text.
With an icon
import { SearchIcon } from "@keyline-icons/react"
import { InputGroup, InputGroupAddon, InputGroupInput } from "@/components/ui/input-group"
export default function InputGroupWithAnIcon() {
return (
<InputGroup className="max-w-xs">
<InputGroupAddon>
<SearchIcon />
</InputGroupAddon>
<InputGroupInput aria-label="Search tasks" placeholder="Search tasks" />
</InputGroup>
)
}
With text
import { InputGroup, InputGroupAddon, InputGroupInput, InputGroupText } from "@/components/ui/input-group"
import { Label } from "@/components/ui/label"
export default function InputGroupWithText() {
return (
<div className="grid w-full max-w-xs gap-4">
<div className="grid gap-2">
<Label htmlFor="input-group-address">Workspace address</Label>
<InputGroup>
<InputGroupAddon>
<InputGroupText>https://</InputGroupText>
</InputGroupAddon>
<InputGroupInput id="input-group-address" defaultValue="atlas" />
<InputGroupAddon align="inline-end">
<InputGroupText>.example.com</InputGroupText>
</InputGroupAddon>
</InputGroup>
</div>
<div className="grid gap-2">
<Label htmlFor="input-group-amount">Amount</Label>
<InputGroup>
<InputGroupAddon>
<InputGroupText>$</InputGroupText>
</InputGroupAddon>
<InputGroupInput id="input-group-amount" inputMode="decimal" defaultValue="1,250.00" />
<InputGroupAddon align="inline-end">
<InputGroupText>USD</InputGroupText>
</InputGroupAddon>
</InputGroup>
</div>
</div>
)
}
With a button
import { CopyIcon } from "@keyline-icons/react"
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group"
import { Label } from "@/components/ui/label"
export default function InputGroupWithAButton() {
return (
<div className="grid w-full max-w-xs gap-4">
<div className="grid gap-2">
<Label htmlFor="input-group-link">Invoice link</Label>
<InputGroup>
<InputGroupInput id="input-group-link" defaultValue="https://example.com/i/INV-2041" readOnly />
<InputGroupAddon align="inline-end">
<InputGroupButton size="icon-xs" aria-label="Copy the invoice link">
<CopyIcon />
</InputGroupButton>
</InputGroupAddon>
</InputGroup>
</div>
<div className="grid gap-2">
<Label htmlFor="input-group-code">Discount code</Label>
<InputGroup>
<InputGroupInput id="input-group-code" placeholder="SPRING-10" />
<InputGroupAddon align="inline-end">
<InputGroupButton variant="secondary">Apply</InputGroupButton>
</InputGroupAddon>
</InputGroup>
</div>
</div>
)
}
With a keyboard shortcut
import { SearchIcon } from "@keyline-icons/react"
import { InputGroup, InputGroupAddon, InputGroupInput } from "@/components/ui/input-group"
import { Kbd } from "@/components/ui/kbd"
export default function InputGroupWithAKeyboardShortcut() {
return (
<InputGroup className="max-w-xs">
<InputGroupAddon>
<SearchIcon />
</InputGroupAddon>
<InputGroupInput aria-label="Search projects" placeholder="Search projects" />
<InputGroupAddon align="inline-end">
<Kbd>/</Kbd>
</InputGroupAddon>
</InputGroup>
)
}
With a textarea
Comment on INV-2041
import { PaperclipIcon } from "@keyline-icons/react"
import {
InputGroup,
InputGroupAddon,
InputGroupButton,
InputGroupText,
InputGroupTextarea,
} from "@/components/ui/input-group"
export default function InputGroupWithATextarea() {
return (
<InputGroup className="max-w-sm">
<InputGroupAddon align="block-start">
<InputGroupText>Comment on INV-2041</InputGroupText>
</InputGroupAddon>
<InputGroupTextarea aria-label="Comment on INV-2041" placeholder="Ask a question or leave a note" />
<InputGroupAddon align="block-end">
<InputGroupButton size="icon-xs" aria-label="Attach a file">
<PaperclipIcon />
</InputGroupButton>
<InputGroupButton size="sm" variant="default" className="ml-auto">
Post
</InputGroupButton>
</InputGroupAddon>
</InputGroup>
)
}
Disabled
import { LockIcon } from "@keyline-icons/react"
import { InputGroup, InputGroupAddon, InputGroupInput } from "@/components/ui/input-group"
import { Label } from "@/components/ui/label"
export default function InputGroupDisabled() {
return (
<div className="group grid w-full max-w-xs gap-2" data-disabled="true">
<Label htmlFor="input-group-reference">Payment reference</Label>
<InputGroup data-disabled="true">
<InputGroupAddon>
<LockIcon />
</InputGroupAddon>
<InputGroupInput id="input-group-reference" defaultValue="PAY-88213" disabled />
</InputGroup>
</div>
)
}
Invalid
Enter a budget above zero.
import { Field, FieldError, FieldLabel } from "@/components/ui/field"
import { InputGroup, InputGroupAddon, InputGroupInput, InputGroupText } from "@/components/ui/input-group"
export default function InputGroupInvalid() {
return (
<Field data-invalid="true" className="max-w-xs">
<FieldLabel htmlFor="input-group-budget">Monthly budget</FieldLabel>
<InputGroup>
<InputGroupAddon>
<InputGroupText>$</InputGroupText>
</InputGroupAddon>
<InputGroupInput
id="input-group-budget"
inputMode="decimal"
defaultValue="0"
aria-invalid
aria-describedby="input-group-budget-error"
/>
</InputGroup>
<FieldError id="input-group-budget-error">Enter a budget above zero.</FieldError>
</Field>
)
}
Installation
npx shadcn@latest add @khaeli/input-groupbunx --bun shadcn@latest add @khaeli/input-groupInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- class-variance-authority ^0.7.1
Keyboard
Tab- Moves from the control to each button in the group, in the order they are written.
EnterSpace- Activate a focused button.
Accessible name
- The name belongs to the control: give
InputGroupInputorInputGroupTextareaanidand point a @khaeli/label at it withhtmlFor, or give it anaria-label. - An icon-only
InputGroupButtonneeds anaria-labelthat says what it does. An icon in an addon is decoration. - Text in an addon, such as a currency sign or a unit, is not part of the name. If the value cannot be understood without it, say it in the label: Amount in US dollars.
- For an invalid group, put
aria-invalidandaria-describedbyon the control. The whole group takes the invalid ring.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/input-group.json