@molecule/app-bulk-action-toolbar-angular

Feature · bulk-action-toolbar · App (browser) · v1.1.4 · Apache-2.0

Selection-aware floating toolbar that appears when N rows are selected

npm install @molecule/app-bulk-action-toolbar-angular

npm · Source on GitHub

How it works

@molecule/app-bulk-action-toolbar-angular is a ready-made bulk-action-toolbar feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

import { Component, signal } from '@angular/core'
import { BulkActionToolbar } from '@molecule/app-bulk-action-toolbar-angular'

@Component({
  selector: 'app-table',
  standalone: true,
  imports: [BulkActionToolbar],
  template: `
    <ng-template #deleteLabel>Delete</ng-template>
    <div
      mol-bulk-action-toolbar
      [count]="selected().length"
      [actions]="[
        {
          id: 'delete',
          label: deleteLabel,
          onClick: () => handleDelete(selected()),
          destructive: true,
        },
        { id: 'export', label: 'Export', onClick: () => handleExport(selected()) },
      ]"
      [onClearSelection]="() => selected.set([])"
    ></div>
  `,
})
export class Table {
  readonly selected = signal<string[]>([])

  readonly handleDelete = (ids: string[]): void => {
    console.log('delete rows', ids)
  }

  readonly handleExport = (ids: string[]): void => {
    console.log('export rows', ids)
  }
}

Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular

Reference

Angular selection-aware bulk action toolbar — the counterpart of @molecule/app-bulk-action-toolbar-react: the same props and the same DOM for the same input.

Exports <BulkActionToolbar> — the selection count + a row of action buttons + a "Clear selection" link, styled via the ClassMap bond, shown only while rows are selected.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { Component, signal } from '@angular/core'
import { BulkActionToolbar } from '@molecule/app-bulk-action-toolbar-angular'

@Component({
  selector: 'app-table',
  standalone: true,
  imports: [BulkActionToolbar],
  template: `
    <ng-template #deleteLabel>Delete</ng-template>
    <div
      mol-bulk-action-toolbar
      [count]="selected().length"
      [actions]="[
        {
          id: 'delete',
          label: deleteLabel,
          onClick: () => handleDelete(selected()),
          destructive: true,
        },
        { id: 'export', label: 'Export', onClick: () => handleExport(selected()) },
      ]"
      [onClearSelection]="() => selected.set([])"
    ></div>
  `,
})
export class Table {
  readonly selected = signal<string[]>([])

  readonly handleDelete = (ids: string[]): void => {
    console.log('delete rows', ids)
  }

  readonly handleExport = (ids: string[]): void => {
    console.log('export rows', ids)
  }
}

Type

feature

Installation

npm install @molecule/app-bulk-action-toolbar-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

BulkAction

Descriptor for a single action button rendered inside BulkActionToolbar. Node props (label, icon) are templated content in Angular (an input cannot carry a node), so they are typed unknown: pass plain strings or <ng-template> refs.

interface BulkAction {
  /** Stable id — the `@for` track key of the action's button. */
  id: string
  /** Button label — text or a template ref. */
  label: unknown
  /** Optional leading icon inside the button. */
  icon?: unknown
  /**
   * Called when the action's button is clicked. React's DOM click handler
   * receives the click event, so the callback may take it.
   */
  onClick: (event?: MouseEvent) => void
  /** Renders the button as a solid error-colored (destructive) button. */
  destructive?: boolean
  /** Disables the button. */
  disabled?: boolean
}

BulkActionToolbarProps

Props accepted by the BulkActionToolbar component.

interface BulkActionToolbarProps {
  /** Selection count — toolbar hides when 0. */
  count: number
  /** Action buttons. */
  actions: BulkAction[]
  /** Called when "Clear selection" is clicked (React passes the click event). */
  onClearSelection?: (event?: MouseEvent) => void
  /** Position — `'sticky-bottom'` (default), `'sticky-top'`, or `'inline'`. */
  position?: 'sticky-bottom' | 'sticky-top' | 'inline'
  /** Extra classes. */
  className?: string
}

Classes

BulkActionToolbar

BulkActionToolbar component.

Selection-aware bulk action bar — appears when the user has selected one or more rows, shows the count + a row of action buttons + a "Clear selection" link. The host IS the root <div> (div[mol-bulk-action-toolbar]), matching React's root exactly.

React renders null while count <= 0 — Angular's host element always exists, so useHostPresence takes it out of the page in that state (no element is left behind, matching React's null) and puts it back when rows are selected again. position defaults to 'sticky-bottom' (16px inset, z-30); 'sticky-top' and 'inline' are also supported. Destructive actions render as solid error-colored buttons.

React keys each action button by the action's id, so a re-ordered or re-passed array moves/keeps the rendered buttons per key; Angular's @for tracks the same id.

@Component({
  selector: 'div[mol-bulk-action-toolbar]',
  standalone: true,
  imports: [Button, NgTemplateOutlet],
  inputs: ['count', 'actions', 'onClearSelection', 'position', 'className'],
  host: {
    role: 'region',
    '[attr.aria-label]': 'toolbarLabel()',
    '[class]': 'classes()',
    '[style]': 'inlineStyleText()',
  },
  template: `<span [class]="countClasses()">{{ selectedText() }}</span>
    <div [class]="rowClasses()">
      @for (action of actions; track action.id) {
        <button
          mol-button
          [variant]="action.destructive ? 'solid' : 'ghost'"
          [color]="action.destructive ? 'error' : 'primary'"
          [size]="'sm'"
          [disabled]="action.disabled ?? false"
          (click)="runAction(action, $event)"
        >
          @if (isTemplate(action.icon)) {
            <ng-container [ngTemplateOutlet]="action.icon" />
          }
          @if (action.label) {
            <span>
              @if (isTemplate(action.label)) {
                <ng-container [ngTemplateOutlet]="action.label" />
              } @else {
                {{ action.label }}
              }
            </span>
          }
        </button>
      }
      @if (onClearSelection) {
        <button mol-button [variant]="'ghost'" [size]="'sm'" (click)="onClearSelection($event)">
          {{ clearText() }}
        </button>
      }
    </div>`,
})
export class BulkActionToolbar {
  /** Selection count — the toolbar leaves the page when 0. */
  count = 0
  /** Action buttons. */
  actions: BulkAction[] = []
  /** Called when "Clear selection" is clicked. */
  onClearSelection?: (event?: MouseEvent) => void
  /** Position (`'sticky-bottom'` until an input says otherwise). */
  position?: 'sticky-bottom' | 'sticky-top' | 'inline'
  /** Extra classes. */
  className?: string

  /** The translation function, captured in the injection context. */
  private readonly i18n = useTranslation()

  constructor() {
    // Leaves no element in the page while nothing is selected (React renders null).
    useHostPresence(() => this.count > 0)
  }

  /**
   * The current class map.
   * @returns The UIClassMap.
   */
  cm(): UIClassMap {
    return getClassMap()
  }

  /**
   * Computes the toolbar class list (React's
   * `cm.cn(cm.flex({ align, justify, gap }), cm.sp('px', 3), cm.sp('py', 2), className)`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(
      this.cm().flex({ align: 'center', justify: 'between', gap: 'md' }),
      this.cm().sp('px', 3),
      this.cm().sp('py', 2),
      this.className,
    )
  }

  /**
   * The count text's class (`cm.cn(cm.textSize('sm'), cm.fontWeight('semibold'))`).
   * @returns The class string.
   */
  countClasses(): string {
    return this.cm().cn(this.cm().textSize('sm'), this.cm().fontWeight('semibold'))
  }

  /**
   * The action row's class (`cm.flex({ align: 'center', gap: 'sm' })`).
   * @returns The class string.
   */
  rowClasses(): string {
    return this.cm().cn(this.cm().flex({ align: 'center', gap: 'sm' }))
  }

  /**
   * The toolbar's accessible name (React's `aria-label`): prop-less `t()` with
   * the English default `'Bulk actions'`, so a locale switch re-renders it.
   * @returns The label text.
   */
  toolbarLabel(): string {
    return this.i18n.t('bulkActions.aria.toolbar', {}, { defaultValue: 'Bulk actions' })
  }

  /**
   * The translated count label (React's `t('bulkActions.selected', { count }, …)`).
   * @returns The label text.
   */
  selectedText(): string {
    return this.i18n.t(
      'bulkActions.selected',
      { count: this.count },
      { defaultValue: '{{count}} selected' },
    )
  }

  /**
   * The translated Clear label (React's `t('bulkActions.clear', {}, …)`).
   * @returns The label text.
   */
  clearText(): string {
    return this.i18n.t('bulkActions.clear', {}, { defaultValue: 'Clear' })
  }

  /**
   * The toolbar's inline style text (React's
   * `style={position === 'sticky-bottom' ? … : …}`); null when inline, so
   * no `style` attribute renders.
   * @returns The style text or null.
   */
  inlineStyleText(): string | null {
    const position = this.position ?? 'sticky-bottom'
    return position === 'sticky-bottom'
      ? styleText(STICKY_BOTTOM)
      : position === 'sticky-top'
        ? styleText(STICKY_TOP)
        : null
  }

  /**
   * Whether the value is a node template (a node-valued prop's Angular
   * representation). Checked structurally — `TemplateRef` is a type-only
   * import here, and the check must survive any bundler.
   * @param value - The prop value.
   * @returns True for a template ref.
   */
  isTemplate(value: unknown): value is TemplateRef<unknown> {
    return (
      typeof value === 'object' &&
      value !== null &&
      typeof (value as TemplateRef<unknown>).createEmbeddedView === 'function'
    )
  }

  /**
   * Runs one action's callback (React passes the click event through).
   * @param action - The action whose button was clicked.
   * @param event - The native click event.
   */
  runAction(action: BulkAction, event: MouseEvent): void {
    action.onClick(event)
  }
}

Injection Notes

Requirements

Peer dependencies:

  • @angular/common ^22.0.0
  • @angular/core ^22.0.0
  • @angular/platform-browser ^22.0.0
  • @molecule/app-angular ^3.12.0 || ^4.0.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-angular ^1.1.0

Runtime Dependencies

  • @angular/common
  • @angular/core
  • @angular/platform-browser
  • @molecule/app-angular
  • @molecule/app-ui
  • @molecule/app-ui-angular

Renders nothing while count <= 0 — mount it unconditionally and drive it from selection state; the host element detaches from the DOM in that state (React returns null) and is restored when rows are selected again. position defaults to 'sticky-bottom' (16px inset, z-30); 'sticky-top' and 'inline' are also supported. Destructive actions render as solid error-colored buttons. The count label, the Clear button and the toolbar's own aria-label are translated via useTranslation() (React's exact keys and default values) and the companion @molecule/app-locales-bulk-action-toolbar locale bond. An action's node-valued fields (label, icon) are <ng-template> refs or plain strings.

Translations

Translation strings are provided by @molecule/app-locales-bulk-action-toolbar.