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. After an error, Try again comes first, because it is the recovery. While working or after an error, the bar keeps the width it had with its actions, so it does not jump.
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. If focus was in the bar, the bar moves it for you: to the bar itself while working, to Keep on a result, and to Try again (or Discard) after an error. Focus elsewhere on the page is never moved.
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.