Compact toasters
Five dense toast triggers for short confirmations: a minimal line, a content-width inline row, a rounded pill, an icon tile and a bare inverted surface. Small close buttons keep an invisible hit-area extension. Each is self-contained (no sonner package), sized, token-driven, and auto-dismisses.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/sonner-compactDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/sonner-compact.tsx"use client"
import * as React from "react"
import { AlertTriangle, CheckCircle, Info, X } from "lucide-react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Styled sonner - compact family: 5 self-contained toasters. NO sonner package and
NO portal - each export renders a demo trigger button; clicking it pushes a toast
onto a LOCAL stack (a useState array), and it appears in the bottom-right corner
(fixed bottom-right, z-50). The difference: a minimal/dense body - narrow
padding, a single line, a pill form, icon-weighted, or without a frame/shadow.
The small close buttons (under 24px) carry an invisible hit-area extension
(after:-inset-2). Color comes ONLY from tokens, via alpha color-mix.
Deterministic: the ids come from a ref counter (NO Date.now/Math.random). */
export type StyledSize = "sm" | "md" | "lg" | "xl"
export type StyledTone = "success" | "info" | "warning" | "danger"
/* Toast width follows size (for fixed-width bodies). */
const toastWidth: Record<StyledSize, string> = {
sm: "w-64",
md: "w-72",
lg: "w-80",
xl: "w-96",
}
/* Yogun govdenin padding + gap olcegi. */
const compactPad: Record<StyledSize, string> = {
sm: "gap-2 px-2.5 py-1.5",
md: "gap-2 px-3 py-2",
lg: "gap-2.5 px-3.5 py-2.5",
xl: "gap-3 px-4 py-3",
}
/* Yogun govdenin yazi olcegi. */
const compactText: Record<StyledSize, string> = {
sm: "text-xs",
md: "text-sm",
lg: "text-sm",
xl: "text-base",
}
/* Tone -> ikon rengi (semantic token). */
const toneIconColor: Record<StyledTone, string> = {
success: "text-success",
info: "text-info",
warning: "text-warning-soft-foreground",
danger: "text-danger",
}
/* Tone -> ikon bileseni. */
const toneIcon: Record<StyledTone, React.ComponentType<{ className?: string }>> = {
success: CheckCircle,
info: Info,
warning: AlertTriangle,
danger: X,
}
/* Tone -> yumusak yuzey (soft token cifti). */
const toneSoft: Record<StyledTone, string> = {
success: "bg-success-soft text-success-soft-foreground",
info: "bg-info-soft text-info-soft-foreground",
warning: "bg-warning-soft text-warning-soft-foreground",
danger: "bg-danger-soft text-danger-soft-foreground",
}
const triggerBtn =
"inline-flex h-9 shrink-0 select-none items-center justify-center gap-2 whitespace-nowrap rounded-lg border border-border bg-secondary px-4 text-sm font-medium text-secondary-foreground outline-none transition-colors hover:bg-[color-mix(in_oklab,var(--color-foreground)_6%,transparent)] focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
/* A close button that stays under 24px: the visual box does not grow, the touch area does (after:-inset-2 -> a 32px target). The icon measurements are written with important: the [&_svg]:size-4 rule on the body has the same specificity and would otherwise override them. */
const tinyCloseBtn =
"relative inline-flex size-4 shrink-0 items-center justify-center rounded-sm text-muted-foreground outline-none transition-colors after:absolute after:-inset-2 hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_svg]:size-3! [&_svg]:shrink-0 [&_i]:text-xs! [&_i]:leading-none"
const compactBase =
"pointer-events-auto relative flex items-center overflow-hidden [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const stackClass =
"pointer-events-none fixed bottom-0 right-0 z-50 flex flex-col items-end gap-2 p-4"
/* Otomatik kapanma suresi (sabit, deterministik). */
const TOAST_MS = 4000
/* A slide+fade enter/exit; instant under reduced-motion (opacity only). */
function toastMotion(reduce: boolean | null) {
if (reduce) {
return {
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { duration: 0.12 },
}
}
return {
initial: { opacity: 0, x: 24, scale: 0.96 },
animate: { opacity: 1, x: 0, scale: 1 },
exit: { opacity: 0, x: 24, scale: 0.96 },
transition: { type: "spring" as const, stiffness: 340, damping: 28 },
}
}
interface ToastItem {
id: number
message: string
}
/* Shared stack logic: ref-counted id, fixed timeout, every timer cleared on
unmount (no setState-after-unmount). */
function useToastStack(message: string) {
const [items, setItems] = React.useState<ToastItem[]>([])
const counter = React.useRef(0)
const timers = React.useRef<Map<number, ReturnType<typeof setTimeout>>>(new Map())
const dismiss = React.useCallback((id: number) => {
setItems((prev) => prev.filter((t) => t.id !== id))
const timer = timers.current.get(id)
if (timer) {
clearTimeout(timer)
timers.current.delete(id)
}
}, [])
const push = React.useCallback(() => {
const id = counter.current++
setItems((prev) => [...prev, { id, message }])
const timer = setTimeout(() => dismiss(id), TOAST_MS)
timers.current.set(id, timer)
}, [dismiss, message])
React.useEffect(() => {
const map = timers.current
return () => {
map.forEach((t) => clearTimeout(t))
map.clear()
}
}, [])
return { items, push, dismiss }
}
interface CompactToasterProps {
className?: string
size?: StyledSize
tone?: StyledTone
label?: React.ReactNode
}
/* Single body: bodyClass plus the content rendering are variant-specific. */
function CompactShell({
message,
className,
size,
tone,
label,
bodyClass,
children,
}: Required<Pick<CompactToasterProps, "size" | "tone">> & {
message: string
className?: string
label?: React.ReactNode
bodyClass: string
children: (item: ToastItem, dismiss: (id: number) => void) => React.ReactNode
}) {
const reduce = useReducedMotion()
const { items, push, dismiss } = useToastStack(message)
const m = toastMotion(reduce)
return (
<div data-slot="styled-sonner" className={cn("inline-flex", className)}>
<button type="button" className={triggerBtn} onClick={push}>
{label}
</button>
<div className={stackClass}>
<AnimatePresence initial={false}>
{items.map((t) => (
<motion.div
key={t.id}
role="status"
data-tone={tone}
className={cn(compactBase, compactPad[size], compactText[size], bodyClass)}
initial={m.initial}
animate={m.animate}
exit={m.exit}
transition={m.transition}
layout={!reduce}
>
{children(t, dismiss)}
</motion.div>
))}
</AnimatePresence>
</div>
</div>
)
}
/* -------------------------------------------------------------- MinimalToaster A one-line message with no icon and tight padding plus a small close. */
export function MinimalToaster({
className,
size = "md",
tone = "info",
label = "Show toast",
}: CompactToasterProps) {
return (
<CompactShell
message="Draft saved."
className={className}
size={size}
tone={tone}
label={label}
bodyClass={cn(
"rounded-lg border border-border bg-popover text-popover-foreground shadow-sm",
toastWidth[size]
)}
>
{(t, dismiss) => (
<>
<div className="min-w-0 flex-1 truncate">{t.message}</div>
<button
type="button"
aria-label="Dismiss"
className={tinyCloseBtn}
onClick={() => dismiss(t.id)}
>
<X />
</button>
</>
)}
</CompactShell>
)
}
/* --------------------------------------------------------------- InlineToaster Icon plus message on one line, width following the content; no close button, it closes on timeout. */
export function InlineToaster({
className,
size = "md",
tone = "info",
label = "Show toast",
}: CompactToasterProps) {
const Icon = toneIcon[tone]
return (
<CompactShell
message="Copied to clipboard"
className={className}
size={size}
tone={tone}
label={label}
bodyClass="max-w-full rounded-lg border border-border bg-popover text-popover-foreground shadow-sm"
>
{(t) => (
<>
<Icon className={toneIconColor[tone]} />
<span className="min-w-0 truncate">{t.message}</span>
</>
)}
</CompactShell>
)
}
/* -----------------------------------------------------------------
PillToaster
A fully rounded pill form, over a tone surface. */
export function PillToaster({
className,
size = "md",
tone = "info",
label = "Show toast",
}: CompactToasterProps) {
const Icon = toneIcon[tone]
return (
<CompactShell
message="Link shared"
className={className}
size={size}
tone={tone}
label={label}
bodyClass={cn(
"max-w-full rounded-full border border-[color-mix(in_oklab,var(--color-foreground)_10%,transparent)] shadow-sm",
toneSoft[tone]
)}
>
{(t, dismiss) => (
<>
<Icon />
<span className="min-w-0 truncate font-medium">{t.message}</span>
<button
type="button"
aria-label="Dismiss"
className={tinyCloseBtn}
onClick={() => dismiss(t.id)}
>
<X />
</button>
</>
)}
</CompactShell>
)
}
/* ----------------------------------------------------------------- IconToaster
Ikon agirlikli: renkli ton karesi + kisa etiket. */
export function IconToaster({
className,
size = "md",
tone = "info",
label = "Show toast",
}: CompactToasterProps) {
const Icon = toneIcon[tone]
return (
<CompactShell
message="Uploaded"
className={className}
size={size}
tone={tone}
label={label}
bodyClass="max-w-full rounded-lg border border-border bg-popover text-popover-foreground shadow-sm"
>
{(t) => (
<>
<span
className={cn(
"inline-flex size-6 shrink-0 items-center justify-center rounded-md",
toneSoft[tone]
)}
>
<Icon />
</span>
<span className="min-w-0 truncate font-medium">{t.message}</span>
</>
)}
</CompactShell>
)
}
/* -----------------------------------------------------------------
BareToaster
No frame and no shadow: just text on a soft surface. */
export function BareToaster({
className,
size = "md",
tone = "info",
label = "Show toast",
}: CompactToasterProps) {
return (
<CompactShell
message="Nothing to see here."
className={className}
size={size}
tone={tone}
label={label}
bodyClass={cn(
"rounded-md bg-[color-mix(in_oklab,var(--color-foreground)_88%,transparent)] text-background",
toastWidth[size]
)}
>
{(t, dismiss) => (
<>
<div className="min-w-0 flex-1 truncate">{t.message}</div>
<button
type="button"
aria-label="Dismiss"
className={cn(tinyCloseBtn, "text-background/70 hover:text-background")}
onClick={() => dismiss(t.id)}
>
<X />
</button>
</>
)}
</CompactShell>
)
}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.
Minimal
One tight line of text with a small close button.
import { MinimalToaster } from "@/components/ui/sonner-compact"
<MinimalToaster />Inline
An icon and a label on a single content-width line.
import { InlineToaster } from "@/components/ui/sonner-compact"
<InlineToaster />Pill
A fully rounded pill on a soft tone surface.
import { PillToaster } from "@/components/ui/sonner-compact"
<PillToaster />Icon
An icon tile leading a very short label.
import { IconToaster } from "@/components/ui/sonner-compact"
<IconToaster />Bare
No border and no shadow, just an inverted surface.
import { BareToaster } from "@/components/ui/sonner-compact"
<BareToaster />ai2 Compact toasters: 5 styled variations on the token system
The ai2 Compact toasters are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around minimal, dense toast triggers for short confirmations. 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: framer-motion slides each compact toast in and out from the right edge. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the slide is skipped and the toast fades in place.
What is in the ai2 Compact toasters?
5 exports in one file: Minimal, Inline, Pill, Icon and Bare. 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: framer-motion slides each compact toast in and out from the right edge.
- Reduced-motion aware: Under prefers-reduced-motion, the slide is skipped and the toast fades in place.
- 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 Compact toasters 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.