# Usage Meter

A plan usage card for AI credits or any metered quota. Pick a range in the segmented control and the total rolls to the new value, the stacked meter and the rows animate, and the per-day bars grow in. Hover a meter segment or a category row to highlight it and see its share. Hover a day bar, or focus the chart and use the arrow keys, to read that day. When usage reaches the limit, the footer switches from credits left and **Upgrade** to **Limit reached** and **Add credits**. The demo cycles through the ranges on its own.

<demo-usage-meter>



</demo-usage-meter>

## Installation

```bash
npx shadcn-vue@latest add "https://nxui.geoql.in/r/usage-meter.json"
```

## Usage

```vue
<script setup lang="ts">
  import { ref } from 'vue';
  import UsageMeter from '~/components/ui/usage-meter/UsageMeter.vue';
  import type {
    UsageMeterCategory,
    UsageMeterRange,
  } from '~/components/ui/usage-meter/types';

  const categories: UsageMeterCategory[] = [
    { key: 'chat', label: 'Chat', color: '#2f5bea', darkColor: '#6573f6' },
    { key: 'search', label: 'Search', color: '#7091f2', darkColor: '#4553c8' },
    { key: 'image', label: 'Image', color: '#b4c4f8', darkColor: '#2c357c' },
    { key: 'api', label: 'API', color: '#9d9da6', darkColor: '#64646d' },
  ];

  const ranges: UsageMeterRange[] = [
    {
      value: 'month',
      label: 'This month',
      usage: { chat: 3980, search: 1640, image: 1120, api: 680 },
      daily: [310, 190, 180, 320, 560],
      days: 30,
      stats: [
        { label: 'Seats', value: 8, max: 10 },
        { label: 'Storage', value: 3.2, max: 5, unit: 'GB' },
        { label: 'Projects', value: 12 },
      ],
    },
  ];

  const range = ref('month');
</script>

<template>
  <UsageMeter
    v-model:range="range"
    plan="Pro plan"
    resets="Resets in 9 days"
    :limit="10000"
    :categories="categories"
    :ranges="ranges"
  />
</template>
```

The total is the sum of the category values in the active range. Categories with a value of `0` collapse out of the list. Day slots after the last `daily` value are drawn as flat future days. A stat without `max` is unlimited and shows `∞` with a hatched track. A stat at 80% or more of its `max` gets a warning dot.

## Props

<table>
<thead>
  <tr>
    <th>
      Prop
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        categories
      </code>
    </td>
    
    <td>
      <code>
        UsageMeterCategory[]
      </code>
    </td>
    
    <td>
      —
    </td>
    
    <td>
      Categories in meter order: <code>
        key
      </code>
      
      , <code>
        label
      </code>
      
      , <code>
        color
      </code>
      
      , <code>
        darkColor
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        ranges
      </code>
    </td>
    
    <td>
      <code>
        UsageMeterRange[]
      </code>
    </td>
    
    <td>
      —
    </td>
    
    <td>
      Ranges for the segmented control, each with its own data.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        range
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      <code>
        v-model:range
      </code>
      
      . The active range value. Falls back to the first range.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        workspace
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'nxui'
      </code>
    </td>
    
    <td>
      Workspace name in the header.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        plan
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Pro plan'
      </code>
    </td>
    
    <td>
      Plan name under the workspace.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        resets
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      Text after the plan, e.g. <code>
        Resets in 9 days
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        limit
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      <code>
        10000
      </code>
    </td>
    
    <td>
      Usage limit. At or over it, the card shows the limit state.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        unit
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'credits'
      </code>
    </td>
    
    <td>
      Unit shown after numbers.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        accent
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      Colour of a hovered day bar. Defaults to the first category colour.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        upgrade-label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Upgrade'
      </code>
    </td>
    
    <td>
      Footer button text below the limit.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        add-credits-label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Add credits'
      </code>
    </td>
    
    <td>
      Footer button text at the limit.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        limit-label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        'Limit reached'
      </code>
    </td>
    
    <td>
      Footer text at the limit.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        class
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        ''
      </code>
    </td>
    
    <td>
      Extra classes for the root element.
    </td>
  </tr>
</tbody>
</table>

## Range options

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        value
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Range id, used by <code>
        v-model:range
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        label
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Text in the segmented control.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        usage
      </code>
    </td>
    
    <td>
      <code>
        Record<string, number>
      </code>
    </td>
    
    <td>
      Usage per category key.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        daily
      </code>
    </td>
    
    <td>
      <code>
        number[]
      </code>
    </td>
    
    <td>
      Usage per day.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        days
      </code>
    </td>
    
    <td>
      <code>
        number
      </code>
    </td>
    
    <td>
      Total day slots. Extra slots are drawn as future days.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        dayLabels
      </code>
    </td>
    
    <td>
      <code>
        string[]
      </code>
    </td>
    
    <td>
      Label per day slot, e.g. <code>
        Oct 5
      </code>
      
      . Defaults to <code>
        Day N
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        stats
      </code>
    </td>
    
    <td>
      <code>
        UsageMeterStat[]
      </code>
    </td>
    
    <td>
      Quota stats: <code>
        label
      </code>
      
      , <code>
        value
      </code>
      
      , optional <code>
        max
      </code>
      
       and <code>
        unit
      </code>
      
      .
    </td>
  </tr>
</tbody>
</table>

## Events

<table>
<thead>
  <tr>
    <th>
      Event
    </th>
    
    <th>
      Payload
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        range-change
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      Fires when the user picks another range.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        upgrade
      </code>
    </td>
    
    <td>
      —
    </td>
    
    <td>
      Fires when <strong>
        Upgrade
      </strong>
      
       is clicked.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        add-credits
      </code>
    </td>
    
    <td>
      —
    </td>
    
    <td>
      Fires when <strong>
        Add credits
      </strong>
      
       is clicked.
    </td>
  </tr>
</tbody>
</table>

## Accessibility

- The range switch is a `tablist`. Arrow keys, Home and End move between ranges.
- The meter uses `role="meter"` with `aria-valuenow`, `aria-valuemax` and a text value.
- Category rows are focusable. Focus highlights the row and its meter segment.
- The per-day chart is focusable. Arrow keys, Home and End read each day, and the label is announced through `aria-live`.
- The limit state is announced with `role="status"`.
- All motion stops when the user prefers reduced motion.
