# Providing Adaptable Context

Canonical page: https://www.adaptabletools.com/docs/dev-guide-tutorial-providing-adaptable-context

- The `adaptableContext` property in [Adaptable Options](https://www.adaptabletools.com/docs/technical-reference-adaptable-options/index.md) allows individual context to be passed into AdapTable
- This can then be leveraged at runtime as required

The `adaptableContext` property allows developers to provide application-specific "context".

This object is very similar in intention to the [AG Grid Context object](https://www.ag-grid.com/javascript-data-grid/context/)

The property is set by developers in the root of [Adaptable Options](https://www.adaptabletools.com/docs/technical-reference-adaptable-options/index.md).

It is subsequently passed by AdapTable into the [`BaseContext`](https://www.adaptabletools.com/docs/reference/basecontext.md) object.

- `BaseContext` is the base object supplied to most functions / callbacks in [Adaptable Options](https://www.adaptabletools.com/docs/technical-reference-adaptable-options/index.md)
- It is also in the `xxxEventInfo` object included in all [Adaptable Events](https://www.adaptabletools.com/docs/technical-reference-adaptable-events/index.md)

This allows developers to provide data or services which they can subsequently leverage elsewhere in AdapTable, e.g. in [Display Formats](https://www.adaptabletools.com/docs/handbook-column-formatting-display-format-custom/index.md).

### `adaptableContext`

Custom Context for the Application

Set this property to provide custom context in your application.

```ts {3}
// Provide a Date Time Service to access in other callbacks
const adaptableOptions: AdaptableOptions = {
  adaptableContext: {
    dateTimeService: dateTimeService
  }
} 
```

## Demo

The demo below shows the typical pattern for using `adaptableContext`:

1. Build an application object — here a `currentUser` plus a tiny `userService` that answers permission questions
2. Put that object on `adaptableContext` once, alongside the rest of `AdaptableOptions`
3. Retrieve it from any AdapTable callback that needs it

**Example: Providing Adaptable Context**

Sharing a `currentUser` object and a `userService` helper via Adaptable Context

- The `userService.ts` file exports a `currentUser` object and a tiny `userService` with a `canPerform()` permission helper — both are plain TypeScript, with no framework dependencies
- `adaptableOptions.ts` then exposes them through `adaptableContext` and reads them back in **three** different places:
  - **Config-time** — the `userName` option is set from `currentUser.name` and is displayed at the top-right of AdapTable
  - **Runtime** — the `Edit` action button's `hidden` callback asks the `userService` whether the current user is allowed to edit
  - **Runtime** — the `Delete` action button's `hidden` callback does the same for deleting
- The default user has the `Manager` role, so `Edit` is visible but `Delete` is hidden

- Edit `userService.ts` and change `currentUser.role` to `'Trader'` (both buttons disappear) or `'Admin'` (both buttons appear)
- Add another consumer — for example, the handler of a custom Expression Function in `expressionOptions.customStandardFunctions` — and access `currentUser` / `userService` exactly the same way via `context.adaptableContext`

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  [template]
  rowData.ts           [template]
  styles.css           [template]
  userService.ts
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/userService.ts`**

```ts
/**
 * A tiny "user session" module.
 *
 * It exports:
 *   - `currentUser` : plain data describing the active user
 *   - `userService` : a small helper that answers permission questions
 *
 * Both are pure TypeScript — no React, Vue, Angular or framework imports —
 * so this same file is used unchanged across every framework Sandpack tab.
 */

export type UserRole = 'Trader' | 'Manager' | 'Admin';

export interface User {
  name: string;
  role: UserRole;
  region: string;
}

/**
 * The user driving this session. Try changing `role` to `'Trader'`,
 * `'Manager'` or `'Admin'` to see how the action buttons in the grid
 * change visibility.
 */
export const currentUser: User = {
  name: 'Jane Smith',
  role: 'Manager',
  region: 'EMEA',
};

/**
 * A trivial service. In a real application this might consult a
 * permissions API or a feature-flag store; for the demo a single
 * pure function is enough.
 */
export const userService = {
  canPerform(user: User, action: 'edit' | 'delete'): boolean {
    if (action === 'edit') {
      return user.role !== 'Trader';
    }
    if (action === 'delete') {
      return user.role === 'Admin';
    }
    return false;
  },
};
```

**`src/adaptableOptions.ts`**

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

/**
 * Three places consume the same `currentUser` + `userService` object:
 *
 *   1. `userName`            — config-time string, read directly
 *   2. `actionColumn['edit']` — runtime callback, reads from adaptableContext
 *   3. `actionColumn['delete']` — runtime callback, reads from adaptableContext
 *
 * Nothing here is framework-specific — exactly the same file runs in the
 * React, Vue, Angular and Vanilla Sandpack tabs.
 */
export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Adaptable Context Demo',

  // (1) Config-time usage — shown in the top-right of AdapTable
  userName: currentUser.name,

  // Single source of truth made available to every AdapTable callback
  adaptableContext: {
    currentUser,
    userService,
  },

  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'action',
        friendlyName: 'Actions',
        controls: [
          // (2) Runtime usage — Edit is hidden for Traders
          {
            type: 'command',
            command: 'edit',
            tooltip: 'Edit row (Managers and Admins only)',
            hidden: ({adaptableContext}: ActionColumnContext) =>
              !adaptableContext.userService.canPerform(
                adaptableContext.currentUser,
                'edit'
              ),
          },
          // (3) Runtime usage — Delete is shown only to Admins
          {
            type: 'command',
            command: 'delete',
            tooltip: 'Delete row (Admins only)',
            hidden: ({adaptableContext}: ActionColumnContext) =>
              !adaptableContext.userService.canPerform(
                adaptableContext.currentUser,
                'delete'
              ),
          },
        ],
      },
    ],
  },

  initialState: {
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'action',
            'name',
            'language',
            'github_stars',
            'license',
            'created_at',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
