{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "usage-meter",
  "type": "registry:item",
  "title": "Usage Meter",
  "description": "What fills an allowance: segments grow into the bar in turn as the figure counts up, and the rest warns near the limit.",
  "categories": [
    "data"
  ],
  "dependencies": [
    "react-native-reanimated",
    "react-native-worklets"
  ],
  "registryDependencies": [
    "https://kinetik-ui.dev/r/kinetik-tokens.json",
    "https://kinetik-ui.dev/r/kinetik-motion.json",
    "https://kinetik-ui.dev/r/kinetik-count.json"
  ],
  "files": [
    {
      "path": "src/components/kinetik/usage-meter.tsx",
      "type": "registry:file",
      "target": "~/components/kinetik/usage-meter.tsx",
      "content": "import { useEffect, useState } from 'react';\nimport { StyleSheet, Text, View, type StyleProp, type ViewStyle } from 'react-native';\nimport Animated, {\n  useAnimatedStyle,\n  useSharedValue,\n  withDelay,\n  withSpring,\n  type SharedValue,\n} from 'react-native-reanimated';\n\nimport { useCountUp } from '../../lib/kinetik/hooks/use-count-up';\nimport { useReduceMotion } from '../../lib/kinetik/hooks/use-reduce-motion';\nimport { spring } from '../../lib/kinetik/motion/motion';\nimport { useKinetikTheme } from '../../lib/kinetik/tokens/theme';\n\nexport type UsageSegment = { label: string; value: number };\n\nexport type UsageMeterProps = {\n  /** What fills the allowance, in the same unit as `total`. */\n  segments: readonly UsageSegment[];\n  /** The allowance, e.g. 200 for 200 GB. */\n  total: number;\n  /** Unit after figures, e.g. \"GB\". */\n  unit?: string;\n  /** Formats figures. Defaults to one decimal place below 10, whole numbers above. */\n  formatValue?: (value: number) => string;\n  /** Share of the allowance at which the remainder turns amber. Defaults to 0.85. */\n  warnAt?: number;\n  /** One colour per segment. Defaults to azure, sky, the theme ink and grey. */\n  colors?: readonly string[];\n  /** Show the legend. Defaults to true. */\n  legend?: boolean;\n  /** What is measured, e.g. \"Storage\". Shown as the heading and read first. */\n  title: string;\n  testID?: string;\n  style?: StyleProp<ViewStyle>;\n};\n\nconst BAR = 12;\nconst SEAM = 2;\n\nconst defaultFormat = (v: number) => (Math.abs(v) < 10 ? v.toFixed(1) : String(Math.round(v)));\n\n/**\n * What fills an allowance. The segments grow into the bar one after another\n * while the figure counts up, and the space left turns amber, then red, as\n * the limit gets close.\n */\nexport function UsageMeter({\n  segments,\n  total,\n  unit,\n  formatValue = defaultFormat,\n  warnAt = 0.85,\n  colors: palette,\n  legend = true,\n  title,\n  testID,\n  style,\n}: UsageMeterProps) {\n  const { colors, font, radius, space } = useKinetikTheme();\n  const reduced = useReduceMotion();\n  const [width, setWidth] = useState(0);\n  const inks = palette ?? [colors.azure, colors.sky, colors.ember, colors.ice];\n\n  const used = segments.reduce((a, s) => a + Math.max(0, s.value), 0);\n  const share = total > 0 ? used / total : 0;\n  const left = total - used;\n  const state = share >= 1 ? 'over' : share >= warnAt ? 'warn' : 'ok';\n  const shown = useCountUp(used, { instant: reduced, duration: 1000 });\n  const u = unit ? ` ${unit}` : '';\n\n  // Each segment's start and width as shares of the allowance, cut off at the end of the bar.\n  let start = 0;\n  const parts = segments.map((s) => {\n    const from = Math.min(1, start / (total || 1));\n    start += Math.max(0, s.value);\n    const to = Math.min(1, start / (total || 1));\n    return { from, to };\n  });\n\n  const leftColor =\n    state === 'over' ? colors.danger : state === 'warn' ? colors.warning : colors.textMuted;\n  const leftText =\n    state === 'over' ? `${formatValue(-left)}${u} over` : `${formatValue(left)}${u} left`;\n\n  return (\n    <View\n      testID={testID}\n      accessible\n      accessibilityRole=\"progressbar\"\n      accessibilityLabel={title}\n      accessibilityValue={{\n        min: 0,\n        max: total,\n        now: Math.min(used, total),\n        text: `${formatValue(used)} of ${formatValue(total)}${u} used, ${leftText}. ${segments\n          .map((s) => `${s.label} ${formatValue(s.value)}${u}`)\n          .join(', ')}`,\n      }}\n      style={[{ gap: space[3] }, style]}\n    >\n      <View style={styles.header}>\n        <View style={{ gap: 2 }}>\n          <Text style={{ color: colors.textMuted, fontSize: font.size.sm }}>{title}</Text>\n          <Text\n            style={{ color: colors.text, fontSize: font.size.xl, fontWeight: font.weight.semibold }}\n          >\n            <Text style={{ fontVariant: ['tabular-nums'] }}>{formatValue(shown)}</Text>\n            <Text\n              style={{\n                color: colors.textMuted,\n                fontSize: font.size.md,\n                fontWeight: font.weight.regular,\n              }}\n            >\n              {` of ${formatValue(total)}${u}`}\n            </Text>\n          </Text>\n        </View>\n        <Text\n          style={{ color: leftColor, fontSize: font.size.sm, fontWeight: font.weight.semibold }}\n        >\n          {leftText}\n        </Text>\n      </View>\n\n      <View\n        onLayout={(e) => setWidth(e.nativeEvent.layout.width)}\n        style={[styles.bar, { backgroundColor: colors.track, borderRadius: radius.pill }]}\n      >\n        {width > 0\n          ? parts.map((p, i) => (\n              <Segment\n                key={segments[i]!.label}\n                index={i}\n                from={p.from}\n                to={p.to}\n                width={width}\n                color={inks[i % inks.length]!}\n                reduced={reduced}\n              />\n            ))\n          : null}\n      </View>\n\n      {legend ? (\n        <View style={styles.legend}>\n          {segments.map((s, i) => (\n            <View key={s.label} style={styles.item}>\n              <View style={[styles.swatch, { backgroundColor: inks[i % inks.length] }]} />\n              <Text style={{ color: colors.textMuted, fontSize: font.size.sm }}>{s.label}</Text>\n              <Text\n                style={{\n                  color: colors.text,\n                  fontSize: font.size.sm,\n                  fontWeight: font.weight.medium,\n                  fontVariant: ['tabular-nums'],\n                }}\n              >\n                {formatValue(s.value)}\n                {u}\n              </Text>\n            </View>\n          ))}\n        </View>\n      ) : null}\n    </View>\n  );\n}\n\ntype SegmentProps = {\n  index: number;\n  from: number;\n  to: number;\n  width: number;\n  color: string;\n  reduced: boolean;\n};\n\nfunction Segment({ index, from, to, width, color, reduced }: SegmentProps) {\n  const a = useSharedValue(reduced ? from : 0);\n  const b = useSharedValue(reduced ? to : 0);\n  useEffect(() => {\n    // Later segments follow the earlier ones in, so the bar fills from the left.\n    const wait = reduced ? 0 : index * 110;\n    a.set(withDelay(wait, withSpring(from, spring('smooth', reduced))));\n    b.set(withDelay(wait, withSpring(to, spring('smooth', reduced))));\n  }, [from, to, index, reduced, a, b]);\n  return <Bar a={a} b={b} width={width} color={color} first={index === 0} />;\n}\n\nfunction Bar({\n  a,\n  b,\n  width,\n  color,\n  first,\n}: {\n  a: SharedValue<number>;\n  b: SharedValue<number>;\n  width: number;\n  color: string;\n  first: boolean;\n}) {\n  const style = useAnimatedStyle(() => {\n    const x = a.get() * width + (first ? 0 : SEAM / 2);\n    const w = Math.max(0, (b.get() - a.get()) * width - (first ? SEAM / 2 : SEAM));\n    return { left: x, width: w, opacity: w > 0.5 ? 1 : 0 };\n  });\n  return <Animated.View style={[styles.segment, { backgroundColor: color }, style]} />;\n}\n\nconst styles = StyleSheet.create({\n  header: {\n    flexDirection: 'row',\n    justifyContent: 'space-between',\n    alignItems: 'flex-end',\n    gap: 12,\n  },\n  bar: { height: BAR, overflow: 'hidden' },\n  segment: { position: 'absolute', top: 0, bottom: 0, borderRadius: 2 },\n  legend: { flexDirection: 'row', flexWrap: 'wrap', columnGap: 16, rowGap: 6 },\n  item: { flexDirection: 'row', alignItems: 'center', gap: 6 },\n  swatch: { width: 8, height: 8, borderRadius: 4 },\n});\n"
    }
  ],
  "meta": {
    "version": "1.0.0",
    "kind": "component",
    "platforms": [
      "ios",
      "android",
      "web"
    ],
    "category": "data",
    "web": "live",
    "perf": {
      "budget": "Plain views: two shared values per segment, and a figure that re-renders on frames only while it counts. Nothing runs at rest.",
      "measurements": [
        {
          "device": "Pixel 9 emulator, API 35",
          "kind": "emulator",
          "date": "2026-10-13",
          "build": "release",
          "frameMs": 17,
          "p90Ms": 21,
          "baselineMs": 20,
          "jankyPercent": 3.63,
          "atRest": "idle",
          "memoryMb": 151
        }
      ]
    },
    "a11y": {
      "reducedMotion": "The bar and the figure show their values at once.",
      "screenReader": "One progress indicator named by the title, whose value reads the total, what is left and every segment.",
      "touchTarget": "Not interactive."
    },
    "requires": "React Native New Architecture (Reanimated 4)",
    "guide": "## Usage\n\n```tsx\nimport { UsageMeter } from '@/components/kinetik/usage-meter';\n\n<UsageMeter\n  title=\"Storage\"\n  unit=\"GB\"\n  total={256}\n  segments={[\n    { label: 'Photos', value: 92 },\n    { label: 'Video', value: 48 },\n    { label: 'Apps', value: 21 },\n  ]}\n/>;\n```\n\n## When to use\n\n- Allowances made of parts: storage, data plans, seats, a monthly budget.\n- Anywhere people need to see what is taking up the space before they free some.\n\n## When not to use\n\n- A single value against a limit. Use Gauge Meter or a plain progress bar.\n- Shares of a whole that has no limit. Use Waffle Chart or Donut Chart.\n\n## Notes\n\n- The remainder turns amber at `warnAt` (85% by default) and red, reading \"over\", past the allowance. The bar stops at its end.\n- Segments grow in one after another from the left; change a value and its segment, and every one after it, springs to the new place.\n- The figure counts up with `useCountUp` from Kinetik Count.\n- With reduced motion the bar and figure show their values at once.\n"
  },
  "docs": "Prefer `npx kinetik-ui add usage-meter`: it installs dependencies with expo install, so versions match your Expo SDK."
}
