Action input groups
Five inputs with a trailing action that really works: a clipboard copy with an announced copied state, a clipboard paste, a password reveal toggle, a deterministic token generator and a fixed timeout loading state. Clipboard access is guarded, so a denied permission never throws.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/input-group-actionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install lucide-reactCopy the source
components/ui/input-group-action.tsx"use client"
import * as React from "react"
import { Check, ClipboardPaste, Copy, Eye, EyeOff, Loader2, RefreshCw } from "lucide-react"
import { cn } from "@/lib/utils"
/* Action input group family: 5 trailing action buttons, with REAL behavior.
copy (navigator.clipboard.writeText; wrapped in try/catch so it cannot throw, and
the copied state is announced with aria-live), paste (clipboard.readText, the same
protection), reveal (a real password show/hide, with the state announced through
aria-pressed), generate (a DETERMINISTIC generator: NO Math.random, a token is
produced from an incrementing index counter over a fixed alphabet - the same index
always gives the same value), loading (a fixed-duration loading state, with the
timeout cleared in the effect cleanup).
All useState lives at the root level, never in a subtree that unmounts.
Color comes ONLY from tokens, via alpha color-mix. No motion (the loader's
animate-spin deliberately keeps spinning so it never reads as a hang). */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const height: Record<StyledSize, string> = {
sm: "h-8 text-sm",
md: "h-9 text-sm",
lg: "h-10 text-base",
xl: "h-12 text-base",
}
const rootBase =
"group inline-flex w-64 max-w-full items-center overflow-hidden rounded-lg border border-field-border bg-transparent transition-colors duration-(--motion-base) focus-within:border-primary focus-within:ring-[3px] focus-within:ring-ring/50 [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const fieldBase =
"h-full w-full min-w-0 bg-transparent px-3 text-foreground outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50"
const actionBtn =
"flex h-full shrink-0 select-none items-center gap-1.5 border-l border-field-border bg-secondary px-3 text-sm font-medium text-secondary-foreground outline-none transition-colors hover:bg-surface-3 focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50"
const iconActionBtn =
"mr-1.5 inline-flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground outline-none transition-colors hover:bg-[color-mix(in_oklab,var(--color-foreground)_8%,transparent)] hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50"
type Props = Omit<React.ComponentProps<"input">, "size"> & { size?: StyledSize }
/* Copy: gercek pano kopyalama. clipboard reddedebilir (izin/guvensiz baglam),
bu yuzden try/catch; hata durumunda buton sessizce eski haline doner. */
export function CopyGroup({ className, size = "md", ...props }: Props) {
const id = React.useId()
const [value, setValue] = React.useState("https://ai2.dev/r/button.json")
const [copied, setCopied] = React.useState(false)
React.useEffect(() => {
if (!copied) return
const t = window.setTimeout(() => setCopied(false), 1500)
return () => window.clearTimeout(t)
}, [copied])
return (
<div data-slot="styled-input-group" className={cn(rootBase, height[size], className)}>
<label htmlFor={id} className="sr-only">
Copyable value
</label>
<input
id={id}
{...props}
value={value}
onChange={(e) => setValue(e.target.value)}
className={fieldBase}
/>
<span aria-live="polite" className="sr-only">
{copied ? "Copied to clipboard" : ""}
</span>
<button
type="button"
aria-label={copied ? "Copied" : "Copy value"}
className={actionBtn}
onClick={async () => {
try {
await navigator.clipboard.writeText(value)
setCopied(true)
} catch {
setCopied(false)
}
}}
>
{copied ? <Check className="text-success" /> : <Copy />}
<span>{copied ? "Copied" : "Copy"}</span>
</button>
</div>
)
}
/* Paste: panodan okur. Okuma izni reddedilebilir; try/catch ile alan degismez. */
export function PasteGroup({ className, size = "md", ...props }: Props) {
const id = React.useId()
const [value, setValue] = React.useState("")
return (
<div data-slot="styled-input-group" className={cn(rootBase, height[size], className)}>
<label htmlFor={id} className="sr-only">
Pasteable value
</label>
<input
id={id}
placeholder="Paste a value"
{...props}
value={value}
onChange={(e) => setValue(e.target.value)}
className={fieldBase}
/>
<button
type="button"
aria-label="Paste from clipboard"
className={actionBtn}
onClick={async () => {
try {
const text = await navigator.clipboard.readText()
if (text) setValue(text)
} catch {
/* Without clipboard read permission the field is left as it is. */
}
}}
>
<ClipboardPaste />
<span>Paste</span>
</button>
</div>
)
}
/* Reveal: gercek sifre goster/gizle. Durum aria-pressed + degisen aria-label
ile duyurulur. */
export function RevealGroup({ className, size = "md", ...props }: Props) {
const id = React.useId()
const [shown, setShown] = React.useState(false)
return (
<div data-slot="styled-input-group" className={cn(rootBase, height[size], className)}>
<label htmlFor={id} className="sr-only">
Password
</label>
<input
id={id}
placeholder="Password"
autoComplete="current-password"
{...props}
type={shown ? "text" : "password"}
className={fieldBase}
/>
<button
type="button"
aria-pressed={shown}
aria-label={shown ? "Hide password" : "Show password"}
className={iconActionBtn}
onClick={() => setShown((v) => !v)}
>
{shown ? <EyeOff /> : <Eye />}
</button>
</div>
)
}
/* Deterministic token generator: NO Math.random. The given index is mapped to a fixed alphabet through a plain multiply and modulo, so the same index always produces the same token (SSR and the client agree). */
const ALPHABET = "abcdefghijkmnpqrstuvwxyz23456789"
function tokenAt(index: number) {
let out = ""
for (let i = 0; i < 12; i++) {
const step = (index + 1) * 7 + i * 13
out += ALPHABET[step % ALPHABET.length]
}
return out
}
/* Generate: increments the counter on every click and writes the deterministic token into the field. */
export function GenerateGroup({ className, size = "md", ...props }: Props) {
const id = React.useId()
const [index, setIndex] = React.useState(0)
const [value, setValue] = React.useState(() => tokenAt(0))
return (
<div data-slot="styled-input-group" className={cn(rootBase, height[size], className)}>
<label htmlFor={id} className="sr-only">
Generated token
</label>
<input
id={id}
{...props}
value={value}
onChange={(e) => setValue(e.target.value)}
className={cn(fieldBase, "font-mono")}
/>
<button
type="button"
aria-label="Generate a new token"
className={actionBtn}
onClick={() => {
const next = index + 1
setIndex(next)
setValue(tokenAt(next))
}}
>
<RefreshCw />
<span>New</span>
</button>
</div>
)
}
/* Loading: sabit sureli (1200ms) yukleme durumu; timeout effect cleanup'inda
temizlenir, bu yuzden unmount sonrasi setState olmaz. */
export function LoadingGroup({ className, size = "md", ...props }: Props) {
const id = React.useId()
const [loading, setLoading] = React.useState(false)
React.useEffect(() => {
if (!loading) return
const t = window.setTimeout(() => setLoading(false), 1200)
return () => window.clearTimeout(t)
}, [loading])
return (
<div data-slot="styled-input-group" className={cn(rootBase, height[size], className)}>
<label htmlFor={id} className="sr-only">
Value to check
</label>
<input id={id} placeholder="Value" {...props} className={fieldBase} />
<span aria-live="polite" className="sr-only">
{loading ? "Checking" : ""}
</span>
<button
type="button"
aria-label={loading ? "Checking" : "Check value"}
disabled={loading}
className={actionBtn}
onClick={() => setLoading(true)}
>
{loading ? <Loader2 className="animate-spin" /> : null}
<span>{loading ? "Checking" : "Check"}</span>
</button>
</div>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Copy
A real clipboard copy with a copied state that is announced.
import { CopyGroup } from "@/components/ui/input-group-action"
<CopyGroup />Paste
Reads the clipboard and fills the field when permission allows.
import { PasteGroup } from "@/components/ui/input-group-action"
<PasteGroup />Reveal
A real password show and hide toggle that announces its state.
import { RevealGroup } from "@/components/ui/input-group-action"
<RevealGroup />Generate
A deterministic index based token generator, never random.
import { GenerateGroup } from "@/components/ui/input-group-action"
<GenerateGroup />Loading
A fixed timeout loading state that is cleared on cleanup.
import { LoadingGroup } from "@/components/ui/input-group-action"
<LoadingGroup />ai2 Action input groups: 5 styled variations on the token system
The ai2 Action input groups are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around inputs with a working trailing action button. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: the loading spinner keeps rotating so a pending state never reads as a hang. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, no transforms are used and every action still works.
What is in the ai2 Action input groups?
5 exports in one file: Copy, Paste, Reveal, Generate and Loading. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: the loading spinner keeps rotating so a pending state never reads as a hang.
- Reduced-motion aware: Under prefers-reduced-motion, no transforms are used and every action still works.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Action input groups sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.