PeakUIPro

KPI

Metric card with a figure, a trend and a sparkline

Usage

KPI is imported from its own subpath and needs the optional recharts peer.

npm install recharts
import {KPI} from "@peakui-pro/react/kpi";

Anatomy

import {KPI} from "@peakui-pro/react/kpi";

// The card is a group named by its own title, so each one in a row announces its own metric.
// `Actions` sits in the card's corner and is written outside the header.
export default () => (
  <KPI>
    <KPI.Header>
      <KPI.Icon />
      <KPI.Title />
    </KPI.Header>
    <KPI.Actions />
    <KPI.Content>
      <KPI.Value value={0} />
      <KPI.Trend />
    </KPI.Content>
    <KPI.Chart data={[]} />
    <KPI.Separator />
    <KPI.Footer />
  </KPI>
);

Missing Observations

A gap in the data is null, never 0: zero is a reading, and it drops the line to the floor.

// Without `connectNulls` the line breaks at the gap, which is what a reader should see.
// With it, the line is drawn across — an estimate, so say so where the card is read.
const data = [{value: 12400}, {value: 14100}, {value: null}, {value: 17800}];

<KPI.Chart data={data} />
<KPI.Chart connectNulls data={data} />

Examples

With Icon

status tints the tile and its glyph; without one the tile is neutral.

With Actions

With Progress

status colours the bar; a metric that is simply filling up reads as success.

With Chart Bottom

With Chart Inline

With Chart Tooltip

With Chart Connect Nulls

Customization

Tailwind CSS

<KPI className="max-w-80" variant="secondary" />

Global CSS

@layer components {
  .kpi {
    --kpi-icon-size: calc(var(--spacing) * 10);
    --kpi-chart-fade: 20%;
  }
}

Styling Reference

PeakUI follows the BEM methodology, so variants and states are plain class names you can target.

CSS Classes

The KPI component uses these CSS classes (view source styles):

Base Classes

  • .kpi - Added to the Card root, beside the Card's own classes

Element Classes

  • .kpi__header - The tile, the title and whatever sits beside them, on one line
  • .kpi__icon - The status tile
  • .kpi__title - The metric's name; quieter than the figure
  • .kpi__actions - The menu, in the card's corner and out of the header's flow
  • .kpi__content - The figure and whatever shares its line, as grid columns
  • .kpi__value - The figure
  • .kpi__trend - The trend chip
  • .kpi__progress - The progress bar; it takes the content's full width
  • .kpi__chart - The sparkline; bleeds to the card's edges and fades at both ends
  • .kpi__separator - The rule above a footer
  • .kpi__footer - The line under the rule

Data Attributes

  • [data-status="success"], [data-status="warning"], [data-status="danger"] - On the icon

Slots

  • [data-slot="card"] - The root element, a Card
  • [data-slot="kpi-header"], [data-slot="kpi-icon"], [data-slot="kpi-title"] - The header row
  • [data-slot="kpi-actions"] - The menu's box
  • [data-slot="kpi-content"], [data-slot="kpi-value"] - The figure's row
  • [data-slot="kpi-chart"] - The sparkline's box
  • [data-slot="kpi-footer"] - The footer

CSS Variables

VariablePurpose
--kpi-icon-sizeWidth and height of the status tile
--kpi-actions-insetDistance of the menu from the card's top and end edges
--kpi-chart-fadeWidth of the sparkline's fade at each end
--kpi-paddingHow far the sparkline bleeds; mirrors the Card's padding

API Reference

KPI

PropTypeDefaultDescription
childrenReactNode-The card's parts
variant'default' | 'secondary' | 'tertiary' | 'transparent''default'Card variant
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Every other Card prop. role is group and aria-labelledby points at the title; both can be overridden

KPI.Icon

PropTypeDefaultDescription
status'success' | 'warning' | 'danger'-Tints the tile and its glyph; neutral without one
childrenReactNode-The glyph
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

KPI.Title

PropTypeDefaultDescription
childrenReactNode-The metric's name. It also names the card, so every card in a row needs one
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes; id is the card's own and overriding it unnames the card

KPI.Value

PropTypeDefaultDescription
valuenumber-Required. The figure
children(formatted: string) => ReactNode-Receives the formatted figure; the result fills the value element
classNamestring-Additional classes, on the value's box
...restNumberValueProps-Every NumberValue prop: style, currency, notation, locale, formatOptions and the rest

KPI.Trend

PropTypeDefaultDescription
...restTrendChipProps-Every TrendChip prop: trend, size, variant, and the parts as children

KPI.Actions

PropTypeDefaultDescription
childrenReactNodethe ellipsis glyphThe button's content
aria-labelstring'More actions'The button's name; set it when the menu is about one metric
...restButtonProps-Every Button prop. The defaults are variant="ghost", size="sm" and isIconOnly

KPI.Progress

PropTypeDefaultDescription
valuenumber-Required. How far along, from 0 to 100
status'success' | 'warning' | 'danger''success'Colours the bar
aria-labelstring'Progress'The bar's name; set it when a card carries more than one number
classNamestring-Additional classes
...restComponentProps<'div'>-Every other ProgressBar prop

KPI.Chart

PropTypeDefaultDescription
dataRecord<string, number | string | null>[]-Required. One object per observation; a missing one is null, never 0
dataKeystring'value'The field each observation is read from
colorstring'currentColor'The series line's colour
fillColorstringcolor at 20%The fill under the line, at its top
connectNullsbooleanfalseDraws across a null instead of breaking the line at it
heightnumber80Height of the plot
strokeWidthnumber2Width of the series line
tooltipReactNode-A Tooltip. With one the plot is reachable from the keyboard and the card's title names it; without one it is hidden from assistive technology
areaPropsOmit<AreaProps, 'dataKey'>-Passed to the series, winning over everything the card sets on it
classNamestring-Additional classes
...restComponentProps<'div'>-Native <div> attributes

KPI.Header · KPI.Content · KPI.Footer

PropTypeDefaultDescription
childrenReactNode-The part's content
classNamestring-Additional classes
renderDOMRenderFunction<'div', undefined>-Replaces the rendered <div> with your own element
...restComponentProps<'div'>-Native <div> attributes

KPI.Separator

PropTypeDefaultDescription
...restSeparatorProps-Every Separator prop

Render Props

PropTypeDescription
children of KPI.Value(formatted: string) => ReactNodeReceives the formatted figure; the result fills .kpi__value

On this page