Ai Branch
smoothui-registry/ai-branchFreeComponent
An interactive AI branch component for displaying conversation flows.
Install it
npx shadcn@latest add https://www.smoothui.dev/r/ai-branch.jsonSource
1"use client";23import { cn } from "@/lib/utils";4import { ChevronLeftIcon, ChevronRightIcon, Copy, Pencil } from "lucide-react";5import { motion, useReducedMotion } from "motion/react";6import type { HTMLAttributes, ReactElement, ReactNode } from "react";7import { createContext, useContext, useEffect, useMemo, useState } from "react";89interface AIBranchContextType {10 branches: ReactElement[];11 currentBranch: number;12 goToNext: () => void;13 goToPrevious: () => void;14 setBranches: (branches: ReactElement[]) => void;15 totalBranches: number;16}1718/**19 * The same two springs every other AI component uses.20 *21 * This file predates that vocabulary and had eight hand-tuned22 * `stiffness`/`damping` pairs — several of which also passed `duration`, which23 * Motion ignores once stiffness is present. Sharing the tokens is what stops24 * `ai-branch` feeling like it came from a different library than `ai-message`.25 */26const SPRING_DEFAULT = {27 bounce: 0.1,28 duration: 0.25,29 type: "spring" as const,30};31const SPRING_SNAPPY = {32 bounce: 0,33 duration: 0.2,34 type: "spring" as const,35};3637const AIBranchContext = createContext<AIBranchContextType | null>(null);3839const useAIBranch = () => {40 const context = useContext(AIBranchContext);41 if (!context) {42 throw new Error("AIBranch components must be used within AIBranch");43 }44 return context;45};4647export type AIBranchProps = HTMLAttributes<HTMLDivElement> & {48 defaultBranch?: number;49 onBranchChange?: (branchIndex: number) => void;50};5152export const AIBranch = ({53 defaultBranch = 0,54 onBranchChange,55 className,56 ...props57}: AIBranchProps) => {58 const [currentBranch, setCurrentBranch] = useState(defaultBranch);59 const [branches, setBranches] = useState<ReactElement[]>([]);6061 const handleBranchChange = (newBranch: number) => {62 setCurrentBranch(newBranch);63 onBranchChange?.(newBranch);64 };6566 const goToPrevious = () => {67 const newBranch =68 currentBranch > 0 ? currentBranch - 1 : branches.length - 1;69 handleBranchChange(newBranch);70 };7172 const goToNext = () => {73 const newBranch =74 currentBranch < branches.length - 1 ? currentBranch + 1 : 0;75 handleBranchChange(newBranch);76 };7778 const contextValue: AIBranchContextType = {79 branches,80 currentBranch,81 goToNext,82 goToPrevious,83 setBranches,84 totalBranches: branches.length,85 };8687 return (88 <AIBranchContext.Provider value={contextValue}>89 <div90// … truncated
What it pulls in
npm packages
Files it writes
More from SmoothUI Registry
All 178 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Agent Avataragent-avatar | SmoothUI Registry | Components | Free | no deps | |
| Ai Approvalai-approval | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Artifactai-artifact | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Citationai-citation | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Context Meterai-context-meter | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Conversationai-conversation | SmoothUI Registry | Components | Free | 2 deps | |
| Ai Coreai-core | SmoothUI Registry | Components | Free | 1 dep | |
| Ai Diffai-diff | SmoothUI Registry | Components | Free | 2 deps |
