# Action Toolbar

> A row of grouped buttons with one Tab stop, such as formatting toggles or bulk actions.

Page: https://uicontext.com/docs/components/action-toolbar

Group: Primitives.

## When to use

A row of grouped buttons with one Tab stop, such as formatting toggles or bulk actions. Use [Menu](https://uicontext.com/docs/components/menu.md) instead when the actions fit in one dropdown instead of a row.

## Usage

```ts
import { ActionToolbar } from "./ui/uicontext/components/ActionToolbar.ts"

const Message = UiSchema.TaggedEnum({ Pressed: { value: Schema.String } })

export const FileActions = (h: Html.H<typeof Message.Type>) =>
	ActionToolbar(h, {
		label: "File",
		onPress: (value) => Message.Pressed({ value }),
		items: [{ value: "download", label: "Download" }, { value: "share", label: "Share" }, { kind: "separator" }, { value: "delete", label: "Delete", tone: "danger" }],
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Formatting a comment

The example the page opens with: Action Toolbar in “Formatting a comment”.

```ts title="src/demo.ts" source="www/src/examples/action-toolbar/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { ActionToolbar } from "./ui/uicontext/components/ActionToolbar.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	wrap: { display: "grid", gap: space.md, inlineSize: "min(100%, 40rem)" },
	status: { margin: 0, fontSize: font.sm, color: color.muted, minBlockSize: "1.4em" },
	// arrows that point along the line flip in right-to-left text
	mirror: { scale: { default: null, ":dir(rtl)": "-1 1" } },
})

const icon = <M>(h: Html.H<M>, d: string, mirror = false) =>
	h.svg.attrs({ width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", "stroke-width": "1.5", "stroke-linecap": "round", "stroke-linejoin": "round", ...sx(mirror && styles.mirror) }).children(h.path.attrs({ d }))

const paths = {
	bold: "M4.5 3h4.25a2.5 2.5 0 0 1 0 5H4.5zM4.5 8h5a2.5 2.5 0 0 1 0 5h-5z",
	italic: "M10 3H6.5M9.5 13H6M8.75 3l-1.5 10",
	strike: "M3 8h10M10.5 4.5C10 3.6 9 3 7.9 3 6.3 3 5 4 5 5.3M5.5 11.5c.5.9 1.5 1.5 2.6 1.5 1.6 0 2.9-1 2.9-2.3",
	code: "M6 4.5 2.5 8 6 11.5M10 4.5l3.5 3.5-3.5 3.5",
	link: "M6.75 9.25a2.5 2.5 0 0 0 3.5 0l2-2a2.5 2.5 0 0 0-3.5-3.5l-.5.5M9.25 6.75a2.5 2.5 0 0 0-3.5 0l-2 2a2.5 2.5 0 0 0 3.5 3.5l.5-.5",
	bullets: "M6 4h7.5M6 8h7.5M6 12h7.5M2.75 4h.01M2.75 8h.01M2.75 12h.01",
	numbers: "M6.5 4H13.5M6.5 8H13.5M6.5 12H13.5M2.5 3l1-.5V5.5M2.5 10.5h1.5l-1.5 2h1.5",
	undo: "M5.5 3 2.5 6l3 3M2.5 6h7a4 4 0 0 1 0 8H8",
}

const Message = UiSchema.TaggedEnum({ Pressed: { value: Schema.String, pressed: Schema.optional(Schema.Boolean) } })

// a comment box's formatting: marks that stay pressed, lists that are one or the other, and undo
export const CommentFormatting = Html.component({
	name: "CommentFormatting",
	model: Schema.Struct({ marks: Schema.Array(Schema.String), list: Schema.NullOr(Schema.String), last: Schema.String }),
	message: Message,
}).make({
	init: () => ({ marks: ["bold"], list: null, last: "" }),
	update: {
		Pressed: ($, { value, pressed }) => {
			if (value === "bullets" || value === "numbers") return $.evolve({ list: pressed === true ? value : null, last: pressed === true ? `${value === "bullets" ? "Bulleted" : "Numbered"} list on` : "List off" })
			if (pressed === undefined) return $.evolve({ last: value === "undo" ? "Undid the last change" : "Opened the link editor" })
			return $.evolve({ marks: (m) => (pressed ? [...m, value] : m.filter((x) => x !== value)), last: `${value[0]!.toUpperCase()}${value.slice(1)} ${pressed ? "on" : "off"}` })
		},
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			ActionToolbar(h, {
				label: "Formatting",
				framed: true,
				onPress: (value, pressed) => Message.Pressed({ value, pressed }),
				items: [
					{ value: "bold", label: "Bold", icon: icon(h, paths.bold), iconOnly: true, shortcut: "mod+b", pressed: model.marks.includes("bold") },
					{ value: "italic", label: "Italic", icon: icon(h, paths.italic), iconOnly: true, shortcut: "mod+i", pressed: model.marks.includes("italic") },
					{ value: "strike", label: "Strikethrough", icon: icon(h, paths.strike), iconOnly: true, shortcut: "mod+shift+x", pressed: model.marks.includes("strike") },
					{ value: "code", label: "Code", icon: icon(h, paths.code), iconOnly: true, shortcut: "mod+e", pressed: model.marks.includes("code") },
					{ kind: "separator" },
					{ value: "bullets", label: "Bulleted list", icon: icon(h, paths.bullets, true), iconOnly: true, pressed: model.list === "bullets" },
					{ value: "numbers", label: "Numbered list", icon: icon(h, paths.numbers, true), iconOnly: true, pressed: model.list === "numbers" },
					{ kind: "separator" },
					{ value: "link", label: "Link", icon: icon(h, paths.link), shortcut: "mod+k" },
					{ kind: "spacer" },
					{ value: "undo", label: "Undo", icon: icon(h, paths.undo, true), iconOnly: true, shortcut: "mod+z" },
				],
			}),
			h.p.attrs({ role: "status", ...sx(styles.status) }).children(model.last),
		),
})
```

### Actions on selected rows

A `text` item says what's acted on; a `spacer` pushes Clear to the end. A disabled button stays in the arrow keys' order so it can be found.

```ts title="src/bulk.ts" source="www/src/examples/action-toolbar/bulk.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { ActionToolbar } from "./ui/uicontext/components/ActionToolbar.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	wrap: { display: "grid", gap: space.md, inlineSize: "min(100%, 40rem)" },
	status: { margin: 0, fontSize: font.sm, color: color.muted, minBlockSize: "1.4em" },
})

const Message = UiSchema.TaggedEnum({ Pressed: { value: Schema.String } })

const said: Readonly<Record<string, string>> = {
	assign: "Choose who takes the 3 issues.",
	label: "Pick labels for the 3 issues.",
	archive: "Archived 3 issues.",
	delete: "Deleting 3 issues needs a confirm.",
	clear: "Selection cleared.",
}

// an issue list's actions on the selected rows, with the count first and Clear at the far end
export const SelectedIssues = Html.component({
	name: "SelectedIssues",
	model: Schema.Struct({ last: Schema.String }),
	message: Message,
}).make({
	init: () => ({ last: "" }),
	update: { Pressed: ($, { value }) => $.evolve({ last: said[value] ?? "" }) },
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			ActionToolbar(h, {
				label: "Selected issues",
				framed: true,
				onPress: (value) => Message.Pressed({ value }),
				items: [
					{ kind: "text", text: "3 selected" },
					{ kind: "separator" },
					{ value: "assign", label: "Assign" },
					{ value: "label", label: "Label" },
					{ value: "archive", label: "Archive" },
					{ value: "move", label: "Move", disabled: true },
					{ value: "delete", label: "Delete", tone: "danger" },
					{ kind: "spacer" },
					{ value: "clear", label: "Clear" },
				],
			}),
			h.p.attrs({ role: "status", ...sx(styles.status) }).children(model.last),
		),
})
```

## API reference

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `key` | `string` | Names the toolbar among others in the same view; needed for more than one. |
| `label` (required) | `string` | What the buttons act on: "Formatting", "Selected rows". |
| `items` (required) | `ActionToolbarItem<Message>[]` | — |
| `onPress` (required) | `(value: string, pressed: boolean \| undefined) => Message` | — |
| `orientation` | `"horizontal" \| "vertical"` | — |
| `framed` | `boolean` | A border and a fill, for a toolbar that floats or sits on its own. |
| `disabled` | `boolean` | — |
| `style` | `Style` | — |

## Keyboard and accessibility

### Keys

| Keys | Does |
| --- | --- |
| Tab | Reach the toolbar once, on the button used last |
| Right arrow, Left arrow | Move to the next or previous button |
| Home, End | Move to the first or last button |
| Enter, Space | Press the button |

On a Mac, Control is Command and Alt is Option. Left and Right trade places in right-to-left text.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/action-toolbar
```

With npm: `npx shadcn@latest add checkfu/uicontext/action-toolbar`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/action-toolbar`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/action-toolbar`.

It writes these files:

- `src/ui/uicontext/components/ActionToolbar.ts`
- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/Kbd.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/button.stylex.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/ActionToolbar.ts](https://github.com/checkfu/uicontext/blob/main/src/ActionToolbar.ts)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
