Skip to content

Message Branch

React version switcher for AI chat replies: step through regenerated answers with ‹ 2 / 3 › buttons or Alt+Arrow keys, with a cross-fade and live count.

src/registry/ui/message-branch.tsx/r/message-branch.json

Installation

bash
npx shadcn@latest add @minidev/message-branch

MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/message-branch.json

Usage

tsx
import { MessageBranch } from "@/components/ui/message-branch"

export function Example() {
  return (
    <MessageBranch />
  )
}

API reference

MessageBranch props
PropTypeDefault
versionsEvery version of the reply, oldest first. When omitted, each child is one version.React.ReactNode[]-
childrenVersions as children, used when `versions` is not passed.React.ReactNode-
indexControlled index of the version shown (0-based).number-
defaultIndexStarting index when uncontrolled. Defaults to the newest version.number-
onIndexChangeFires with the new 0-based index when the user switches version.(index: number) => void-
hideWhenSingleHide the selector while there is only one version.booleantrue
labelWhat one version is called in the screen reader text: "Response 2 of 3", "Previous response".string"Response"
actionsRendered on the selector row after the switcher, for example MessageActions.React.ReactNode-
classNamestring-

When to use

Wrap the assistant reply: pass every version in versions (or as children) and it shows one, with a compact selector underneath. The selector hides itself while there is only one version, and in uncontrolled mode a newly added version is shown straight away. Put MessageActions in actions to get the selector and the copy, retry and vote buttons on one row. For the "Not quite right?" footer use RegenerateBar.

Keyboard interactions

Keyboard interactions
KeyAction
TaborShiftTabMoves between the previous and next buttons and the rest of the row.
EnterorSpaceOn a chevron, shows the previous or next version. At either end the button is disabled but keeps focus.
AltArrowLeftorAltArrowRightAnywhere inside the branch, shows the previous or next version. Ignored inside text fields, where it moves by word.

Accessibility

  • The switcher is a role="group" named "Response versions" (from label). The chevrons are labelled "Previous response" and "Next response".
  • The visible "2 / 3" is aria-hidden. A polite, atomic live region reads "Response 2 of 3" instead, so screen readers announce each switch.
  • At the first or last version the chevron is disabled with aria-disabled, but it stays focusable, so focus does not drop to the page.
  • Under reduced motion the cross-fade and the height change both use { duration: 0 }, so the version swaps instantly.

Frequently asked questions

How do I show the new reply after the user clicks Regenerate?

Append it to versions. Uncontrolled, MessageBranch sees the count grow and shows the newest version. If you control index, set it yourself when you append, for example setIndex(replies.length).

How do I keep the chosen version after a reload?

Control it. Store the index with the message and pass index and onIndexChange. Without index, it starts on defaultIndex, which defaults to the newest version.

Can the switcher sit in the same row as copy and retry?

Yes. Pass MessageActions as actions and it renders right after the "‹ 2 / 3 ›" switcher. While there is only one version the switcher hides itself and the actions stay.

Should each version be a MessageBubble?

No. MessageBranch mounts the version again on every switch, and MessageBubble plays a rise-in animation on mount, so it would run on top of the cross-fade. Wrap the whole branch instead: <MessageBubble role="assistant"><MessageBranch versions={replies} /></MessageBubble>.

Dependencies

npm

lucide-reactmotion

Built from

Examples

Regenerate adds a version

Retry appends a reply; uncontrolled, the branch jumps to the new one. Alt+Left and Alt+Right switch while focus is inside.

Edited prompt, controlled

Branch the user's message with label="Prompt" and keep the index in state, so the reply below follows the prompt version. className="items-end" moves the switcher under the message.

One version

With a single version the switcher hides itself and only the actions show. Pass hideWhenSingle={false} to keep "1 / 1".

All AI chat UI components for React