Try 'magnetic button'
View code

Specification

Catalogue
No. 99 of 110
Interaction
Hover, Press
Element
Chart, Counter
Preview
Live component
Imports
react 19.3.1
Added

Notes

How full a model's context window is: one bar split into chat, files, tools, system and memory, the total against the limit, and a Compact button. Point at a key and its part of the bar stays lit while the others fade, and the figure becomes that part's size. Compact shrinks the chat and the files and the bar closes up.

Each part is a flex item whose flex-grow is its size in thousands, with the free space as one more item at the end, so compacting is a change of numbers and the bar animates itself. The figure is keyed to what it shows, so it rolls in from below each time it changes.

The figures are sample data.

Code

React and plain CSS
"use client";

import { useEffect, useState, type CSSProperties } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./context-meter.module.css";

type Part = { id: string; label: string; hue: number; full: number; compact: number };

/** Tokens in a model's context window, in thousands. Compacting summarises the chat and the files. */
const PARTS: Part[] = [
  { id: "chat", label: "Chat", hue: 1, full: 108.6, compact: 31.4 },
  { id: "files", label: "Files", hue: 5, full: 41.2, compact: 18.0 },
  { id: "tools", label: "Tools", hue: 7, full: 12.8, compact: 12.8 },
  { id: "system", label: "System", hue: 3, full: 4.2, compact: 4.2 },
  { id: "memory", label: "Memory", hue: 2, full: 8.8, compact: 8.8 },
];
const LIMIT = 200;

/** The card demo: read each part, compact, then put it back. */
const DEMO: Array<{ focus: string | null; compact: boolean; wait: number }> = [
  { focus: null, compact: false, wait: 700 },
  ...PARTS.map((p) => ({ focus: p.id, compact: false, wait: 520 })),
  { focus: null, compact: false, wait: 500 },
  { focus: null, compact: true, wait: 1500 },
  { focus: "chat", compact: true, wait: 900 },
];

const k = (n: number) => `${n.toFixed(1)}K`;
const hue = (n: number) => ({ "--hue": `var(--cat-${n}, var(--accent, currentColor))` }) as CSSProperties;

export default function ContextMeter({ active, context }: LivePreviewProps) {
  const card = context === "card";
  const [focus, setFocus] = useState<string | null>(null);
  const [compact, setCompact] = useState(false);
  const [step, setStep] = useState(0);
  const demo = card && active;

  const [running, setRunning] = useState(demo);
  if (running !== demo) {
    setRunning(demo);
    setStep(0);
  }

  useEffect(() => {
    if (!demo) return;
    const id = window.setTimeout(() => setStep((s) => s + 1), DEMO[step % DEMO.length]?.wait ?? 600);
    return () => window.clearTimeout(id);
  }, [demo, step]);

  const frame = demo ? DEMO[step % DEMO.length] : undefined;
  const shownFocus = card ? (frame?.focus ?? null) : focus;
  const shownCompact = card ? Boolean(frame?.compact) : compact;
  const sizes = PARTS.map((p) => (shownCompact ? p.compact : p.full));
  const used = sizes.reduce((a, b) => a + b, 0);
  const focused = PARTS.findIndex((p) => p.id === shownFocus);
  const tab = card ? -1 : 0;

  return (
    <div className={styles.area}>
      <div className={styles.panel}>
        <div className={styles.head}>
          <span className={styles.title}>Context</span>
          <button type="button" className={styles.compact} tabIndex={tab} aria-pressed={shownCompact} onClick={() => setCompact((c) => !c)}>
            {shownCompact ? "Restore" : "Compact"}
          </button>
        </div>
        <p className={styles.reading}>
          <span key={`${shownFocus}-${shownCompact}`} className={styles.value}>
            {focused >= 0 ? k(sizes[focused] ?? 0) : k(used)}
          </span>
          <span className={styles.of}>{focused >= 0 ? PARTS[focused]?.label : `of ${LIMIT}K`}</span>
        </p>
        <div className={styles.bar} data-focusing={focused >= 0 || undefined} aria-hidden="true">
          {PARTS.map((p, i) => (
            <span key={p.id} className={styles.seg} data-on={focused === i || undefined} style={{ ...hue(p.hue), flexGrow: sizes[i] }} />
          ))}
          <span className={styles.free} style={{ flexGrow: LIMIT - used }} />
        </div>
        <ul className={styles.legend}>
          {PARTS.map((p, i) => (
            <li key={p.id}>
              <button
                type="button"
                className={styles.key}
                style={hue(p.hue)}
                tabIndex={tab}
                data-on={focused === i || undefined}
                aria-label={`${p.label}, ${k(sizes[i] ?? 0)} tokens`}
                onPointerEnter={() => setFocus(p.id)}
                onPointerLeave={() => setFocus(null)}
                onFocus={() => setFocus(p.id)}
                onBlur={() => setFocus(null)}
              >
                {p.label}
              </button>
            </li>
          ))}
        </ul>
      </div>
      <p className="visually-hidden" role="status">
        {shownCompact ? `Compacted: ${k(used)} of ${LIMIT}K used.` : ""}
      </p>
    </div>
  );
}

More like this

Donut that changes period

Hover, PressCharts and numbers

Slope chart for before and after

Hover, PressCharts and numbers

Traffic in stacked bars

HoverCharts and numbers

How the tools connect

HoverCharts and numbers

Balanced logo wall

Hover, PressPage sections

Sphere of dots that parts for the pointer

Hover, PressPage sections