Custom Menu Slots

All menu plugins (Header Menu, Cell Menu, Context Menu, Grid Menu) support cross-framework compatible slot rendering for custom content injection in menu items. This is achieved through the slotRenderer callback at the item level combined with an optional defaultMenuItemRenderer at the menu level.

Note: This documentation covers how menu items are rendered (visual presentation). If you need to dynamically modify which commands appear in the menu (filtering, sorting, adding/removing items), see the commandListBuilder callback documented in Grid Menu, Context Menu, or Header Menu.

TypeScript Tip: Type Inference with commandListBuilder

When using commandListBuilder to add custom menu items with slotRenderer callbacks, cast the return value to the appropriate type to enable proper type parameters in callbacks:

contextMenu: {
  commandListBuilder: (builtInItems) => {
    return [
      ...builtInItems,
      {
        command: 'custom-action',
        title: 'My Action',
        slotRenderer: (cmdItem, args) => `<div>${cmdItem.title}</div>`,
      }
    ] as Array<MenuCommandItem | GridMenuItem | 'divider'>;
  }
}

Alternatively, if you only have built-in items and dividers, you can use the simpler cast:

return [...] as Array<MenuCommandItem | 'divider'>;

Core Concept

Each menu item can define a slotRenderer callback function that receives the item and args, and returns either an HTML string or an HTMLElement. This single API works uniformly across all menu plugins.

Slot Renderer Callback

  • cmdItem - The menu cmdItem object containing command, title, iconCssClass, etc.

  • args - The callback args providing access to grid, column, dataContext, and other context

  • event - Optional DOM event passed during click handling (allows stopPropagation())

Basic Example - HTML String Rendering

Advanced Example - HTMLElement Objects

Default Menu-Level Renderer

Set a defaultMenuItemRenderer at the menu option level to apply to all items (unless overridden by individual slotRenderer):

The slotRenderer and defaultMenuItemRenderer work identically across all menu plugins:

Header Menu

Cell Menu

Context Menu

Grid Menu

Framework Integration Examples

Vanilla JavaScript

Angular - Dynamic Components

React - Using Hooks

Vue - Using createApp

Real-World Use Cases

1. Add Keyboard Shortcuts

2. Add Status Indicators

3. Add Dynamic Content Based on Context

4. Add Interactive Elements

5. Add Badges and Status Labels

6. Gradient and Styled Icons

Notes and Best Practices

  • HTML strings are inserted via innerHTML - ensure content is sanitized if user-provided

  • HTMLElement objects are appended directly - safer for dynamic content and allows event listeners

  • Cross-framework compatible - works in vanilla JS, Angular, React, Vue, Aurelia using the same API

  • Priority order - Item-level slotRenderer overrides menu-level defaultMenuItemRenderer

  • Built-in command preservation - When overriding a built-in command (e.g., sort-asc, sort-desc, hide, etc.) with custom properties like slotRenderer or iconCssClass, if you don't provide an action callback, the library will automatically preserve and use the built-in action for that command. This means you can safely customize the appearance of built-in commands without losing their functionality.

  • Accessibility - Include proper ARIA attributes when creating custom elements

  • Event handling - Call event.stopPropagation() in interactive elements to prevent menu commands from firing

  • Default fallback - If neither slotRenderer nor defaultMenuItemRenderer is provided, the default icon + text rendering is used

  • Performance - Avoid heavy DOM manipulation inside renderer callbacks (they may be called multiple times)

  • Event parameter - The optional event parameter is passed during click handling and allows you to control menu behavior

  • All menus supported - This API works uniformly across Header Menu, Cell Menu, Context Menu, and Grid Menu

Styling Custom Menu Items

Migration from Static Rendering

Before (Static HTML Title):

After (Custom Rendering):

Error Handling

When creating custom renderers, handle potential errors gracefully:

Last updated