Can the conversation scroll beneath the composer?
Kits
ChatLayout
A scrolling conversation with a pinned header, message turns, and a floating composer dock.
Examples
Custom jump control
useChatThread reads whether the thread is at the bottom and scrolls it there, so any control inside ChatThread can replace the built-in scroll button.
Composition
Conversation and composer
- <ChatLayout>
- <ChatThread>
- <ChatTurn>
- <ChatMessage />
- <Activity>
- <ActivityTrigger>
- <ActivityTitle />
- <ActivityContent>
- reasoning content
- <ActivityTrigger>
- <ChatThreadScrollButton />
- <ChatComposer />
- <ChatTurn>
- <ChatThread>
Channel header
- <ChatLayout>
- <ChatLayoutHeader>
- leading trigger
- <ChatLayoutTitle />
- <ChatLayoutDescription />
- <ChatLayoutActions />
- <ChatThread />
- <ChatLayoutHeader>
Installation
Use this command to install the component.
npx shadcn@latest add https://control-ui.dev/r/chat-layout.jsonUsage
import type { ReactNode } from "react";
import { ChatLayout, ChatThread, ChatTurn } from "@/components/control-ui/chat-layout";
export function Example({ children }: { children: ReactNode }) { return ( <ChatLayout> <ChatThread> <ChatTurn from="assistant">{children}</ChatTurn> </ChatThread> </ChatLayout> );}
Dependencies
Use chat thread scroll
src/registry/hooks/use-chat-thread-scroll.ts
Hook
Motion
src/registry/lib/motion.ts
Util
Button
button
Component
ChatMessage
chat-message
Kit
Live status
live-status
Component
Scroll area
scroll-area
Component
Raw code
Installed component source and support files
"use client";
import { useRender } from "@base-ui/react/use-render";import { ArrowDown } from "lucide-react";import type { ComponentProps, CSSProperties, MouseEvent, ReactNode, RefObject } from "react";import { createContext, useContext, useState } from "react";import { createPortal } from "react-dom";import type { RenderProp } from "@/components/control-ui/control-props";import { ChatThreadAnnounceContext, ChatThreadDockContext } from "@/components/control-ui/hooks/use-chat-message";import { useChatThreadScroll } from "@/components/control-ui/hooks/use-chat-thread-scroll";import type { ChatLayoutKnobStyle } from "@/components/control-ui/knob-contracts/chat-layout-knobs";import { cn } from "@/components/control-ui/lib/cn";import { SkinAdornment, useSkinIcon } from "@/components/control-ui/skin-provider";import { Button } from "@/components/control-ui/ui/button";import { LiveStatus } from "@/components/control-ui/ui/live-status";import { ScrollArea } from "@/components/control-ui/ui/scroll-area";
export type ChatLayoutChrome = "panel" | "embedded";
export type ChatLayoutProps = Omit<ComponentProps<"section">, "style"> & { chrome?: ChatLayoutChrome; style?: CSSProperties & ChatLayoutKnobStyle;};
const chromeSizing: Record<ChatLayoutChrome, string> = { panel: "mx-auto min-h-[640px] max-w-3xl", embedded: "h-full min-h-0 flex-1",};
export function ChatLayout({ children, chrome = "panel", className, ...props }: ChatLayoutProps) { return ( <section data-control-ui="chat-layout" data-control-family="chat-layout" data-slot="root" data-chrome={chrome} data-surface={chrome === "panel" ? "panel" : undefined} className={cn("relative flex w-full flex-col overflow-hidden", chromeSizing[chrome], className)} {...props} > {chrome === "panel" ? <SkinAdornment scope="chat-layout" part="titlebar" context={{}} /> : null} {children} </section> );}
export type ChatLayoutHeaderProps = Omit<ComponentProps<"header">, "style"> & { style?: CSSProperties & ChatLayoutKnobStyle };
export function ChatLayoutHeader({ className, ...props }: ChatLayoutHeaderProps) { return ( <header data-control-ui="chat-layout" data-control-family="chat-layout" data-slot="header" className={cn("grid shrink-0 grid-cols-[auto_minmax(0,1fr)_auto] items-center", className)} {...props} /> );}
export type ChatLayoutTitleProps = Omit<ComponentProps<"div">, "style"> & { render?: RenderProp<ComponentProps<"div">>; style?: CSSProperties & ChatLayoutKnobStyle;};
/** Renders a `div`; pass `render={(props) => <h2 {...props} />}` for the heading level the page needs. */export function ChatLayoutTitle({ render, className, ...props }: ChatLayoutTitleProps) { return useRender({ defaultTagName: "div", render, props: { ...props, className: cn("col-start-2 min-w-0 truncate", className), "data-control-ui": "chat-layout", "data-control-family": "chat-layout", "data-slot": "title", }, });}
export type ChatLayoutDescriptionProps = Omit<ComponentProps<"p">, "style"> & { style?: CSSProperties & ChatLayoutKnobStyle };
export function ChatLayoutDescription({ className, ...props }: ChatLayoutDescriptionProps) { return ( <p data-control-ui="chat-layout" data-control-family="chat-layout" data-slot="description" className={cn("col-start-2 truncate", className)} {...props} /> );}
export type ChatLayoutActionsProps = Omit<ComponentProps<"div">, "style"> & { style?: CSSProperties & ChatLayoutKnobStyle };
export function ChatLayoutActions({ className, ...props }: ChatLayoutActionsProps) { return ( <div data-control-ui="chat-layout" data-control-family="chat-layout" data-slot="actions" className={cn("col-start-3 row-span-2 row-start-1 flex items-center", className)} {...props} /> );}
type ChatThreadScrollContextValue = { atBottom: boolean; scrollToBottom: (behavior?: ScrollBehavior) => void; viewportRef: RefObject<HTMLDivElement | null>; anchorLayer: HTMLDivElement | null;};
const ChatThreadScrollContext = createContext<ChatThreadScrollContextValue | null>(null);
/** Scroll state of the surrounding `ChatThread`, for a custom jump control or an unread marker. */export function useChatThread(): Pick<ChatThreadScrollContextValue, "atBottom" | "scrollToBottom"> { const scroll = useContext(ChatThreadScrollContext); if (!scroll) throw new Error("useChatThread() must be called inside <ChatThread>."); return { atBottom: scroll.atBottom, scrollToBottom: scroll.scrollToBottom };}
export type ChatThreadProps = ComponentProps<"div"> & { composer?: ReactNode;};
export function ChatThread({ children, composer, className, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledby, ...props}: ChatThreadProps) { const { rootRef, viewportRef, contentRef, dockRef, atBottom, scrollToBottom, followFromDock } = useChatThreadScroll(); const [anchorLayer, setAnchorLayer] = useState<HTMLDivElement | null>(null); const [announcement, setAnnouncement] = useState(""); const [announce] = useState(() => (message: string) => { // A trailing no-break space makes a repeated message a real text change, so it is read again. setAnnouncement((current) => (current === message ? `${message}\u00a0` : message)); });
return ( <ChatThreadAnnounceContext.Provider value={announce}> <ChatThreadScrollContext.Provider value={{ atBottom, scrollToBottom, viewportRef, anchorLayer }}> <div ref={rootRef} data-control-ui="chat-thread" data-control-family="chat-layout" data-chat-layout-kind="thread" data-slot="root" data-at-bottom={atBottom ? "" : undefined} className={cn("relative flex min-h-0 min-w-0 flex-1 flex-col", className)} {...props} > {/* The viewport is the focusable scroller, so a name belongs there (as a region), not on the role-less root. */} <ScrollArea aria-label={ariaLabel ?? (ariaLabelledby ? undefined : "Conversation")} aria-labelledby={ariaLabelledby} className="min-h-0 flex-1" contentClassName="flex min-h-full flex-col" viewportRef={viewportRef} lockAxis="x" blur={false} > <div ref={contentRef} data-control-ui="chat-thread" data-control-family="chat-layout" data-slot="track" className="relative flex min-w-0 flex-1 flex-col" > <div data-control-ui="chat-thread" data-control-family="chat-layout" data-slot="thread-content"> {children} </div> {composer ? ( <div ref={dockRef} data-control-ui="chat-thread" data-control-family="chat-layout" data-slot="dock" data-scroll-area-pinned="" className="sticky bottom-0 z-1 shrink-0" > <ChatThreadDockContext.Provider value={followFromDock}>{composer}</ChatThreadDockContext.Provider> </div> ) : null} </div> </ScrollArea> <div ref={setAnchorLayer} data-control-ui="chat-thread" data-control-family="chat-layout" data-slot="anchor-layer" /> <LiveStatus message={announcement} /> </div> </ChatThreadScrollContext.Provider> </ChatThreadAnnounceContext.Provider> );}
const PEN_STROKE = "M2 10.5c1.6-4.2 3.4-4.2 3.6-1.4.2 2.6 1.5 3.4 3-.2 1.4-3.5 3-4.4 3.5-1.6.4 2.4 1.6 3.2 2.9.6.8-1.6 1.7-2.6 3-2.9";
export type ChatThreadScrollButtonProps = Omit<ComponentProps<typeof Button>, "children"> & { label?: string; busy?: boolean;};
export function ChatThreadScrollButton({ label = "Scroll to latest", busy = false, variant = "surface", size = "sm", shape = "circle", onClick, ...props}: ChatThreadScrollButtonProps) { const ArrowDownGlyph = useSkinIcon("arrowDown", ArrowDown); const scroll = useContext(ChatThreadScrollContext); if (!scroll) throw new Error("<ChatThreadScrollButton> must be rendered inside <ChatThread>.");
function handleClick(event: MouseEvent<HTMLButtonElement>) { onClick?.(event); if (event.defaultPrevented || !scroll) return; scroll.scrollToBottom(); // The button hides once the thread is at the bottom; hand its focus to the scroller instead of dropping it on <body>. if (document.activeElement === event.currentTarget) scroll.viewportRef.current?.focus({ preventScroll: true }); }
if (!scroll.anchorLayer) return null; return createPortal( <div data-control-ui="chat-thread" data-control-family="chat-layout" data-slot="scroll-anchor" data-hidden={scroll.atBottom ? "" : undefined} data-busy={busy ? "" : undefined} > <Button {...props} type="button" variant={variant} size={size} shape={shape} iconOnly aria-label={label} onClick={handleClick}> <span data-control-family="chat-layout" data-slot="scroll-glyph" aria-hidden="true"> <ArrowDownGlyph data-control-family="chat-layout" data-slot="scroll-arrow" /> <svg data-control-family="chat-layout" data-slot="scroll-writing" viewBox="0 0 20 16" fill="none" aria-hidden="true"> <path pathLength={1} d={PEN_STROKE} /> </svg> </span> </Button> </div>, scroll.anchorLayer, );}
export type ChatTurnProps = ComponentProps<"section"> & { from: "user" | "assistant";};
export function ChatTurn({ from, children, className, ...props }: ChatTurnProps) { return ( <section data-control-ui="chat-turn" data-control-family="chat-layout" data-slot="turn" data-from={from} className={cn("flex w-full flex-col", from === "user" && "items-end", className)} {...props} > {children} </section> );}
Knobs
Typed custom properties the recipe paints with. Set one on the root — style, a utility class, or a skin — and every slot inherits it.
| Knob | Syntax | Default |
|---|---|---|
--cui-chat-layout-radius | <length-percentage> | var(--radius-scene) |
--cui-chat-layout-background | <color> | var(--background) |
--cui-chat-layout-border-color | <color> | var(--border) |
--cui-chat-layout-border-width | <length> | var(--control-rim-width) |
--cui-chat-layout-shadow | * | var(--shadow-md) |
--cui-chat-layout-gutter-size | <length-percentage> | calc(var(--spacing) * 4) |
--cui-chat-layout-column-max-inline-size | <length-percentage> | 48rem |
--cui-chat-layout-veil-background | <color> | var(--cui-chat-layout-background) |
--cui-chat-layout-veil-backdrop-filter | * | none |
--cui-chat-layout-scroll-button-gap | <length-percentage> | calc(var(--spacing) * 3) |
--cui-chat-layout-turn-gap | <length-percentage> | calc(var(--spacing) * 2) |