Motion toasters
Five toast triggers that keep the same body and vary only the entrance and exit: a spring slide, a snappy pop, a quiet fade, a perspective flip and a bouncing spring. 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-motionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/sonner-motion.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 - motion 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: the character of the enter/exit motion
(slide, pop, fade, flip, bounce). AnimatePresence drives it; under reduced-motion
there is NO transform, only opacity.
Color comes ONLY from tokens, via alpha color-mix. Deterministic: the ids come
from a ref counter (NO Date.now/Math.random) and the timeout is fixed. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
export type StyledTone = "success" | "info" | "warning" | "danger"
/* Toast genisligi size'a bagli. */
const toastWidth: Record<StyledSize, string> = {
sm: "w-64",
md: "w-72",
lg: "w-80",
xl: "w-96",
}
/* 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,
}
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"
const closeBtn =
"inline-flex size-6 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 [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const toastBase =
"pointer-events-auto relative flex items-start gap-3 overflow-hidden rounded-xl border border-border bg-popover p-4 text-sm text-popover-foreground shadow-lg [&_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
type Flavor = "slide" | "pop" | "fade" | "flip" | "bounce"
/* Under reduced-motion every flavour is the same: no transform, a short opacity transition. */
const reducedMotionSpec = {
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { duration: 0.12 },
}
/* Flavour -> enter/exit spec. */
function flavorMotion(flavor: Flavor, reduce: boolean | null) {
if (reduce) return reducedMotionSpec
switch (flavor) {
case "slide":
return {
initial: { opacity: 0, x: 40 },
animate: { opacity: 1, x: 0 },
exit: { opacity: 0, x: 40 },
transition: { type: "spring" as const, stiffness: 320, damping: 30 },
}
case "pop":
return {
initial: { opacity: 0, scale: 0.8 },
animate: { opacity: 1, scale: 1 },
exit: { opacity: 0, scale: 0.8 },
transition: { type: "spring" as const, stiffness: 520, damping: 24 },
}
case "fade":
return {
initial: { opacity: 0, y: 8 },
animate: { opacity: 1, y: 0 },
exit: { opacity: 0, y: 8 },
transition: { duration: 0.28, ease: "easeOut" as const },
}
case "flip":
return {
initial: { opacity: 0, rotateX: -70, y: 12 },
animate: { opacity: 1, rotateX: 0, y: 0 },
exit: { opacity: 0, rotateX: 55, y: 12 },
transition: { duration: 0.34, ease: "easeOut" as const },
}
case "bounce":
default:
return {
initial: { opacity: 0, y: 48, scale: 0.94 },
animate: { opacity: 1, y: 0, scale: 1 },
exit: { opacity: 0, y: 24, scale: 0.94 },
transition: { type: "spring" as const, stiffness: 480, damping: 12, mass: 0.9 },
}
}
}
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 MotionToasterProps {
className?: string
size?: StyledSize
tone?: StyledTone
label?: React.ReactNode
}
/* A single body: only the motion flavor and the message change. */
function MotionToaster({
flavor,
message,
className,
size = "md",
tone = "info",
label = "Show toast",
}: MotionToasterProps & { flavor: Flavor; message: string }) {
const reduce = useReducedMotion()
const { items, push, dismiss } = useToastStack(message)
const m = flavorMotion(flavor, reduce)
const Icon = toneIcon[tone]
return (
<div data-slot="styled-sonner" className={cn("inline-flex", className)}>
<button type="button" className={triggerBtn} onClick={push}>
{label}
</button>
<div className={stackClass} style={reduce ? undefined : { perspective: 800 }}>
<AnimatePresence initial={false}>
{items.map((t) => (
<motion.div
key={t.id}
role="status"
data-tone={tone}
data-motion={flavor}
className={cn(toastBase, toastWidth[size])}
initial={m.initial}
animate={m.animate}
exit={m.exit}
transition={m.transition}
layout={!reduce}
>
<Icon className={cn("mt-0.5", toneIconColor[tone])} />
<div className="min-w-0 flex-1 pt-0.5">{t.message}</div>
<button
type="button"
aria-label="Dismiss"
className={closeBtn}
onClick={() => dismiss(t.id)}
>
<X />
</button>
</motion.div>
))}
</AnimatePresence>
</div>
</div>
)
}
/* ---------------------------------------------------------------- SlideToaster
Kenardan yaylanarak iceri kayar. */
export function SlideToaster(props: MotionToasterProps) {
return <MotionToaster flavor="slide" message="Slid in from the edge." {...props} />
}
/* ------------------------------------------------------------------
PopToaster
It pops open by growing from small. */
export function PopToaster(props: MotionToasterProps) {
return <MotionToaster flavor="pop" message="Popped into place." {...props} />
}
/* ----------------------------------------------------------------- FadeToaster
Kisa bir yukselisle yumusakca belirir. */
export function FadeToaster(props: MotionToasterProps) {
return <MotionToaster flavor="fade" message="Faded in quietly." {...props} />
}
/* -----------------------------------------------------------------
FlipToaster
It opens by rotating on the X axis (with perspective). */
export function FlipToaster(props: MotionToasterProps) {
return <MotionToaster flavor="flip" message="Flipped in on its edge." {...props} />
}
/* --------------------------------------------------------------- BounceToaster
Dusuk sonumlu yay ile zipliyarak oturur. */
export function BounceToaster(props: MotionToasterProps) {
return <MotionToaster flavor="bounce" message="Bounced up from below." {...props} />
}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.
Slide
A spring slide in from the right edge.
import { SlideToaster } from "@/components/ui/sonner-motion"
<SlideToaster />Pop
A snappy scale-up that pops into place.
import { PopToaster } from "@/components/ui/sonner-motion"
<PopToaster />Fade
A quiet fade with a short lift.
import { FadeToaster } from "@/components/ui/sonner-motion"
<FadeToaster />Flip
A perspective flip around the X axis.
import { FlipToaster } from "@/components/ui/sonner-motion"
<FlipToaster />Bounce
A low-damping spring that bounces up from below.
import { BounceToaster } from "@/components/ui/sonner-motion"
<BounceToaster />ai2 Motion toasters: 5 styled variations on the token system
The ai2 Motion toasters are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around toast triggers with different entrance and exit motion flavors. 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 drives the slide, pop, fade, flip or bounce on enter and exit. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, every flavor drops its transforms and the toast simply fades.
What is in the ai2 Motion toasters?
5 exports in one file: Slide, Pop, Fade, Flip and Bounce. 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 drives the slide, pop, fade, flip or bounce on enter and exit.
- Reduced-motion aware: Under prefers-reduced-motion, every flavor drops its transforms and the toast simply fades.
- 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 Motion 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.