Command palette

@yasinsensoy/web

Web · Components

Stat

Metric (KPI) card for dashboards: name, large value, change from the previous period, description and trend chart.

7 exports

Live example

Preview

Monthly revenue
$0
12.4% increasevs last month

Code

import { Stat } from "@yasinsensoy/web";

<Stat label="Monthly revenue" format="currency" changeLabel="vs last month" animate value={48230} previousValue={42900}
  sparkline={[32, 35, 31, 38, 42, 40, 47]} />

Events

  1. Events appear here when you interact with the preview.

Dashboard grid

Revenue
$48,230
12.4% increase
New members
1,284
2% decreasethis week
Error rate
1.2%
18% decrease
Target: below 1%
Response time
182ms
No change

Import

import { Stat, statChange } from "@yasinsensoy/web";

Stat

componentClient component

Metric (KPI) card for dashboards: name, large value, change from the previous period, description and trend chart.

  • Numbers are formatted by the UIProvider locale (format: currency, percent, compact…).
  • If previousValue is given, the change ratio is computed; the arrow direction and color are chosen by higherIsBetter (a cost decrease is green).
  • With sparkline a small trend chart is drawn below the value.
  • With animate the number counts up when it enters the screen.
  • While loading, placeholders are shown without layout shift.
  • Screen readers read the change as "12.5% increase"; the arrow icon is hidden.

Use a grid to line cards up side by side: <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">…</div>.

Stat({ label, value, format, prefix, suffix, previousValue, change: changeProp, changeFormat, changeLabel, higherIsBetter, description, icon, sparkline, sparklineType, sparklineColor, animate, loading, action, variant: variantProp, effect, size, locale: localeProp, currency: currencyProp, classNames, className, ...props }: StatProps): JSX.Element

Examples

<Stat label="Revenue" value={48230} format="currency" previousValue={42900}
  changeLabel="vs last month" sparkline={[31, 35, 33, 40, 44, 42, 48]} icon="wallet" />

<Stat label="Error rate" value={0.012} format="percent" change={-0.18}
  higherIsBetter={false} description="Target: below 1%" />

statChange

function

Computes the change ratio and direction.

statChange(value: number, previousValue: number): {
    change: number

Direction of a change.

type StatTrend = "up" | "down" | "flat"

Format of the value. Same as ChartFormat: "number", "compact" (1.2M), "percent" (0.25 → 25%), "currency" ($1,250), Intl.NumberFormatOptions or a function.

type StatFormat = ChartFormat

Look.

  • card: bordered card; lined up side by side on dashboards.
  • plain: frameless; inside a card or a header.
type StatVariant = "card" | "plain"

Classes that can be passed to the inner parts of Stat.

NameTypeDescription
label?stringLabel.
icon?stringIcon box.
value?stringMain value.
change?stringChange badge.
description?stringDescription row.
sparkline?stringSmall chart.

StatProps

interface

Props of the Stat component.

Extends: Omit<ComponentProps<"div">, "children" | "prefix">

NameTypeDefaultDescription
labelReactNodeName of the metric (e.g. "Monthly revenue").
valuenumber | ReactNodeValue. A number is written for the locale according to format; text or an element is shown as is.
format?StatFormat"number"Format of a numeric value. See StatFormat.
prefix?ReactNodePrefix before the value (e.g. "≈").
suffix?ReactNodeUnit after the value (e.g. "pcs", "ms").
previousValue?numberValue of the previous period. If given, the change ratio (change) is computed automatically.
change?numberChange ratio: 0.125 = 12.5% increase, -0.04 = 4% decrease. If omitted, computed from previousValue.
changeFormat?StatFormat"percent"Format of the change.
changeLabel?ReactNodePeriod written next to the change (e.g. "vs last month").
higherIsBetter?booleantrueIs an increase good? For metrics like error count, cost or response time pass false: decreases look green, increases red.
description?ReactNodeDescription below the value (e.g. "Target: $50,000").
icon?IconPropIcon next to the label: name or element.
sparkline?readonly (number | null | undefined)[]Data of the small chart showing the trend (e.g. values of the last 12 days). See Sparkline.
sparklineType?"line" | "area" | "bar""area"Chart type.
sparklineColor?ChartColorChart color. If omitted, by trend (good: green, bad: red).
animate?booleanfalseWhether the number counts up from zero/the previous value when it enters the screen and whenever it changes. Off with the "reduced motion" preference.
loading?booleanShows placeholders instead of the value and chart while data loads.
action?ReactNodeAction in the top right corner (e.g. a menu).
variant?StatVariant | CustomVariant<"stat">"card"Look: built-in (StatVariant) or a custom look added with defineAppearance. If omitted, UIProvider appearance.defaults.stat.variant.
effect?EffectPropEffect or effects (e.g. ["lift", "glow"]). The accent color is the theme's primary color. See BuiltinEffect.
size?"sm" | "md" | "lg""md"Size.
locale?string{@link UIProvider}'s `localeLocale (number format).
currency?string{@link UIProvider}'s `currencyCurrency.
classNames?StatClassNamesClasses added to the inner parts.