# Selection toolbar

> Selectable list paired with a contextual batch-action toolbar that appears when items are checked for labeling, archiving or deleting.

- Kind: block · Category: App shell · Interactivity: script
- Install: `npx shadcn@latest add @haze/selection-toolbar`
- File: `src/components/haze/blocks/selection-toolbar.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/blocks/selection-toolbar

## Use 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 when
- Items only support single-row actions; use dropdown-menu or swipe-actions

## Alternatives
- [toolbar](https://hazeui.com/ui/toolbar.md)
- [inbox-list](https://hazeui.com/ui/inbox-list.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` | "haze-selection-toolbar" | no | Unique prefix for section heading IDs. |
| heading | `string` | "Release backlog" | no | List heading. |
| items | `{ id: string; title: string; meta: string; selected?: boolean }[]` | 4 backlog items | no | Selectable list rows. |
| class | `string` |  | no | Extra classes on the outer section. |

## 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.
- Key Tab: Move across row checkboxes and batch toolbar buttons
- Key Enter / Space: Toggle row selection, run Label/Archive/Delete or clear selection

## Agent prompts

### 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": "https://hazeui.com/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: https://hazeui.com/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.

## Example requests that should lead here
- Add a selectable list with a contextual floating batch-action toolbar for Label, Archive and Delete

## Source

```astro
---
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>
```
