Chat Workspace
A complete local chat journey with stop, retry, and history.
Make it yours.
Install the editable source and its dependencies into your React + Tailwind project.
Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/chat-workspace.jsonUse it
Scroll horizontally for long lines
"use client";
import { ChatWorkspace } from "@/components/jez-ui/blocks/chat-workspace";
export default function Example() {
return (
<>
<ChatWorkspace
onSend={async (prompt) =>
"Demo response: define a clear outcome for “" +
prompt +
"”, then try a small version."
}
/>
</>
);
}
Compose your own
These styled parts are included in the same install. Use children to build your layout; add className only when you want an override.
Scroll horizontally for long lines
"use client";
import {
ChatWorkspace,
ChatWorkspaceError,
ChatWorkspaceToolbar,
ChatWorkspaceMessages,
ChatWorkspaceControls,
ChatWorkspaceComposer,
ChatWorkspaceFooter,
} from "@/components/jez-ui/blocks/chat-workspace";
export default function Example() {
return (
<ChatWorkspace
onSend={async (prompt) => `Your provider response to: ${prompt}`}
>
<ChatWorkspaceError />
<ChatWorkspaceToolbar />
<ChatWorkspaceMessages />
<ChatWorkspaceControls />
<ChatWorkspaceComposer />
<ChatWorkspaceFooter />
</ChatWorkspace>
);
}
Scroll horizontally for long lines
import { ChatWorkspace, ChatWorkspaceHeader, ChatWorkspaceContent, ChatWorkspaceTitle, ChatWorkspaceForm, ChatWorkspaceDescription, ChatWorkspaceItem, ChatWorkspaceError, ChatWorkspaceToolbar, ChatWorkspaceMessages, ChatWorkspaceControls, ChatWorkspaceComposer, ChatWorkspaceFooter } from "@/components/jez-ui/blocks/chat-workspace";Props & types
Scroll horizontally for long lines
export type Message = {
id: string;
role: "user" | "assistant";
content: string;
};
export type ChatWorkspaceOptions = {
className?: string;
conversationId?: string;
onSend?: (
prompt: string,
context: { messages: Message[]; signal: AbortSignal },
) => Promise<string>;
value?: Message[];
defaultValue?: Message[];
onValueChange?: (value: Message[]) => void;
items?: typeof ChatWorkspaceDefaultItems;
heading?: React.ReactNode;
description?: React.ReactNode;
};
export type ChatWorkspaceProps = Omit<
React.ComponentProps<"section">,
keyof ChatWorkspaceOptions
> &
ChatWorkspaceOptions;
ChatWorkspacePropsSource
View 5 source files
registry/blocks/chat-workspace.tsx
Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "../ui/utils";
import {
ArrowUp,
Feather,
Lightbulb,
ListChecks,
PenLine,
RotateCcw,
} from "lucide-react";
import { Button } from "../ui/button";
import { Textarea } from "../ui/textarea";
import { useControllable } from "../ui/use-controllable";
export type Message = {
id: string;
role: "user" | "assistant";
content: string;
};
const initial: Message[] = [];
export type ChatWorkspaceOptions = {
className?: string;
conversationId?: string;
onSend?: (
prompt: string,
context: { messages: Message[]; signal: AbortSignal },
) => Promise<string>;
value?: Message[];
defaultValue?: Message[];
onValueChange?: (value: Message[]) => void;
items?: typeof ChatWorkspaceDefaultItems;
heading?: React.ReactNode;
description?: React.ReactNode;
};
export type ChatWorkspaceProps = Omit<
React.ComponentProps<"section">,
keyof ChatWorkspaceOptions
> &
ChatWorkspaceOptions;
const ChatWorkspaceDefaultItems = [
{
title: "Find the words",
prompt: "Help me explain a complicated idea clearly",
icon: PenLine,
},
{
title: "Make a plan",
prompt: "Break a website launch into practical steps",
icon: ListChecks,
},
{
title: "Think it through",
prompt: "Help me weigh the tradeoffs in a decision",
icon: Lightbulb,
},
];
function useChatWorkspaceModel({
items = ChatWorkspaceDefaultItems,
heading = "What are we working on?",
description = "Simulated responses. Conversations stay on this device.",
className,
conversationId = "default",
onSend,
value: controlledValue,
defaultValue = initial,
onValueChange,
children,
...rootProps
}: ChatWorkspaceProps) {
const [messages, setMessages] = useControllable<Message[]>(
controlledValue,
defaultValue,
onValueChange,
);
const reset = () => setMessages(defaultValue);
const [draft, setDraft] = React.useState("");
const [running, setRunning] = React.useState(false);
const request = React.useRef<AbortController | null>(null);
const [error, setError] = React.useState<string>();
React.useEffect(() => () => request.current?.abort(), []);
const previousConversation = React.useRef(conversationId);
React.useEffect(() => {
if (previousConversation.current === conversationId) return;
previousConversation.current = conversationId;
request.current?.abort();
setRunning(false);
if (controlledValue === undefined) setMessages(defaultValue);
// Changing the conversation starts a fresh local history. Controlled history belongs to the caller.
}, [conversationId]);
function stop() {
request.current?.abort();
setRunning(false);
}
async function respond(prompt: string, retry = false) {
stop();
setError(undefined);
if (!onSend) {
setError("Connect an onSend handler to receive a response.");
return;
}
const controller = new AbortController();
request.current = controller;
const history: Message[] = retry
? messages.filter(
(m, index) => index !== messages.length - 1 || m.role !== "assistant",
)
: [
...messages,
{ id: crypto.randomUUID(), role: "user", content: prompt },
];
setMessages(history);
setRunning(true);
try {
const content = await onSend(prompt, {
messages: history,
signal: controller.signal,
});
if (!controller.signal.aborted)
setMessages([
...history,
{ id: crypto.randomUUID(), role: "assistant", content },
]);
} catch (error) {
if (!controller.signal.aborted)
setError(
error instanceof Error
? error.message
: "The response failed. Please try again.",
);
} finally {
if (request.current === controller) setRunning(false);
}
}
return {
items,
heading,
description,
className,
conversationId,
onSend,
controlledValue,
defaultValue,
onValueChange,
children,
rootProps,
messages,
setMessages,
reset,
draft,
setDraft,
running,
setRunning,
request,
error,
setError,
previousConversation,
stop,
respond,
};
}
const ChatWorkspaceCompositionContext = React.createContext<ReturnType<
typeof useChatWorkspaceModel
> | null>(null);
function useChatWorkspaceComposition() {
const context = React.useContext(ChatWorkspaceCompositionContext);
if (!context)
throw new Error("ChatWorkspace parts must be inside ChatWorkspace.");
return context;
}
export function ChatWorkspace(props: ChatWorkspaceProps) {
const model = useChatWorkspaceModel(props);
const { className, rootProps, children } = model;
return (
<ChatWorkspaceCompositionContext.Provider value={model}>
<section
{...rootProps}
className={cn(
"mx-auto flex min-h-[620px] max-w-3xl flex-col gap-5",
className,
)}
>
{children !== undefined ? (
children
) : (
<>
<ChatWorkspaceError />
<ChatWorkspaceToolbar />
<ChatWorkspaceMessages />
<ChatWorkspaceControls />
<ChatWorkspaceComposer />
<ChatWorkspaceFooter />
</>
)}
</section>
</ChatWorkspaceCompositionContext.Provider>
);
}
export function ChatWorkspaceHeader({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
data-slot="chat-workspace-header"
className={cn("flex items-center justify-between gap-3", className)}
{...props}
/>
);
}
export function ChatWorkspaceContent({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
data-slot="chat-workspace-content"
className={cn("flex-1 space-y-5", className)}
{...props}
/>
);
}
export function ChatWorkspaceTitle({
className,
...props
}: React.ComponentProps<"h2">) {
return (
<h2
data-slot="chat-workspace-title"
className={cn(
"font-display text-3xl tracking-tight sm:text-4xl",
className,
)}
{...props}
/>
);
}
export function ChatWorkspaceForm({
className,
...props
}: React.ComponentProps<"form">) {
return (
<form
data-slot="chat-workspace-form"
className={cn(
"flex items-end gap-2 rounded-2xl border border-border bg-background p-2 shadow-sm focus-within:border-primary/40 focus-within:ring-3 focus-within:ring-primary/8",
className,
)}
{...props}
/>
);
}
export function ChatWorkspaceDescription({
className,
...props
}: React.ComponentProps<"p">) {
return (
<p
data-slot="chat-workspace-description"
className={cn("text-xs text-muted-foreground", className)}
{...props}
/>
);
}
export function ChatWorkspaceItem({
className,
...props
}: React.ComponentProps<"button">) {
return (
<button
type="button"
data-slot="chat-workspace-item"
className={cn(
"rounded-xl border border-border p-4 text-left transition-colors hover:border-primary/40 hover:bg-muted/40",
className,
)}
{...props}
/>
);
}
export function ChatWorkspaceError({ children }: React.PropsWithChildren) {
const { error } = useChatWorkspaceComposition();
return children === undefined
? error && <p role="alert">{error}</p>
: children;
}
export function ChatWorkspaceToolbar({
children,
...props
}: Partial<React.ComponentProps<typeof ChatWorkspaceHeader>> & {
children?: React.ReactNode;
}) {
const { reset, stop } = useChatWorkspaceComposition();
return (
<ChatWorkspaceHeader {...props}>
{children === undefined ? (
<>
<span className="flex items-center gap-2 text-sm font-medium">
<Feather size={17} />
Margin{" "}
<span className="font-normal text-muted-foreground">
/ Personal
</span>
</span>
<Button
size="sm"
variant="ghost"
onClick={() => {
stop();
reset();
}}
>
New conversation
</Button>
</>
) : (
children
)}
</ChatWorkspaceHeader>
);
}
export function ChatWorkspaceMessages({
children,
...props
}: Partial<React.ComponentProps<typeof ChatWorkspaceContent>> & {
children?: React.ReactNode;
}) {
const { items, heading, messages, setDraft, running } =
useChatWorkspaceComposition();
return (
<ChatWorkspaceContent
role="log"
aria-label="Conversation"
aria-live={running ? "off" : "polite"}
{...props}
>
{children === undefined ? (
<>
{!messages.length && (
<div className="mx-auto max-w-xl py-10 sm:py-16">
<span className="mb-6 inline-flex rounded-xl border border-border p-3">
<Feather size={25} strokeWidth={1.4} />
</span>
<ChatWorkspaceTitle>{heading}</ChatWorkspaceTitle>
<p className="mb-8 mt-3 text-sm leading-relaxed text-muted-foreground">
A rough draft. A difficult decision. The beginning of something.
</p>
<div className="grid gap-2 sm:grid-cols-3">
{items.map((item) => (
<ChatWorkspaceItem
key={item.title}
onClick={() => setDraft(item.prompt)}
>
<item.icon
size={18}
className="mb-4 text-muted-foreground"
/>
<span className="text-sm font-medium">{item.title}</span>
<span className="mt-2 block text-xs leading-relaxed text-muted-foreground">
{item.prompt}
</span>
</ChatWorkspaceItem>
))}
</div>
</div>
)}
{messages.map((m) => (
<article
key={m.id}
className={cn(
"max-w-[90%] whitespace-pre-wrap rounded-xl p-4 text-sm leading-relaxed",
m.role === "user" ? "ml-auto bg-muted" : "",
)}
>
<p className="mb-2 text-xs font-medium text-muted-foreground">
{m.role === "user" ? "You" : "Assistant"}
</p>
{m.content || "Thinking…"}
</article>
))}
</>
) : (
children
)}
</ChatWorkspaceContent>
);
}
export function ChatWorkspaceControls({ children }: React.PropsWithChildren) {
const { messages, running, stop, respond } = useChatWorkspaceComposition();
return children === undefined ? (
running ? (
<Button variant="outline" className="self-start" onClick={stop}>
Stop response
</Button>
) : (
messages.length > 0 && (
<Button
variant="ghost"
className="self-start"
onClick={() =>
respond(
[...messages].reverse().find((m) => m.role === "user")?.content ??
"Hello",
true,
)
}
>
<RotateCcw size={14} />
Retry response
</Button>
)
)
) : (
children
);
}
export function ChatWorkspaceComposer({
children,
...props
}: Partial<React.ComponentProps<typeof ChatWorkspaceForm>> & {
children?: React.ReactNode;
}) {
const { draft, setDraft, running, respond } = useChatWorkspaceComposition();
const defaultonSubmit: NonNullable<
React.ComponentProps<typeof ChatWorkspaceForm>["onSubmit"]
> = (e) => {
e.preventDefault();
if (draft.trim()) {
respond(draft.trim());
setDraft("");
}
};
return (
<ChatWorkspaceForm
{...props}
onSubmit={(event) => {
props.onSubmit?.(event);
if (!event.defaultPrevented) defaultonSubmit(event);
}}
>
{children === undefined ? (
<>
<Textarea
aria-label="Message"
placeholder="What’s on your mind?"
value={draft}
onChange={(e) => setDraft(e.target.value)}
className="min-h-20 resize-none border-0 bg-transparent shadow-none focus-visible:ring-0"
required
/>
<Button
type="submit"
aria-label="Send"
className="mb-1 mr-1 size-9 shrink-0 p-0"
disabled={running || !draft.trim()}
>
<ArrowUp size={18} />
<span className="sr-only">Send</span>
</Button>
</>
) : (
children
)}
</ChatWorkspaceForm>
);
}
export function ChatWorkspaceFooter({
children,
...props
}: Partial<React.ComponentProps<typeof ChatWorkspaceDescription>> & {
children?: React.ReactNode;
}) {
const { description } = useChatWorkspaceComposition();
return (
<ChatWorkspaceDescription {...props}>
{children === undefined ? description : children}
</ChatWorkspaceDescription>
);
}
registry/ui/utils.ts
Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
return twMerge(clsx(values));
}
registry/ui/button.tsx
Scroll horizontally for long lines
"use client";
import * as React from "react";
import { Slot } from "radix-ui";
import { LoaderCircle } from "lucide-react";
import { cn } from "./utils";
export type ButtonProps = React.ComponentProps<"button"> & {
variant?: "primary" | "secondary" | "outline" | "ghost" | "danger";
size?: "sm" | "md" | "lg";
loading?: boolean;
asChild?: boolean;
};
export function Button({
variant = "primary",
size = "md",
loading,
asChild,
className,
children,
disabled,
...props
}: ButtonProps) {
const Comp = asChild ? Slot.Root : "button";
return (
<Comp
type={asChild ? undefined : "button"}
disabled={asChild ? undefined : disabled || loading}
aria-disabled={disabled || loading || undefined}
aria-busy={loading || undefined}
className={cn(
"relative inline-flex shrink-0 items-center justify-center gap-2 whitespace-nowrap rounded-lg border border-transparent text-sm font-medium leading-none transition-[background-color,border-color,box-shadow,transform] duration-150 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary active:scale-[.98] disabled:pointer-events-none disabled:opacity-45 aria-disabled:pointer-events-none [&_svg]:shrink-0",
{
"bg-primary text-primary-foreground shadow-[inset_0_1px_0_#ffffff26,0_1px_2px_#00000012] hover:bg-primary/90":
variant === "primary",
"border-border/60 bg-muted text-foreground hover:bg-muted/70":
variant === "secondary",
"border-border bg-background text-foreground shadow-sm hover:bg-muted/60":
variant === "outline",
"text-muted-foreground hover:bg-muted hover:text-foreground":
variant === "ghost",
"bg-danger text-danger-foreground hover:bg-danger/90":
variant === "danger",
},
{
"h-8 px-3 text-xs": size === "sm",
"h-10 px-4": size === "md",
"h-12 px-6": size === "lg",
},
className,
)}
{...props}
>
{asChild ? (
children
) : (
<>
<span
className={cn(
"inline-flex min-w-0 flex-1 items-center justify-center gap-2",
loading && "invisible",
)}
>
{children}
</span>
{loading && (
<LoaderCircle
aria-hidden="true"
className="absolute size-4 animate-spin motion-reduce:animate-none"
/>
)}
</>
)}
</Comp>
);
}
registry/ui/textarea.tsx
Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
export function Textarea({
className,
...props
}: React.ComponentProps<"textarea">) {
return (
<textarea
className={cn(
"block min-h-32 w-full min-w-0 resize-y rounded-lg border border-border bg-background px-3.5 py-3 text-sm leading-relaxed shadow-[0_1px_2px_#00000004] transition-[border-color,box-shadow] placeholder:text-muted-foreground/70 hover:border-foreground/25 focus:border-primary focus:outline-none focus:ring-3 focus:ring-primary/10 aria-invalid:border-danger disabled:bg-muted/50 disabled:opacity-50",
className,
)}
{...props}
/>
);
}
registry/ui/use-controllable.ts
Scroll horizontally for long lines
"use client";
import { useRef, useState, type SetStateAction } from "react";
/** Controlled or instance-local state. Functional updates also work before a rerender. */
export function useControllable<T>(
value: T | undefined,
initial: T,
onChange?: (value: T) => void,
) {
const [local, setLocal] = useState(initial);
const current = useRef(value === undefined ? local : value);
current.current = value === undefined ? local : value;
return [
current.current,
(action: SetStateAction<T>) => {
const next =
typeof action === "function"
? (action as (previous: T) => T)(current.current)
: action;
current.current = next;
if (value === undefined) setLocal(next);
onChange?.(next);
},
] as const;
}
Dependencies
lucide-react@0.577.0 · clsx@2.1.1 · tailwind-merge@3.5.0 · radix-ui@1.6.7
Accessibility & behaviour
Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.
Read the accessibility and performance guide →