SpreadsheetGrid
Guides

Conditional formatting

Draw data bars / color scales / status chips with one column option, conditionalFormat

A lightweight helper corresponding to Excel's "Conditional Formatting". With one column option, conditionalFormat, data bars (amounts), color scales (amounts, or direction from a baseline) and status chips (states) are layered on the default cell rendering. min / max are computed automatically from the visible rows (fixed values work too).

const columns: GridColumn<Row>[] = [
  { key: 'qty', title: 'Stock', width: 100, align: 'right', conditionalFormat: { dataBar: {} } },
  { key: 'delta', title: 'MoM %', width: 100, conditionalFormat: { colorScale: { type: 'diverging', min: -30, mid: 0, max: 30 } } },
  { key: 'price', title: 'Unit price', width: 100, conditionalFormat: { colorScale: {} } },
  { key: 'status', title: 'Status', width: 110, conditionalFormat: { chips: { Active: 'good', Stopped: 'warning', Discontinued: 'critical' } } },
];

Demo

Rows 10 of 10Columns 6 of 6Sort: —
#
1
2
3
4
5
6
7
8
9
10
Rows 10 of 10Columns 6 of 6
Cell —Range —0 cells / 0 rows0 columns
Change Stock, MoM or Unit price and the bar lengths and colors update right away (filter the Status column and min / max are recomputed).

Data bars (dataBar)

Shows the size of a value with a band in the background. Without min / max, the minimum / maximum of the numbers in the view rows (after filtering and sorting; the loaded range with SSRM) are used, and they're recomputed when editing or filtering changes them. Columns with negative values extend left from a 0 axis, and columns with only negative values use the right edge as the axis (the same as Excel).

  • color / negativeColor: band colors (by default the tokens --ssg-cf-bar / --ssg-cf-bar-negative).
  • showValue: false: show only the band without text.

Color scales (colorScale)

Colors the cell background by value. The text color doesn't change, and it's limited to light steps to keep contrast.

  • type: 'sequential' (default): light → dark of one hue.
  • type: 'diverging': mid (when omitted, 0 if min < 0 < max, otherwise the middle) is the neutral color, and the negative and positive sides darken in different hues.
  • colors: replace the color steps with an array of CSS colors ([light, dark] / [negative, neutral, positive]). Interpolation uses CSS color-mix(), so tokens (var(--ssg-accent)) can be mixed in as is.

Status chips (chips)

A map from value (String(value)) to tone, or a function returning a chip from the cell context (the same as the function form of cellClassName). Tones are 'neutral' | 'info' | 'good' | 'warning' | 'critical', and chips are pills with an icon + label (meaning isn't carried by color alone). In columns narrower than 80px, the padding shrinks and the icon is dropped to give the text priority (since 1.0.2).

chips: {
  Active: 'good',
  Stopped: { tone: 'warning', label: 'Paused' },
  Discontinued: { tone: 'critical', icon: false },
  Internal: { color: '#7c3aed' },  // any color
}
// function form
chips: ({ row }) => (row.stock === 0 ? 'critical' : null)

Without label, the cell's display text is used (after valueFormatter; find in cells highlighting stays too). With label, find in cells searches the label shown on screen too. Values not in the map stay plain text.

Look and scope

  • One column can have both dataBar and colorScale (the band goes over the background color). Explicit styles from cellClassName / getRowClassName win over conditional formatting.
  • It's a display-only feature and doesn't affect CSV / TSV / the clipboard / getExportData / sorting / filtering.
  • In renderCell columns, data bars and color scales are drawn in the cell background, and renderCell draws the contents (chips and showValue: false of dataBar don't apply).
  • With motion at 'on', band lengths and background colors change smoothly as values change.
  • Colors can be changed with tokens: --ssg-cf-bar / --ssg-cf-seq-min / --ssg-cf-seq-max / --ssg-cf-div-negative / --ssg-cf-div-mid / --ssg-cf-div-positive / --ssg-cf-<tone> / --ssg-cf-<tone>-bg. The dark theme has separate values with re-chosen steps (Theming).
  • State classes: .ssg-body-cell--cf-bar / .ssg-cf-bar(--negative) / .ssg-body-cell--cf-scale / .ssg-chip(--<tone>).

On this page