Components · Tables

Scrollable table

An infinite-scroll table: you serve pages of 50 rows and it appends each as the reader scrolls.

Open in playground

Preview

Revenue and customers by day

No results.

It's a bit empty here.

Usage

tsx
import { useMemo, useState } from 'react';
import {
  ChartsProvider,
  TableScrollable,
  dimension,
  measure,
  results,
} from 'em-kit';
import type { TableScrollableState } from 'em-kit';

const startDate = dimension({
  name: 'startDate',
  title: 'Start date',
  type: 'time',
  granularity: 'day',
});
const revenue = measure({
  name: 'revenue',
  title: 'Revenue',
  inputs: { currency: 'USD', decimalPlaces: 0 },
});
const customers = measure({ name: 'customers', title: 'Customers' });
// Identifies the query: when it changes, the table starts again from page 0
const filters = { search: '' };

export const Example = () => {
  const [state, setState] = useState<TableScrollableState>({
    page: 0,
    isLoadingDownloadData: false,
  });
  // Stand-in for your API. Memoised: the table appends every new array.
  const sorted = useMemo(() => sortRows(rows, state.sort), [state.sort]);
  return (
    <ChartsProvider>
      <div style={{ height: 360 }}>
        <TableScrollable
          title="Revenue and customers by day"
          dimensionsAndMeasures={[startDate, revenue, customers]}
          clickDimension={startDate}
          results={results(sorted)}
          // Every row, unpaged: the card menu’s downloads wait for it.
          allResults={results(sorted)}
          resetKey={filters}
          state={state}
          setState={setState}
        />
      </div>
    </ChartsProvider>
  );
};

const rows: Record<string, string | number>[] = [
  { startDate: '2026-01-01T00:00:00.000', revenue: 4709, customers: 15 },
  { startDate: '2026-01-02T00:00:00.000', revenue: 4722, customers: 16 },
  { startDate: '2026-01-03T00:00:00.000', revenue: 3246, customers: 10 },
  { startDate: '2026-01-04T00:00:00.000', revenue: 2890, customers: 9 },
  { startDate: '2026-01-05T00:00:00.000', revenue: 4631, customers: 14 },
  { startDate: '2026-01-06T00:00:00.000', revenue: 4762, customers: 14 },
  { startDate: '2026-01-07T00:00:00.000', revenue: 4563, customers: 14 },
  { startDate: '2026-01-08T00:00:00.000', revenue: 5046, customers: 17 },
  { startDate: '2026-01-09T00:00:00.000', revenue: 4492, customers: 14 },
  { startDate: '2026-01-10T00:00:00.000', revenue: 2989, customers: 11 },
  { startDate: '2026-01-11T00:00:00.000', revenue: 3247, customers: 10 },
  { startDate: '2026-01-12T00:00:00.000', revenue: 4395, customers: 15 },
  { startDate: '2026-01-13T00:00:00.000', revenue: 4204, customers: 14 },
  { startDate: '2026-01-14T00:00:00.000', revenue: 4949, customers: 13 },
  { startDate: '2026-01-15T00:00:00.000', revenue: 4675, customers: 15 },
];

// The rows in the order the table asks for. With more rows, serve 50 a page by
// state.page; a shorter page ends the scroll.
const sortRows = (data: typeof rows, sort: TableScrollableState['sort']) => {
  if (!sort) return data;
  const sign = sort.direction === 'desc' ? -1 : 1;
  return [...data].sort((a, b) => (a[sort.id]! > b[sort.id]! ? sign : -sign));
};
  • Sorting, or a new resetKey, starts again from page 0.

Install with npm install em-kit. Needs <ChartsProvider> once and a parent with a height: Quick start.

Examples

Auto height

The card grows to fit every row instead of scrolling: for printable reports and embedded summaries. heightMode="auto": pass all the rows at once (up to your own cap) with results.total, and the table says when rows were left out.

tsxDiffers from Usage
Show full code (66 lines)
import { useMemo, useState } from 'react';
import {
  ChartsProvider,
  TableScrollable,
  dimension,
  measure,
  results,
} from 'em-kit';
import type { TableScrollableState } from 'em-kit';

const startDate = dimension({
  name: 'startDate',
  title: 'Start date',
  type: 'time',
  granularity: 'month',
});
const revenue = measure({
  name: 'revenue',
  title: 'Revenue',
  inputs: { currency: 'USD', decimalPlaces: 0 },
});
const customers = measure({ name: 'customers', title: 'Customers' });
// Identifies the query: when it changes, the table starts again from page 0
const filters = { search: '' };

export const Example = () => {
  const [state, setState] = useState<TableScrollableState>({
    page: 0,
    isLoadingDownloadData: false,
  });
  // Stand-in for your API. Memoised: the table appends every new array.
  const sorted = useMemo(() => sortRows(rows, state.sort), [state.sort]);
  return (
    <ChartsProvider>
      <TableScrollable
        title="Revenue and customers by month"
        dimensionsAndMeasures={[startDate, revenue, customers]}
        clickDimension={startDate}
        results={results(sorted)}
        // Every row, unpaged: the card menu’s downloads wait for it.
        allResults={results(sorted)}
        resetKey={filters}
        state={state}
        setState={setState}
        heightMode="auto"
      />
    </ChartsProvider>
  );
};

const rows: Record<string, string | number>[] = [
  { startDate: '2026-01-01T00:00:00.000', revenue: 131229, customers: 406 },
  { startDate: '2026-02-01T00:00:00.000', revenue: 116685, customers: 385 },
  { startDate: '2026-03-01T00:00:00.000', revenue: 141619, customers: 469 },
  { startDate: '2026-04-01T00:00:00.000', revenue: 138438, customers: 410 },
  { startDate: '2026-05-01T00:00:00.000', revenue: 148882, customers: 428 },
  { startDate: '2026-06-01T00:00:00.000', revenue: 160625, customers: 476 },
];

// The rows in the order the table asks for. With more rows, serve 50 a page by
// state.page; a shorter page ends the scroll.
const sortRows = (data: typeof rows, sort: TableScrollableState['sort']) => {
  if (!sort) return data;
  const sign = sort.direction === 'desc' ? -1 : 1;
  return [...data].sort((a, b) => (a[sort.id]! > b[sort.id]! ? sign : -sign));
};
const startDate = dimension({
  name: 'startDate',
  title: 'Start date',
  type: 'time',
  granularity: 'month',
});

<TableScrollable
  title="Revenue and customers by month"
  dimensionsAndMeasures={[startDate, revenue, customers]}
  clickDimension={startDate}
  results={results(sorted)}
  // Every row, unpaged: the card menu’s downloads wait for it.
  allResults={results(sorted)}
  resetKey={filters}
  state={state}
  setState={setState}
  heightMode="auto"
/>

Props

PropTypeDefaultDescription
dimensionsAndMeasuresrequiredDimensionOrMeasure[]The table columns, in order. Dimensions show as text, measures as formatted numbers.
resultsrequiredDataResponseYour query’s state: { isLoading, data, error }, where data is your rows. results(rows) builds it from an array.
allResultsDataResponseEvery row, unpaged, for the downloads in the card menu. A download waits until this is set and not loading, so without it the menu spins for good. state.isLoadingDownloadData turns true when a reader asks, if you would rather fetch only then.
clickDimensionDimensionThe dimension whose value onRowClicked receives. Rows are only clickable when it is set.
displayNullAsstringText shown where a value is null, for example "–".
showIndexbooleantrueNumber the rows.
stateTableScrollableStatePaging and sorting, controlled: keep it in useState and build your query from it. Without state and setState the table cannot page or sort; downloads need only allResults.TableScrollableState = { page: number; pageSize?: number | undefined; sort?: { id: string; direction: "asc" | "desc"; } | undefined; isLoadingDownloadData: boolean; heightMode?: "fixed" | "auto" | undefined; }
onRowClicked(rowDimensionValue: TableScrollableOnRowClickArg) => voidCalled with the clicked row’s clickDimension value.TableScrollableOnRowClickArg = { dimensionValue: string | null | undefined; dimensionTimeRange: TimeRange; }
setState(value: SetStateAction<TableScrollableState>) => voidA function that stores the new state, such as the setter from useState. Called when the reader pages, sorts or downloads, and when the page size is first known or changes.
componentNamestringA name for this component, included in the interaction events it dispatches on the DOM, for your analytics.
trackingIdstringAn id included in the same events, to tell two instances apart.
titlestringThe card’s title. A "key|fallback" string is looked up in your translations.
descriptionstringSmaller text under the title. Translates like title.
tooltipstringText for an info icon beside the title.
hideMenubooleanfalseHide the card’s header: the title, description and menu.
menuOptions("csv" | "xlsx" | "png" | "maximize")[]["csv", "xlsx"]Which entries the card menu offers. Pass [] for none.
resetKeyunknownAny value that identifies the current query, such as your filters. When it changes (compared by value), the table goes back to the top and starts again from page 0.
heightMode"fixed" | "auto""fixed""fixed" scrolls inside the card and loads pages as the reader scrolls. "auto" grows the card to fit every row you pass.

A blank default means the prop is unset unless you pass it.

TypeDefinition
TimeRange{ from: Date | undefined; to: Date | undefined; relativeTimeString?: string; } | undefinedWhat a click’s TimeRange holds: Events.

Field options

Set in a field’s inputs, beside the common options:

OptionTypeSet onDescription
widthnumberdimensionsAndMeasuresThe column’s width in pixels.
align'left' | 'center' | 'right'dimensionsAndMeasuresAlign the column’s cells left, centre or right.
displayFormat'Markdown' | 'Json'dimensionsAndMeasuresRender the cell’s text as Markdown, or as indented JSON.
tableCellStyle'Bold' | 'Italic' | 'Positive vs Negative'dimensionsAndMeasuresStyle the column’s cells. Positive vs Negative colours values above zero as good and the rest as bad.

Keyboard

KeyWhereWhat it does
Tab, Shift + TabTableMoves between the column headers and each cell’s copy button, scrolling the table to keep the focus in view.
Enter or SpaceColumn headerSorts ascending, then descending, then clears the sort.
Enter or SpaceCopy button in a cellCopies the cell’s value. The button shows on hover and while focus is in its cell.

The card menu’s keys, and what is still missing, are in Accessibility.