"use client"; import type { Table } from "@tanstack/react-table"; import { AnimatePresence, motion } from "motion/react"; import * as React from "react"; import * as ReactDOM from "react-dom"; import { LuX } from "react-icons/lu"; import { Button } from "@/components/ui/button"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; interface DataTableActionBarProps extends React.ComponentProps { table: Table; visible?: boolean; portalContainer?: Element | DocumentFragment | null; } function DataTableActionBar({ table, visible: visibleProp, portalContainer: portalContainerProp, children, className, ...props }: DataTableActionBarProps) { const [mounted, setMounted] = React.useState(false); React.useLayoutEffect(() => { setMounted(true); }, []); React.useEffect(() => { function onKeyDown(event: KeyboardEvent) { if (event.key === "Escape") { table.toggleAllRowsSelected(false); } } window.addEventListener("keydown", onKeyDown); return () => window.removeEventListener("keydown", onKeyDown); }, [table]); const portalContainer = portalContainerProp ?? (mounted ? globalThis.document?.body : null); if (!portalContainer) return null; const visible = visibleProp ?? table.getFilteredSelectedRowModel().rows.length > 0; return ReactDOM.createPortal( {visible && ( {children} )} , portalContainer, ); } interface DataTableActionBarActionProps extends React.ComponentProps { tooltip?: string; isPending?: boolean; } function DataTableActionBarAction({ size = "sm", tooltip, isPending, disabled, className, children, ...props }: DataTableActionBarActionProps) { const trigger = ( ); if (!tooltip) return trigger; return ( {trigger}

{tooltip}

); } interface DataTableActionBarSelectionProps { table: Table; } function DataTableActionBarSelection({ table, }: DataTableActionBarSelectionProps) { const onClearSelection = React.useCallback(() => { table.toggleAllRowsSelected(false); }, [table]); return (
{table.getFilteredSelectedRowModel().rows.length} selected

Clear selection

Esc
); } export { DataTableActionBar, DataTableActionBarAction, DataTableActionBarSelection, };