# AG Grid Themes

Canonical page: https://www.adaptabletools.com/docs/handbook-theming-aggrid

- AdapTable works with AG Grid **Theming API** and **legacy** theming — no extra AdapTable config for built-in themes
- **Quartz**, **Alpine**, **Balham**, and **Material** switch **light/dark modes** with AdapTable's `light` / `dark` / `os` themes
- **Custom grid branding** pairs custom theme modes with AdapTable via [`SystemThemeOptions.AgThemeMode`](https://www.adaptabletools.com/docs/reference/systemthemeoptions.md#agthememode) — full walkthrough demo on [Theming AdapTable UI](https://www.adaptabletools.com/docs/handbook-theming/index.md#co-ordinated-themes)

## Topics on this Page

| Topic                                           | Summary                             |
| ----------------------------------------------- | ----------------------------------- |
| [Theming API](#theming-api)                     | AG Grid 33+ `theme` option          |
| [Built-in themes](#built-in-ag-grid-themes)     | Quartz, Alpine, Balham, Material    |
| [Custom AG Grid themes](#custom-ag-grid-themes) | `withParams` + `AgThemeMode`        |
| [CSS cascade layers](#css-cascade-layers)       | `themeCssLayer` and the layer order |

## Theming API

AdapTable supports AG Grid's [Theming API](https://www.ag-grid.com/javascript-data-grid/theming/) (AG Grid 33+):

```ts
import {GridOptions, themeQuartz} from 'ag-grid-community';

const options: GridOptions = {
  theme: themeQuartz,
};
```

Legacy theming (`theme: 'legacy'`) remains supported — see [AG Grid legacy theming](https://www.ag-grid.com/javascript-data-grid/theming-v32/).

## Built-in AG Grid themes

AdapTable works with [built-in AG Grid themes](https://www.ag-grid.com/javascript-data-grid/themes/). When users change AdapTable's theme, AdapTable switches the grid's **light/dark mode** for those themes automatically.

**Example: AG Grid Themes**

AG Grid built-in themes in AdapTable

- Built-in AG Grid themes with AdapTable
- Switching AdapTable **light** / **dark** updates the grid theme mode

- Use the AdapTable [Custom Toolbar](https://www.adaptabletools.com/docs/ui-dashboard-custom-toolbars/index.md) to switch **Dark** and **Light**
- Use the AG Grid toolbar to switch **Quartz**, **Alpine**, **Balham**, and **Material**

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {
  AdaptableApi,
  AdaptableOptions,
  ButtonStyle,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';
import {
  Theme,
  themeAlpine,
  themeBalham,
  themeMaterial,
  themeQuartz,
} from 'ag-grid-enterprise';

let currentAgThemeName = 'Quartz';

const setAgGridTheme = (
  adaptableApi: AdaptableApi,
  agTheme: Theme,
  themeName: string
) => {
  adaptableApi.agGridApi.setGridOption('theme', agTheme);
  currentAgThemeName = themeName;
};

const getButtonStyle = (themeName: string): ButtonStyle => ({
  variant: currentAgThemeName === themeName ? 'raised' : 'outlined',
  tone: currentAgThemeName === themeName ? 'success' : 'neutral',
});

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Adaptable Demo',

  dashboardOptions: {
    customToolbars: [
      {
        name: 'Adaptable Theme',
        title: 'AdapTable',
        toolbarButtons: [
          {
            label: 'Light',
            onClick: (_, context) =>
              context.adaptableApi.themeApi.loadLightTheme(),
            buttonStyle: (_, context) => {
              const currentThemeName =
                context.adaptableApi.themeApi.getCurrentTheme();
              return {
                variant: currentThemeName === 'light' ? 'raised' : 'outlined',
                tone: currentThemeName === 'light' ? 'info' : 'neutral',
              };
            },
          },
          {
            label: 'Dark',
            onClick: (_, context) =>
              context.adaptableApi.themeApi.loadDarkTheme(),
            buttonStyle: (_, context) => {
              const currentThemeName =
                context.adaptableApi.themeApi.getCurrentTheme();
              return {
                variant: currentThemeName === 'dark' ? 'raised' : 'outlined',
                tone: currentThemeName === 'dark' ? 'info' : 'neutral',
              };
            },
          },
        ],
      },
      {
        name: 'AG Grid Theme',
        title: 'AG Grid',
        toolbarButtons: [
          {
            label: 'Quartz',
            onClick: (_, context) =>
              setAgGridTheme(context.adaptableApi, themeQuartz, 'Quartz'),
            buttonStyle: (_, context) => getButtonStyle('Quartz'),
          },
          {
            label: 'Alpine',
            onClick: (_, context) => {
              setAgGridTheme(context.adaptableApi, themeAlpine, 'Alpine');
            },
            buttonStyle: (_, context) => getButtonStyle('Alpine'),
          },
          {
            label: 'Balham',
            onClick: (_, context) => {
              setAgGridTheme(context.adaptableApi, themeBalham, 'Balham');
            },
            buttonStyle: (_, context) => getButtonStyle('Balham'),
          },
          {
            label: 'Material',
            onClick: (_, context) => {
              setAgGridTheme(context.adaptableApi, themeMaterial, 'Material');
            },
            buttonStyle: (_, context) => getButtonStyle('Material'),
          },
        ],
      },
    ],
  },
  initialState: {
    Theme: {
      CurrentTheme: 'dark',
    },
    Dashboard: {
      Tabs: [
        {
          Name: 'Themes',
          Toolbars: ['Adaptable Theme', 'AG Grid Theme'],
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'open_issues_count',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'description',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
        },
      ],
    },
  },
};
```

**`src/gridOptions.ts`**

```ts
import {GridOptions, themeQuartz} from 'ag-grid-enterprise';

import {rowData, WebFramework} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions<WebFramework> = {
  theme: themeQuartz,
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  columnMenu: 'new',
  cellSelection: true,
};
```

## Custom AG Grid themes

AdapTable supports [custom AG Grid themes](https://www.ag-grid.com/javascript-data-grid/themes/#customising-built-in-themes).

Set [`SystemThemeOptions.AgThemeMode`](https://www.adaptabletools.com/docs/reference/systemthemeoptions.md#agthememode) on each `SystemThemes` entry so AdapTable applies the right grid mode when users switch theme.

Pair AdapTable CSS variable overrides (from [DevTools — Theme Builder](https://www.adaptabletools.com/docs/dev-guide-support-devtools-theme-builder/index.md) or hand-written CSS) with custom `themeQuartz.withParams(...)` modes — the **Co-ordinated Themes** demo on the [theming handbook](https://www.adaptabletools.com/docs/handbook-theming/index.md#co-ordinated-themes) shows the full light/dark brand.

**Example: AG Grid Custom Theme**

Custom `themeQuartz` modes paired with AdapTable `dark`

- Registers `light-red` and `dark-red` modes on `themeQuartz`
- Pairs them with AdapTable system themes via `AgThemeMode`

- Switch **Light** and **Dark** in the toolbar — the custom grid theme follows

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Adaptable Demo',

  initialState: {
    Theme: {
      CurrentTheme: 'dark',
      SystemThemes: [
        {
          Name: 'light',
          AgThemeMode: 'light-red',
        },
        {
          Name: 'dark',
          AgThemeMode: 'dark-red',
        },
      ],
    },
    Dashboard: {
      Tabs: [
        {
          Name: 'Themes',
          Toolbars: ['Theme'],
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'open_issues_count',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'description',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
        },
      ],
    },
  },
};
```

**`src/gridOptions.ts`**

```ts
import {GridOptions, themeQuartz} from 'ag-grid-enterprise';

import {rowData, WebFramework} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions<WebFramework> = {
  theme: themeQuartz
    .withParams(
      {
        backgroundColor: '#FFF0F0',
        foregroundColor: '#4A1010',
        accentColor: '#C62828',
        headerBackgroundColor: '#FFCDD2',
        browserColorScheme: 'light',
      },
      'light-red'
    )
    .withParams(
      {
        backgroundColor: '#4A1010',
        foregroundColor: '#FFEBEE',
        accentColor: '#EF5350',
        headerBackgroundColor: '#6D1B1B',
        oddRowBackgroundColor: '#5C1515',
        browserColorScheme: 'dark',
      },
      'dark-red'
    ),
  defaultColDef: {
    resizable: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  columnMenu: 'new',
  cellSelection: true,
};
```

[AG Grid theme modes](https://www.ag-grid.com/javascript-data-grid/theming-colors/#theme-modes)

## CSS cascade layers

AdapTable's CSS is in these cascade layers: `adaptable.reset`, `adaptable.components`, `adaptable.utilities` and `adaptable.theming`.

AG Grid's theme CSS is unlayered by default, so it takes precedence over all of AdapTable's layers.

AG Grid's `themeCssLayer` GridOptions property puts the theme CSS in a named layer, and AG Grid inserts it at the top of the document `<head>`.

If AG Grid's layer is declared before AdapTable's layers, AdapTable's reset styles take precedence over AG Grid's theme (e.g. AG Grid buttons lose their padding and the Side Bar tabs collapse)

When using `themeCssLayer`, also set `themeStyleContainer` to `document.body`.

This makes AG Grid's theme CSS come after AdapTable's CSS, so AG Grid's layer is declared after AdapTable's layers:

```ts
const gridOptions: GridOptions = {
  theme: themeQuartz,
  themeCssLayer: 'ag-grid',
  themeStyleContainer: document.body,
};
```

If the application's CSS declares its own layer order with an `@layer` statement, list AG Grid's layer after AdapTable's layers, e.g. `@layer adaptable.reset, adaptable.components, adaptable.utilities, adaptable.theming, ag-grid;`

[CSS cascade layers (MDN)](https://developer.mozilla.org/en-US/docs/Web/CSS/@layer)

### FAQ

**Theming API or legacy?**
Prefer the Theming API on current AG Grid versions; AdapTable supports both.

**Where is the full AdapTable + grid brand demo?**
[Co-ordinated Themes](https://www.adaptabletools.com/docs/handbook-theming/index.md#co-ordinated-themes) on the theming handbook — this page focuses on grid configuration.
