{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "count-badge",
  "type": "registry:item",
  "title": "Count Badge",
  "description": "A count on the corner of an icon that springs in, jumps as it rises, rolls its number and shrinks away at zero.",
  "categories": [
    "feedback"
  ],
  "dependencies": [
    "react-native-reanimated",
    "react-native-worklets"
  ],
  "registryDependencies": [
    "https://kinetik-ui.dev/r/kinetik-tokens.json",
    "https://kinetik-ui.dev/r/kinetik-motion.json"
  ],
  "files": [
    {
      "path": "src/components/kinetik/count-badge.tsx",
      "type": "registry:file",
      "target": "~/components/kinetik/count-badge.tsx",
      "content": "import { useEffect, useState, type ReactNode } from 'react';\nimport { StyleSheet, View, type StyleProp, type ViewStyle } from 'react-native';\nimport Animated, {\n  useAnimatedStyle,\n  useSharedValue,\n  withSequence,\n  withSpring,\n  withTiming,\n} from 'react-native-reanimated';\n\nimport { useReduceMotion } from '../../lib/kinetik/hooks/use-reduce-motion';\nimport { durations, easings, spring } from '../../lib/kinetik/motion/motion';\nimport { useKinetikTheme } from '../../lib/kinetik/tokens/theme';\n\nexport type CountBadgeProps = {\n  count: number;\n  /** What sits under the badge: an icon, an avatar, a tab. */\n  children: ReactNode;\n  /** Above this the badge shows \"99+\". Defaults to 99. */\n  max?: number;\n  /** A plain dot instead of a number. */\n  dot?: boolean;\n  /** Defaults to \"danger\". */\n  tone?: 'danger' | 'azure';\n  /** Read by screen readers, e.g. (n) => `${n} unread`. Defaults to \"n new\". */\n  formatLabel?: (count: number) => string;\n  /** The colour around the badge that cuts it out from what is under it. Defaults to the theme background. */\n  ringColor?: string;\n  testID?: string;\n  style?: StyleProp<ViewStyle>;\n};\n\nconst H = 20;\n\n/** The text a badge shows for a count. */\nexport function badgeText(count: number, max = 99): string {\n  return count > max ? `${max}+` : String(Math.max(0, count));\n}\n\n/**\n * A count on the corner of an icon or avatar. It springs out of nothing for\n * the first item, gives a small jump each time the count goes up while the\n * number rolls upwards (downwards when it falls), and shrinks away at zero.\n */\nexport function CountBadge({\n  count,\n  children,\n  max = 99,\n  dot = false,\n  tone = 'danger',\n  formatLabel = (n) => `${n} new`,\n  ringColor,\n  testID,\n  style,\n}: CountBadgeProps) {\n  const { colors, font } = useKinetikTheme();\n  const reduced = useReduceMotion();\n  const visible = count > 0;\n  const text = badgeText(count, max);\n\n  const [roll, setRoll] = useState({ count, text, prev: text, dir: 1, n: 0 });\n  if (roll.count !== count) {\n    setRoll({ count, text, prev: roll.text, dir: count > roll.count ? 1 : -1, n: roll.n + 1 });\n  }\n\n  const shown = useSharedValue(visible ? 1 : 0);\n  const jump = useSharedValue(1);\n  const p = useSharedValue(1);\n  useEffect(() => {\n    shown.set(\n      visible\n        ? withSpring(1, spring('bouncy', reduced))\n        : withTiming(0, { duration: durations.quick, easing: easings.exit }),\n    );\n  }, [visible, reduced, shown]);\n  useEffect(() => {\n    if (roll.n === 0 || reduced) return;\n    p.set(0);\n    p.set(withTiming(1, { duration: durations.base, easing: easings.standard }));\n    if (roll.dir > 0) {\n      jump.set(withSequence(withTiming(1.25, { duration: 110 }), withSpring(1, spring('bouncy'))));\n    }\n  }, [roll.n, roll.dir, reduced, p, jump]);\n\n  const badge = useAnimatedStyle(() => ({\n    opacity: Math.min(1, shown.get() * 2),\n    transform: [{ scale: Math.max(0, shown.get()) * jump.get() }],\n  }));\n  const incoming = useAnimatedStyle(() => ({\n    opacity: p.get(),\n    transform: [{ translateY: (1 - p.get()) * 10 * roll.dir }],\n  }));\n  const outgoing = useAnimatedStyle(() => ({\n    opacity: 1 - p.get(),\n    transform: [{ translateY: -p.get() * 10 * roll.dir }],\n  }));\n\n  const fill = tone === 'danger' ? colors.danger : colors.azure;\n  const number = {\n    // Dark ink on the light red of the dark theme, white on the deep red of the light one.\n    color: tone === 'danger' ? colors.onEmber : colors.onAzure,\n    fontSize: font.size.xs,\n    fontWeight: font.weight.bold,\n    fontVariant: ['tabular-nums' as const],\n  };\n\n  return (\n    <View testID={testID} style={[styles.root, style]}>\n      {children}\n      <Animated.View\n        pointerEvents=\"none\"\n        accessible={visible}\n        accessibilityLabel={visible ? formatLabel(count) : undefined}\n        importantForAccessibility={visible ? 'yes' : 'no-hide-descendants'}\n        style={[\n          dot ? styles.dot : styles.badge,\n          { backgroundColor: fill, borderColor: ringColor ?? colors.background },\n          badge,\n        ]}\n      >\n        {dot ? null : (\n          <View style={styles.window}>\n            {roll.prev !== roll.text ? (\n              <Animated.Text style={[number, styles.ghost, outgoing]}>{roll.prev}</Animated.Text>\n            ) : null}\n            <Animated.Text style={[number, incoming]}>{text}</Animated.Text>\n          </View>\n        )}\n      </Animated.View>\n    </View>\n  );\n}\n\nconst styles = StyleSheet.create({\n  root: { alignSelf: 'flex-start' },\n  badge: {\n    position: 'absolute',\n    top: -6,\n    right: -8,\n    minWidth: H + 4,\n    height: H + 4,\n    borderRadius: (H + 4) / 2,\n    borderWidth: 2,\n    paddingHorizontal: 5,\n    alignItems: 'center',\n    justifyContent: 'center',\n  },\n  dot: {\n    position: 'absolute',\n    top: -2,\n    right: -2,\n    width: 14,\n    height: 14,\n    borderRadius: 7,\n    borderWidth: 2,\n  },\n  window: { height: H - 2, overflow: 'hidden', alignItems: 'center', justifyContent: 'center' },\n  ghost: { position: 'absolute' },\n});\n"
    }
  ],
  "meta": {
    "version": "1.0.0",
    "kind": "component",
    "platforms": [
      "ios",
      "android",
      "web"
    ],
    "category": "feedback",
    "web": "live",
    "perf": {
      "budget": "One small view and three shared values; it animates only when the count changes. Nothing runs at rest.",
      "measurements": [
        {
          "device": "Pixel 9 emulator, API 35",
          "kind": "emulator",
          "date": "2026-10-13",
          "build": "release",
          "frameMs": 21,
          "p90Ms": 24,
          "baselineMs": 18,
          "jankyPercent": 1.02,
          "atRest": "idle",
          "memoryMb": 146
        }
      ]
    },
    "a11y": {
      "reducedMotion": "The number changes in place without the jump; the badge still appears and disappears.",
      "screenReader": "The badge reads its label, such as \"3 unread\"; it is hidden at zero.",
      "touchTarget": "Not interactive; the element under it keeps its own target."
    },
    "requires": "React Native New Architecture (Reanimated 4)",
    "guide": "## Usage\n\n```tsx\nimport { CountBadge } from '@/components/kinetik/count-badge';\n\n<CountBadge count={unread} formatLabel={(n) => `${n} unread`}>\n  <InboxIcon />\n</CountBadge>;\n```\n\n## When to use\n\n- Unread messages, new notifications, items in a cart: a count that changes while people watch.\n- A presence or \"something new\" dot with `dot`.\n\n## When not to use\n\n- Static labels or categories. Use a plain tag.\n- Counts so large they stop meaning anything; prefer a dot, or cap with `max`.\n\n## Notes\n\n- The badge springs in for the first item, jumps when the count rises, rolls the number up or down, and shrinks away at zero.\n- It reads its own label (`formatLabel`) for screen readers; give the element underneath its own name too.\n- `ringColor` should match what is behind the anchor so the badge looks cut out of it.\n- With reduced motion the number changes in place, without the jump.\n"
  },
  "docs": "Prefer `npx kinetik-ui add count-badge`: it installs dependencies with expo install, so versions match your Expo SDK."
}
