# Message actions

The row under an assistant's answer: copy, regenerate, good and bad feedback and more, as quiet icon buttons with tooltips, where copy draws a check and feedback fills its glyph.

Tier: free. Type: component. Category: ai. Tags: chat, keyboard.

## Install

```bash
npx shadcn@latest add @pixilui/message-actions
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json"
  }
}
```

Files: `components/pixilui/message-actions.tsx`.

## Preview

Live preview: https://pixilui.com/components/message-actions

Signature: Copy swaps to a check that draws itself, and feedback fills its glyph instead of tinting the button.

Styles: `quiet` (default), `lively`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop. Standalone files: quiet https://pixilui.com/r/standalone/message-actions.tsx, lively https://pixilui.com/r/standalone/message-actions.lively.tsx.

## Code

`components/pixilui/message-actions.tsx`

```tsx
"use client";

import { forwardRef, useEffect, useRef, useState, type ComponentPropsWithoutRef, type ReactNode } from "react";
import { cn } from "./lib/utils";
import { Button } from "./button";
import { Tooltip, TooltipProvider } from "./tooltip";

/*
  The row under an answer, as on the locked buttons page: copy (then Copied, with the button's
  drawn check), regenerate, good and bad feedback, more. Each is a small Secondary 2 button with a
  tooltip; feedback fills its glyph when chosen and a second press clears it.
*/

const COPIED_MS = 1500;

/** Which feedback is chosen: good, bad or none. */
export type MessageActionsFeedback = "good" | "bad" | null;

/** The five icons, passed in; the component never imports an icon library. */
export type MessageActionsIcons = {
  /** Copy (Lucide Copy). The check that replaces it is the button's own. */
  copy: ReactNode;
  /** Regenerate (Lucide RotateCw). */
  regenerate: ReactNode;
  /** Good response (Lucide ThumbsUp). */
  good: ReactNode;
  /** Bad response (Lucide ThumbsDown). */
  bad: ReactNode;
  /** More actions (Lucide Ellipsis). */
  more: ReactNode;
};

export type MessageActionsProps = Omit<ComponentPropsWithoutRef<"div">, "children"> & {
  /** The five icons: copy, regenerate, good, bad, more. */
  icons: MessageActionsIcons;
  /** What Copy puts on the clipboard: the answer's text, or a function that returns it when pressed. */
  copyValue: string | (() => string | Promise<string>);
  /** Runs when Regenerate is pressed. */
  onRegenerate?: () => void | Promise<void>;
  /** The chosen feedback, controlled. Pair with onFeedbackChange. */
  feedback?: MessageActionsFeedback;
  /** The starting feedback when uncontrolled. */
  defaultFeedback?: MessageActionsFeedback;
  /** Runs with the new feedback; pressing the chosen one again sends null. */
  onFeedbackChange?: (feedback: MessageActionsFeedback) => void | Promise<void>;
  /** Runs when More is pressed: open your menu from here. */
  onMore?: () => void | Promise<void>;
  /** The shortcut shown in Regenerate's tooltip. */
  regenerateShortcut?: string;
};

/** Copy, regenerate, good and bad feedback and more, under an assistant's answer. */
export const MessageActions = forwardRef<HTMLDivElement, MessageActionsProps>(function MessageActions(
  { icons, copyValue, onRegenerate, feedback: feedbackProp, defaultFeedback = null, onFeedbackChange, onMore, regenerateShortcut = "⌘R", className, ...rest },
  ref,
) {
  const [copied, setCopied] = useState(false);
  const [inner, setInner] = useState<MessageActionsFeedback>(defaultFeedback);
  const feedback = feedbackProp === undefined ? inner : feedbackProp;
  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
  useEffect(() => () => clearTimeout(timer.current), []);

  const copy = async () => {
    try {
      const text = typeof copyValue === "function" ? await copyValue() : copyValue;
      await navigator.clipboard.writeText(text);
      setCopied(true);
      clearTimeout(timer.current);
      timer.current = setTimeout(() => setCopied(false), COPIED_MS);
    } catch {
      // The clipboard refused (no permission, an insecure page): the button stays at rest.
    }
  };
  const pick = (value: "good" | "bad") => {
    const next = feedback === value ? null : value;
    if (feedbackProp === undefined) setInner(next);
    void onFeedbackChange?.(next);
  };

  return (
    <TooltipProvider>
      <div ref={ref} role="group" aria-label="Message actions" className={cn("flex items-center gap-0.5 font-body", className)} data-rating={feedback ?? undefined} {...rest}>
        <Tooltip label={copied ? "Copied" : "Copy"}>
          <Button variant="secondary-2" size="sm" aria-label="Copy" icon={icons.copy} copied={copied} onClick={() => void copy()} />
        </Tooltip>
        <Tooltip label="Regenerate" shortcut={regenerateShortcut}>
          <Button variant="secondary-2" size="sm" aria-label="Regenerate" icon={icons.regenerate} onClick={() => void onRegenerate?.()} />
        </Tooltip>
        <Tooltip label="Good response">
          <Button variant="secondary-2" size="sm" aria-label="Good response" icon={icons.good} pressed={feedback === "good"} pressedLook="glyph" onClick={() => pick("good")} />
        </Tooltip>
        <Tooltip label="Bad response">
          <Button variant="secondary-2" size="sm" aria-label="Bad response" icon={icons.bad} pressed={feedback === "bad"} pressedLook="glyph" onClick={() => pick("bad")} />
        </Tooltip>
        <Tooltip label="More">
          <Button variant="secondary-2" size="sm" aria-label="More" icon={icons.more} onClick={() => void onMore?.()} />
        </Tooltip>
      </div>
    </TooltipProvider>
  );
});
```

## Usage

When to use:

- Under every assistant answer in a chat or an agent's report.
- Anywhere a person rates a generated result and may ask for another.

When not to use:

- Copying a single value such as a link or an ID: use Copy button.
- Actions that change data (approve, send): use real labelled buttons, not icons.

```tsx
import { IconCopyCheck } from "@/components/pixilui/icons/copy-check";
import { IconDislike } from "@/components/pixilui/icons/dislike";
import { IconLike } from "@/components/pixilui/icons/like";
import { IconMore } from "@/components/pixilui/icons/more";
import { IconRegenerate } from "@/components/pixilui/icons/regenerate";
import { MessageActions } from "@/components/pixilui/message-actions";

// One finished element, no rows.
export const knobs = {} as const;

const ANSWER = "Checkout errors started at 09:42 UTC, four minutes after deploy 4f3a9c1 shipped new retry logic in payments-client.";

// The actions sit under the answer they act on, on the answer's surface (the glass mixes with it).
export default function MessageActionsDemo() {
  return (
    <div className="flex w-full max-w-md flex-col gap-2 rounded-panel bg-surface p-4 pb-2 font-body elevation-raised" style={{ ["--pixilui-under" as string]: "var(--pixilui-surface)" }}>
      <p className="px-0.5 text-body text-secondary">{ANSWER}</p>
      <MessageActions
        className="-ms-1.5"
        icons={{ copy: <IconCopyCheck />, regenerate: <IconRegenerate />, good: <IconLike />, bad: <IconDislike />, more: <IconMore /> }}
        copyValue={ANSWER}
      />
    </div>
  );
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `icons` (required) | `MessageActionsIcons` |  | The five icons: copy, regenerate, good, bad, more. |
| `copyValue` (required) | `string \| (() => string \| Promise<string>)` |  | What Copy puts on the clipboard: the answer's text, or a function that returns it when pressed. |
| `onRegenerate` | `() => void \| Promise<void>` |  | Runs when Regenerate is pressed. |
| `feedback` | `"good" \| "bad" \| null` |  | The chosen feedback, controlled. Pair with onFeedbackChange. |
| `defaultFeedback` | `"good" \| "bad" \| null` | `null` | The starting feedback when uncontrolled. |
| `onFeedbackChange` | `(feedback: MessageActionsFeedback) => void \| Promise<void>` |  | Runs with the new feedback; pressing the chosen one again sends null. |
| `onMore` | `() => void \| Promise<void>` |  | Runs when More is pressed: open your menu from here. |
| `regenerateShortcut` | `string` | `"⌘R"` | The shortcut shown in Regenerate's tooltip. |

Also accepts every native `<div>` attribute, and a ref to it.

## Keyboard

- `Tab`: Moves through copy, regenerate, good, bad and more; each shows its tooltip on focus.
- `Enter`: Presses the focused action.
- `Space`: The same as Enter, on release.
- `Esc`: Closes the open tooltip.

## Accessibility

- A group named Message actions holding five native buttons with aria-labels that match their tooltips.
- Good and bad response are toggles with aria-pressed; pressing the chosen one again clears it.
- data-rating on the root is good or bad while one is chosen, for styling and tests.

## Motion

- Copy swaps its icon deep to a check that draws itself, then returns after 1500ms.
- Chosen feedback fills its glyph in 150ms; the button keeps its size.
- Tooltips wait once, then open instantly as you move along the row.
- Reduced motion: the icon changes at once, with no draw.

## Responsive

- One row of five 28px buttons with 40px hit areas on desktop and 44 on touch; fits at 375 wide.

## Performance

- One timer for Copied; tooltips mount only when open.

## Notes for AI

- Pass icons as an object (Lucide Copy, RotateCw, ThumbsUp, ThumbsDown, Ellipsis); never import an icon library inside the component.
- copyValue is the answer's text, or a function that returns it on press.
- Feedback is "good", "bad" or null: control it with feedback and onFeedbackChange, or start it with defaultFeedback.
- onMore is where your own menu opens; onRegenerate starts a new answer.

## Related

- [Copy button](https://pixilui.com/components/copy-button/markdown): A button that copies a link, an ID or a snippet and says so: the icon swaps to a check that draws itself and the label to Copied, at a width that never moves.
- [Button group](https://pixilui.com/components/button-group/markdown): Icon buttons on one glass tray, each named by a tooltip with its key hint, with arrow keys to move along the row: the toolbar under a composer or an editor.
- [Button](https://pixilui.com/components/button/markdown): The PixilUI button in four main styles and four more, five sizes from 24 to 44, with icons, loading, disabled, a count and a shortcut.
