# States
Package: @mantine/data-grid
Import: import { States } from '@mantine/data-grid';
Description: DataGrid empty state and loading variants

## Empty state

Use the `emptyState` prop of `DataGrid` to render content when there are no rows to display.
The content is rendered in a full-width cell without padding, use the `emptyState` Styles API
selector to style it. The empty state is not displayed while `loading` is set.

`emptyState` can be a React node or a function that receives the following object:

```ts
interface DataGridEmptyStatePayload<TData extends RowData = any> {
  /** Total rows before filtering. Server-side grids use the rowCount option. */
  rowCount: number;

  /** Number of rows left after filtering. */
  filteredRowCount: number;

  /** Whether a column or global filter is active. */
  isFiltered: boolean;

  /** Table instance, for example to clear filters. */
  table: DataGridTable<TData>;
}
```

```tsx
// Demo.tsx
import { useState } from 'react';
import { Button, Stack, Switch, Text } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

const noEmployees: typeof employees = [];

function Demo() {
  const [noData, setNoData] = useState(false);

  const store = useDataGrid({
    data: noData ? noEmployees : employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary', meta: { align: 'end' } },
    ],
    withGlobalFilter: true,
  });

  return (
    <>
      <Switch
        mb="md"
        label="Simulate an empty data source"
        checked={noData}
        onChange={(event) => setNoData(event.currentTarget.checked)}
      />
      <DataGrid
        store={store}
        emptyState={({ isFiltered, rowCount }) => (
          <Stack align="center" gap="xs" py="xl">
            <Text fw={500}>{isFiltered ? 'No employees match your search' : 'No employees yet'}</Text>
            <Text size="sm" c="dimmed">
              {isFiltered
                ? `Nothing matched in ${rowCount} employees`
                : 'Add an employee to get started'}
            </Text>
            {isFiltered ? (
              <Button size="xs" variant="default" onClick={() => store.setGlobalFilter('')}>
                Clear search
              </Button>
            ) : (
              <Button size="xs">Add employee</Button>
            )}
          </Stack>
        )}
      >
        <DataGrid.Toolbar />
      </DataGrid>
    </>
  );
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## Loading

Set the `loading` prop on `DataGrid` to display the loading state:

* If the grid has no rows yet, skeleton rows are displayed
* If the grid has rows, the `loadingVariant` prop defines what is displayed over them

```tsx
// Demo.tsx
import { useState } from 'react';
import { Group, SegmentedControl, Switch } from '@mantine/core';
import { DataGrid, DataGridProps, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const [loading, setLoading] = useState(true);
  const [variant, setVariant] = useState<DataGridProps['loadingVariant']>('overlay');

  const store = useDataGrid({
    data: employees.slice(0, 6),
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary', meta: { align: 'end' } },
    ],
  });

  return (
    <>
      <Group mb="md">
        <SegmentedControl
          size="xs"
          data={['overlay', 'bar', 'skeleton']}
          value={variant}
          onChange={(value) => setVariant(value as DataGridProps['loadingVariant'])}
        />
        <Switch
          label="Loading"
          checked={loading}
          onChange={(event) => setLoading(event.currentTarget.checked)}
        />
      </Group>
      <DataGrid store={store} loading={loading} loadingVariant={variant} skeletonRowCount={6} />
    </>
  );
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## Loading variants

The number of skeleton rows is the page size when pagination is enabled and `skeletonRowCount`
(`10` by default) otherwise. Use `loadingOverlayProps` to pass props to `LoadingOverlay`
of the `overlay` variant:

```tsx
<DataGrid
  store={store}
  loading={isFetching}
  loadingVariant="overlay"
  loadingOverlayProps={{ loaderProps: { type: 'dots' }, overlayProps: { blur: 0 } }}
/>

<DataGrid store={store} loading={isFetching} loadingVariant="skeleton" skeletonRowCount={5} />
```

In list view there are no skeleton rows, the `skeleton` variant displays the overlay instead.

## Server-side example

The [full server-side example](https://alpha.mantine.dev/llms/data-grid-server-side.md#full-example) uses `loading` with
`loadingVariant="bar"`: skeleton rows are displayed on the first load and the bar is displayed
while a new page, sort or filter is fetched.
