Selection Actions
Offer AI edits for a piece of selected text, then ask the person to keep or discard the result.
Overview
Selection Actions is a small bar of edits for selected text: Improve, Shorten, or an instruction the person types. While the edit runs it shows progress. When the edit returns, it asks the person to keep or discard it.
The bar is the controls only. Tracking the selection, placing the bar near it, making the edit, and showing the rewritten text are your application's work. You tell the bar where things stand through status.
Anatomy
- ActionsOne button for each edit you offer
- PromptA field for a typed instruction, when you handle it
- StatusWorking, or an error
- DecisionKeep, Discard, and Try again
Installing selection-actions brings Button and Text Shimmer along as registry dependencies.
Behavior
Status
idleshows the actions and the prompt field.workingshows a shimmering label and no controls.resultshows Keep and Discard.errorshows a message and Discard.
Pass onRetry to add Try again to a result or an error.
Prompt
Pass onPrompt to add the field. Send is disabled while it is empty, and the field clears after sending.
Accessibility
The bar is a toolbar with a name. Working and the error are announced through a status that stays mounted. The prompt field has a name, and Send is a submit button.
When the status changes, the buttons under focus are replaced. Move focus to the new controls from your handler if the person triggered the change from the keyboard.
With reduced motion, the working label does not shimmer.
Installation
Usage
import { SelectionActions } from "@/components/ai/selection-actions"
<SelectionActions
status={edit.status}
actions={[
{ id: "improve", label: "Improve" },
{ id: "shorten", label: "Shorten" },
]}
onAction={(actionId) => edit.run(actionId, selection.text)}
onPrompt={(instruction) => edit.runCustom(instruction, selection.text)}
onKeep={edit.accept}
onDiscard={edit.reject}
onRetry={edit.retry}
/>Examples
A failed edit
The error state, with Try again.
API reference
SelectionActions
Accepts native div props beyond those listed.