Selection toolbar
Selectable list paired with a contextual batch-action toolbar that appears when items are checked for labeling, archiving or deleting.
pnpm dlx shadcn@latest add @haze/selection-toolbar---
import { Archive, Check, Tag, Trash2, X } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface ListItem {
id: string;
title: string;
meta: string;
selected?: boolean;
}
interface Props {
id?: string;
heading?: string;
items?: ListItem[];
class?: string;
}
const {
id = "haze-selection-toolbar",
heading = "Release backlog",
items = [
{ id: "st1", title: "Audit dark-mode contrast ratios", meta: "Accessibility · High", selected: true },
{ id: "st2", title: "Add keyboard shortcuts dialog", meta: "Navigation · Medium", selected: true },
{ id: "st3", title: "Publish v2.8 registry manifest", meta: "Release · High" },
{ id: "st4", title: "Write Astro 5 migration guide", meta: "Docs · Low" },
],
class: className,
} = Astro.props;
---
<section aria-labelledby={`${id}-title`} data-haze-sel-toolbar class={cn("bg-background px-4 py-10 sm:px-6 sm:py-14", className)}>
<div class="mx-auto max-w-2xl space-y-4">
<div class="rounded-2xl border border-border bg-card p-5">
<div class="flex items-center justify-between gap-2 border-b border-border pb-3">
<h2 id={`${id}-title`} class="text-base/6 font-semibold tracking-tight text-foreground">{heading}</h2>
<button type="button" data-st-all class="inline-flex h-7 cursor-pointer items-center rounded-md px-2 text-xs font-medium text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">Select all</button>
</div>
<ul role="list" data-st-list class="mt-3 divide-y divide-border">
{
items.map((item) => (
<li data-st-row class="py-2.5 first:pt-1 last:pb-1">
<label class="flex cursor-pointer items-center gap-3 rounded-lg px-2 py-1.5 transition-colors duration-150 hover:bg-accent">
<span class="relative inline-grid size-4 shrink-0 place-items-center"><input type="checkbox" checked={item.selected} data-st-chk class="peer col-start-1 row-start-1 shrink-0 focus-visible:outline-2 focus-visible:outline-ring size-4 shrink-0 cursor-pointer appearance-none rounded-sm border border-border-strong bg-field transition-[background-color,border-color,scale] duration-150 ease-snappy hover:border-foreground active:scale-90 checked:border-primary checked:bg-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring forced-colors:appearance-auto" /><Check class="pointer-events-none col-start-1 row-start-1 size-3 scale-50 text-primary-foreground opacity-0 transition-[opacity,scale] duration-200 ease-spring peer-checked:scale-100 peer-checked:opacity-100 forced-colors:hidden" stroke-width={3} aria-hidden="true" /></span>
<span class="min-w-0 flex-1">
<span class="block truncate text-sm/5 font-medium text-foreground">{item.title}</span>
<span class="block text-xs/4 text-muted-foreground">{item.meta}</span>
</span>
</label>
</li>
))
}
</ul>
</div>
<div
data-st-bar
role="toolbar"
aria-label="Batch selection actions"
class="flex flex-wrap items-center justify-between gap-2 rounded-xl border border-border bg-popover px-3.5 py-2.5 shadow-float"
>
<span data-st-count class="text-xs font-semibold text-foreground tabular-nums">2 selected</span>
<div class="flex flex-wrap items-center gap-1.5">
<button type="button" data-st-act="Labeled" class="inline-flex h-8 cursor-pointer items-center gap-1.5 rounded-lg border border-frame bg-field px-2.5 text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
<Tag class="size-3.5 text-muted-foreground" aria-hidden="true" />
Label
</button>
<button type="button" data-st-act="Archived" class="inline-flex h-8 cursor-pointer items-center gap-1.5 rounded-lg border border-frame bg-field px-2.5 text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
<Archive class="size-3.5 text-muted-foreground" aria-hidden="true" />
Archive
</button>
<button type="button" data-st-act="Deleted" class="inline-flex h-8 cursor-pointer items-center gap-1.5 rounded-lg border border-frame bg-field px-2.5 text-xs font-medium text-destructive hover:bg-destructive/8 focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
<Trash2 class="size-3.5" aria-hidden="true" />
Delete
</button>
<button type="button" data-st-clear aria-label="Clear selection" class="inline-flex size-8 cursor-pointer items-center justify-center rounded-lg text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
<X class="size-4" aria-hidden="true" />
</button>
</div>
</div>
<p data-st-status role="status" class="min-h-4 text-xs/4 text-muted-foreground"></p>
</div>
</section>
<script>
function initSelToolbars() {
document.querySelectorAll<HTMLElement>("[data-haze-sel-toolbar]").forEach((root) => {
if (root.hasAttribute("data-enhanced")) return;
root.setAttribute("data-enhanced", "");
const bar = root.querySelector<HTMLElement>("[data-st-bar]");
const count = root.querySelector<HTMLElement>("[data-st-count]");
const status = root.querySelector<HTMLElement>("[data-st-status]");
const checks = () => Array.from(root.querySelectorAll<HTMLInputElement>("[data-st-chk]"));
const sync = () => {
const n = checks().filter((c) => c.checked).length;
if (bar) bar.hidden = n === 0;
if (count) count.textContent = `${n} selected`;
};
root.querySelector<HTMLElement>("[data-st-list]")?.addEventListener("change", sync);
root.querySelector<HTMLButtonElement>("[data-st-all]")?.addEventListener("click", () => {
checks().forEach((c) => (c.checked = true));
sync();
});
root.querySelector<HTMLButtonElement>("[data-st-clear]")?.addEventListener("click", () => {
checks().forEach((c) => (c.checked = false));
sync();
if (status) status.textContent = "Selection cleared.";
});
root.querySelectorAll<HTMLButtonElement>("[data-st-act]").forEach((btn) => {
btn.addEventListener("click", () => {
const act = btn.dataset.stAct ?? "Updated";
const picked = checks().filter((c) => c.checked);
if (act === "Deleted" || act === "Archived") picked.forEach((c) => c.closest("[data-st-row]")?.remove());
else picked.forEach((c) => (c.checked = false));
sync();
if (status) status.textContent = `${act} ${picked.length} item${picked.length === 1 ? "" : "s"}.`;
});
});
sync();
});
}
initSelToolbars();
document.addEventListener("astro:page-load", initSelToolbars);
</script>When to use
- Enabling multi-item batch actions (label, archive, delete) without cluttering every row
- Showing a floating selection count bar that hides automatically when nothing is checked
When not to use
- Items only support single-row actions; use dropdown-menu or swipe-actions
Installation
Add the Haze registry to components.json once, then install Selection toolbar with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/selection-toolbarnpx shadcn@latest add @haze/selection-toolbaryarn dlx shadcn@latest add @haze/selection-toolbarbunx --bun shadcn@latest add @haze/selection-toolbarFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | "haze-selection-toolbar" | Unique prefix for section heading IDs. |
| heading | string | "Release backlog" | List heading. |
| items | { id: string; title: string; meta: string; selected?: boolean }[] | 4 backlog items | Selectable list rows. |
| class | string | – | Extra classes on the outer section. |
Keyboard interactions
- Tab
- Move across row checkboxes and batch toolbar buttons
- EnterSpace
- Toggle row selection, run Label/Archive/Delete or clear selection
Accessibility
- The batch bar uses role="toolbar" with an accessible label and hides cleanly when zero checkboxes are checked.
- Bulk actions announce the exact number of affected items via a polite role="status" message.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI block "Selection toolbar" with `npx shadcn@latest add @haze/selection-toolbar`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import SelectionToolbar from "@/components/haze/blocks/selection-toolbar.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/blocks/selection-toolbar.md
Customize with real content
Adapt the Haze selection-toolbar block for our product. Replace the demo values of these props with our real content: id, heading, items, class. Keep the layout, the heading level rules and these accessibility details: The batch bar uses role="toolbar" with an accessible label and hides cleanly when zero checkboxes are checked. Bulk actions announce the exact number of affected items via a polite role="status" message. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using selection-toolbar, check it fits: use it when enabling multi-item batch actions (label, archive, delete) without cluttering every row; showing a floating selection count bar that hides automatically when nothing is checked. Avoid it when items only support single-row actions; use dropdown-menu or swipe-actions. Alternatives: @haze/toolbar, @haze/inbox-list.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.