Skip to content

Metric Card ​

Renders report indicators in an equal-width table grid. Each card shows a label, a prominent value, and optional unit and comparison text.

Import ​

ts
import { metricCardPlugin, type MetricCardOptions, type MetricItem } from 'docx-kit'

The standalone package exports the same factory and types from @docxkit/plugin-metric-card. The invocation name is metricCard.

Options ​

OptionTypeDefaultDescription
itemsMetricItem[]RequiredIndicators in display order; empty arrays render (no metrics)
columnsnumber3Maximum cards per row; must be a positive safe integer

MetricItem ​

FieldTypeDescription
labelstringIndicator name
valuenumber | stringDisplay value, including zero; no automatic locale formatting
unitstringUnit appended after a space
changestringLiteral comparison text such as +12% vs last month
trend'up' | 'down' | 'neutral'Optional arrow and color: ↑ success, ↓ danger, → muted

change and trend work independently. No trend is inferred from the value or change text. When a decrease is desirable, omit trend and provide your own comparison text or override the invocation color.

Example ​

ts
import { createDocx, metricCardPlugin } from 'docx-kit'

const doc = createDocx()
  .use(metricCardPlugin())
  .plugin('metricCard', {
    columns: 3,
    items: [
      { label: 'Revenue', value: '128,000', unit: 'USD', trend: 'up', change: '+12% vs last month' },
      { label: 'Active users', value: 4200, trend: 'down', change: '-3%' },
      { label: 'Uptime', value: 99.9, unit: '%', trend: 'neutral', change: 'Unchanged' },
      { label: 'Incidents', value: 0 },
    ],
  })

await doc.toBlob()

Rows do not split across pages. The final row is padded with empty cells. If there are fewer items than columns, the available cards share the full width. Invalid column counts throw DocxKitError with code PLUGIN_RENDER_FAILED, including for empty input.

The grid uses theme colors surface, primary, muted, success, and danger with built-in fallbacks. Invocation styles override text, paragraphs, and cell backgrounds or borders.

Style customization ​

Pass { styles } to the factory for instance defaults, and options.styles for invocation overrides. The package and docx-kit entry points export MetricCardConfig and MetricCardStyles. Priority: built-in/theme defaults → initialization styles → common node style → invocation part styles. Nested fields merge; arrays replace; undefined retains defaults; 0 and false apply. Units and theme tokens such as $colors.primary are supported.

PartsStyle typeScope
change, emptyText, label, unit, valueTextStyleRuleRun typography
changeParagraph, emptyParagraph, labelParagraph, paddingParagraph, valueParagraphParagraphStyleRuleBackground, borders, alignment, spacing and flow
cellOmit<CellStyleRule, 'height'>Cell shading, borders, width and padding
ts
metricCardPlugin({
  styles: { value: { color: '#2563EB', fontSize: 14 } },
})

Use the same part names in options.styles for a single call. JSON DSL, AI tool schemas and MCP validation share these contracts; unknown parts and misplaced properties are rejected.

Released under the MIT License.