react ~3 KB 0 deps v0.1.0 ↗ GitHub ↗

@mshafiqyajid/react-stat

Headless hook + styled card for statistic displays. Smooth count-up animation, trend direction (up/down/neutral), loading skeleton, prefix/suffix slots, primary tone.

Playground #

Total Users
12.5%vs last month
Revenue
8.2%vs last month
Conversion
1.2%vs last month
Total Users
12.5%vs last month
Props
TSX
import { StatStyled } from "@mshafiqyajid/react-stat/styled";
import "@mshafiqyajid/react-stat/styles.css";

<StatStyled />

Install #

npm install @mshafiqyajid/react-stat

Quick start #

import { StatStyled } from "@mshafiqyajid/react-stat/styled";
import "@mshafiqyajid/react-stat/styles.css";

<StatStyled
  value={12847}
  label="Total Users"
  trend={12.5}
  trendLabel="vs last month"
/>

With prefix and suffix #

<StatStyled value={98420} label="Revenue" trend={8.2} prefix="$" />
<StatStyled value={3.4}  label="Conversion" trend={-1.2} suffix="%" />

Derived trend #

Pass previousValue instead of trend and the component derives the percentage change automatically.

<StatStyled
  value={1250}
  label="Sign-ups"
  previousValue={1000}
  trendLabel="vs last week"
/>

Loading skeleton #

<StatStyled value={0} label="Revenue" loading={true} />

Headless #

import { useStat } from "@mshafiqyajid/react-stat";

const { rootProps, valueProps, animatedValue, trendDirection } = useStat({
  value: 12847,
  label: "Total Users",
  trend: 12.5,
  countUp: true,
  countUpDuration: 1200,
});

return (
  <figure {...rootProps}>
    <p>Total Users</p>
    <span {...valueProps}>{animatedValue.toLocaleString()}</span>
    {trendDirection === "up" && <span aria-label="trending up">↑ 12.5%</span>}
  </figure>
);

API #

PropTypeDefaultDescription
valuestring | numberThe statistic value to display
labelstringDescriptive label above the value
previousValuenumberPrevious value — derives trend automatically
trendnumberExplicit trend percentage (overrides derived)
trendLabelstringContextual label beside trend (e.g. "vs last month")
trendFormat"percent" | "absolute""percent"How trend magnitude is displayed
prefixReactNodeContent before value (e.g. "$")
suffixReactNodeContent after value (e.g. "%")
iconReactNodeIcon displayed beside the label
size"sm" | "md" | "lg""md"Card size
tone"neutral" | "primary""neutral"Color tone
countUpbooleantrueAnimate value counting up on mount
countUpDurationnumber1000Count-up animation duration (ms)
countUpDelaynumber0Delay before animation starts (ms)
loadingbooleanfalseShow pulsing skeleton placeholder
classNamestringExtra class on the root element
styleCSSPropertiesInline style override
Edit this page on GitHub