Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.0 KB
Raw Blame
import clsx from 'clsx'
import { createPortal } from 'react-dom'

import { LemonButton } from 'lib/lemon-ui/LemonButton'

import { BulkSelectionConfig, BulkSelectionContext, BulkSelectionKey } from './useBulkSelection'

export interface BulkSelectionBarProps<T extends Record<string, any>, K extends BulkSelectionKey = BulkSelectionKey> {
    context: BulkSelectionContext<T, K>
    config: BulkSelectionConfig<T, K>
    noun: [string, string]
}

export function BulkSelectionBar<T extends Record<string, any>, K extends BulkSelectionKey = BulkSelectionKey>({
    context,
    config,
    noun,
}: BulkSelectionBarProps<T, K>): JSX.Element | null {
    const isOpen = context.selectedCount > 0
    const [singular, plural] = noun
    const word = context.selectedCount === 1 ? singular : plural
    const selectionLabel = config.getSelectionLabel?.(context) ?? `${context.selectedCount} ${word} selected`
    const summary = <span className="text-secondary text-sm">{selectionLabel}</span>
    const clearButton = (
        <LemonButton type="secondary" size="small" onClick={context.clearSelection}>
            Clear
        </LemonButton>
    )

    const bar = (
        <div
            className={clsx(
                'flex items-center justify-end gap-2 min-h-9 px-1 LemonTable__bulk-selection-bar',
                config.barClassName
            )}
        >
            {config.actionsPlacement !== 'start' && summary}
            {config.actionsPlacement !== 'start' && clearButton}
            {config.renderActions(context)}
            {config.actionsPlacement === 'start' && clearButton}
            {config.actionsPlacement === 'start' && summary}
        </div>
    )

    if (config.barPortalTarget) {
        return isOpen ? createPortal(bar, config.barPortalTarget) : null
    }

    return (
        <div
            className={clsx('LemonTable__bulk-selection-reveal', isOpen && 'LemonTable__bulk-selection-reveal--open')}
            aria-hidden={!isOpen}
        >
            <div className="min-h-0 overflow-hidden">{bar}</div>
        </div>
    )
}