Conditional formatting

Conditional formatting colors a cell by its value. SvGrid ships it as a declarative engine prop, conditionalFormats, so you describe the rules once and the grid paints every cell - no per-cell cell snippet required.

It goes beyond the cellClass(ctx) callback (which only toggles static CSS classes): color scales and data bars need a value computed against the column's min/max range, which the engine does for you.

<script lang="ts">
  import { SvGrid, type ColumnDef, type ConditionalFormat } from 'sv-grid-community'

  type Row = { rep: string; revenue: number; score: number }

  const conditionalFormats: ConditionalFormat<Row>[] = [
    { type: 'dataBar', columns: ['revenue'], color: '#3b82f6' },
    { type: 'colorScale', columns: ['score'], min: '#fca5a5', mid: '#fde68a', max: '#86efac' },
  ]
</script>

<SvGrid {data} {columns} {conditionalFormats} />

Format kinds

colorScale - gradient fill

A 2-stop (min/max) or 3-stop (min/mid/max) gradient mapped across the column's value range. Fix the scale with minValue/maxValue to make rows comparable.

{ type: 'colorScale', columns: ['score'], min: '#fca5a5', mid: '#fde68a', max: '#86efac', minValue: 0, maxValue: 100 }

dataBar - in-cell bar

An in-cell horizontal bar proportional to the value. Diverging data (can go negative) gets negativeColor. showValue: false hides the text and shows the bar alone.

{ type: 'dataBar', columns: ['revenue'], color: '#3b82f6', negativeColor: '#ef4444' }

iconSet - threshold icons

An icon chosen by ascending thresholds (n thresholds => n+1 buckets). Built-in sets: 'arrows', 'traffic', 'triangles'. iconOnly: true hides the number.

// growth < 0 -> down, 0..10 -> flat, >= 10 -> up
{ type: 'iconSet', columns: ['growth'], set: 'arrows', thresholds: [0, 10] }

rule - style on a predicate

Apply background / color / fontWeight when when(ctx) returns true. The predicate receives the typed row, so you can key off other fields.

{ type: 'rule', columns: ['churn'], when: ({ value }) => Number(value) >= 20,
  background: '#fee2e2', color: '#991b1b', fontWeight: 700 }

Scoping and precedence

Notes

See the live Conditional formatting demo.