# Keyboard Navigation for the Column Header

> Documentation for navigating the column header with the keyboard in your React Infinite Table DataGrid component

Canonical page: https://infinite-table.com/docs/learn/keyboard-navigation/navigating-the-header

The column header can be used from the keyboard, just like the [table cells](./navigating-cells) - you can move between the column headers and the column group headers, sort, open the column menu and get to the column filters without using the mouse.

Press `Tab` from the element before the grid, and focus goes to a column header - the one in the first column, or the one that last had focus, when you come back to the header. Clicking a column header also gives it focus, so you can continue with the keyboard from there.

Click the button above the grid and press `Tab` to focus the header. Then use the arrow keys to move around, `Enter` to sort, `Alt+ArrowDown` to open the column menu and `Tab` to go to the column filters.

**Example: Navigating the column header**

```tsx
import * as React from 'react';
import { InfiniteTable, DataSource } from '@infinite-table/infinite-react';
import type {
  DataSourceData,
  InfiniteTablePropColumns,
  InfiniteTablePropColumnGroups,
} from '@infinite-table/infinite-react';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';
  salary: number;
  age: number;
};

const dataSource: DataSourceData<Developer> = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + `/developers1k-sql?`)
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const columnGroups: InfiniteTablePropColumnGroups = {
  person: { header: 'Person' },
  location: { header: 'Location' },
  work: { header: 'Work' },
  skills: { header: 'Skills', columnGroup: 'work' },
};

const columns: InfiniteTablePropColumns<Developer> = {
  id: { field: 'id', type: 'number', defaultWidth: 80 },
  firstName: { field: 'firstName', columnGroup: 'person' },
  lastName: { field: 'lastName', columnGroup: 'person' },
  country: { field: 'country', columnGroup: 'location' },
  city: { field: 'city', columnGroup: 'location' },
  stack: { field: 'stack', columnGroup: 'skills' },
  preferredLanguage: { field: 'preferredLanguage', columnGroup: 'skills' },
  canDesign: { field: 'canDesign', columnGroup: 'work' },
  age: { field: 'age', type: 'number' },
  salary: { field: 'salary', type: 'number' },
};

const pinning = { id: 'start' } as const;

const toolbarStyle: React.CSSProperties = {
  color: 'var(--infinite-cell-color)',
  padding: 10,
};

export default function KeyboardNavigationForTheHeader() {
  const [showFilters, setShowFilters] = React.useState(true);

  return (
    <>
      <div style={toolbarStyle}>
        <label>
          <input
            type="checkbox"
            checked={showFilters}
            onChange={(event) => setShowFilters(event.target.checked)}
          />
          Show column filters
        </label>{' '}
        {/* the element right before the grid - Tab goes from it to the header */}
        <button>Click here, then press Tab</button>
      </div>
      <DataSource<Developer>
        primaryKey="id"
        data={dataSource}
        defaultFilterValue={[]}
      >
        <InfiniteTable<Developer>
          debugId="navigating-the-header-example"
          columns={columns}
          columnGroups={columnGroups}
          defaultColumnPinning={pinning}
          showColumnFilters={showFilters}
        />
      </DataSource>
    </>
  );
}
```

- Use `ArrowLeft` and `ArrowRight` to go to the previous and next header on the same header row.
- Use `Home` and `End` to go to the first and the last header on the same header row.
- Use `ArrowUp` and `ArrowDown` to move between the rows of [column groups](../columns/column-grouping) and the column headers.

---

- Use `Enter` to toggle the sort of the focused column, same as clicking it. With `Ctrl` / `Cmd`, the column is added to the current sort, same as `Ctrl` / `Cmd` + click (see [`multiSortBehavior`](https://infinite-table.com/docs/reference/infinite-table-props.md#multiSortBehavior)). `Enter` does nothing on a column group header.
- Use `Alt+ArrowDown` to open the [column menu](../columns/column-menus) of the focused column. When the menu closes (`Escape`, or after picking an item), focus goes back to the column header.

---

- Use `ArrowDown` on a column header to go to its filter editor (when [column filters](../filtering) are shown), and from there to the body of the table.
- Use `ArrowUp` on the first row of the body to go back to the header: to the filter editor of the column, or to the column header when there are no column filters.

When `ArrowDown` goes from the header to the body, the cell in the same column on the first visible row becomes the active cell - or, with [keyboardNavigation="row"](https://infinite-table.com/docs/reference/infinite-table-props.md#keyboardNavigation), the first visible row becomes the active row.

The keys in the header work whatever the value of [`keyboardNavigation`](https://infinite-table.com/docs/reference/infinite-table-props.md#keyboardNavigation) - but with `keyboardNavigation={false}`, there is no active cell or row in the body, so `ArrowUp` from the body doesn't go to the header.

## Tab order

`Tab` and `Shift+Tab` go through the header in the order the columns are shown (pinned columns included): each column header, followed by its filter editor, when column filters are shown.

- `Tab` from the last column (or from its filter editor) goes to the body of the table.
- `Shift+Tab` from the body goes to the last column of the header - to its filter editor, when it has one.
- `Shift+Tab` from the first column header goes to the element before the grid.

Column group headers are not `Tab` stops - use `ArrowUp` to reach them.

## Keys in filter editors

In a filter editor, the keys belong to the editor - `ArrowLeft` and `ArrowRight` move the caret in a text input, for example. Only a few keys take you out of the editor:

- `ArrowUp` goes to the column header.
- `ArrowDown` goes to the body of the table.
- `Tab` goes to the next column header, and `Shift+Tab` back to the column header of the filter. In a filter editor with several inputs (for example a range), `Tab` and `Shift+Tab` first move between its own inputs.

If a [custom filter editor](../filtering/providing-a-custom-filter-editor) needs `ArrowUp` or `ArrowDown` itself (for example to change a value or to open a list), call `event.preventDefault()` in its `keydown` handler, and the grid leaves the key to the editor.

## Reordering columns

A focused column header keeps focus when you drag it to a new position, so you can continue with the keyboard from its new place.

## Preventing header navigation

Return `{ preventNavigation: true }` from [`onKeyDown`](https://infinite-table.com/docs/reference/infinite-table-props.md#onKeyDown) to stop the grid from handling a key, in the header as well as in the body.

## Theming

A column header (or column group header) focused from the keyboard gets the same background as the active cell in the body, so the CSS variables in the [cell navigation theming section](./navigating-cells#theming) change it too - for example `--infinite-active-cell-background`, or the `--infinite-active-cell-border-color--r` / `--g` / `--b` variables.

The focused header also gets a className, which you can use to style it in your own way:

- `InfiniteHeaderCell--focused` for a column header
- `InfiniteHeaderGroup--focused` for a column group header

The background is shown only for keyboard focus (it uses `:focus-visible`), while the classNames are set for any focus, including after a click on the header.
