Bubble
Holds one turn of a conversation, with a visual weight that tells the reader who is speaking and what kind of message it is.
Installation
pnpm dlx shadcn@latest add @silkware/bubbleTo copy the component by hand, install its dependencies:
pnpm add class-variance-authority cnThen copy the source into components/ui/bubble.tsx:
import * as React from "react"import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"
function BubbleGroup({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="bubble-group" className={cn("flex min-w-0 flex-col gap-2", className)} {...props} /> )}
const bubbleVariants = cva( "group/bubble relative flex w-fit max-w-[80%] min-w-0 flex-col gap-1 group-data-[align=end]/message:self-end data-[align=end]:self-end data-[variant=ghost]:max-w-full", { variants: { variant: { default: "*:data-[slot=bubble-content]:bg-primary *:data-[slot=bubble-content]:text-primary-foreground [&>[data-slot=bubble-content]:is(button,a):hover]:bg-primary/80", secondary: "*:data-[slot=bubble-content]:bg-secondary *:data-[slot=bubble-content]:text-secondary-foreground [&>[data-slot=bubble-content]:is(button,a):hover]:bg-[color-mix(in_oklch,var(--secondary),var(--foreground)_5%)]", muted: "*:data-[slot=bubble-content]:bg-muted [&>[data-slot=bubble-content]:is(button,a):hover]:bg-[color-mix(in_oklch,var(--muted),var(--foreground)_5%)]", tinted: "*:data-[slot=bubble-content]:bg-[oklch(from_var(--primary)_0.93_calc(c*0.4)_h)] *:data-[slot=bubble-content]:text-foreground dark:*:data-[slot=bubble-content]:bg-[oklch(from_var(--primary)_0.3_calc(c*0.4)_h)] [&>[data-slot=bubble-content]:is(button,a):hover]:bg-[oklch(from_var(--primary)_0.88_calc(c*0.5)_h)] dark:[&>[data-slot=bubble-content]:is(button,a):hover]:bg-[oklch(from_var(--primary)_0.35_calc(c*0.5)_h)]", outline: "*:data-[slot=bubble-content]:border-border *:data-[slot=bubble-content]:bg-background [&>[data-slot=bubble-content]:is(button,a):hover]:bg-muted [&>[data-slot=bubble-content]:is(button,a):hover]:text-foreground dark:[&>[data-slot=bubble-content]:is(button,a):hover]:bg-input/30", ghost: "border-none *:data-[slot=bubble-content]:rounded-none *:data-[slot=bubble-content]:bg-transparent *:data-[slot=bubble-content]:p-0 [&>[data-slot=bubble-content]:is(button,a):hover]:bg-muted [&>[data-slot=bubble-content]:is(button,a):hover]:text-foreground dark:[&>[data-slot=bubble-content]:is(button,a):hover]:bg-muted/50", destructive: "*:data-[slot=bubble-content]:bg-destructive/10 *:data-[slot=bubble-content]:text-destructive dark:*:data-[slot=bubble-content]:bg-destructive/20 [&>[data-slot=bubble-content]:is(button,a):hover]:bg-destructive/20 dark:[&>[data-slot=bubble-content]:is(button,a):hover]:bg-destructive/30", }, }, defaultVariants: { variant: "default", }, })
function Bubble({ variant = "default", align = "start", className, ...props}: React.ComponentProps<"div"> & VariantProps<typeof bubbleVariants> & { align?: "start" | "end" }) { return ( <div data-slot="bubble" data-variant={variant} data-align={align} className={cn(bubbleVariants({ variant }), className)} {...props} /> )}
function BubbleContent({ className, render, children, ...props}: React.ComponentProps<"div"> & { render?: (props: React.HTMLAttributes<HTMLElement>) => React.ReactNode}) { if (render) { const renderProps = { ...props, "data-slot": "bubble-content", className: cn( "w-fit max-w-full min-w-0 overflow-hidden rounded-xl border border-transparent px-3 py-2 text-sm leading-relaxed wrap-break-word group-data-[align=end]/bubble:self-end [button]:text-left [button,a]:transition-colors [button,a]:outline-none [button,a]:focus-visible:border-ring [button,a]:focus-visible:ring-3 [button,a]:focus-visible:ring-ring/50", className ), children, }
return render(renderProps) }
return ( <div {...props} data-slot="bubble-content" className={cn( "w-fit max-w-full min-w-0 overflow-hidden rounded-xl border border-transparent px-3 py-2 text-sm leading-relaxed wrap-break-word group-data-[align=end]/bubble:self-end [button]:text-left [button,a]:transition-colors [button,a]:outline-none [button,a]:focus-visible:border-ring [button,a]:focus-visible:ring-3 [button,a]:focus-visible:ring-ring/50", className )} > {children} </div> )}
const bubbleReactionsVariants = cva( "absolute z-10 flex w-fit shrink-0 items-center justify-center gap-1 rounded-full bg-muted px-1.5 py-0.5 text-sm ring-3 ring-card has-[button]:p-0", { variants: { side: { top: "top-0 -translate-y-3/4", bottom: "bottom-0 translate-y-3/4", }, align: { start: "left-3", end: "right-3", }, }, defaultVariants: { side: "bottom", align: "end", }, })
function BubbleReactions({ side = "bottom", align = "end", className, ...props}: React.ComponentProps<"div"> & { align?: "start" | "end" side?: "top" | "bottom"}) { return ( <div data-slot="bubble-reactions" data-align={align} data-side={side} className={cn(bubbleReactionsVariants({ side, align }), className)} {...props} /> )}
export { BubbleGroup, Bubble, BubbleContent, BubbleReactions }Usage
import { Bubble, BubbleContent, BubbleGroup, BubbleReactions,} from "@/components/ui/bubble"<Bubble variant="muted"> <BubbleContent>Lamivo Metalli is two days late on order 4417.</BubbleContent></Bubble>A bubble is the surface of a message and nothing more. The row around it, with the avatar, the header and the footer, is a Message. Inside a Message the bubble takes its side from the message, so set align on the Message, and set it on the Bubble only when you use a bubble on its own. The patterns that put both in a conversation are Chat and Message.
Examples
Variants
Seven weights. default is the strongest and the colour of the primary action; secondary and muted are neutral tints; tinted is a pale version of the primary colour; outline has an edge and no fill; ghost has neither fill, padding nor width limit; destructive is crimson, for failures.
Alignment
align is start or end. Put the person using the interface at the end and everyone else at the start, in every conversation.
Group
BubbleGroup stacks bubbles from the same sender with a small gap. Set align on each bubble, not on the group.
Ghost for the agent
A ghost bubble drops the frame so a long answer reads as text and can use the full width. It is the usual choice for an agent on Agentic OS, with the person's own message in a filled bubble.
Reactions
BubbleReactions sits on the edge of a bubble for a small mark, such as who has seen or approved it. side is top or bottom and align is start or end. It overlaps the bubble, so leave vertical room between messages. Give it role="img" and an aria-label when it holds only icons.
Link or button
BubbleContent takes a render function, so a bubble can be a real button or a. The bubble then gains a hover colour and a focus ring. Use it for quick replies.
Accessibility
A plain bubble is text in a div, so it has no keyboard behaviour. The keys below apply to a bubble rendered as a button or a link.
| Key | Action |
|---|---|
| Tab | Moves focus to the next bubble that is a button or a link. |
| Enter | Activates a bubble rendered as a button or a link. |
| Space | Activates a bubble rendered as a button. |
The component provides:
- Text that wraps and breaks long words, so a bubble never forces the page to scroll sideways.
- A visible focus ring, through
focus-visible, on a bubble rendered as a button or a link. - Layout only. The bubble sets no role, so what it contains is read as ordinary content in order.
You provide:
- The speaker. Colour and side are not enough: the sender must also be in the text around the bubble, for example in a
MessageHeader, or in the structure of a transcript with alogrole such as Message Scroller. role="img"and anaria-labelonBubbleReactionsthat hold icons, such as "Approved by Giulia". Without them the reaction is silent.- The right element when a bubble is interactive: pass
renderwith abuttonor ana, never anonClickon adiv. - Text for failures in a destructive bubble. The crimson tint is not the message.
Guidelines
When to use
- For each turn in a chat between people, or between a person and an agent.
- For quick replies the person can choose, rendered as buttons.
- For a failed turn, with the destructive variant.
When not to use
- For a notice about the page, such as a failed sync. Use an Alert.
- For a short confirmation after an action. Use a Toast.
- For a card with a title, a body and actions. Use a Card.
- For a label or a status. Use a Badge.
- For a line that marks a time or a step in the conversation, such as "Today". Use a Marker.
Content
- Write the way a colleague would say it: short sentences, the object named, no filler.
- Keep one idea per bubble, and use a group for several in a row.
- Use sentence case, and no exclamation marks or emoji.
- For an agent, say what it did and what it found, not that it was happy to help.
Variants and roles
Choose one pairing and keep it everywhere. In Silkware the person is default or secondary at the end, other people are muted at the start, and an agent is ghost. Use destructive only for a failure.
Give each role one variant and keep it for the whole conversation.
Don’t use a different variant for each message. The reader cannot tell who said what, and the colour carries no meaning.
Use the destructive variant for a failure and say what failed.
Don’t use the destructive variant for emphasis or for good news. Crimson means something went wrong.
Label a reaction that is only an icon so it is announced.
Don’t leave an icon reaction without a label. A screen reader reads nothing, and a sighted reader may not know what the mark means.
API reference
Bubble
Renders a <div> that sizes to its content, up to 80% of the row. The ghost variant has no limit. It accepts every prop of a div.
| Prop | Type | Default | Description |
|---|---|---|---|
variant? | "default" | "secondary" | "muted" | "tinted" | "outline" | "ghost" | "destructive" | "default" | The visual weight of the bubble. |
align? | "start" | "end" | "start" | The side of the row the bubble sits on. Inside a Message, the message sets it. |
className? | string | None | Classes merged after the variant classes. |
BubbleContent
Renders the <div> that holds the text, with the fill, padding and corners. With render it renders your element, such as a button or an a, and passes it the props to spread.
| Prop | Type | Default | Description |
|---|---|---|---|
render? | (props: HTMLAttributes<HTMLElement>) => ReactNode | None | A function that returns the element to render. The element receives the classes, the data slot and the children. |
children? | ReactNode | None | The message. |
className? | string | None | Classes merged after the content classes. |
BubbleReactions
Renders a <div> that overlaps an edge of the bubble, for marks and quick actions.
| Prop | Type | Default | Description |
|---|---|---|---|
side? | "top" | "bottom" | "bottom" | The edge of the bubble it sits on. |
align? | "start" | "end" | "end" | The end of that edge it sits near. |
role? | string | None | Set it to img on a reaction made of icons, with an aria-label. |
className? | string | None | Classes merged after the reaction classes. |
BubbleGroup
Renders a <div> that stacks bubbles from one sender with a small gap.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | None | Classes merged after the layout classes. |