Skip to content

ResponsivePlugin

Responsive Plugin for tbw-grid

Adds automatic card layout mode when the grid width falls below a configurable breakpoint. Perfect for responsive designs, split-pane UIs, and mobile viewports.

// Basic usage - switch to card layout below 500px
const config: GridConfig = {
plugins: [new ResponsivePlugin({ breakpoint: 500 })],
};
// Hide less important columns in card mode
const config: GridConfig = {
plugins: [
new ResponsivePlugin({
breakpoint: 600,
hiddenColumns: ['createdAt', 'updatedAt'],
}),
],
};
// Custom card renderer for advanced layouts
const config: GridConfig = {
plugins: [
new ResponsivePlugin({
breakpoint: 400,
cardRenderer: (row) => {
const card = document.createElement('div');
card.className = 'custom-card';
card.innerHTML = `<strong>${row.name}</strong><br>${row.email}`;
return card;
},
}),
],
};

Extends BaseGridPlugin

Inherited methods like attach(), detach(), afterRender(), etc. are documented in the base class.

PropertyTypeDescription
nameresponsiveUnique plugin identifier (derived from class name by default)
version1.0.0Plugin version - defaults to grid version for built-in plugins. Third-party plugins can override with their own semver.
stylesstringCSS styles to inject into the grid’s shadow DOM
manifestPluginManifestPlugin manifest declaring incompatibilities with other plugins.

Check if currently in responsive mode.

isResponsive(): boolean

boolean - true if the grid is in card layout mode


Force responsive mode regardless of width. Useful for testing or manual control.

setResponsive(enabled: boolean): void
NameTypeDescription
enabledbooleanWhether to enable responsive mode

Update breakpoint dynamically.

setBreakpoint(width: number): void
NameTypeDescription
widthnumberNew breakpoint width in pixels

Set a custom card renderer. This allows framework adapters to provide template-based renderers at runtime.

setCardRenderer(renderer: (row: T, rowIndex: number, column: ColumnConfig<any>) => HTMLElement | undefined): void
NameTypeDescription
renderer(row: T, rowIndex: number, column: ColumnConfig<any>) => HTMLElement | undefinedThe card renderer function, or undefined to use default

Get current grid width.

getWidth(): number

number - Width of the grid element in pixels


Get the currently active breakpoint config (multi-breakpoint mode only).

getActiveBreakpoint(): BreakpointConfig | null

BreakpointConfig | null - The active BreakpointConfig, or null if no breakpoint is active


override — Called when the plugin is attached to a grid. Override to set up event listeners, initialize state, etc.

attach(grid: GridElement): void
NameTypeDescription
gridGridElement
attach(grid: GridElement): void {
super.attach(grid);
// Set up document-level listeners with auto-cleanup
document.addEventListener('keydown', this.handleEscape, {
signal: this.disconnectSignal
});
}

override — Called when the plugin is detached from a grid. Override to clean up event listeners, timers, etc.

detach(): void
detach(): void {
// Clean up any state not handled by disconnectSignal
this.selectedRows.clear();
this.cache = null;
}

override — Apply hidden and value-only columns. In legacy mode (single breakpoint), only applies when in responsive mode. In multi-breakpoint mode, applies whenever there’s an active breakpoint.

afterRender(): void

override — Custom row rendering when cardRenderer is provided and in responsive mode.

When a cardRenderer is configured, this hook takes over row rendering to display the custom card layout instead of the default cell structure.

renderRow(row: unknown, rowEl: HTMLElement, rowIndex: number): boolean | void
NameTypeDescription
rowunknownThe row data object
rowElHTMLElementThe row DOM element to render into
rowIndexnumberThe index of the row in the data array

boolean | void - true if rendered (prevents default), void for default rendering


override — Handle keyboard navigation in responsive mode.

In responsive mode, the visual layout is inverted:

  • Cells are stacked vertically within each “card” (row)
  • DOWN/UP visually moves within the card (between fields)
  • Page Down/Page Up or Ctrl+Down/Up moves between cards

For custom cardRenderers, keyboard navigation is disabled entirely since the implementor controls the card content and should handle navigation via their own event handlers.

onKeyDown(e: KeyboardEvent): boolean
NameTypeDescription
eKeyboardEvent

boolean - true if the event was handled and default behavior should be prevented


⚠️ Deprecated: Use getRowHeight() instead. This hook will be removed in v2.0.

override — Return total extra height contributed by mixed row heights. This is called by the grid’s virtualization system to adjust scrollbar height.

The grid calculates: totalRows * baseRowHeight + pluginExtraHeight

For mixed layouts (groups + cards), we need to report the difference between actual heights and what the base calculation assumes:

  • Extra for groups: groupCount * (groupHeight - baseHeight)
  • Extra for cards: cardCount * (cardHeight - baseHeight)
getExtraHeight(): number

⚠️ Deprecated: Use getRowHeight() instead. This hook will be removed in v2.0.

override — Return extra height that appears before a given row index. Used by virtualization to correctly calculate scroll positions.

Like getExtraHeight, this accounts for both group and card row heights.

getExtraHeightBefore(beforeRowIndex: number): number
NameTypeDescription
beforeRowIndexnumber

override — Get the height of a specific row based on its type (group row vs card row). Returns undefined if not in responsive mode.

getRowHeight(row: unknown, _index: number): number | undefined
NameTypeDescription
rowunknownThe row data
_indexnumberThe row index (unused, but part of the interface)

number | undefined - The row height in pixels, or undefined if not in responsive mode


AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt