Bulk action bar
86 lines · 1 file · MIT
The dark strip that takes the toolbar’s place while rows are ticked, so the table never moves.
3 selected
1,284 selected
import type { ReactNode } from "react";
import { cn } from "./cn";
import { X } from "./icons";
const ACTION =
"inline-flex h-8 cursor-pointer items-center gap-1.5 whitespace-nowrap rounded-lg px-2.5 text-sm font-medium transition-[background-color,scale] duration-150 ease-out active:scale-[0.97] disabled:cursor-default disabled:opacity-60 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-bg [@media(hover:hover)]:hover:bg-bg/10";
/**
* What to do with the ticked rows. It takes the toolbar's place while
* anything is ticked, at the toolbar's height, so ticking a box never moves
* the table: the search and filters come back as soon as the selection
* clears.
*
* Ink on paper inverted (the one dark strip on the page) because it is a
* mode: everything on it acts on the selection, not on the table.
*/
export function BulkBar({
count,
label = "Selected rows",
onClear,
children,
className,
}: {
count: number;
/** Names the toolbar for assistive tech: "Selected links". */
label?: string;
onClear: () => void;
/** The actions, each a `BulkAction`. Leave one out for somebody who cannot do it. */
children?: ReactNode;
className?: string;
}) {
return (
<div
role="toolbar"
aria-label={label}
className={cn(
"flex h-11 items-center gap-1 rounded-xl bg-text py-1.5 pr-1.5 pl-2 text-sm text-bg",
className,
)}
>
<button
type="button"
onClick={onClear}
aria-label="Clear selection"
className={cn(ACTION, "w-8 justify-center px-0")}
>
<X size={15} stroke={2} aria-hidden />
</button>
<span aria-live="polite" className="tnum whitespace-nowrap">
<b className="font-semibold">{count.toLocaleString("en-US")}</b> selected
</span>
<span className="flex-1" />
{children}
</div>
);
}
/** One action on the bar. `short` is the label on a phone, where the bar is narrow. */
export function BulkAction({
onClick,
disabled,
icon,
short,
children,
}: {
onClick: () => void;
disabled?: boolean;
icon?: ReactNode;
short?: string;
children: ReactNode;
}) {
return (
<button type="button" onClick={onClick} disabled={disabled} className={ACTION}>
{icon}
{short ? (
<>
<span className="max-sm:hidden">{children}</span>
<span className="sm:hidden">{short}</span>
</>
) : (
children
)}
</button>
);
}
Install
One command. The files land in your repo, with everything they import.
$ bunx shadcn@latest add @blocco/bulk-bar- Category
- Actions
- Files
- bulk-bar.tsx