仕組み
A 12px pill with absolutely placed segments separated by 1px borders in the surface colour, so the gaps stay crisp on any background. The whole bar is revealed with one clip-path sweep from the left on the reveal curve, so it reads as a single stroke rather than separate pieces; the marker then drops onto it on a spring. The marker is a dark chip above a 2px pin that crosses the bar with a ring in the surface colour; near either end it aligns inward. Pointing at a segment or its legend row fades the others to 30%.
向いている場面
- Composition of one whole: storage, a budget, time spent, a portfolio.
- A value placed on a banded scale: a score, a quality index, a risk level.
向かない場面
- Comparing many wholes side by side: use a stacked bar chart.
- More than about seven categories: group the small ones.
使い方
import { CategoryBar } from './CategoryBar.jsx'
export function StorageUse({ usage }) {
return (
<CategoryBar
label="Storage use"
total={100}
data={[
{ name: 'Photos', value: usage.photos },
{ name: 'Documents', value: usage.documents, tone: 3 },
{ name: 'Free', value: 100 - usage.used, tone: 'track' },
]}
marker={{ value: 80, label: 'Alert at 80 GB' }}
formatValue={(v) => `${v} GB`}
/>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
data | { name: string, value: number, tone?: 1-8 | 'track', color?: string, display?: string }[] | — | Segments left to right. tone 'track' draws an empty part (free space); display replaces the value in the legend, for example a range. |
total | number | — | The whole; defaults to the sum. Anything left over stays empty. |
marker | { value: number, label?: string } | — | A dark chip and a pin at a value, aligned so it stays inside near either end. |
boundaries / legend | boolean | false / true | Cumulative values under the segment edges (crowded ones are skipped), and a legend with value and share. |
formatValue / formatShare | (v) => string / (pct) => string | — | Number formats; return '' from formatShare to hide shares. |
label | string | 'Breakdown' | Start of the accessible name; every category, its value and share and the marker are appended. |
アクセシビリティ
- The bar is one image whose accessible name lists every category with its value and share, and the marker label.
- The legend repeats it visually and is hidden from screen readers to avoid reading it twice.
- Hover highlighting is a visual aid only; nothing is lost without a pointer.
- Reduced motion shows the bar and marker at once.
モーション
Bar: one 1000ms clip-path sweep on the reveal curve. Marker: drops from 520ms with a 520ms spring. Highlight: 160ms.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
CategoryBar.jsxcategory-bar.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

