{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"badge-ping-counter","type":"registry:component","title":"Badge Ping Counter","description":"A notification badge that earns its number: the count rolls like a counter wheel, the pill pops, two rings ping outward and the bell swings on every increment. 99+ overflow, dot mode, any icon or avatar, light and dark. No dependencies.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/badge-ping-counter.tsx","content":"\"use client\";\n\nimport { useState, type CSSProperties, type ReactNode } from \"react\";\n\nexport type BadgePingCounterProps = {\n  /** Starting count for the uncontrolled counter. Default 0. */\n  initialCount?: number;\n  /** Controlled count. When set, the component only reports clicks via onCountChange. */\n  count?: number;\n  /** Called with count + 1 on every press. */\n  onCountChange?: (next: number) => void;\n  /** Counts above this show as \"99+\". Default 99. */\n  max?: number;\n  /** Show a plain dot instead of a number. */\n  dot?: boolean;\n  /** Built-in icon, or pass children to draw your own (an avatar, a logo). */\n  icon?: \"bell\" | \"inbox\";\n  /** Accessible name of the button. Default \"Notifications\". */\n  label?: string;\n  /** Badge colour (hex, rgb or hsl). Default #ef4444. */\n  color?: string;\n  /** Button diameter in px. Default 44. */\n  size?: number;\n  /** \"light\" inherits the page and works on white; \"dark\" paints pale-on-black. */\n  tone?: \"light\" | \"dark\";\n  /** Custom icon content; replaces the built-in icon. */\n  children?: ReactNode;\n  className?: string;\n};\n\nconst COLOR_RE = /^(#[0-9a-f]{3,8}|(rgb|hsl)a?\\([0-9.,%\\s/-]+\\))$/i;\n\nconst CSS = [\n  \".bpc{position:relative;display:inline-flex;align-items:center;justify-content:center;flex:none;cursor:pointer;color:var(--bpc-fg,#737373);\",\n  \"background:color-mix(in srgb,currentColor 4%,transparent);border:1px solid color-mix(in srgb,currentColor 24%,transparent);\",\n  \"transition:transform .38s cubic-bezier(.34,1.56,.64,1),background-color .2s,border-color .2s,color .2s;-webkit-tap-highlight-color:transparent;touch-action:manipulation}\",\n  \".bpc[data-tone=dark]{--bpc-fg:rgba(255,255,255,.74);background:linear-gradient(180deg,rgba(255,255,255,.07),rgba(255,255,255,.02));border-color:rgba(255,255,255,.12);box-shadow:inset 0 1px 0 rgba(255,255,255,.08),0 10px 24px -12px rgba(0,0,0,.8)}\",\n  \".bpc:hover{background:color-mix(in srgb,currentColor 9%,transparent);border-color:color-mix(in srgb,currentColor 40%,transparent)}\",\n  \".bpc[data-tone=dark]:hover{background:linear-gradient(180deg,rgba(255,255,255,.11),rgba(255,255,255,.04));border-color:rgba(255,255,255,.22);color:#fff}\",\n  \".bpc:active{transform:scale(.9);transition-duration:.1s}\",\n  \".bpc-ico{display:flex;align-items:center;justify-content:center;transform-origin:50% 12%;transition:transform .3s cubic-bezier(.34,1.56,.64,1)}\",\n  \".bpc:hover .bpc-ico{transform:scale(1.08)}\",\n  \".bpc-ico[data-s]{animation:bpc-swing .8s cubic-bezier(.36,.07,.19,.97) both}\",\n  \".bpc-ico[data-s=hop]{animation:bpc-hop .55s cubic-bezier(.34,1.56,.64,1) both}\",\n  \".bpc-slot{position:absolute;top:calc(-4px*var(--bpc-k,1));right:calc(-4px*var(--bpc-k,1));display:block;z-index:2;pointer-events:none}\",\n  \".bpc-slot[data-up]{animation:bpc-pop .55s cubic-bezier(.34,1.56,.64,1) both}\",\n  \".bpc-slot[data-dn]{animation:bpc-dip .32s cubic-bezier(.2,.8,.2,1) both}\",\n  \".bpc-slot[data-out]{animation:bpc-out .28s cubic-bezier(.4,0,1,1) forwards}\",\n  \".bpc-pill{position:relative;display:flex;align-items:center;justify-content:center;height:calc(20px*var(--bpc-k,1));min-width:calc(20px*var(--bpc-k,1));padding:0 calc(6px*var(--bpc-k,1));border-radius:999px;box-sizing:border-box;\",\n  \"background:linear-gradient(180deg,color-mix(in srgb,var(--bpc-c) 82%,#fff),var(--bpc-c) 55%,color-mix(in srgb,var(--bpc-c) 82%,#000));\",\n  \"color:#fff;font:700 calc(11px*var(--bpc-k,1))/1 ui-sans-serif,system-ui,sans-serif;font-variant-numeric:tabular-nums;letter-spacing:.01em;\",\n  \"box-shadow:0 0 0 2px var(--bpc-cut,transparent),0 6px 14px -4px color-mix(in srgb,var(--bpc-c) 70%,transparent),inset 0 1px 0 rgba(255,255,255,.4)}\",\n  \".bpc-pill[data-dot]{height:calc(11px*var(--bpc-k,1));min-width:calc(11px*var(--bpc-k,1));width:calc(11px*var(--bpc-k,1));padding:0}\",\n  \".bpc-num{position:relative;display:block;overflow:hidden;height:calc(14px*var(--bpc-k,1));line-height:calc(14px*var(--bpc-k,1));text-align:center}\",\n  \".bpc-num>span{display:block;white-space:nowrap}\",\n  \".bpc-num[data-roll]>span:first-child{visibility:hidden}\",\n  \".bpc-num[data-roll]>span+span{position:absolute;left:0;right:0;top:0}\",\n  \".bpc-in{animation:bpc-up .46s cubic-bezier(.2,.9,.25,1.12) both}\",\n  \".bpc-in[data-d]{animation-name:bpc-dn}\",\n  \".bpc-gone{animation:bpc-away .3s cubic-bezier(.4,0,.6,1) both}\",\n  \".bpc-gone[data-d]{animation-name:bpc-away-d}\",\n  \".bpc-ring{position:absolute;inset:0;border-radius:999px;pointer-events:none;z-index:-1}\",\n  \".bpc-ring[data-a]{background:var(--bpc-c);animation:bpc-ping .8s cubic-bezier(0,0,.2,1) both}\",\n  \".bpc-ring[data-b]{border:2px solid var(--bpc-c);animation:bpc-ping2 1s cubic-bezier(0,0,.2,1) .1s both}\",\n  \".bpc-sr{position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;border:0}\",\n  \"@keyframes bpc-pop{0%{transform:scale(.4)}55%{transform:scale(1.28)}100%{transform:scale(1)}}\",\n  \"@keyframes bpc-dip{0%{transform:scale(1)}40%{transform:scale(.82)}100%{transform:scale(1)}}\",\n  \"@keyframes bpc-out{to{transform:scale(0);opacity:0}}\",\n  \"@keyframes bpc-up{from{transform:translateY(120%);opacity:0}to{transform:none;opacity:1}}\",\n  \"@keyframes bpc-dn{from{transform:translateY(-120%);opacity:0}to{transform:none;opacity:1}}\",\n  \"@keyframes bpc-away{to{transform:translateY(-120%);opacity:0}}\",\n  \"@keyframes bpc-away-d{to{transform:translateY(120%);opacity:0}}\",\n  \"@keyframes bpc-ping{0%{transform:scale(1);opacity:.55}100%{transform:scale(2.6,2.2);opacity:0}}\",\n  \"@keyframes bpc-ping2{0%{transform:scale(1);opacity:.7}100%{transform:scale(3.4,2.9);opacity:0}}\",\n  \"@keyframes bpc-swing{0%{transform:rotate(0)}14%{transform:rotate(-17deg)}30%{transform:rotate(13deg)}46%{transform:rotate(-9deg)}62%{transform:rotate(5deg)}80%{transform:rotate(-2deg)}100%{transform:rotate(0)}}\",\n  \"@keyframes bpc-hop{0%{transform:translateY(0) scale(1)}30%{transform:translateY(-12%) scale(1.08)}60%{transform:translateY(3%) scale(.97)}100%{transform:none}}\",\n  \"@keyframes bpc-fade{0%{opacity:.5}100%{opacity:0}}\",\n  \"@media(pointer:coarse){.bpc{min-width:44px;min-height:44px}}\",\n  \"@media(prefers-reduced-motion:reduce){.bpc,.bpc-ico{transition:none}.bpc:active{transform:none}.bpc:hover .bpc-ico{transform:none}\",\n  \".bpc-ico[data-s],.bpc-slot[data-up],.bpc-slot[data-dn],.bpc-in,.bpc-gone,.bpc-ring[data-b]{animation:none}\",\n  \".bpc-slot[data-out]{animation:none;opacity:0}.bpc-num>.bpc-gone{display:none}\",\n  \".bpc-ring[data-a]{animation:bpc-fade .5s ease-out both;transform:scale(1.5)}}\",\n].join(\"\");\n\nfunction fmt(n: number, max: number, dot: boolean) {\n  if (dot) return \"\";\n  return n > max ? max + \"+\" : String(n);\n}\n\nfunction Bell() {\n  return (\n    <svg width=\"100%\" height=\"100%\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <path d=\"M6 9a6 6 0 0 1 12 0c0 6 2.5 7.5 2.5 7.5h-17S6 15 6 9Z\" />\n      <path d=\"M10 20a2.2 2.2 0 0 0 4 0\" />\n    </svg>\n  );\n}\n\nfunction Inbox() {\n  return (\n    <svg width=\"100%\" height=\"100%\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <path d=\"M3.5 13.5 6.3 5.8A1.5 1.5 0 0 1 7.7 4.8h8.6a1.5 1.5 0 0 1 1.4 1l2.8 7.7\" />\n      <path d=\"M3.5 13.5V18a1.5 1.5 0 0 0 1.5 1.5h14a1.5 1.5 0 0 0 1.5-1.5v-4.5h-5.2a3.3 3.3 0 0 1-6.6 0Z\" />\n    </svg>\n  );\n}\n\ntype Shown = { cur: number; prev: number; tick: number };\n\nexport function BadgePingCounter({\n  initialCount = 0,\n  count: controlled,\n  onCountChange,\n  max = 99,\n  dot = false,\n  icon = \"bell\",\n  label = \"Notifications\",\n  color = \"#ef4444\",\n  size = 44,\n  tone = \"light\",\n  children,\n  className,\n}: BadgePingCounterProps) {\n  const [inner, setInner] = useState(initialCount);\n  const count = Math.max(0, Math.floor(controlled ?? inner));\n  const [shown, setShown] = useState<Shown>({ cur: count, prev: count, tick: 0 });\n  // Track the change in render so every new count remounts the animated layer.\n  if (shown.cur !== count) setShown({ cur: count, prev: shown.cur, tick: shown.tick + 1 });\n\n  const accent = COLOR_RE.test(color) ? color : \"#ef4444\";\n  const up = shown.tick > 0 && shown.cur > shown.prev;\n  const down = shown.tick > 0 && shown.cur < shown.prev;\n  const cur = fmt(shown.cur, max, dot);\n  const prev = fmt(shown.prev, max, dot);\n  const roll = !dot && shown.tick > 0 && shown.prev > 0 && shown.cur > 0 && cur !== prev;\n  const gone = shown.tick > 0 && shown.cur === 0;\n  const present = shown.cur > 0 || shown.tick > 0;\n  const k = shown.tick;\n  const announce = shown.tick > 0 && shown.cur > shown.prev ? shown.cur + \" new, \" + shown.cur + \" unread\" : \"\";\n\n  const press = () => {\n    if (controlled === undefined) setInner(count + 1);\n    onCountChange?.(count + 1);\n  };\n\n  return (\n    <>\n      <style>{CSS}</style>\n      <button\n        type=\"button\"\n        onClick={press}\n        data-tone={tone}\n        aria-label={label + (count > 0 ? \", \" + count + \" unread\" : \"\")}\n        className={\n          \"bpc focus-visible:!outline-2 focus-visible:!outline-offset-2 focus-visible:!outline-[var(--bpc-c)] \" + (className ?? \"\")\n        }\n        style={{ width: size, height: size, borderRadius: 9999, [\"--bpc-c\" as string]: accent, [\"--bpc-k\" as string]: Math.max(0.8, 1 + (size / 44 - 1) * 0.5) } as CSSProperties}\n      >\n        <span key={\"i\" + (up ? k : 0)} className=\"bpc-ico\" data-s={up ? (children || icon === \"inbox\" ? \"hop\" : \"swing\") : undefined} style={children ? undefined : { width: Math.round(size * 0.43), height: Math.round(size * 0.43) }}>\n          {children ?? (icon === \"inbox\" ? <Inbox /> : <Bell />)}\n        </span>\n        {present && (\n          <span\n            key={\"s\" + k}\n            className=\"bpc-slot\"\n            aria-hidden=\"true\"\n            data-up={up && !gone ? \"\" : undefined}\n            data-dn={down && !gone ? \"\" : undefined}\n            data-out={gone ? \"\" : undefined}\n          >\n            <span className=\"bpc-pill\" data-dot={dot ? \"\" : undefined}>\n              {up && <span className=\"bpc-ring\" data-a=\"\" />}\n              {up && <span className=\"bpc-ring\" data-b=\"\" />}\n              {!dot && (\n                <span className=\"bpc-num\" data-roll={roll ? \"\" : undefined}>\n                  <span>{roll ? cur : gone ? prev : cur}</span>\n                  {roll && (\n                    <span className=\"bpc-gone\" data-d={down ? \"\" : undefined}>\n                      {prev}\n                    </span>\n                  )}\n                  {roll && (\n                    <span className=\"bpc-in\" data-d={down ? \"\" : undefined}>\n                      {cur}\n                    </span>\n                  )}\n                </span>\n              )}\n            </span>\n          </span>\n        )}\n      </button>\n      <span className=\"bpc-sr\" role=\"status\" aria-live=\"polite\">\n        {announce}\n      </span>\n    </>\n  );\n}\n","type":"registry:component","target":"components/viberdy/badge-ping-counter.tsx"}],"meta":{"tier":"free","tags":["badge","counter","notification","ping","bell","unread","avatar","micro-interaction"],"href":"https://viberdy.dev/library/badge-ping-counter","aiPrompt":"Create a dependency-free React + Tailwind component named BadgePingCounter (\"use client\", one file, no framer-motion, no icon library; React 18 and 19 safe).\n\nProps (all optional): initialCount = 0 (uncontrolled start), count (controlled), onCountChange(next), max = 99 (above it show \"99+\"), dot = false, icon \"bell\" | \"inbox\" = \"bell\" (inline SVG, stroke 1.8, round caps), label = \"Notifications\", color = \"#ef4444\" (validate hex/rgb/hsl with a regex, else fall back), size = 44 (button diameter px), tone \"light\" | \"dark\" = \"light\", children (custom icon content such as an avatar), className.\n\nStructure: a fragment with a plain <style> element, a round <button type=\"button\"> (width/height = size, border-radius 9999, relative) and a visually hidden role=\"status\" aria-live=\"polite\" span. Clicking calls onCountChange(count + 1) and, when uncontrolled, increments its own state. aria-label = label plus \", N unread\" when N > 0. Pass the colour as --bpc-c and the scale max(0.8, size/44) as --bpc-k; every badge dimension is calc(px * var(--bpc-k)). Track the shown change in state set during render ({cur, prev, tick}); key the badge layer on tick so every change remounts and replays the animation.\n\nButton: 1px border and 4% fill from color-mix(currentColor), text colour var(--bpc-fg, #737373) so it inherits a host override; hover raises the fill to 9% and the border to 40% and scales the icon to 1.08; active scales the button to 0.9 (0.1s), release on cubic-bezier(.34,1.56,.64,1) 0.38s. tone dark: white 74% text, vertical white 7%->2% glass gradient, 12% white border, inset top highlight. Icon is 43% of size.\n\nBadge: absolute at top/right -4px (times scale), pill 20px high, min-width 20, padding 0 6px, fully rounded, vertical gradient from the colour mixed 82% with white through the colour to the colour mixed 82% with black, white 700 11px tabular numbers, box-shadow 0 0 0 2px var(--bpc-cut, transparent) (the host can set the cut-out colour), a coloured drop glow and an inset top highlight. Dot mode: 11px circle with no number.\n\nIncrement (count goes up, not on first render): pill pops keyframe scale .4 -> 1.28 (55%) -> 1 over 0.55s cubic-bezier(.34,1.56,.64,1); the number window (overflow hidden, 14px tall) rolls the old label out upward (0.3s) and the new one in from translateY(120%) over 0.46s cubic-bezier(.2,.9,.25,1.12) (only when the label text changes, so 99+ -> 99+ does not roll); a filled ring (inset 0, same colour) scales 1 -> 2.6 x 2.2 while opacity .55 -> 0 over 0.8s cubic-bezier(0,0,.2,1); a 2px outline ring scales to 3.4 x 2.9, opacity .7 -> 0, over 1s with 0.1s delay; the icon swings about its top hinge: rotate 0, -17, 13, -9, 5, -2, 0 degrees over 0.8s cubic-bezier(.36,.07,.19,.97). Decrease: rolls the other direction, pill dips scale 1 -> .82 -> 1 over .32s, no ring. Count 0 after being positive: badge scales to 0 over .28s and stays hidden. A polite live region announces \"N new, N unread\" on increases.\n\nFocus: outline 2px in the badge colour, offset 2px, important utilities (the host may define a global :focus-visible rule). Touch: min 44px. prefers-reduced-motion: no transitions, roll, pop, swing or outer ring; the filled ring becomes a 0.5s opacity fade; the old label is not rendered.","license":"https://viberdy.dev/license"}}