{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"motion-badge","type":"registry:component","title":"Badge (Motion)","description":"Badge with motion: a new count rolls up or down while the badge glides to its width, a new color pulses the dot, and it pops in when added.","dependencies":["motion"],"registryDependencies":["https://dashral.com/r/utils.json","https://dashral.com/r/lib-locale.json","https://dashral.com/r/motion-odometer.json","https://dashral.com/r/motion-reduced.json","https://dashral.com/r/motion-tokens.json","https://dashral.com/r/badge.json"],"files":[{"path":"registry/dashral/motion/badge.tsx","type":"registry:file","target":"components/dashral/motion/badge.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, animate, motion, PresenceContext, useMotionValue } from \"motion/react\"\nimport { badgeVariants, type Badge as KitBadge } from \"../badge\"\nimport { useLocale } from \"../lib/locale\"\nimport { cn } from \"../lib/utils\"\n\nimport { Odometer } from \"../lib/motion-odometer\"\nimport { useReducedMotion } from \"../lib/motion-reduced\"\nimport { duration, ease, spring } from \"../lib/motion-tokens\"\n\nexport type { BadgeVariant, BadgeColor } from \"../badge\"\n\n// motion/react gives these its own meaning (drag and animation events), so a plain span's versions are left out.\nexport type BadgeProps = Omit<React.ComponentProps<typeof KitBadge>, \"asChild\" | \"onDrag\" | \"onDragStart\" | \"onDragEnd\" | \"onAnimationStart\"> & {\n  /** With a number for its label: above this it shows \"99+\" (for max={99}) instead of the number. */\n  max?: number\n}\n\n// The status dot, in the color's fill (on a solid badge it takes the text color instead), as the kit's Badge has it.\nconst dots = {\n  primary: \"bg-primary\",\n  neutral: \"bg-muted-foreground\",\n  info: \"bg-info\",\n  success: \"bg-success\",\n  warning: \"bg-warning\",\n  destructive: \"bg-destructive\",\n  highlight: \"bg-highlight\",\n} as const\n\n/** What the label is, for telling a new label from a re-render of the same one; only text and numbers cross over. */\nfunction labelKey(node: React.ReactNode) {\n  return typeof node === \"string\" || typeof node === \"number\" ? String(node) : null\n}\n\n/** The number in a label (\"$96.00\", \"4 unread\", \"1,284\"), to tell a count going up from one going down; NaN without one. */\nfunction amount(key: string | null) {\n  const digits = key?.replace(/,/g, \"\").match(/-?\\d+(\\.\\d+)?/)\n  return digits ? Number(digits[0]) : NaN\n}\n\n/**\n * The kit's Badge with motion: the same variants, colors, dot and props. A number turns on odometer wheels, each digit\n * as far as it travelled, and past max it reads \"99+\"; a new text label crosses over the old one, rolling up for a\n * bigger number in it and down for a smaller, while the badge glides to the new width; a new color makes the dot\n * pulse. Added inside AnimatePresence, it pops in, and taken away it shrinks out. With reduced motion it only fades.\n * No asChild: for a link that looks like a badge, use the kit's Badge.\n */\nexport function Badge({ className, variant, color, dot = false, max, children: label, ...props }: BadgeProps) {\n  const reduced = useReducedMotion()\n  const { locale } = useLocale()\n  const format = React.useMemo(() => new Intl.NumberFormat(locale), [locale])\n  // A count turns on wheels; past max it's text (\"99+\").\n  const count = typeof label === \"number\" && (max === undefined || label <= max) ? label : null\n  const children = typeof label === \"number\" && count === null ? `${format.format(max!)}+` : label\n  // Pops in only where something was added (inside AnimatePresence), never on the page's first paint.\n  const presence = React.useContext(PresenceContext)\n  const kind = variant ?? \"solid\"\n  const tone = color ?? (kind === \"outline\" ? \"neutral\" : \"primary\")\n\n  const key = labelKey(children)\n  // Which way a count rolls: up when it grows, down when it shrinks; words just rise.\n  const [shownKey, setShownKey] = React.useState(key)\n  const [rise, setRise] = React.useState(1)\n  if (shownKey !== key) {\n    setShownKey(key)\n    setRise(amount(key) < amount(shownKey) ? -1 : 1)\n  }\n  // How many times the color has changed: each change pulses the dot once, the first paint doesn't.\n  const [shownTone, setShownTone] = React.useState(tone)\n  const [pulses, setPulses] = React.useState(0)\n  if (shownTone !== tone) {\n    setShownTone(tone)\n    setPulses(pulses + 1)\n  }\n\n  // The width glides only when the label changes; before it's measured it's natural.\n  const words = React.useRef<HTMLSpanElement>(null)\n  const width = useMotionValue<number | \"auto\">(\"auto\")\n  const gliding = React.useRef(false)\n  const lastKey = React.useRef(key)\n  React.useLayoutEffect(() => {\n    if (lastKey.current === key) return\n    lastKey.current = key\n    gliding.current = true\n  }, [key])\n  React.useEffect(() => {\n    const node = words.current\n    if (!node) return\n    const observer = new ResizeObserver(([entry]) => {\n      const next = entry?.borderBoxSize[0]?.inlineSize ?? node.offsetWidth\n      if (!next) return\n      if (!gliding.current || reduced || width.get() === \"auto\") {\n        width.jump(next)\n        gliding.current = false\n        return\n      }\n      animate(width, next, { ...spring.glide, onComplete: () => (gliding.current = false) })\n    })\n    observer.observe(node)\n    return () => observer.disconnect()\n  }, [reduced, width])\n\n  const from = reduced ? { opacity: 0 } : { opacity: 0, scale: 0.6 }\n\n  return (\n    <motion.span\n      data-slot=\"badge\"\n      {...props}\n      className={badgeVariants({ variant, color, className })}\n      initial={presence ? from : false}\n      animate={{ opacity: 1, scale: 1 }}\n      exit={{ ...from, transition: { duration: duration.fast, ease: ease.exit } }}\n      transition={reduced ? { duration: duration.fast } : { ...spring.pop, opacity: { duration: duration.fast, ease: ease.enter } }}\n    >\n      {dot && (\n        <motion.span\n          // A new color restarts it, so the dot pulses once in its new color.\n          key={pulses}\n          aria-hidden\n          data-slot=\"badge-dot\"\n          className={cn(\"size-1.5 shrink-0 rounded-full\", kind === \"solid\" ? \"bg-current\" : dots[tone])}\n          initial={false}\n          animate={reduced || pulses === 0 ? undefined : { scale: [1, 1.8, 1] }}\n          transition={{ duration: duration.considered, ease: ease.standard }}\n        />\n      )}\n      {count !== null ? (\n        <Odometer value={count} format={format} />\n      ) : key === null ? (\n        children\n      ) : (\n        <motion.span className=\"flex justify-center overflow-hidden\" style={{ width }}>\n          <span ref={words} className=\"grid w-max\">\n            {/* popLayout lifts the old label out of the flow at once, so the new one sets the width straight away. */}\n            <AnimatePresence mode=\"popLayout\" initial={false} custom={rise}>\n              <motion.span\n                key={key}\n                className=\"col-start-1 row-start-1\"\n                custom={rise}\n                variants={{\n                  in: (dir: number) => (reduced ? { opacity: 0 } : { opacity: 0, y: dir * 8 }),\n                  shown: { opacity: 1, y: 0 },\n                  out: (dir: number) => (reduced ? { opacity: 0 } : { opacity: 0, y: dir * -8 }),\n                }}\n                initial=\"in\"\n                animate=\"shown\"\n                exit=\"out\"\n                transition={{ duration: duration.standard, ease: ease.enter }}\n              >\n                {children}\n              </motion.span>\n            </AnimatePresence>\n          </span>\n        </motion.span>\n      )}\n    </motion.span>\n  )\n}\n"}],"cssVars":{"theme":{"color-info":"var(--info)","color-info-foreground":"var(--info-foreground)","color-info-text":"var(--info-text)","color-success":"var(--success)","color-success-foreground":"var(--success-foreground)","color-success-text":"var(--success-text)","color-warning":"var(--warning)","color-warning-foreground":"var(--warning-foreground)","color-warning-text":"var(--warning-text)","color-highlight":"var(--highlight)","color-highlight-foreground":"var(--highlight-foreground)","color-highlight-text":"var(--highlight-text)"},"light":{"info":"oklch(0.5 0.134 242.749)","info-foreground":"oklch(0.985 0 0)","info-text":"oklch(0.443 0.11 240.79)","success":"oklch(0.508 0.118 165.612)","success-foreground":"oklch(0.985 0 0)","success-text":"oklch(0.432 0.095 166.913)","warning":"#f59200","warning-foreground":"oklch(0.3 0.07 60)","warning-text":"#964e00","highlight":"#b8008c","highlight-foreground":"oklch(0.985 0 0)","highlight-text":"#a3007b"},"dark":{"info":"oklch(0.685 0.169 237.323)","info-foreground":"oklch(0.2 0 0)","info-text":"oklch(0.746 0.16 232.661)","success":"oklch(0.696 0.17 162.48)","success-foreground":"oklch(0.2 0.04 160)","success-text":"oklch(0.765 0.177 163.223)","warning":"#f59200","warning-foreground":"oklch(0.25 0.06 60)","warning-text":"#ffba42","highlight":"#ff2ecf","highlight-foreground":"oklch(0.2 0 0)","highlight-text":"#ff75df"}}}