# Count-up metric

Subtle web animation sketch from [https://mighil.com/animations](https://mighil.com/animations).

- **Live demo:** https://mighil.com/animations/count-up-metric
- **Standalone HTML:** https://mighil.com/animations/count-up-metric?download=1
- **This file:** https://mighil.com/animations/count-up-metric/count-up-metric.md
- **Technique:** `stats`

## What it does

Number eases up to the target.

## Instructions for AI / coding agents

Implement this micro-interaction in the user's stack (vanilla HTML, React, Vue, Svelte, etc.).

1. Use the **HTML** as the DOM structure (adapt JSX/templates as needed; keep class names and ids).
2. Include the **Theme** CSS (or map the same variables into the host design system). It switches light/dark with `prefers-color-scheme`.
3. Apply the **CSS** as-is when possible. It uses `var(--text-color)`, `var(--heading-color)`, `var(--background-color)`, etc. from the theme so both modes work.
4. Run the **JavaScript** after the markup mounts. Prefer `DOMContentLoaded` or a framework `useEffect` / `onMount`.
5. Keep motion respectful of `prefers-reduced-motion` if the snippet already checks it.
6. Do not depend on mighil.com runtime or fonts. Theme variables are enough for color.
7. If porting to a component, expose the interactive root via a ref and preserve accessibility attributes (`aria-*`, `role`, `button` types).

## Theme (auto light / dark)

```css
/* Auto light / dark — follows prefers-color-scheme */
:root{
  color-scheme: light dark;
  --text-color: hsl(0, 0%, 18%);
  --heading-color: hsl(0, 0%, 18%);
  --background-color: hsl(0, 0%, 99%);
  --bg-color: hsl(0, 0%, 99%);
  --link-color: hsl(0, 0%, 18%);
  --visited-color: hsl(0, 0%, 18%);
  --blockquote-color: hsl(0, 0%, 18%);
  --code-bg-color: hsl(0, 0%, 95%);
  --code-color: hsl(0, 0%, 18%);
  --sub-color: hsl(0, 0%, 29%);
}
@media (prefers-color-scheme: dark){
:root{
  --text-color: hsl(35, 25%, 85%);
  --heading-color: hsl(35.2, 32.6%, 82.5%);
  --background-color: hsl(60, 2.2%, 8.8%);
  --bg-color: hsl(60, 2.2%, 8.8%);
  --link-color: hsl(34.7, 55.3%, 79.8%);
  --visited-color: hsla(34.7, 55.3%, 79.8%, 0.85);
  --blockquote-color: hsl(35, 30%, 75%);
  --code-bg-color: hsl(30, 5%, 7.8%);
  --code-color: limegreen;
  --sub-color: hsla(34.7, 24.7%, 84.9%, 0.78);
}
}
```

## HTML

```html
<div class="metric">
        <span class="metric-val" id="metric-val">0</span>
        <span class="metric-label">sessions today</span>
      </div>
```

## CSS

```css
.metric{display:flex;flex-direction:column;gap:2px;align-items:flex-start}

.metric-val{
  font-size:1.75rem;font-weight:600;font-variant-numeric:tabular-nums;letter-spacing:-.02em;
  line-height:1.1;
}

.metric-label{font-size:.8rem;color:color-mix(in srgb,var(--text-color) 55%,transparent)}
```

## JavaScript

```js
// Count-up metric
  const metricVal = document.getElementById('metric-val');
  let metricRaf = 0;
  function runMetric() {
    if (!metricVal) return;
    cancelAnimationFrame(metricRaf);
    const target = 1284;
    if (reduce) {
      metricVal.textContent = target.toLocaleString('en-US');
      return;
    }
    const start = performance.now();
    const dur = 1100;
    const tick = (now) => {
      const t = Math.min(1, (now - start) / dur);
      const eased = 1 - Math.pow(1 - t, 3);
      metricVal.textContent = Math.round(target * eased).toLocaleString('en-US');
      if (t < 1) metricRaf = requestAnimationFrame(tick);
    };
    metricVal.textContent = '0';
    metricRaf = requestAnimationFrame(tick);
  }
  runMetric();

document.querySelectorAll('[data-act]').forEach((btn) => {
    btn.addEventListener('click', (e) => {
      e.stopPropagation();
      const act = btn.getAttribute('data-act');
      if (act === 'metric') runMetric();
    });
  });
```

---

_Source: https://mighil.com/animations/count-up-metric_
