# Heatmap (/ui/components/react/heatmap)

A heatmap shows a magnitude at each crossing of 2 dimensions as the depth of 1 color. It is a recipe without a chart library. A heatmap is read cell by cell, so it is a `table`, which is also what a screen reader expects.

## Installation [#installation]

Nothing to install beyond Appica UI. The tint is 1 hue mixed toward transparent by the cell's value, a fraction from 0 to 1 set as a CSS variable:

```tsx
<td
  style={{ '--cell': 0.62 } as CSSProperties}
  className="bg-[color-mix(in_oklch,var(--secondary-emphasis)_calc(var(--cell)*100%),transparent)]"
/>
```

## Examples [#examples]

### Cohort [#cohort]

A retention table: 1 row per signup cohort, 1 column per month since signup. The number stays in the cell and the tint carries the pattern, so the table reads with or without color.

```tsx
import type { CSSProperties } from 'react'

const months = ['Month 0', 'Month 1', 'Month 2', 'Month 3', 'Month 4', 'Month 5']

const cohorts = [
  { label: 'Jul 2026', size: 1240, retention: [1, 0.62, 0.48, 0.41, 0.37, 0.34] },
  { label: 'Aug 2026', size: 1410, retention: [1, 0.64, 0.51, 0.44, 0.39] },
  { label: 'Sep 2026', size: 1360, retention: [1, 0.59, 0.46, 0.4] },
  { label: 'Oct 2026', size: 1680, retention: [1, 0.66, 0.53] },
  { label: 'Nov 2026', size: 1920, retention: [1, 0.68] },
  { label: 'Dec 2026', size: 1790, retention: [1] },
]

const count = new Intl.NumberFormat('en-US')
const percent = new Intl.NumberFormat('en-US', { style: 'percent' })

export default function HeatmapCohort() {
  return (
    <div className="w-full max-w-2xl overflow-x-auto">
      <table className="w-full border-separate border-spacing-0.5 text-xs tabular-nums">
        <caption className="sr-only">Share of each monthly signup cohort still active, by month since signup</caption>
        <thead>
          <tr className="text-foreground-muted *:px-2 *:pb-1 *:font-medium">
            <th scope="col" className="text-start">
              Cohort
            </th>
            <th scope="col" className="text-end">
              Users
            </th>
            {months.map((month) => (
              <th key={month} scope="col">
                {month}
              </th>
            ))}
          </tr>
        </thead>
        <tbody>
          {cohorts.map((cohort) => (
            <tr key={cohort.label}>
              <th scope="row" className="px-2 text-start font-normal whitespace-nowrap">
                {cohort.label}
              </th>
              <td className="text-foreground-muted px-2 text-end">{count.format(cohort.size)}</td>
              {months.map((month, index) => {
                const value = cohort.retention[index]
                return value == null ? (
                  <td key={month} />
                ) : (
                  <td
                    key={month}
                    style={{ '--cell': value } as CSSProperties}
                    className="text-foreground-intense rounded-3xs h-8 min-w-14 bg-[color-mix(in_oklch,var(--secondary-emphasis)_calc(var(--cell)*100%),transparent)] text-center"
                  >
                    {percent.format(value)}
                  </td>
                )
              })}
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  )
}
```

### Calendar [#calendar]

A contribution grid: 1 column per week, 1 row per weekday. The mix starts at 10%, the `-subtle` step, so an empty day still draws its cell. Each cell carries its value as visually hidden text and shows it in a [`Tooltip`](/ui/components/react/tooltip) on hover.

```tsx
import type { CSSProperties } from 'react'
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@appica/ui-react/tooltip'

const WEEKS = 26
const DAY = 86_400_000
const start = Date.UTC(2026, 5, 1)

const weeks = Array.from({ length: WEEKS }, (_, week) =>
  Array.from({ length: 7 }, (_, day) => {
    const index = week * 7 + day
    const wave = Math.sin(index / 9) + Math.sin(index / 2.3)
    const orders = Math.max(0, Math.round(14 + wave * 9 - (day > 4 ? 10 : 0) + week / 3))
    return { date: new Date(start + index * DAY), orders }
  }),
)

const max = Math.max(...weeks.flat().map((cell) => cell.orders))
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

const month = new Intl.DateTimeFormat('en-US', { month: 'short', timeZone: 'UTC' })
const fullDay = new Intl.DateTimeFormat('en-US', { weekday: 'long', month: 'long', day: 'numeric', timeZone: 'UTC' })
const count = new Intl.NumberFormat('en-US')

const tint =
  'bg-[color-mix(in_oklch,var(--secondary-emphasis)_calc(10%+var(--cell)*90%),transparent)] rounded-4xs block aspect-square'

export default function HeatmapCalendar() {
  return (
    <div className="flex w-full max-w-2xl flex-col gap-3">
      <TooltipProvider>
        <table className="w-full table-fixed border-separate border-spacing-0.5 text-xs">
          <caption className="sr-only">Orders per day over 26 weeks</caption>
          <thead>
            <tr className="text-foreground-muted">
              <td className="w-8" />
              {weeks.map((week, index) => {
                const first = week[0]!.date
                const label = first.getUTCDate() <= 7 ? month.format(first) : ''
                return (
                  <th
                    key={index}
                    scope="col"
                    className="overflow-visible pb-1 text-start font-normal whitespace-nowrap"
                  >
                    {label}
                  </th>
                )
              })}
            </tr>
          </thead>
          <tbody>
            {days.map((day, row) => (
              <tr key={day}>
                <th scope="row" className="text-foreground-muted pe-1 text-start font-normal">
                  <span className={row % 2 ? 'sr-only' : undefined}>{day}</span>
                </th>
                {weeks.map((week, index) => {
                  const cell = week[row]!
                  return (
                    <td key={index} className="p-0">
                      <Tooltip>
                        <TooltipTrigger
                          render={<span />}
                          style={{ '--cell': cell.orders / max } as CSSProperties}
                          className={tint}
                        >
                          <span className="sr-only">
                            {count.format(cell.orders)} orders on {fullDay.format(cell.date)}
                          </span>
                        </TooltipTrigger>
                        <TooltipContent>
                          {count.format(cell.orders)} orders on {fullDay.format(cell.date)}
                        </TooltipContent>
                      </Tooltip>
                    </td>
                  )
                })}
              </tr>
            ))}
          </tbody>
        </table>
      </TooltipProvider>

      <p className="text-foreground-muted flex items-center justify-end gap-1.5 text-xs">
        Less
        {[0, 0.25, 0.5, 0.75, 1].map((step) => (
          <span key={step} style={{ '--cell': step } as CSSProperties} className={`${tint} size-3`} />
        ))}
        More
      </p>
    </div>
  )
}
```

## Accessibility [#accessibility]

- Both examples are real tables with a visually hidden `caption`, column headers and row headers, so a screen reader announces each cell with its cohort and month, or its weekday.
- In the cohort table the value is the cell's text. In the calendar every cell has visually hidden text with its count and date, since a hover tooltip alone never reaches a keyboard or a screen reader.
- Depth of color is the only encoding of a calendar cell, so keep the "Less" to "More" scale beside it.
- Use 1 hue for a magnitude. For values on both sides of a midpoint, mix `--success-emphasis` and `--error-emphasis` around `--background-muted` instead.
