The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(ui): forward refs through ui primitives and fail tests on swallowed refs (#32401)
ryan-crabbe-berri7d63b86e00cf
diff --git a/ui/litellm-dashboard/src/components/ui/label.tsx b/ui/litellm-dashboard/src/components/ui/label.tsxindex ded2dfc1a7b..1ac4eed0d4e 100644--- a/ui/litellm-dashboard/src/components/ui/label.tsx+++ b/ui/litellm-dashboard/src/components/ui/label.tsx@@ -4,9 +4,10 @@ import * as React from "react";import { cn } from "@/lib/cva.config";-function Label({ className, ...props }: React.ComponentProps<"label">) {- return (+const Label = React.forwardRef<HTMLLabelElement, React.ComponentPropsWithoutRef<"label">>(+ ({ className, ...props }, ref) => (<label+ ref={ref}data-slot="label"className={cn("flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50",@@ -14,7 +15,8 @@ function Label({ className, ...props }: React.ComponentProps<"label">) {)}{...props}/>- );-}+ ),+);+Label.displayName = "Label";export { Label };diff --git a/ui/litellm-dashboard/src/components/ui/ref-forwarding.test.tsx b/ui/litellm-dashboard/src/components/ui/ref-forwarding.test.tsxnew file mode 100644index 00000000000..e01fc00bb0a--- /dev/null+++ b/ui/litellm-dashboard/src/components/ui/ref-forwarding.test.tsx@@ -0,0 +1,103 @@+import { render } from "@testing-library/react";+import * as React from "react";+import { describe, expect, it } from "vitest";++import { Button } from "./button";+import { Input } from "./input";+import { Label } from "./label";+import { Separator } from "./separator";+import { Skeleton } from "./skeleton";+import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from "./table";+import { UiLoadingSpinner } from "./ui-loading-spinner";++describe("ui primitives forward refs to their DOM node", () => {+ it("Button", () => {+ const ref = React.createRef<HTMLButtonElement>();+ render(<Button ref={ref}>ok</Button>);+ expect(ref.current).toBeInstanceOf(HTMLButtonElement);+ });++ it("Input", () => {+ const ref = React.createRef<HTMLInputElement>();+ render(<Input ref={ref} />);+ expect(ref.current).toBeInstanceOf(HTMLInputElement);+ });++ it("Label", () => {+ const ref = React.createRef<HTMLLabelElement>();+ render(<Label ref={ref}>ok</Label>);+ expect(ref.current).toBeInstanceOf(HTMLLabelElement);+ });++ it("Separator", () => {+ const ref = React.createRef<HTMLDivElement>();+ render(<Separator ref={ref} />);+ expect(ref.current).toBeInstanceOf(HTMLElement);+ });++ it("Skeleton", () => {+ const ref = React.createRef<HTMLDivElement>();+ render(<Skeleton ref={ref} />);+ expect(ref.current).toBeInstanceOf(HTMLDivElement);+ });++ it("UiLoadingSpinner", () => {+ const ref = React.createRef<SVGSVGElement>();+ render(<UiLoadingSpinner ref={ref} />);+ expect(ref.current).toBeInstanceOf(SVGSVGElement);+ });++ it("Table family", () => {+ const table = React.createRef<HTMLTableElement>();+ const caption = React.createRef<HTMLTableCaptionElement>();+ const header = React.createRef<HTMLTableSectionElement>();+ const body = React.createRef<HTMLTableSectionElement>();+ const footer = React.createRef<HTMLTableSectionElement>();+ const row = React.createRef<HTMLTableRowElement>();+ const head = React.createRef<HTMLTableCellElement>();+ const cell = React.createRef<HTMLTableCellElement>();++ render(+ <Table ref={table}>+ <TableCaption ref={caption}>caption</TableCaption>+ <TableHeader ref={header}>+ <TableRow ref={row}>+ <TableHead ref={head}>h</TableHead>+ </TableRow>+ </TableHeader>+ <TableBody ref={body}>+ <TableRow>+ <TableCell ref={cell}>d</TableCell>+ </TableRow>+ </TableBody>+ <TableFooter ref={footer}>+ <TableRow>+ <TableCell>f</TableCell>+ </TableRow>+ </TableFooter>+ </Table>,+ );++ expect(table.current).toBeInstanceOf(HTMLTableElement);+ expect(caption.current).toBeInstanceOf(HTMLTableCaptionElement);+ expect(header.current?.tagName).toBe("THEAD");+ expect(body.current?.tagName).toBe("TBODY");+ expect(footer.current?.tagName).toBe("TFOOT");+ expect(row.current).toBeInstanceOf(HTMLTableRowElement);+ expect(head.current?.tagName).toBe("TH");+ expect(cell.current?.tagName).toBe("TD");+ });+});++describe("setupTests ref tripwire", () => {+ it("records a violation when a ref is passed to a plain function component", () => {+ const Plain = (props: React.ComponentPropsWithoutRef<"span">) => <span {...props} />;+ const ref = React.createRef<HTMLSpanElement>();+ render(React.createElement(Plain as never, { ref }));+ const consume = (globalThis as { __consumePendingRefWarnings?: () => string[] }).__consumePendingRefWarnings;+ expect(consume).toBeDefined();+ const violations = consume!();+ expect(violations).toHaveLength(1);+ expect(violations[0]).toContain("Function components cannot be given refs");+ });+});diff --git a/ui/litellm-dashboard/src/components/ui/separator.tsx b/ui/litellm-dashboard/src/components/ui/separator.tsxindex 443f8e905f9..a8a8d9cf5c2 100644--- a/ui/litellm-dashboard/src/components/ui/separator.tsx+++ b/ui/litellm-dashboard/src/components/ui/separator.tsx@@ -1,12 +1,14 @@"use client";import { Separator as SeparatorPrimitive } from "@base-ui/react/separator";+import * as React from "react";import { cn } from "@/lib/cva.config";-function Separator({ className, orientation = "horizontal", ...props }: SeparatorPrimitive.Props) {- return (+const Separator = React.forwardRef<React.ComponentRef<typeof SeparatorPrimitive>, SeparatorPrimitive.Props>(+ ({ className, orientation = "horizontal", ...props }, ref) => (<SeparatorPrimitive+ ref={ref}data-slot="separator"orientation={orientation}className={cn(@@ -15,7 +17,8 @@ function Separator({ className, orientation = "horizontal", ...props }: Separato)}{...props}/>- );-}+ ),+);+Separator.displayName = "Separator";export { Separator };diff --git a/ui/litellm-dashboard/src/components/ui/skeleton.tsx b/ui/litellm-dashboard/src/components/ui/skeleton.tsxindex e27145708a2..69ff4891cec 100644--- a/ui/litellm-dashboard/src/components/ui/skeleton.tsx+++ b/ui/litellm-dashboard/src/components/ui/skeleton.tsx@@ -1,7 +1,12 @@+import * as React from "react";+import { cn } from "@/lib/cva.config";-function Skeleton({ className, ...props }: React.ComponentProps<"div">) {- return <div data-slot="skeleton" className={cn("animate-pulse rounded-md bg-accent", className)} {...props} />;-}+const Skeleton = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<"div">>(+ ({ className, ...props }, ref) => (+ <div ref={ref} data-slot="skeleton" className={cn("animate-pulse rounded-md bg-accent", className)} {...props} />+ ),+);+Skeleton.displayName = "Skeleton";export { Skeleton };diff --git a/ui/litellm-dashboard/src/components/ui/table.tsx b/ui/litellm-dashboard/src/components/ui/table.tsxindex aff687f432d..6271a9e89ac 100644--- a/ui/litellm-dashboard/src/components/ui/table.tsx+++ b/ui/litellm-dashboard/src/components/ui/table.tsx@@ -4,35 +4,45 @@ import * as React from "react";import { cn } from "@/lib/cva.config";-function Table({ className, ...props }: React.ComponentProps<"table">) {- return (+const Table = React.forwardRef<HTMLTableElement, React.ComponentPropsWithoutRef<"table">>(+ ({ className, ...props }, ref) => (<div data-slot="table-container" className="relative w-full overflow-x-auto">- <table data-slot="table" className={cn("w-full caption-bottom text-sm", className)} {...props} />+ <table ref={ref} data-slot="table" className={cn("w-full caption-bottom text-sm", className)} {...props} /></div>- );-}+ ),+);+Table.displayName = "Table";-function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {- return <thead data-slot="table-header" className={cn("[&_tr]:border-b", className)} {...props} />;-}+const TableHeader = React.forwardRef<HTMLTableSectionElement, React.ComponentPropsWithoutRef<"thead">>(+ ({ className, ...props }, ref) => (+ <thead ref={ref} data-slot="table-header" className={cn("[&_tr]:border-b", className)} {...props} />+ ),+);+TableHeader.displayName = "TableHeader";-function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {- return <tbody data-slot="table-body" className={cn("[&_tr:last-child]:border-0", className)} {...props} />;-}+const TableBody = React.forwardRef<HTMLTableSectionElement, React.ComponentPropsWithoutRef<"tbody">>(+ ({ className, ...props }, ref) => (+ <tbody ref={ref} data-slot="table-body" className={cn("[&_tr:last-child]:border-0", className)} {...props} />+ ),+);+TableBody.displayName = "TableBody";-function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {- return (+const TableFooter = React.forwardRef<HTMLTableSectionElement, React.ComponentPropsWithoutRef<"tfoot">>(+ ({ className, ...props }, ref) => (<tfoot+ ref={ref}data-slot="table-footer"className={cn("border-t bg-muted/50 font-medium [&>tr]:last:border-b-0", className)}{...props}/>- );-}+ ),+);+TableFooter.displayName = "TableFooter";-function TableRow({ className, ...props }: React.ComponentProps<"tr">) {- return (+const TableRow = React.forwardRef<HTMLTableRowElement, React.ComponentPropsWithoutRef<"tr">>(+ ({ className, ...props }, ref) => (<tr+ ref={ref}data-slot="table-row"className={cn("border-b transition-colors hover:bg-muted/50 has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted",@@ -40,12 +50,14 @@ function TableRow({ className, ...props }: React.ComponentProps<"tr">) {)}{...props}/>- );-}+ ),+);+TableRow.displayName = "TableRow";-function TableHead({ className, ...props }: React.ComponentProps<"th">) {- return (+const TableHead = React.forwardRef<HTMLTableCellElement, React.ComponentPropsWithoutRef<"th">>(+ ({ className, ...props }, ref) => (<th+ ref={ref}data-slot="table-head"className={cn("h-10 px-2 text-left align-middle font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",@@ -53,12 +65,14 @@ function TableHead({ className, ...props }: React.ComponentProps<"th">) {)}{...props}/>- );-}+ ),+);+TableHead.displayName = "TableHead";-function TableCell({ className, ...props }: React.ComponentProps<"td">) {- return (+const TableCell = React.forwardRef<HTMLTableCellElement, React.ComponentPropsWithoutRef<"td">>(+ ({ className, ...props }, ref) => (<td+ ref={ref}data-slot="table-cell"className={cn("p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",@@ -66,13 +80,20 @@ function TableCell({ className, ...props }: React.ComponentProps<"td">) {)}{...props}/>- );-}+ ),+);+TableCell.displayName = "TableCell";-function TableCaption({ className, ...props }: React.ComponentProps<"caption">) {- return (- <caption data-slot="table-caption" className={cn("mt-4 text-sm text-muted-foreground", className)} {...props} />- );-}+const TableCaption = React.forwardRef<HTMLTableCaptionElement, React.ComponentPropsWithoutRef<"caption">>(+ ({ className, ...props }, ref) => (+ <caption+ ref={ref}+ data-slot="table-caption"+ className={cn("mt-4 text-sm text-muted-foreground", className)}+ {...props}+ />+ ),+);+TableCaption.displayName = "TableCaption";export { Table, TableHeader, TableBody, TableFooter, TableHead, TableRow, TableCell, TableCaption };diff --git a/ui/litellm-dashboard/src/components/ui/ui-loading-spinner.tsx b/ui/litellm-dashboard/src/components/ui/ui-loading-spinner.tsxindex 09e52ef0d48..5fd62d92973 100644--- a/ui/litellm-dashboard/src/components/ui/ui-loading-spinner.tsx+++ b/ui/litellm-dashboard/src/components/ui/ui-loading-spinner.tsx@@ -4,39 +4,43 @@ import { cx } from "@/lib/cva.config";type LoadingSpinnerProps = React.SVGProps<SVGSVGElement>;-export function UiLoadingSpinner({ className = "", ...props }: LoadingSpinnerProps) {- const id = useId();+export const UiLoadingSpinner = React.forwardRef<SVGSVGElement, LoadingSpinnerProps>(+ ({ className = "", ...props }, ref) => {+ const id = useId();- useSafeLayoutEffect(() => {- const animations = document- .getAnimations()- .filter((a) => a instanceof CSSAnimation && a.animationName === "spin") as CSSAnimation[];+ useSafeLayoutEffect(() => {+ const animations = document+ .getAnimations()+ .filter((a) => a instanceof CSSAnimation && a.animationName === "spin") as CSSAnimation[];- const self = animations.find((a) => (a.effect as KeyframeEffect).target?.getAttribute("data-spinner-id") === id);+ const self = animations.find((a) => (a.effect as KeyframeEffect).target?.getAttribute("data-spinner-id") === id);- const anyOther = animations.find(- (a) => a.effect instanceof KeyframeEffect && a.effect.target?.getAttribute("data-spinner-id") !== id,- );+ const anyOther = animations.find(+ (a) => a.effect instanceof KeyframeEffect && a.effect.target?.getAttribute("data-spinner-id") !== id,+ );- if (self && anyOther) {- self.currentTime = anyOther.currentTime;- }- }, [id]);+ if (self && anyOther) {+ self.currentTime = anyOther.currentTime;+ }+ }, [id]);- return (- <svg- data-spinner-id={id}- className={cx("pointer-events-none size-12 animate-spin text-current", className)}- fill="none"- viewBox="0 0 24 24"- {...props}- >- <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>- <path- className="opacity-75"- fill="currentColor"- d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"- ></path>- </svg>- );-}+ return (+ <svg+ ref={ref}+ data-spinner-id={id}+ className={cx("pointer-events-none size-12 animate-spin text-current", className)}+ fill="none"+ viewBox="0 0 24 24"+ {...props}+ >+ <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>+ <path+ className="opacity-75"+ fill="currentColor"+ d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"+ ></path>+ </svg>+ );+ },+);+UiLoadingSpinner.displayName = "UiLoadingSpinner";diff --git a/ui/litellm-dashboard/tests/setupTests.ts b/ui/litellm-dashboard/tests/setupTests.tsindex 6bc2e7775a1..69506a9f246 100644--- a/ui/litellm-dashboard/tests/setupTests.ts+++ b/ui/litellm-dashboard/tests/setupTests.ts@@ -149,8 +149,30 @@ vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({}),}));+const pendingRefWarnings: string[] = [];+const consumePendingRefWarnings = (): string[] => pendingRefWarnings.splice(0, pendingRefWarnings.length);+(globalThis as { __consumePendingRefWarnings?: () => string[] }).__consumePendingRefWarnings =+ consumePendingRefWarnings;++const originalConsoleError = console.error.bind(console);+vi.spyOn(console, "error").mockImplementation((...args: unknown[]) => {+ originalConsoleError(...args);+ if (typeof args[0] === "string" && args[0].includes("Function components cannot be given refs")) {+ pendingRefWarnings.push(args.map(String).join(" "));+ }+});+afterEach(() => {cleanup();+ const refWarnings = consumePendingRefWarnings();+ if (refWarnings.length > 0) {+ throw new Error(+ "A ref was passed to a plain function component and silently dropped under React 18, which breaks " ++ "ref-based composition (Base UI render triggers, tooltips, focus). Wrap the component in React.forwardRef. " ++ "This tripwire lives in tests/setupTests.ts and can be removed after the React 19 upgrade.\n\n" ++ refWarnings.join("\n\n"),+ );+ }});// Make toLocaleString deterministic in tests; individual tests can override