← Components

Communication

Bubble

The surface for chat text. A run of bubbles from one sender melts its facing corners together. Reactions pop; adding one again bounces.

Examples and variants

Run and reactions

Add bubbles to see the corners melt.

First thought.
Example
function BubbleRun() {
  const [count, setCount] = useState(1)
  const [reactions, setReactions] = useState([{ emoji: "👍", label: "Thumbs up", count: 2, mine: false }])
  return (
    <div className="flex w-full max-w-sm flex-col gap-2">
      <BubbleGroup>
        {Array.from({ length: count }, (_, i) => (
          <Bubble key={i}>{["First thought.", "A second one joins the run.", "Corners melt together."][i % 3]}</Bubble>
        ))}
      </BubbleGroup>
      <BubbleReactions
        reactions={reactions}
        onReact={() => setReactions((r) => r.map((x) => ({ ...x, count: x.count + (x.mine ? -1 : 1), mine: !x.mine })))}
      />
      <Button size="sm" variant="secondary" className="self-start" onClick={() => setCount((c) => (c >= 3 ? 1 : c + 1))}>
        Add a bubble
      </Button>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/bubble.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/bubble.tsxbubble
      components/ui/bubble.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      
      /*
        Bubble: the surface for chat text. In a BubbleGroup, consecutive bubbles
        from one sender melt their facing corners together, so a run reads as one
        shape. Reactions pop in; adding the same one again bounces it.
      */
      function BubbleGroup({ className, ...props }: React.ComponentProps<"div">) {
        return <div data-slot="bubble-group" className={cn("bubble-group flex flex-col gap-0.5", className)} {...props} />
      }
      
      function Bubble({
        from = "agent",
        className,
        ...props
      }: React.ComponentProps<"div"> & { from?: "user" | "agent" }) {
        return (
          <div
            data-slot="bubble"
            data-from={from}
            className={cn(
              "bubble w-fit max-w-(--bubble-max-width) rounded-(--bubble-corner) px-(--bubble-padding-x) py-(--bubble-padding-y) text-sm leading-relaxed break-words",
              from === "user" ? "ms-auto bg-(--bubble-user) text-(--bubble-user-label)" : "bg-(--bubble-agent) text-(--bubble-agent-label)",
              className
            )}
            {...props}
          />
        )
      }
      
      function BubbleReactions({
        reactions,
        onReact,
        className,
      }: {
        reactions: { emoji: string; label: string; count: number; mine?: boolean }[]
        onReact?: (emoji: string) => void
        className?: string
      }) {
        const [bounce, setBounce] = React.useState<string | null>(null)
        return (
          <div data-slot="bubble-reactions" className={cn("flex flex-wrap gap-1", className)}>
            {reactions.map((reaction) => (
              <button
                key={`${reaction.emoji}-${bounce === reaction.emoji ? "b" : "a"}`}
                type="button"
                aria-pressed={reaction.mine}
                aria-label={`${reaction.label}, ${reaction.count}`}
                onClick={() => {
                  setBounce(reaction.emoji)
                  onReact?.(reaction.emoji)
                }}
                className={cn(
                  "bubble-reaction liquid-press inline-flex h-6 items-center gap-1 rounded-(--shape-full) px-2 text-xs tabular-nums outline-none focus-visible:shadow-(--focus-ring)",
                  reaction.mine ? "bg-(--color-bg-selected)" : "bg-(--surface-container)"
                )}
              >
                <span aria-hidden>{reaction.emoji}</span>
                {reaction.count}
              </button>
            ))}
          </div>
        )
      }
      
      export { BubbleGroup, Bubble, BubbleReactions }
      

Imports

What the examples on this page import:

Imports
import { Attachment } from "@/components/ui/attachment"
import { Avatar, AvatarFallback } from "@/components/ui/avatar"
import { Bubble, BubbleGroup, BubbleReactions } from "@/components/ui/bubble"
import { Button } from "@/components/ui/button"
import { CodeBlock } from "@/components/ui/code-block"
import { Marker } from "@/components/ui/marker"
import { Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader } from "@/components/ui/message"
import { MessageScroller } from "@/components/ui/message-scroller"
import { PromptInput } from "@/components/ui/prompt-input"
import { Questionnaire } from "@/components/ui/questionnaire"
import { StreamingText } from "@/components/ui/streaming-text"
import { Trace, TraceItem } from "@/components/ui/trace"
import { VoiceInput } from "@/components/ui/voice-input"
import { tokenMs } from "@/lib/motion/token-ms"

Anatomy

  1. 1Bubble · --bubble-corner, --bubble-padding-*, --bubble-user / --bubble-agent.
  2. 2Group · Joins corners to --bubble-corner-joined.
  3. 3Reactions · Pill buttons with counts.

Props

PropTypeDefaultDescription
from"user" | "agent""agent"Colour and side.
BubbleReactions reactions, onReact{ emoji, label, count, mine }[]—Reaction pills.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--bubble-max-width80%80%same
--bubble-padding-xvar(--space-4)16pxsame
--bubble-padding-yvar(--space-2)8pxsame
--bubble-cornervar(--shape-lg)22pxsame
--bubble-corner-joinedvar(--shape-xs)6pxsame
--bubble-uservar(--color-brand)oklch(0.18 0.008 260)oklch(0.98 0 0)
--bubble-user-labelvar(--color-fg-on-accent)oklch(0.97 0.004 95)oklch(0.14 0 0)
--bubble-agentvar(--surface-container)oklch(0.965 0.005 95)oklch(0.12 0 0)
--bubble-agent-labelvar(--on-surface)oklch(0.22 0.01 260)oklch(1 0 0)

Motion

TriggerWhat happensTokens
Join / reactCorners melt on the liquid curve; reactions pop.--duration-medium, --ease-liquid

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Reactions are buttons with labels like “Thumbs up, 2”.

Usage

Do

  • Keep bubbles short; use Code block for code.

Don’t

  • Don't put whole layouts in a bubble.

Code

components/ui/bubble.tsx
"use client"

import * as React from "react"
import { cn } from "cn"

/*
  Bubble: the surface for chat text. In a BubbleGroup, consecutive bubbles
  from one sender melt their facing corners together, so a run reads as one
  shape. Reactions pop in; adding the same one again bounces it.
*/
function BubbleGroup({ className, ...props }: React.ComponentProps<"div">) {
  return <div data-slot="bubble-group" className={cn("bubble-group flex flex-col gap-0.5", className)} {...props} />
}

function Bubble({
  from = "agent",
  className,
  ...props
}: React.ComponentProps<"div"> & { from?: "user" | "agent" }) {
  return (
    <div
      data-slot="bubble"
      data-from={from}
      className={cn(
        "bubble w-fit max-w-(--bubble-max-width) rounded-(--bubble-corner) px-(--bubble-padding-x) py-(--bubble-padding-y) text-sm leading-relaxed break-words",
        from === "user" ? "ms-auto bg-(--bubble-user) text-(--bubble-user-label)" : "bg-(--bubble-agent) text-(--bubble-agent-label)",
        className
      )}
      {...props}
    />
  )
}

function BubbleReactions({
  reactions,
  onReact,
  className,
}: {
  reactions: { emoji: string; label: string; count: number; mine?: boolean }[]
  onReact?: (emoji: string) => void
  className?: string
}) {
  const [bounce, setBounce] = React.useState<string | null>(null)
  return (
    <div data-slot="bubble-reactions" className={cn("flex flex-wrap gap-1", className)}>
      {reactions.map((reaction) => (
        <button
          key={`${reaction.emoji}-${bounce === reaction.emoji ? "b" : "a"}`}
          type="button"
          aria-pressed={reaction.mine}
          aria-label={`${reaction.label}, ${reaction.count}`}
          onClick={() => {
            setBounce(reaction.emoji)
            onReact?.(reaction.emoji)
          }}
          className={cn(
            "bubble-reaction liquid-press inline-flex h-6 items-center gap-1 rounded-(--shape-full) px-2 text-xs tabular-nums outline-none focus-visible:shadow-(--focus-ring)",
            reaction.mine ? "bg-(--color-bg-selected)" : "bg-(--surface-container)"
          )}
        >
          <span aria-hidden>{reaction.emoji}</span>
          {reaction.count}
        </button>
      ))}
    </div>
  )
}

export { BubbleGroup, Bubble, BubbleReactions }