component
Legend
What a chart's colours mean: a small square of each, its name and, if it helps, its value, in a row that wraps.
- Earlier months
- November€2,760
Defaultimport { Legend } from 'rost/components/legend/Legend'
<Legend items={[{ label: 'Earlier months' }, { label: 'November', value: '€2,760', current: true }]} /> src/components/legend/Legend.tsx
import type { HTMLAttributes, ReactNode } from 'react'
import './legend.css'
export interface LegendItem {
readonly label: ReactNode
readonly value?: ReactNode
/** The green one, as in BarChart. */
readonly current?: boolean
}
export interface LegendProps extends Omit<HTMLAttributes<HTMLUListElement>, 'children'> {
/** One per colour, in the order the chart uses them. */
readonly items: readonly LegendItem[]
}
const b = 'rost-legend'
/** What a chart's colours mean. Built from contracts/legend.contract.yaml. */
export function Legend({ items, className, ...rest }: LegendProps) {
return (
<ul className={[b, className].filter(Boolean).join(' ')} {...rest}>
{items.map((it, i) => (
<li key={i} className={`${b}__item`}>
<span className={`${b}__${it.current ? 'current-swatch' : 'swatch'}`} aria-hidden="true" />
<span className={`${b}__label`}>{it.label}</span>
{it.value != null && <span className={`${b}__value`}>{it.value}</span>}
</li>
))}
</ul>
)
}
The source for everything on this page: contracts/legend.contract.yaml (opens in a new tab). The CSS, the docs and the checks in each app are built from it.
# yaml-language-server: $schema=../schema/component-contract.schema.json
schemaVersion: 2
id: legend
name: Legend
kind: component
status: review
summary: "What a chart's colours mean: a small square of each, its name and, if it helps, its value, in a row that wraps."
category: display
related: [ bar-chart, sparkline, stat ]
source:
tool: figma
fileKey: 1Zy0wiWKXQAvsfDBX3KR0d
nodeId: "753:25"
url: https://www.figma.com/design/1Zy0wiWKXQAvsfDBX3KR0d/UI-Kit-v01?node-id=753-25
extractedAt: "2026-11-17"
extractedBy: hand
frozenAt: "2026-11-17"
props:
items: { type: node, required: true, notes: "`{ label, value?, current? }`, one per colour. `current` is the green one, as in BarChart." }
elements:
root:
notes: "A `<ul>`: the items in a row, 16px apart, wrapping when there's no room."
styles:
display: { value: flex, assert: false }
flex-wrap: { value: wrap, assert: false }
column-gap: { token: spacing/gap/medium, value: 16px }
row-gap: { token: spacing/gap/small, value: 8px }
margin: { token: spacing/gap/none, value: 0px }
padding: { token: spacing/gap/none, value: 0px }
list-style: { value: none, assert: false }
item:
notes: "An `<li>`: the square, the name and the value, 6px apart."
styles:
display: { value: inline-flex, assert: false }
align-items: { value: center }
gap: { token: spacing/gap/xsmall, value: 6px }
swatch:
notes: "An 8px square with 2px corners, in BarChart's grey."
styles:
display: { value: block, assert: false }
flex-shrink: { value: "0", assert: false }
width: { token: spacing/gap/small, value: 8px }
height: { token: spacing/gap/small, value: 8px }
border-radius: { token: spacing/radius/2xsmall, value: 2px }
background: { token: legend/swatch, value: "#747573" }
current-swatch:
notes: "The same square in BarChart's green."
styles:
display: { value: block, assert: false }
flex-shrink: { value: "0", assert: false }
width: { token: spacing/gap/small, value: 8px }
height: { token: spacing/gap/small, value: 8px }
border-radius: { token: spacing/radius/2xsmall, value: 2px }
background: { token: legend/current-swatch, value: "#00d669" }
label:
notes: "Label type, in the secondary text colour."
styles:
font-family: { token: font-family/base, value: "'General Sans Variable'" }
font-weight: { token: typography/label/font-weight/regular, value: "400" }
font-size: { token: typography/label/font-size, value: 12px }
line-height: { token: typography/label/line-height, value: 14px }
letter-spacing: { token: typography/label/letter-spacing, value: 0px }
color: { token: legend/label, value: "#d3d9d5" }
value:
optional: true
notes: "Label type, medium weight, the main text colour."
styles:
font-family: { token: font-family/base, value: "'General Sans Variable'" }
font-weight: { token: typography/label/font-weight/medium, value: "500" }
font-size: { token: typography/label/font-size, value: 12px }
line-height: { token: typography/label/line-height, value: 14px }
letter-spacing: { token: typography/label/letter-spacing, value: 0px }
color: { token: legend/value, value: "#ededed" }
guidance:
status: proposed
use:
- title: Under a chart
text: "Under a BarChart, saying that green is this month and grey the ones before, with this month's total."
avoid:
- title: One colour
text: "A chart with one colour needs a title, not a legend."
behaviour: "It lists what each colour means, in the order the chart uses them. It doesn't draw the chart or know about it."
accessibility:
element: "`<ul>`; the squares are drawing, hidden from screen readers, so each item reads as its name and value."
colour: "The names are 11:1 and the values 17:1 on the dark page. The squares are 4.3:1 and 10:1."
content: "Name what the colour stands for (This month, Earlier), not the colour."
Install
npx github:Runceanu/rost add legend It lands in src/rost/legend/, with what it needs and its contract. Install has the rest.
When to use
Under a chart
Under a BarChart, saying that green is this month and grey the ones before, with this month's total.
When not to use
One colour
A chart with one colour needs a title, not a legend.
Behaviour
It lists what each colour means, in the order the chart uses them. It doesn't draw the chart or know about it.
Accessibility
- Element
<ul>; the squares are drawing, hidden from screen readers, so each item reads as its name and value. - Colour
The names are 11:1 and the values 17:1 on the dark page. The squares are 4.3:1 and 10:1.
Content
Name what the colour stands for (This month, Earlier), not the colour.
Props
| Prop | Type | Values | Default |
|---|---|---|---|
items | node | | |
Values
Straight from the contract. Each value sits next to its token, because six tokens here all come out as #3a3a3a and the colour alone can't tell you which one is right.
Never changes
root
-
column-gap16pxspacing/gap/medium -
row-gap8pxspacing/gap/small -
margin0pxspacing/gap/none -
padding0pxspacing/gap/none
displayflex · flex-wrapwrap · list-stylenone
item
-
gap6pxspacing/gap/xsmall
displayinline-flex · align-itemscenter
swatch
-
width8pxspacing/gap/small -
height8pxspacing/gap/small -
border-radius2pxspacing/radius/2xsmall -
background#747573legend/swatch
displayblock · flex-shrink0
current-swatch
-
width8pxspacing/gap/small -
height8pxspacing/gap/small -
border-radius2pxspacing/radius/2xsmall -
background#00d669legend/current-swatch
displayblock · flex-shrink0
label
-
font-family'General Sans Variable'font-family/base -
font-weight400typography/label/font-weight/regular -
font-size12pxtypography/label/font-size -
line-height14pxtypography/label/line-height -
letter-spacing0pxtypography/label/letter-spacing -
color#d3d9d5legend/label
value
-
font-family'General Sans Variable'font-family/base -
font-weight500typography/label/font-weight/medium -
font-size12pxtypography/label/font-size -
line-height14pxtypography/label/line-height -
letter-spacing0pxtypography/label/letter-spacing -
color#edededlegend/value