Skip to content

Message branches

The “‹ 2 / 3 ›” switcher under a reply that was regenerated or a prompt that was edited, with a one-line note. Stepping slides the old version out, the new one in, and the box eases to its height.

A regenerated reply

Plan a menu for 8 on Saturday. Priya is vegetarian.

For 8, with Priya vegetarian, make the vegetarian dish the centre and the meat a side, so nobody is served the lesser plate.

  • Starter: burrata, roasted grapes and grilled bread, plated before the doorbell.
  • Main: two mushroom and leek galettes, cut at the table.
  • On the side: a small porchetta for the six who want it, roasted on Friday and sliced cold.
  • Dessert: olive oil cake with whipped crème fraîche, made the day before.
Version 3 of 3
Regenerated · Deep

An edited prompt

Can Maya bring dessert, and Theo the wine?

Version 2 of 2
Edited prompt

Yes to both. Maya brings the olive oil cake, whole, with the crème fraîche on the side. Theo brings four bottles: two white for the burrata and two light reds for the galette. Copper has sent them each a note with the time.

Installation

With UI Polish connected, ask your agent:

Add the UI Polish message branches to this project.

Or with the shadcn CLI:

npx shadcn@latest add https://uipolish.com/r/message-branches.json
Package@base-ui/react
Brings inAnimated size, Button, Icons, Text, Text morph, Tooltip

A message with more than one version: a reply that was regenerated, or a prompt the person edited. The version on screen sits above a small "‹ 2 / 3 ›" switcher with an optional note about it ("Regenerated · Deep"). Stepping slides the old version out and the new one in from the side you moved toward, and the box eases to the new height.

TSX
<MessageBranches  versions={reply.versions.map((v) => ({    id: v.id,    content: <Markdown>{v.text}</Markdown>,    note: v.note,  }))}  index={reply.shown}  onIndexChange={(i) => showVersion(reply.id, i)}/>

When to use it

  • Under an assistant reply that has been regenerated, so the earlier answers stay one press away.
  • Under a user message that was edited and sent again (align="end", so the switcher sits under the bubble's edge).

When not to

  • A message with one version: pass it anyway and the switcher stays out (nothing renders under it), or render the content on its own.
  • Comparing versions side by side, or more than a handful of them: show a list or a diff instead; stepping one at a time hides what changed.
  • A reply that is still streaming: render the new version only once it ends, or keep the switcher's space and step to it when it does.

Props

PropWhat it does
versions{ id, content, note? }[], oldest first. content is any node; note is one line, cut with an ellipsis if it doesn't fit.
indexControlled: the version shown, from 0. Pass it when the rest of the thread depends on it (an edited prompt has its own replies below).
defaultIndexUncontrolled start. Defaults to the newest version.
onIndexChangeCalled with the new index when the person steps.
align"start" (default) under a reply; "end" under a user bubble, with the note on the far side of the switcher.
classNameOn the root.

States

StateWhat you seeHow
RestMuted 16px chevrons on 32px square buttons around "2 / 3" in 12px text, its figures tabular; the note after it in the same muted 12px
HoverThe chevron darkens, a 6% fill appears@media (hover: hover)
PressThe fill darkens and shrinks to --ui-scale-press; the chevron never movesButton :active
First / lastPrevious or Next at 50% with no hover or press. It stays focusable, so focus doesn't drop to the page when you reach the endaria-disabled
SteppingThe old version slides 16px away and fades on the instant step, the new one slides in from the other side and fades in half a beat later; the box eases to its height; the number morphs; the notes trade places in one cell
One versionContent only, no switcherversions.length === 1

Each number holds the width of the total's digits in tabular figures, so "9 / 12" → "10 / 12" never moves Next. The switcher keeps to its edge and the note sits on the far side of it, so a longer note never moves the buttons.

Keyboard

KeyWhereDoes
TabSwitcherPrevious, then Next
Enter, SpacePrevious, NextStep one version; at the ends, nothing

Focus stays on the button you pressed while the content changes above it.

Motion

WhatDurationEasing
Version slides 16px (in and out)--ui-dur-glide--ui-ease-glide
Old version fades out--ui-dur-instant--ui-ease-color
New version fades in, after half the instant step--ui-dur-fast--ui-ease-enter
Height grows / shrinks (Animated size)--ui-dur-panel / --ui-dur-base--ui-ease-expand
Number changes (Text morph)--ui-dur-glide--ui-ease-glide
Note out / in--ui-dur-instant / --ui-dur-fast--ui-ease-color / --ui-ease-enter
Press in / release0ms / --ui-dur-snap--ui-ease-snap

The glide spring lands without passing its place, so a version never swings past the edge. Stepped again mid-slide, the version leaves from where it visibly is. The content is clipped sideways only while it slides, so focus rings and shadows inside it aren't cut at rest and a scrolling thread never shows a horizontal scrollbar for a frame. On the start side, where every line begins, the clip edge stands one slide (16px) out, so a version arriving from there is never cut through its first letters. A note that arrives with a new version (a retry) fades in like the others. Nothing animates on mount. Under prefers-reduced-motion: reduce the versions crossfade in place, the height changes at once and the number swaps.

Layout

The switcher row sits 16px under a reply (--ui-space-4), the same as Response actions, and 8px under a user bubble, whose fill already makes an edge. Only the 16px icons and the text take layout space; the 32px buttons overflow it, so the row lines up with the content's edge. Set --branches-gap on the root to change the space above the row.

Under a reply, put Response actions after Message branches and give it the shown version's text, so Copy and the rating follow the version on screen:

TSX
<MessageBranches versions={versions} index={shown} onIndexChange={setShown} /><ResponseActions text={texts[shown]} onRetry={regenerate} />

Accessibility

  • Previous and Next are real buttons named "Previous version" and "Next version", with tooltips that repeat the name for pointer users.
  • The switcher is a group named "Versions". Its counter reads "Version 2 of 3"; the visible digits are aria-hidden.
  • A step is announced politely ("Version 2 of 3") from a role="status" region; focus stays on the button.
  • The leaving version is aria-hidden and inert while it fades, so it's never read or tabbed into.
  • Notes for the versions not shown are aria-hidden; the current one is read as text.
  • Small buttons take a 40px touch target on coarse pointers.

Before / after

Before: the content snaps, the page below jumps to the new height, the counter changes width and nothing is announced.

TSX
<div>{versions[i].content}</div><button disabled={i === 0} onClick={() => setI(i - 1)}>‹</button>{i + 1}/{versions.length}<button disabled={i === versions.length - 1} onClick={() => setI(i + 1)}>›</button>

After:

TSX
<MessageBranches versions={versions} />