Input Group
One drawn box around a field and what sits beside it: an icon, a prefix, a button, a shortcut.
import { InkIcon } from "@/components/ui/ink-icons";
import { InputGroup, InputGroupAddon, InputGroupInput, InputGroupText } from "@/components/ui/input-group";
export default function InputGroupDemo() {
return (
<div className="flex w-full max-w-md flex-col gap-6">
<InputGroup seed="ig-search">
<InputGroupInput placeholder="Search…" aria-label="Search" />
<InputGroupAddon>
<InkIcon name="search" />
</InputGroupAddon>
<InputGroupAddon align="inline-end">12 results</InputGroupAddon>
</InputGroup>
<InputGroup seed="ig-email">
<InputGroupInput type="email" placeholder="you" aria-label="Email" />
<InputGroupAddon align="inline-end">
<InputGroupText>@example.com</InputGroupText>
</InputGroupAddon>
</InputGroup>
<InputGroup seed="ig-price">
<InputGroupAddon>
<InputGroupText>$</InputGroupText>
</InputGroupAddon>
<InputGroupInput placeholder="0.00" aria-label="Amount" inputMode="decimal" />
<InputGroupAddon align="inline-end">
<InputGroupText>USD</InputGroupText>
</InputGroupAddon>
</InputGroup>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ input-group"use client";
import type { ComponentProps } from "react";
import { Input as InputPrimitive } from "@base-ui/react/input";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { InkOutline } from "@/lib/ink-outline";
import { Button } from "@/components/ui/button";
/**
* One drawn box around a field and the things beside it: an icon, a prefix
* such as https://, a button, a shortcut hint. The box is drawn once, round
* all of it; focus inside draws one more pass in full ink.
*/
function InputGroup({
className,
children,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: ComponentProps<"div"> & Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & { seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
return (
<div
role="group"
data-slot="input-group"
className={cn(
"group/input-group ink-within relative flex h-10 w-full min-w-0 items-center",
// A textarea or a block addon makes it taller and stacks the parts.
"has-[>textarea]:h-auto has-[>[data-align=block-end]]:h-auto has-[>[data-align=block-start]]:h-auto has-[>[data-align=block-end]]:flex-col has-[>[data-align=block-start]]:flex-col has-[>[data-align=block-end]]:items-stretch has-[>[data-align=block-start]]:items-stretch",
"has-[>[data-align=block-start]]:[&>input]:pb-3 has-[>[data-align=block-end]]:[&>input]:pt-3",
className,
)}
{...props}
>
<InkOutline
pen={pen}
seed={seed}
estimate={[320, 40]}
focusPass
maxRadius={18}
className={cn(
"text-ink-3 transition-colors duration-(--dur-hover)",
"group-hover/input-group:text-ink group-focus-within/input-group:text-ink",
"group-has-[[aria-invalid=true]]/input-group:text-destructive group-has-[[data-invalid]]/input-group:text-destructive",
"group-has-[[data-slot=input-group-control]:disabled]/input-group:opacity-50",
)}
/>
{children}
</div>
);
}
const inputGroupAddonVariants = cva(
"relative flex h-auto cursor-text items-center justify-center gap-2 py-1.5 text-base text-ink-3 select-none group-data-[disabled=true]/input-group:opacity-50 [&>svg:not(.ink-sketch):not([class*='size-'])]:size-4 [&>kbd]:rounded-none",
{
variants: {
align: {
"inline-start": "order-first pl-3 has-[>button]:-ml-1.5",
"inline-end": "order-last pr-3 has-[>button]:-mr-1.5",
"block-start": "order-first w-full justify-start px-3.5 pt-3.5 [.border-b]:pb-3",
"block-end": "order-last w-full justify-start px-3.5 pb-3.5 [.border-t]:pt-3",
},
},
defaultVariants: { align: "inline-start" },
},
);
/** The part beside the field. Clicking it puts the cursor in the field. */
function InputGroupAddon({ className, align = "inline-start", onClick, ...props }: ComponentProps<"div"> & VariantProps<typeof inputGroupAddonVariants>) {
return (
<div
role="group"
data-slot="input-group-addon"
data-align={align}
className={cn(inputGroupAddonVariants({ align }), className)}
onClick={(e) => {
onClick?.(e);
if ((e.target as HTMLElement).closest("button")) return;
e.currentTarget.parentElement?.querySelector<HTMLElement>("input, textarea")?.focus();
}}
{...props}
/>
);
}
/** A button small enough to live inside the box. */
function InputGroupButton({ className, variant = "ghost", size = "xs", type = "button", ...props }: Omit<ComponentProps<typeof Button>, "size"> & { size?: "xs" | "sm" | "icon-xs" | "icon-sm" }) {
return <Button type={type} variant={variant} size={size} data-size={size} className={cn("shadow-none", className)} {...props} />;
}
function InputGroupText({ className, ...props }: ComponentProps<"span">) {
return <span data-slot="input-group-text" className={cn("flex items-center gap-2 text-base text-ink-3 [&_svg:not(.ink-sketch)]:pointer-events-none [&_svg:not(.ink-sketch):not([class*='size-'])]:size-4", className)} {...props} />;
}
function InputGroupInput({ className, ...props }: InputPrimitive.Props) {
return (
<InputPrimitive
data-slot="input-group-control"
className={cn(
"relative h-full min-w-0 flex-1 bg-transparent px-3 text-base text-foreground outline-none placeholder:text-ink-3 disabled:cursor-not-allowed",
"group-has-[>[data-align=inline-start]]/input-group:pl-1.5 group-has-[>[data-align=inline-end]]/input-group:pr-1.5",
className as string,
)}
{...props}
/>
);
}
function InputGroupTextarea({ className, ...props }: ComponentProps<"textarea">) {
return (
<textarea
data-slot="input-group-control"
className={cn("relative min-h-20 w-full flex-1 resize-none bg-transparent px-3 py-2.5 text-base text-foreground outline-none placeholder:text-ink-3 disabled:cursor-not-allowed", className)}
{...props}
/>
);
}
export { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, InputGroupText, InputGroupTextarea };Usage
import { InputGroup, InputGroupAddon, InputGroupInput } from "@/components/ui/input-group"
<InputGroup>
<InputGroupInput placeholder="Search…" />
<InputGroupAddon>…</InputGroupAddon>
</InputGroup>Examples
With buttons
Copy a value, or show and hide a password, from inside the box.
"use client";
import { useState } from "react";
import { InkIcon } from "@/components/ui/ink-icons";
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group";
export default function InputGroupButtonExample() {
const [copied, setCopied] = useState(false);
const [shown, setShown] = useState(false);
return (
<div className="flex w-full max-w-md flex-col gap-6">
<InputGroup seed="igb-copy">
<InputGroupInput readOnly defaultValue="https://ballpoint.st9wd.com/r/button.json" aria-label="Registry address" />
<InputGroupAddon align="inline-end">
<InputGroupButton
aria-label="Copy"
size="icon-xs"
onClick={() => {
setCopied(true);
setTimeout(() => setCopied(false), 1500);
}}
>
<InkIcon name={copied ? "check" : "copy"} />
</InputGroupButton>
</InputGroupAddon>
</InputGroup>
<InputGroup seed="igb-password">
<InputGroupInput type={shown ? "text" : "password"} defaultValue="correct horse" aria-label="Password" />
<InputGroupAddon align="inline-end">
<InputGroupButton onClick={() => setShown((v) => !v)} aria-pressed={shown}>
{shown ? "Hide" : "Show"}
</InputGroupButton>
</InputGroupAddon>
</InputGroup>
</div>
);
}With a textarea
A message field with its tools underneath.
0 / 280
import { InkIcon } from "@/components/ui/ink-icons";
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupText, InputGroupTextarea } from "@/components/ui/input-group";
export default function InputGroupTextareaExample() {
return (
<InputGroup seed="igt" className="max-w-md">
<InputGroupTextarea placeholder="Write a note to the margin…" aria-label="Note" />
<InputGroupAddon align="block-end">
<InputGroupButton aria-label="Attach a file" size="icon-xs">
<InkIcon name="plus" />
</InputGroupButton>
<InputGroupText className="ml-auto text-sm">0 / 280</InputGroupText>
<InputGroupButton variant="default" size="sm">
Post
</InputGroupButton>
</InputGroupAddon>
</InputGroup>
);
}Above and below
A label or hint inside the box, over or under the field.
Database name
As it should appear on the page.
import { InkIcon } from "@/components/ui/ink-icons";
import { InputGroup, InputGroupAddon, InputGroupInput, InputGroupText } from "@/components/ui/input-group";
export default function InputGroupBlock() {
return (
<div className="flex w-full max-w-md flex-col gap-6">
<InputGroup seed="igk-top">
<InputGroupAddon align="block-start">
<InputGroupText>
<InkIcon name="database" /> Database name
</InputGroupText>
</InputGroupAddon>
<InputGroupInput placeholder="notebook_production" aria-label="Database name" className="h-10" />
</InputGroup>
<InputGroup seed="igk-bottom">
<InputGroupInput placeholder="Your name" aria-label="Your name" className="h-10" />
<InputGroupAddon align="block-end">
<InputGroupText className="text-sm">As it should appear on the page.</InputGroupText>
</InputGroupAddon>
</InputGroup>
</div>
);
}API reference
- InputGroupAddon aligndefault
"inline-start" "inline-start" | "inline-end" | "block-start" | "block-end"Which side of the field the addon sits on. Block addons stack above or below it.
- InputGroupButton sizedefault
"xs" "xs" | "sm" | "icon-xs" | "icon-sm"Buttons small enough to live inside the box.
- InputGroupInput / Textarea
input / textareaThe field itself, with no box of its own.
- seed
string | numberPins the drawing.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.