Message actions
FreeThe 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.
npx shadcn@latest add @pixilui/message-actionsPreview
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>
);
}When to use
Use it when
- Under every assistant answer in a chat or an agent's report.
- Anywhere a person rates a generated result and may ask for another.
Use something else when
- 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.
Installation
npx shadcn@latest add @pixilui/message-actionsAdds message-actions.tsx with button.tsx and tooltip.tsx, and installs clsx, tailwind-merge and @base-ui/react. First PixilUI item in this app? Add the registry once, as in Installation.
"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>
);
});
It uses Button and Tooltip: add them the same way. Then npm i clsx tailwind-merge @base-ui/react, and add the foundation once per app, as in Installation.
Usage
import { MessageActions } from "@/components/pixilui/message-actions";
<MessageActions
icons={icons}
copyValue={copyValue}
onRegenerate={onRegenerate}
onFeedbackChange={onFeedbackChange}
/>One row of five 28px buttons with 40px hit areas on desktop and 44 on touch; fits at 375 wide.
API reference
iconsRequiredMessageActionsIcons–The five icons: copy, regenerate, good, bad, more.copyValueRequiredstring | (() => 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" | nullnullThe 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.regenerateShortcutstring"⌘R"The shortcut shown in Regenerate's tooltip.Also takes every native <div> attribute, and a ref.
Keyboard
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.