# Tool Panel Buttons

Canonical page: https://www.adaptabletools.com/docs/ui-tool-panel-buttons

- The area at the top of the AdapTable ToolPanel Component can display a group of Buttons
- Similar to ToolPanels, these can be of 2 types:
  - Module Buttons - open the relevant Module Page in the Settings Panel
  - Custom Buttons - provided by users at design-time and containing bespoke functionality

ToolPanel Buttons are buttons which are placed at the top of the AdapTable ToolPanel Component.

They can be either:

- Module Buttons - shipped with AdapTable and open the relevant Module Page in the Settings Panel
- Custom Buttons - supplied by users at design-time and can display any content which is required

By default, AdapTable displays **no** ToolPanel Buttons at start-up, but this can be changed at design-time (through Initial State) or at runtime (in the ToolPanel section of the Settings Panel).

**Example: ToolPanel Module Buttons**

Module Buttons in AdapTable ToolPanel

- This example contains 3 Module Buttons: `Layout`, `CellSummary`, `Dashboard`

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts
  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 {
  AdaptableButton,
  AdaptableOptions,
  ToolPanelButtonContext,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Module ToolPanel Buttons',

  initialState: {
    ToolPanel: {
      ModuleButtons: ['Layout', 'CellSummary', 'Dashboard'],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          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',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  adaptableApi.toolPanelApi.openAdapTableToolPanel();
};
```

## Configuring ToolPanel Buttons

ToolPanel Buttons are defined at design-time in 2 different ways:

- Module Buttons are supplied in Initial Adaptable State
- Custom Buttons are provided through ToolPanel Options

### `customButtons`

Custom Buttons to display in the Adaptable Tool Panel Component

  [`AdaptableButton[<ToolPanelButtonContext>]`](https://www.adaptabletools.com/docs/reference/adaptablebutton.md)

These are custom buttons to display in the Adaptable Tool Panel Component.

Each button is of type [Adaptable Button](https://www.adaptabletools.com/docs/ui-tutorial-displaying-adaptable-buttons/index.md) which is used in multiple places in AdapTable.

As with all Adaptable Buttons, the button can be styled to user requirements with many properties and functions available to configure visibility, styling, onClick and other features.

See the [Adaptable Buttons](https://www.adaptabletools.com/docs/ui-tutorial-displaying-adaptable-buttons/index.md) documentation for full details on configuring buttons

All these functions receive as context a [`ToolPanelButtonContext`](https://www.adaptabletools.com/docs/reference/toolpanelbuttoncontext.md) object which contains one `toolPanelState` property:

| Property | Type | Description |
| --- | --- | --- |
| [toolPanelState](https://www.adaptabletools.com/docs/reference/toolpanelbuttoncontext.md#toolpanelstate) | [`ToolPanelState`](https://www.adaptabletools.com/docs/reference/toolpanelstate.md) | Current ToolPanel State |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/toolpanelbuttoncontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

```ts {3}
const adaptableOptions: AdaptableOptions = {
  toolPanelOptions: {
    customButtons: [
      {
        label: 'Query Editor',
        icon: {
          src: 'https://img.icons8.com/zoom-in.png',
        },
        onClick: (
          button: AdaptableButton<ToolPanelButtonContext>,
          context: ToolPanelButtonContext
        ) => {
          // Perform an action here
        },
      },
    ],
  },
};
```

### Providing ToolPanel Buttons

Use the `ModuleButtons` property of [Tool Panel Initial State](https://www.adaptabletools.com/docs/technical-reference-tool-panel/index.md) to specify which Modules Buttons should be displayed.

```ts {4}
// Show the Custom Sort and Export Module Buttons
const initialState: InitialState = {
  ToolPanel: {
    ModuleButtons: ['CustomSort', 'Export'],
  },
};
```

Additionally supply your own bespoke Buttons via the `customButtons` property of [Tool Panel Options](https://www.adaptabletools.com/docs/technical-reference-tool-panel/index.md).

Each button is of type [Adaptable Button](https://www.adaptabletools.com/docs/ui-tutorial-displaying-adaptable-buttons/index.md) which is used in multiple places in AdapTable.

You can set how and whether the Button appears and what happens when it is clicked.

```ts {3}
const adaptableOptions: AdaptableOptions = {
  toolPanelOptions: {
    customButtons: [
      {
        label: 'Query Editor',
        icon: {
          src: 'https://img.icons8.com/zoom-in.png',
        },
        onClick: (
          button: AdaptableButton<ToolPanelButtonContext>,
          context: ToolPanelButtonContext
        ) => {
          // Perform an action here
        },
      },
    ],
  },
};
```

### Configuring Custom Buttons in the AdapTable ToolPanel Component

As with all Adaptable Buttons, the button can be styled to user requirements with many properties and functions available to configure visibility, styling, onClick and other features.

See the [Adaptable Buttons](https://www.adaptabletools.com/docs/ui-tutorial-displaying-adaptable-buttons/index.md) documentation for full details on configuring buttons

All these functions receive as context a [`ToolPanelButtonContext`](https://www.adaptabletools.com/docs/reference/toolpanelbuttoncontext.md) object:

| Property | Type | Description |
| --- | --- | --- |
| [toolPanelState](https://www.adaptabletools.com/docs/reference/toolpanelbuttoncontext.md#toolpanelstate) | [`ToolPanelState`](https://www.adaptabletools.com/docs/reference/toolpanelstate.md) | Current ToolPanel State |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/toolpanelbuttoncontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

**Example: ToolPanel Custom Buttons**

Custom Buttons in AdapTable ToolPanel

- This Demo shows 3 Custom Buttons in the Tool Panel:
  - one which links to Super Mario website
  - one which opens the Layout Editor
  - one which fires an Alert

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts
  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 {
  AdaptableButton,
  AdaptableOptions,
  ToolPanelButtonContext,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Custom ToolPanel Buttons',
  toolPanelOptions: {
    customButtons: [
      {
        icon: {
          style: {height: 30, width: 30},
          src: 'https://img.icons8.com/glyph-neue/64/000000/super-mario.png',
        },
        onClick: (
          button: AdaptableButton<ToolPanelButtonContext>,
          context: ToolPanelButtonContext
        ) => {
          Object.assign(document.createElement('a'), {
            target: '_blank',
            href: 'https://supermarioemulator.com/',
          }).click();
        },
      },
      {
        label: 'Layout Editor',
        tooltip: (
          button: AdaptableButton<ToolPanelButtonContext>,
          context: ToolPanelButtonContext
        ) => {
          return 'Edit Current Layout';
        },
        icon: {
          // style: { height: 30, width: 30 },
          src: 'https://img.icons8.com/glyph-neue/64/000000/zoom-in.png',
        },
        buttonStyle: {
          variant: 'outlined',
          // tone: 'accent',
        },
        onClick: (
          button: AdaptableButton<ToolPanelButtonContext>,
          context: ToolPanelButtonContext
        ) => {
          context.adaptableApi.layoutApi.showLayoutEditor();
        },
      },
      {
        label: 'Alert',
        tooltip: 'Show Alert',
        buttonStyle: {
          variant: 'raised',
          tone: 'accent',
        },
        onClick: (
          button: AdaptableButton<ToolPanelButtonContext>,
          context: ToolPanelButtonContext
        ) => {
          context.adaptableApi.alertApi.showAlertInfo(
            'CustomToolPanelButton',
            'Styled button & icon'
          );
        },
      },
    ],
  },
  initialState: {
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          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',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  adaptableApi.toolPanelApi.openAdapTableToolPanel();
};
```
