{"slug":"animated-badge","name":"Animated Badge","description":"Status badge with animated state icons, pulse feedback and compact size variants.","category":"motion","source_url":"https://beui.dev/r/animated-badge/raw","detail_url":"https://beui.dev/r/animated-badge","raw_url":"https://beui.dev/r/animated-badge/raw","page_url":"https://beui.dev/components/motion/animated-badge","markdown_url":"https://beui.dev/components/motion/animated-badge.md","published_at":"2026-06-05","updated_at":"2026-11-24","dependencies":["clsx","lucide-react","motion","react","tailwind-merge"],"internal":["@/components/motion/animated-badge","@/lib/ease","@/lib/utils"],"install_slugs":["animated-badge"],"requires_theme":false,"props":[{"displayName":"AnimatedBadge","props":[{"name":"status","type":"\"info\" | \"success\" | \"warning\" | \"loading\" | \"neutral\" | \"danger\"","required":false,"defaultValue":"neutral","description":"Sets the color and default icon; changing it rolls the icon. Default: \"neutral\""},{"name":"size","type":"\"sm\" | \"md\"","required":false,"defaultValue":"md","description":"Badge height, padding and text size. Default: \"md\""},{"name":"icon","type":"ReactNode","required":false,"defaultValue":null,"description":"Custom icon in place of the status icon."},{"name":"showIcon","type":"boolean","required":false,"defaultValue":"true","description":"Show the icon before the label. Default: true"},{"name":"pulse","type":"boolean","required":false,"defaultValue":"status === \"loading\"","description":"Breathe the background to show ongoing work. Default: true while status is \"loading\""},{"name":"contentKey","type":"string | number","required":false,"defaultValue":null,"description":"Key that triggers the label roll when it changes. Default: the text label, else the status."},{"name":"className","type":"string","required":false,"defaultValue":null,"description":"Classes merged onto the root element."}]}],"types":[],"files":[{"path":"components/motion/animated-badge.tsx","type":"component","content":"\"use client\";\n// beui.dev/components/motion/animated-badge\n\nimport {\n  AlertTriangle,\n  Check,\n  Circle,\n  Info,\n  LoaderCircle,\n  X,\n  type LucideIcon,\n} from \"lucide-react\";\nimport {\n  AnimatePresence,\n  motion,\n  useReducedMotion,\n  type HTMLMotionProps,\n  type Variants,\n} from \"motion/react\";\nimport type { ReactNode } from \"react\";\nimport { EASE_IN_OUT, EASE_OUT, SPRING_LAYOUT, SPRING_SWAP } from \"@/lib/ease\";\nimport { cn } from \"@/lib/utils\";\n\nexport type AnimatedBadgeStatus =\n  | \"neutral\"\n  | \"info\"\n  | \"success\"\n  | \"warning\"\n  | \"danger\"\n  | \"loading\";\n\nexport type AnimatedBadgeSize = \"sm\" | \"md\";\n\nexport interface AnimatedBadgeProps extends Omit<\n  HTMLMotionProps<\"span\">,\n  \"children\"\n> {\n  /** Sets the color and default icon; changing it rolls the icon. Default: \"neutral\" */\n  status?: AnimatedBadgeStatus;\n  /** Badge height, padding and text size. Default: \"md\" */\n  size?: AnimatedBadgeSize;\n  children?: ReactNode;\n  /** Custom icon in place of the status icon. */\n  icon?: ReactNode;\n  /** Show the icon before the label. Default: true */\n  showIcon?: boolean;\n  /** Breathe the background to show ongoing work. Default: true while status is \"loading\" */\n  pulse?: boolean;\n  /** Key that triggers the label roll when it changes. Default: the text label, else the status. */\n  contentKey?: string | number;\n}\n\nconst STATUS_CLASS: Record<AnimatedBadgeStatus, string> = {\n  neutral: \"border-border bg-card text-muted-foreground\",\n  info: \"border-primary/30 bg-muted text-primary\",\n  success: \"border-emerald-500/30 bg-emerald-500/10 text-emerald-600 dark:text-emerald-400\",\n  warning: \"border-amber-500/30 bg-amber-500/10 text-amber-600 dark:text-amber-400\",\n  danger: \"border-destructive/30 bg-destructive/10 text-destructive\",\n  loading: \"border-primary/30 bg-muted text-primary\",\n};\n\nconst SIZE_CLASS: Record<AnimatedBadgeSize, string> = {\n  sm: \"h-6 gap-1.5 px-2 text-[11px]\",\n  md: \"h-8 gap-2 px-3 text-xs\",\n};\n\nconst ICON_CLASS: Record<AnimatedBadgeSize, string> = {\n  sm: \"h-3 w-3\",\n  md: \"h-3.5 w-3.5\",\n};\n\nconst ICONS: Record<AnimatedBadgeStatus, LucideIcon> = {\n  neutral: Circle,\n  info: Info,\n  success: Check,\n  warning: AlertTriangle,\n  danger: X,\n  loading: LoaderCircle,\n};\n\n// Status swaps roll like the action swap primitives: the leaving glyphs\n// slide up and out while the next ones slide in from below. Both fade from\n// fully transparent through a light blur, so the label never reads as a\n// solid smudge sitting on top of the badge, and the exit outruns the entry.\nconst ROLL_EXIT_TRANSITION = { duration: 0.14, ease: EASE_OUT } as const;\nconst ROLL_BLUR = \"blur(3px)\";\n\nconst ICON_ROLL_VARIANTS: Variants = {\n  initial: { opacity: 0, y: 12, filter: ROLL_BLUR },\n  animate: {\n    opacity: 1,\n    y: 0,\n    filter: \"blur(0px)\",\n    transition: SPRING_SWAP,\n  },\n  exit: {\n    opacity: 0,\n    y: -12,\n    filter: ROLL_BLUR,\n    transition: ROLL_EXIT_TRANSITION,\n  },\n};\n\nconst TEXT_ROLL_VARIANTS: Variants = {\n  initial: { opacity: 0, y: \"90%\", filter: ROLL_BLUR },\n  animate: {\n    opacity: 1,\n    y: \"0%\",\n    filter: \"blur(0px)\",\n    transition: SPRING_SWAP,\n  },\n  exit: {\n    opacity: 0,\n    y: \"-90%\",\n    filter: ROLL_BLUR,\n    transition: ROLL_EXIT_TRANSITION,\n  },\n};\n\n// Reduced motion swaps in place: the next content lands at rest and the\n// leaving content hides on its first frame instead of overlapping it.\nconst REST = { opacity: 1, y: 0, filter: \"blur(0px)\" } as const;\nconst REST_EXIT = { opacity: 0, transition: { duration: 0 } } as const;\n\nexport function AnimatedBadge({\n  status = \"neutral\",\n  size = \"md\",\n  children,\n  icon,\n  showIcon = true,\n  pulse = status === \"loading\",\n  contentKey,\n  className,\n  style,\n  ...rest\n}: AnimatedBadgeProps) {\n  const reduce = useReducedMotion();\n  // Reduced motion skips the layout projection altogether: a zero-duration\n  // layout animation still paints one transformed frame.\n  const slotLayout = reduce ? false : (\"position\" as const);\n  const Icon = ICONS[status];\n  // `0` is a real label; booleans and \"\" from conditional callers are not and\n  // must not reserve the gap and an empty slot.\n  const hasLabel = children != null && typeof children !== \"boolean\" && children !== \"\";\n  const resolvedContentKey =\n    contentKey ??\n    (typeof children === \"string\" || typeof children === \"number\"\n      ? children\n      : status);\n\n  return (\n    <motion.span\n      layout={!reduce}\n      transition={SPRING_LAYOUT}\n      style={style}\n      className={cn(\n        \"relative inline-flex shrink-0 items-center overflow-hidden whitespace-nowrap rounded-full border font-medium tabular-nums\",\n        \"transition-colors duration-300\",\n        STATUS_CLASS[status],\n        SIZE_CLASS[size],\n        className,\n      )}\n      {...rest}\n    >\n      {/* The pulse breathes through opacity alone. Scaling this layer would\n          shrink it inside the pill and open a ring between fill and border. */}\n      {pulse && !reduce ? (\n        <motion.span\n          aria-hidden\n          className=\"absolute inset-0 rounded-full bg-current\"\n          initial={{ opacity: 0.06 }}\n          animate={{ opacity: [0.06, 0.18, 0.06] }}\n          transition={{ duration: 1.6, repeat: Infinity, ease: EASE_IN_OUT }}\n        />\n      ) : null}\n      {/* The badge animates its width with a scale transform. Slots opt into\n          position-only layout so motion counter-scales them and the glyphs\n          keep their shape while the pill stretches around them. */}\n      {showIcon ? (\n        <motion.span\n          layout={slotLayout}\n          transition={SPRING_LAYOUT}\n          className=\"relative z-10 inline-grid shrink-0 place-items-center overflow-hidden\"\n        >\n          <AnimatePresence mode=\"popLayout\" initial={false}>\n            <motion.span\n              key={status}\n              aria-hidden\n              data-badge-icon\n              variants={ICON_ROLL_VARIANTS}\n              initial={reduce ? false : \"initial\"}\n              animate={reduce ? REST : \"animate\"}\n              exit={reduce ? REST_EXIT : \"exit\"}\n              className=\"col-start-1 row-start-1 inline-flex items-center justify-center\"\n            >\n              {status === \"loading\" && !reduce && !icon ? (\n                <motion.span\n                  animate={{ rotate: 360 }}\n                  transition={{ duration: 1, repeat: Infinity, ease: \"linear\" }}\n                  className=\"inline-flex\"\n                >\n                  <Icon className={ICON_CLASS[size]} />\n                </motion.span>\n              ) : (\n                (icon ?? <Icon className={ICON_CLASS[size]} />)\n              )}\n            </motion.span>\n          </AnimatePresence>\n        </motion.span>\n      ) : null}\n      {hasLabel ? (\n        // Clip the roll to the line box only; a longer leaving label may run\n        // past the slot and is clipped by the pill instead of cut mid-glyph.\n        <motion.span\n          layout={slotLayout}\n          transition={SPRING_LAYOUT}\n          className=\"relative z-10 inline-flex\"\n          style={{ clipPath: \"inset(0 -999px)\" }}\n        >\n          <AnimatePresence mode=\"popLayout\" initial={false}>\n            <motion.span\n              key={resolvedContentKey}\n              data-badge-label\n              variants={TEXT_ROLL_VARIANTS}\n              initial={reduce ? false : \"initial\"}\n              animate={reduce ? REST : \"animate\"}\n              exit={reduce ? REST_EXIT : \"exit\"}\n              className=\"inline-block\"\n            >\n              {children}\n            </motion.span>\n          </AnimatePresence>\n        </motion.span>\n      ) : null}\n    </motion.span>\n  );\n}\n"},{"path":"lib/ease.ts","type":"util","content":"// Shared motion tokens. Easing curves mirror the CSS custom properties in\n// globals.css; springs are the canonical physics used across components.\n// Strong custom variants - defaults like `ease-in`/`ease-out` feel weak.\n\nexport const EASE_OUT = [0.16, 1, 0.3, 1] as const;\n/** Accelerating curve for things that fall (a dropped ball, a toppled block). Not for UI entrances. */\nexport const EASE_IN = [0.55, 0, 1, 0.45] as const;\nexport const EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;\nexport const EASE_DRAWER = [0.32, 0.72, 0, 1] as const;\n\n/** CSS string form of EASE_OUT for inline style transitions. */\nexport const EASE_OUT_CSS = \"cubic-bezier(0.16, 1, 0.3, 1)\";\n\n/** Press feedback on buttons and other tappable surfaces. */\nexport const SPRING_PRESS = {\n  type: \"spring\",\n  stiffness: 500,\n  damping: 30,\n  mass: 0.6,\n} as const;\n\n/** Content swaps - label/icon slots trading places inside a control. */\nexport const SPRING_SWAP = {\n  type: \"spring\",\n  stiffness: 460,\n  damping: 30,\n  mass: 0.55,\n} as const;\n\n/** Overlay panel entrances - modals and sheets summoned by pointer. */\nexport const SPRING_PANEL = {\n  type: \"spring\",\n  stiffness: 420,\n  damping: 40,\n  mass: 0.5,\n} as const;\n\n/** Shared-layout glides - pills, indicators and panels morphing between positions. */\nexport const SPRING_LAYOUT = {\n  type: \"spring\",\n  stiffness: 360,\n  damping: 32,\n  mass: 0.6,\n} as const;\n\n/** Cursor-follow physics for decorative mouse tracking (magnetic, tilt, dock). */\nexport const SPRING_MOUSE = {\n  stiffness: 200,\n  damping: 15,\n  mass: 0.3,\n} as const;\n\n/** Dragged handles and fills (sliders) - critically damped `useSpring` config,\n * so the value follows the pointer butterily and never rebounds off an end. */\nexport const SPRING_GLIDE = {\n  stiffness: 700,\n  damping: 50,\n  mass: 0.5,\n} as const;\n\n/** Shared-surface morphs: a trigger growing into its panel and back on one\n * weighted spring, so opening and closing travel the same path. */\nexport const SPRING_MORPH = {\n  type: \"spring\",\n  duration: 0.5,\n  bounce: 0.22,\n} as const;\n"},{"path":"lib/utils.ts","type":"util","content":"import { clsx, type ClassValue } from \"clsx\"\nimport { twMerge } from \"tailwind-merge\"\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs))\n}\n"},{"path":"components/previews/motion/animated-badge.preview.tsx","type":"preview","content":"\"use client\";\n\nimport { useEffect, useState } from \"react\";\nimport { AnimatedBadge, type AnimatedBadgeStatus } from \"@/components/motion/animated-badge\";\n\nconst STATES: Array<{ status: AnimatedBadgeStatus; label: string }> = [\n  { status: \"loading\", label: \"Syncing\" },\n  { status: \"success\", label: \"Synced\" },\n  { status: \"warning\", label: \"Review\" },\n  { status: \"danger\", label: \"Failed\" },\n];\n\nexport function AnimatedBadgePreview() {\n  const [active, setActive] = useState(0);\n  const state = STATES[active];\n\n  useEffect(() => {\n    const id = window.setInterval(() => {\n      setActive((current) => (current + 1) % STATES.length);\n    }, 1600);\n\n    return () => window.clearInterval(id);\n  }, []);\n\n  return (\n    <div className=\"flex flex-col items-center gap-6\">\n      <div className=\"flex h-16 items-center justify-center\">\n        <AnimatedBadge status={state.status} size=\"md\" aria-live=\"polite\">\n          {state.label}\n        </AnimatedBadge>\n      </div>\n\n      <div className=\"grid grid-cols-2 gap-2 sm:grid-cols-3\">\n        <AnimatedBadge status=\"neutral\" size=\"sm\">Queued</AnimatedBadge>\n        <AnimatedBadge status=\"info\" size=\"sm\">Live</AnimatedBadge>\n        <AnimatedBadge status=\"loading\" size=\"sm\">Indexing</AnimatedBadge>\n        <AnimatedBadge status=\"success\" size=\"sm\">Verified</AnimatedBadge>\n        <AnimatedBadge status=\"warning\" size=\"sm\">Pending</AnimatedBadge>\n        <AnimatedBadge status=\"danger\" size=\"sm\">Blocked</AnimatedBadge>\n      </div>\n    </div>\n  );\n}\n"}]}