Bubble
A message's frame: a rounded box drawn round what is said, shaded solid, hatched, washed or just pencilled, in the ink or the red pen.
import { Bubble, BubbleContent, BubbleGroup } from "@/components/ui/bubble";
export default function BubbleDemo() {
return (
<div className="flex w-full max-w-md flex-col gap-3">
<Bubble variant="secondary">
<BubbleContent seed="bubble-a">How can I help you today?</BubbleContent>
</Bubble>
<Bubble align="end">
<BubbleContent seed="bubble-b">Draw me a ballpoint pen.</BubbleContent>
</Bubble>
<BubbleGroup>
<Bubble variant="secondary">
<BubbleContent seed="bubble-c">Here is one.</BubbleContent>
</Bubble>
<Bubble variant="secondary">
<BubbleContent seed="bubble-d">I hatched the shadow lightly.</BubbleContent>
</Bubble>
</BubbleGroup>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ bubble"use client";
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { Children, createContext, useContext, useId, useMemo, type ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { penRadius, usePen, type Pen } from "@/hooks/use-ink-box";
import { Stroke } from "@/lib/ink";
import { InkOutline } from "@/lib/ink-outline";
import { hatchStrokes, roundedRectPath } from "@/lib/ink-sketch";
type Variant = "default" | "secondary" | "muted" | "tinted" | "outline" | "ghost" | "destructive";
const BubbleContext = createContext<Variant>("default");
// How each kind of bubble is coloured in, and what its words are written in.
const looks: Record<Variant, { ink: string; text: string; wash?: { gap: number; opacity: number } | "flat" | "solid" }> = {
default: { ink: "text-primary", text: "text-primary-foreground", wash: "solid" },
secondary: { ink: "text-ink-line", text: "text-foreground", wash: { gap: 5.5, opacity: 0.35 } },
muted: { ink: "text-ink-4", text: "text-ink-2", wash: { gap: 8, opacity: 0.25 } },
tinted: { ink: "text-ink-3", text: "text-foreground", wash: "flat" },
outline: { ink: "text-ink-line", text: "text-foreground" },
ghost: { ink: "", text: "text-foreground" },
destructive: { ink: "text-destructive", text: "text-destructive", wash: { gap: 6, opacity: 0.35 } },
};
/**
* A message's frame: a rounded box drawn round what is said. `default` is
* shaded solid (the current user), `secondary` hatched, `muted` hatched
* lightly, `tinted` washed, `outline` just pencilled, `ghost` unframed and
* `destructive` in red pen. `align="end"` sets it against the other edge.
*/
function Bubble({ className, variant = "default", align = "start", ...props }: ComponentProps<"div"> & { variant?: Variant; align?: "start" | "end" }) {
return (
<BubbleContext value={variant}>
<div
data-slot="bubble"
data-variant={variant}
data-align={align}
className={cn(
"group/bubble relative flex w-fit max-w-[85%] min-w-0 flex-col text-base data-[align=end]:items-end data-[align=end]:self-end data-[variant=ghost]:w-full data-[variant=ghost]:max-w-full",
className,
)}
{...props}
/>
</BubbleContext>
);
}
/** The surface the words sit on. Render it as a link or a button with `render`. */
function BubbleContent({
className,
render,
children,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: useRender.ComponentProps<"div"> & Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & { seed?: string | number }) {
const variant = useContext(BubbleContext);
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
const look = looks[variant];
return useRender({
defaultTagName: "div",
props: mergeProps<"div">(
{
className: cn(
"relative isolate min-w-0 px-4 py-2.5 text-left break-words whitespace-pre-wrap outline-none",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-3 focus-visible:outline-ring focus-visible:[border-radius:var(--hand-radius)]",
variant === "ghost" && "px-0",
look.text,
className,
),
children: (
<>
{variant !== "ghost" && (
<InkOutline pen={pen} seed={seed} estimate={[240, 44]} maxRadius={20} className={cn("-z-10", look.ink)}>
{({ w, h, r, s }) => look.wash && <BubbleWash look={look.wash} w={w} h={h} r={r} s={s} draw={pen.draw} />}
</InkOutline>
)}
{children}
</>
),
} as ComponentProps<"div">,
props,
),
render,
state: { slot: "bubble-content", variant },
});
}
/** What a bubble is coloured in with, kept inside its box. */
function BubbleWash({ look, w, h, r, s, draw }: { look: NonNullable<(typeof looks)[Variant]["wash"]>; w: number; h: number; r: number; s: number; draw: Pen["draw"] }) {
const id = useId().replace(/[^\w-]/g, "");
const body = useMemo(() => roundedRectPath(w, h, r), [w, h, r]);
const gap = look === "solid" ? 3 : typeof look === "object" ? look.gap : 0;
const hatch = useMemo(() => (gap ? hatchStrokes(s + 30, w, h, { gap, angle: -50, jitter: 0.5, inset: 1.5 }).join("") : ""), [gap, s, w, h]);
return (
<>
<defs>
<clipPath id={`${id}-clip`}>
<path d={body} />
</clipPath>
</defs>
{look === "solid" && <path d={body} className="ink-fill" />}
{look === "flat" && <path d={body} className="ink-fill" style={{ opacity: 0.1 }} />}
{hatch && (
<g clipPath={`url(#${id}-clip)`}>
<Stroke d={hatch} draw={draw ?? "auto"} delay={200} duration={560} width={0.9} opacity={look === "solid" ? 0.5 : typeof look === "object" ? look.opacity : 0.3} />
</g>
)}
</>
);
}
/** Reactions pinned to the edge of the bubble, each a small pencilled pill. */
function BubbleReactions({ className, children, side = "bottom", align = "end", ...props }: ComponentProps<"div"> & { side?: "top" | "bottom"; align?: "start" | "end" }) {
return (
<div
data-slot="bubble-reactions"
data-side={side}
data-align={align}
className={cn(
"absolute z-10 flex -space-x-1 data-[align=end]:right-3 data-[align=start]:left-3 data-[side=bottom]:top-full data-[side=bottom]:-translate-y-1/3 data-[side=top]:bottom-full data-[side=top]:translate-y-1/3",
className,
)}
{...props}
>
{Children.map(children, (child) => (
<Reaction>{child}</Reaction>
))}
</div>
);
}
function Reaction({ children }: { children: React.ReactNode }) {
const pen = usePen({ passes: 1, corners: "joined" });
return (
<span className="ink-paper relative isolate flex h-6 min-w-6 items-center justify-center rounded-(--pen-r) px-1.5 text-sm text-foreground" style={{ "--pen-r": `${penRadius(pen, 12)}px` } as React.CSSProperties}>
<InkOutline pen={pen} estimate={[28, 24]} pad={3} className="text-ink-line" />
{children}
</span>
);
}
/** Bubbles from the same sender, set close together. */
function BubbleGroup({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="bubble-group" className={cn("flex min-w-0 flex-col gap-1.5 [&>[data-align=end]]:self-end", className)} {...props} />;
}
export { Bubble, BubbleContent, BubbleReactions, BubbleGroup };Usage
import { Bubble, BubbleContent } from "@/components/ui/bubble"
<Bubble variant="secondary">
<BubbleContent>How can I help you today?</BubbleContent>
</Bubble>Examples
Variants
Seven ways to colour it in, from the solid bubble for the current user to unframed text.
import { Bubble, BubbleContent } from "@/components/ui/bubble";
const variants = ["default", "secondary", "muted", "tinted", "outline", "ghost", "destructive"] as const;
export default function BubbleVariants() {
return (
<div className="flex w-full max-w-md flex-col gap-3">
{variants.map((variant) => (
<Bubble key={variant} variant={variant} align={variant === "default" ? "end" : "start"}>
<BubbleContent seed={`bv-${variant}`}>{variant === "destructive" ? "That page didn't save." : `A ${variant} bubble.`}</BubbleContent>
</Bubble>
))}
</div>
);
}Reactions
Small pencilled chips pinned to the edge, on the bottom or the top.
import { Bubble, BubbleContent, BubbleReactions } from "@/components/ui/bubble";
export default function BubbleReactionsExample() {
return (
<div className="flex w-full max-w-md flex-col gap-6 pb-3">
<Bubble variant="secondary">
<BubbleContent seed="br-a">I checked the registry output and removed the stale route.</BubbleContent>
<BubbleReactions>
<span>👍</span>
<span>2</span>
</BubbleReactions>
</Bubble>
<Bubble align="end">
<BubbleContent seed="br-b">Lovely, thank you.</BubbleContent>
<BubbleReactions side="top" align="start">
<span>❤️</span>
</BubbleReactions>
</Bubble>
</div>
);
}As a link or a button
Render the surface as another element with `render`.
import { Bubble, BubbleContent } from "@/components/ui/bubble";
export default function BubbleLink() {
return (
<div className="flex w-full max-w-md flex-col gap-3">
<Bubble variant="outline">
<BubbleContent render={<a href="#" />} seed="bl-a" className="underline decoration-ink-4 underline-offset-4 hover:decoration-ink">
Read the full registry notes
</BubbleContent>
</Bubble>
<Bubble variant="tinted">
<BubbleContent render={<button type="button" />} seed="bl-b" className="cursor-pointer">
Try a different pen
</BubbleContent>
</Bubble>
</div>
);
}API reference
- Bubble variantdefault
"default" "default" | "secondary" | "muted" | "tinted" | "outline" | "ghost" | "destructive"Solid ink, hatched, lightly hatched, washed, pencilled, unframed, or red pen.
- Bubble aligndefault
"start" "start" | "end"Which edge of the conversation it sits against.
- BubbleContent render
ReactElementRender the surface as a link or a button.
- BubbleReactions side / aligndefault
"bottom" / "end" "top" | "bottom" / "start" | "end"Where on the edge the reactions sit.
- BubbleGroup
divGroups consecutive bubbles from one sender.
- seed
string | numberPins the drawing.
Every other prop goes to Base UI's Use Render.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.