Reasoning
prompt-kit/reasoningFreeComponent
A collapsible component for showing AI reasoning, explanations, or logic. You can control it manually or let it auto-close when the stream ends. Markdown is supported.
Live preview
live · rendered by the registry
Rendered by prompt-kit on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://raw.githubusercontent.com/ibelick/prompt-kit/HEAD/public/c/reasoning.jsonSource
1"use client"23import { cn } from "@/lib/utils"4import { ChevronDownIcon } from "lucide-react"5import React, {6 createContext,7 useContext,8 useEffect,9 useRef,10 useState,11} from "react"12import { Markdown } from "./markdown"1314type ReasoningContextType = {15 isOpen: boolean16 onOpenChange: (open: boolean) => void17}1819const ReasoningContext = createContext<ReasoningContextType | undefined>(20 undefined21)2223function useReasoningContext() {24 const context = useContext(ReasoningContext)25 if (!context) {26 throw new Error(27 "useReasoningContext must be used within a Reasoning provider"28 )29 }30 return context31}3233export type ReasoningProps = {34 children: React.ReactNode35 className?: string36 open?: boolean37 onOpenChange?: (open: boolean) => void38 isStreaming?: boolean39}40function Reasoning({41 children,42 className,43 open,44 onOpenChange,45 isStreaming,46}: ReasoningProps) {47 const [internalOpen, setInternalOpen] = useState(false)48 const [wasAutoOpened, setWasAutoOpened] = useState(false)4950 const isControlled = open !== undefined51 const isOpen = isControlled ? open : internalOpen5253 const handleOpenChange = (newOpen: boolean) => {54 if (!isControlled) {55 setInternalOpen(newOpen)56 }57 onOpenChange?.(newOpen)58 }5960 useEffect(() => {61 if (isStreaming && !wasAutoOpened) {62 if (!isControlled) setInternalOpen(true)63 setWasAutoOpened(true)64 }6566 if (!isStreaming && wasAutoOpened) {67 if (!isControlled) setInternalOpen(false)68 setWasAutoOpened(false)69 }70 }, [isStreaming, wasAutoOpened, isControlled])7172 return (73 <ReasoningContext.Provider74 value={{75 isOpen,76 onOpenChange: handleOpenChange,77 }}78 >79 <div className={className}>{children}</div>80 </ReasoningContext.Provider>81 )82}8384export type ReasoningTriggerProps = {85 children: React.ReactNode86 className?: string87} & React.HTMLAttributes<HTMLButtonElement>8889function ReasoningTrigger({90 children,91 className,92 ...props93}: ReasoningTriggerProps) {94 const { isOpen, onOpenChange } = useReasoningContext()9596 return (97 <button98 className={cn("flex cursor-pointer items-center gap-2", className)}99 onClick={() => onOpenChange(!isOpen)}100 {...props}101 >102 <span className="text-primary">{children}</span>103 <div104 className={cn(105 "transform transition-transform",106 isOpen ? "rotate-180" : ""107 )}108 >109 <ChevronDownIcon className="size-4" />110 </div>111// … truncated
What it pulls in
npm packages
Files it writes
More from prompt-kit
All 23 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Chain Of Thoughtchain-of-thought | prompt-kit | Components | Free | 1 dep | |
| Chat Containerchat-container | prompt-kit | Components | Free | 1 dep | |
| Code Blockcode-block | prompt-kit | Components | Free | 1 dep | |
| Feedback Barfeedback-bar | prompt-kit | Components | Free | 1 dep | |
| File Uploadfile-upload | prompt-kit | Components | Free | no deps | |
| Imageimage | prompt-kit | Components | Free | no deps | |
| Jsx Previewjsx-preview | prompt-kit | Components | Free | 1 dep | |
| Loaderloader | prompt-kit | Components | Free | no deps |
