registry.tsx 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. "use client";
  2. import { useState } from "react";
  3. import { defineRegistry, useStateBinding } from "@json-render/react";
  4. import { toast } from "sonner";
  5. import { playgroundCatalog } from "./catalog";
  6. // shadcn components
  7. import { Button } from "@/components/ui/button";
  8. import { Input } from "@/components/ui/input";
  9. import { Label } from "@/components/ui/label";
  10. import { Textarea } from "@/components/ui/textarea";
  11. import { Checkbox } from "@/components/ui/checkbox";
  12. import { Switch } from "@/components/ui/switch";
  13. import { Progress } from "@/components/ui/progress";
  14. import { Separator } from "@/components/ui/separator";
  15. import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
  16. import {
  17. Dialog as DialogPrimitive,
  18. DialogContent,
  19. DialogDescription,
  20. DialogHeader,
  21. DialogTitle,
  22. } from "@/components/ui/dialog";
  23. import {
  24. Accordion as AccordionPrimitive,
  25. AccordionContent,
  26. AccordionItem,
  27. AccordionTrigger,
  28. } from "@/components/ui/accordion";
  29. import { Badge } from "@/components/ui/badge";
  30. import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
  31. import {
  32. Select,
  33. SelectContent,
  34. SelectItem,
  35. SelectTrigger,
  36. SelectValue,
  37. } from "@/components/ui/select";
  38. import {
  39. Carousel as CarouselPrimitive,
  40. CarouselContent,
  41. CarouselItem,
  42. CarouselNext,
  43. CarouselPrevious,
  44. } from "@/components/ui/carousel";
  45. import {
  46. Collapsible,
  47. CollapsibleContent,
  48. CollapsibleTrigger,
  49. } from "@/components/ui/collapsible";
  50. import {
  51. Table as TablePrimitive,
  52. TableBody,
  53. TableCaption,
  54. TableCell,
  55. TableHead,
  56. TableHeader,
  57. TableRow,
  58. } from "@/components/ui/table";
  59. import {
  60. Drawer as DrawerPrimitive,
  61. DrawerContent,
  62. DrawerDescription,
  63. DrawerHeader,
  64. DrawerTitle,
  65. } from "@/components/ui/drawer";
  66. import {
  67. DropdownMenu as DropdownMenuPrimitive,
  68. DropdownMenuContent,
  69. DropdownMenuItem,
  70. DropdownMenuTrigger,
  71. } from "@/components/ui/dropdown-menu";
  72. import {
  73. Pagination as PaginationPrimitive,
  74. PaginationContent,
  75. PaginationItem,
  76. PaginationLink,
  77. PaginationNext,
  78. PaginationPrevious,
  79. } from "@/components/ui/pagination";
  80. import {
  81. Popover as PopoverPrimitive,
  82. PopoverContent,
  83. PopoverTrigger,
  84. } from "@/components/ui/popover";
  85. import { Skeleton } from "@/components/ui/skeleton";
  86. import { Slider } from "@/components/ui/slider";
  87. import {
  88. Tabs as TabsPrimitive,
  89. TabsList,
  90. TabsTrigger,
  91. } from "@/components/ui/tabs";
  92. import { Toggle } from "@/components/ui/toggle";
  93. import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
  94. import {
  95. Tooltip as TooltipPrimitive,
  96. TooltipContent,
  97. TooltipProvider,
  98. TooltipTrigger,
  99. } from "@/components/ui/tooltip";
  100. // =============================================================================
  101. // Registry — components + actions, types inferred from catalog
  102. // =============================================================================
  103. export const { registry, executeAction } = defineRegistry(playgroundCatalog, {
  104. components: {
  105. // ── Layout ────────────────────────────────────────────────────────
  106. Card: ({ props, children }) => {
  107. const maxWidthClass =
  108. props.maxWidth === "sm"
  109. ? "max-w-xs sm:min-w-[280px]"
  110. : props.maxWidth === "md"
  111. ? "max-w-sm sm:min-w-[320px]"
  112. : props.maxWidth === "lg"
  113. ? "max-w-md sm:min-w-[360px]"
  114. : "w-full";
  115. const centeredClass = props.centered ? "mx-auto" : "";
  116. return (
  117. <div
  118. className={`border border-border rounded-lg p-4 bg-card text-card-foreground overflow-hidden ${maxWidthClass} ${centeredClass}`}
  119. >
  120. {(props.title || props.description) && (
  121. <div className="mb-4">
  122. {props.title && (
  123. <h3 className="font-semibold text-lg text-left">
  124. {props.title}
  125. </h3>
  126. )}
  127. {props.description && (
  128. <p className="text-sm text-muted-foreground mt-1 text-left">
  129. {props.description}
  130. </p>
  131. )}
  132. </div>
  133. )}
  134. <div className="space-y-3">{children}</div>
  135. </div>
  136. );
  137. },
  138. Stack: ({ props, children }) => {
  139. const isHorizontal = props.direction === "horizontal";
  140. const gapClass =
  141. props.gap === "lg"
  142. ? "gap-4"
  143. : props.gap === "md"
  144. ? "gap-3"
  145. : props.gap === "sm"
  146. ? "gap-2"
  147. : props.gap === "none"
  148. ? "gap-0"
  149. : "gap-3";
  150. const alignClass =
  151. props.align === "center"
  152. ? "items-center"
  153. : props.align === "end"
  154. ? "items-end"
  155. : props.align === "stretch"
  156. ? "items-stretch"
  157. : "items-start";
  158. const justifyClass =
  159. props.justify === "center"
  160. ? "justify-center"
  161. : props.justify === "end"
  162. ? "justify-end"
  163. : props.justify === "between"
  164. ? "justify-between"
  165. : props.justify === "around"
  166. ? "justify-around"
  167. : "";
  168. return (
  169. <div
  170. className={`flex ${isHorizontal ? "flex-row flex-wrap" : "flex-col"} ${gapClass} ${alignClass} ${justifyClass}`}
  171. >
  172. {children}
  173. </div>
  174. );
  175. },
  176. Grid: ({ props, children }) => {
  177. const n = props.columns ?? 1;
  178. const cols =
  179. n >= 6
  180. ? "grid-cols-6"
  181. : n >= 5
  182. ? "grid-cols-5"
  183. : n >= 4
  184. ? "grid-cols-4"
  185. : n >= 3
  186. ? "grid-cols-3"
  187. : n >= 2
  188. ? "grid-cols-2"
  189. : "grid-cols-1";
  190. const gridGap =
  191. props.gap === "lg" ? "gap-4" : props.gap === "sm" ? "gap-2" : "gap-3";
  192. return <div className={`grid ${cols} ${gridGap}`}>{children}</div>;
  193. },
  194. Separator: ({ props }) => (
  195. <Separator
  196. orientation={props.orientation ?? "horizontal"}
  197. className={props.orientation === "vertical" ? "h-full mx-2" : "my-3"}
  198. />
  199. ),
  200. Tabs: ({ props, emit }) => {
  201. const tabs = props.tabs ?? [];
  202. const [boundValue, setBoundValue] = props.statePath
  203. ? useStateBinding<string>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  204. : [undefined, undefined];
  205. const [localValue, setLocalValue] = useState(
  206. props.defaultValue ?? tabs[0]?.value ?? "",
  207. );
  208. const value = props.statePath
  209. ? (boundValue ?? tabs[0]?.value ?? "")
  210. : localValue;
  211. const setValue = props.statePath ? setBoundValue! : setLocalValue;
  212. return (
  213. <TabsPrimitive
  214. value={value}
  215. onValueChange={(v) => {
  216. setValue(v);
  217. emit?.("change");
  218. }}
  219. >
  220. <TabsList>
  221. {tabs.map((tab) => (
  222. <TabsTrigger key={tab.value} value={tab.value}>
  223. {tab.label}
  224. </TabsTrigger>
  225. ))}
  226. </TabsList>
  227. </TabsPrimitive>
  228. );
  229. },
  230. Accordion: ({ props }) => {
  231. const items = props.items ?? [];
  232. const accordionType = props.type ?? "single";
  233. if (accordionType === "multiple") {
  234. return (
  235. <AccordionPrimitive type="multiple" className="w-full">
  236. {items.map((item, i) => (
  237. <AccordionItem key={i} value={`item-${i}`}>
  238. <AccordionTrigger>{item.title}</AccordionTrigger>
  239. <AccordionContent>{item.content}</AccordionContent>
  240. </AccordionItem>
  241. ))}
  242. </AccordionPrimitive>
  243. );
  244. }
  245. return (
  246. <AccordionPrimitive type="single" collapsible className="w-full">
  247. {items.map((item, i) => (
  248. <AccordionItem key={i} value={`item-${i}`}>
  249. <AccordionTrigger>{item.title}</AccordionTrigger>
  250. <AccordionContent>{item.content}</AccordionContent>
  251. </AccordionItem>
  252. ))}
  253. </AccordionPrimitive>
  254. );
  255. },
  256. Collapsible: ({ props, children }) => {
  257. const [open, setOpen] = useState(props.defaultOpen ?? false);
  258. return (
  259. <Collapsible open={open} onOpenChange={setOpen} className="w-full">
  260. <CollapsibleTrigger asChild>
  261. <button className="flex w-full items-center justify-between rounded-md border border-border px-4 py-2 text-sm font-medium hover:bg-muted transition-colors">
  262. {props.title}
  263. <svg
  264. className={`h-4 w-4 transition-transform ${open ? "rotate-180" : ""}`}
  265. fill="none"
  266. viewBox="0 0 24 24"
  267. stroke="currentColor"
  268. strokeWidth={2}
  269. >
  270. <path
  271. strokeLinecap="round"
  272. strokeLinejoin="round"
  273. d="M19 9l-7 7-7-7"
  274. />
  275. </svg>
  276. </button>
  277. </CollapsibleTrigger>
  278. <CollapsibleContent className="pt-2">{children}</CollapsibleContent>
  279. </Collapsible>
  280. );
  281. },
  282. Dialog: ({ props, children }) => {
  283. const [open, setOpen] = useStateBinding<boolean>(props.openPath);
  284. return (
  285. <DialogPrimitive open={open ?? false} onOpenChange={(v) => setOpen(v)}>
  286. <DialogContent>
  287. <DialogHeader>
  288. <DialogTitle>{props.title}</DialogTitle>
  289. {props.description && (
  290. <DialogDescription>{props.description}</DialogDescription>
  291. )}
  292. </DialogHeader>
  293. {children}
  294. </DialogContent>
  295. </DialogPrimitive>
  296. );
  297. },
  298. Drawer: ({ props, children }) => {
  299. const [open, setOpen] = useStateBinding<boolean>(props.openPath);
  300. return (
  301. <DrawerPrimitive open={open ?? false} onOpenChange={(v) => setOpen(v)}>
  302. <DrawerContent>
  303. <DrawerHeader>
  304. <DrawerTitle>{props.title}</DrawerTitle>
  305. {props.description && (
  306. <DrawerDescription>{props.description}</DrawerDescription>
  307. )}
  308. </DrawerHeader>
  309. <div className="p-4">{children}</div>
  310. </DrawerContent>
  311. </DrawerPrimitive>
  312. );
  313. },
  314. Carousel: ({ props }) => {
  315. const items = props.items ?? [];
  316. return (
  317. <CarouselPrimitive className="w-full">
  318. <CarouselContent>
  319. {items.map((item, i) => (
  320. <CarouselItem
  321. key={i}
  322. className="basis-3/4 md:basis-1/2 lg:basis-1/3"
  323. >
  324. <div className="border border-border rounded-lg p-4 bg-card h-full">
  325. {item.title && (
  326. <h4 className="font-semibold text-sm mb-1">{item.title}</h4>
  327. )}
  328. {item.description && (
  329. <p className="text-sm text-muted-foreground">
  330. {item.description}
  331. </p>
  332. )}
  333. </div>
  334. </CarouselItem>
  335. ))}
  336. </CarouselContent>
  337. <CarouselPrevious />
  338. <CarouselNext />
  339. </CarouselPrimitive>
  340. );
  341. },
  342. // ── Data Display ──────────────────────────────────────────────────
  343. Table: ({ props }) => {
  344. const columns = props.columns ?? [];
  345. const rawRows: unknown[] = Array.isArray(props.rows) ? props.rows : [];
  346. const rows = rawRows.map((row) => {
  347. if (Array.isArray(row)) return row.map(String);
  348. if (row && typeof row === "object") {
  349. const obj = row as Record<string, unknown>;
  350. return columns.map((col) =>
  351. String(obj[col] ?? obj[col.toLowerCase()] ?? ""),
  352. );
  353. }
  354. return columns.map(() => "");
  355. });
  356. return (
  357. <div className="rounded-md border border-border overflow-hidden">
  358. <TablePrimitive>
  359. {props.caption && <TableCaption>{props.caption}</TableCaption>}
  360. <TableHeader>
  361. <TableRow>
  362. {columns.map((col) => (
  363. <TableHead key={col}>{col}</TableHead>
  364. ))}
  365. </TableRow>
  366. </TableHeader>
  367. <TableBody>
  368. {rows.map((row, i) => (
  369. <TableRow key={i}>
  370. {row.map((cell, j) => (
  371. <TableCell key={j}>{cell}</TableCell>
  372. ))}
  373. </TableRow>
  374. ))}
  375. </TableBody>
  376. </TablePrimitive>
  377. </div>
  378. );
  379. },
  380. Heading: ({ props }) => {
  381. const level = props.level ?? "h2";
  382. const headingClass =
  383. level === "h1"
  384. ? "text-2xl font-bold"
  385. : level === "h3"
  386. ? "text-base font-semibold"
  387. : level === "h4"
  388. ? "text-sm font-semibold"
  389. : "text-lg font-semibold";
  390. if (level === "h1")
  391. return <h1 className={`${headingClass} text-left`}>{props.text}</h1>;
  392. if (level === "h3")
  393. return <h3 className={`${headingClass} text-left`}>{props.text}</h3>;
  394. if (level === "h4")
  395. return <h4 className={`${headingClass} text-left`}>{props.text}</h4>;
  396. return <h2 className={`${headingClass} text-left`}>{props.text}</h2>;
  397. },
  398. Text: ({ props }) => {
  399. const textClass =
  400. props.variant === "caption"
  401. ? "text-xs"
  402. : props.variant === "muted"
  403. ? "text-sm text-muted-foreground"
  404. : props.variant === "lead"
  405. ? "text-xl text-muted-foreground"
  406. : props.variant === "code"
  407. ? "font-mono text-sm bg-muted px-1.5 py-0.5 rounded"
  408. : "text-sm";
  409. if (props.variant === "code") {
  410. return <code className={`${textClass} text-left`}>{props.text}</code>;
  411. }
  412. return <p className={`${textClass} text-left`}>{props.text}</p>;
  413. },
  414. Image: ({ props }) => (
  415. <div
  416. className="bg-muted border border-border rounded flex items-center justify-center text-xs text-muted-foreground aspect-video"
  417. style={{ width: props.width ?? 80, height: props.height ?? 60 }}
  418. >
  419. {props.alt || "img"}
  420. </div>
  421. ),
  422. Avatar: ({ props }) => {
  423. const name = props.name || "?";
  424. const initials = name
  425. .split(" ")
  426. .map((n) => n[0])
  427. .join("")
  428. .slice(0, 2)
  429. .toUpperCase();
  430. const avatarSize =
  431. props.size === "lg"
  432. ? "w-12 h-12 text-base"
  433. : props.size === "sm"
  434. ? "w-8 h-8 text-xs"
  435. : "w-10 h-10 text-sm";
  436. return (
  437. <div
  438. className={`${avatarSize} rounded-full bg-muted flex items-center justify-center font-medium`}
  439. >
  440. {initials}
  441. </div>
  442. );
  443. },
  444. Badge: ({ props }) => {
  445. const variant =
  446. props.variant === "success" || props.variant === "warning"
  447. ? "secondary"
  448. : props.variant === "danger"
  449. ? "destructive"
  450. : "default";
  451. const customClass =
  452. props.variant === "success"
  453. ? "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-100"
  454. : props.variant === "warning"
  455. ? "bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-100"
  456. : "";
  457. return (
  458. <Badge variant={variant} className={customClass}>
  459. {props.text}
  460. </Badge>
  461. );
  462. },
  463. Alert: ({ props }) => {
  464. const variant = props.type === "error" ? "destructive" : "default";
  465. const customClass =
  466. props.type === "success"
  467. ? "border-green-200 bg-green-50 text-green-900 dark:border-green-800 dark:bg-green-950 dark:text-green-100"
  468. : props.type === "warning"
  469. ? "border-yellow-200 bg-yellow-50 text-yellow-900 dark:border-yellow-800 dark:bg-yellow-950 dark:text-yellow-100"
  470. : props.type === "info"
  471. ? "border-blue-200 bg-blue-50 text-blue-900 dark:border-blue-800 dark:bg-blue-950 dark:text-blue-100"
  472. : "";
  473. return (
  474. <Alert variant={variant} className={customClass}>
  475. <AlertTitle>{props.title}</AlertTitle>
  476. {props.message && (
  477. <AlertDescription>{props.message}</AlertDescription>
  478. )}
  479. </Alert>
  480. );
  481. },
  482. Progress: ({ props }) => {
  483. const value = Math.min(100, Math.max(0, props.value || 0));
  484. return (
  485. <div className="space-y-2">
  486. {props.label && (
  487. <Label className="text-sm text-muted-foreground">
  488. {props.label}
  489. </Label>
  490. )}
  491. <Progress value={value} />
  492. </div>
  493. );
  494. },
  495. Skeleton: ({ props }) => (
  496. <Skeleton
  497. className={props.rounded ? "rounded-full" : "rounded-md"}
  498. style={{
  499. width: props.width ?? "100%",
  500. height: props.height ?? "1.25rem",
  501. }}
  502. />
  503. ),
  504. Spinner: ({ props }) => {
  505. const sizeClass =
  506. props.size === "lg"
  507. ? "h-8 w-8"
  508. : props.size === "sm"
  509. ? "h-4 w-4"
  510. : "h-6 w-6";
  511. return (
  512. <div className="flex items-center gap-2">
  513. <svg
  514. className={`${sizeClass} animate-spin text-muted-foreground`}
  515. viewBox="0 0 24 24"
  516. fill="none"
  517. >
  518. <circle
  519. className="opacity-25"
  520. cx="12"
  521. cy="12"
  522. r="10"
  523. stroke="currentColor"
  524. strokeWidth="4"
  525. />
  526. <path
  527. className="opacity-75"
  528. fill="currentColor"
  529. d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"
  530. />
  531. </svg>
  532. {props.label && (
  533. <span className="text-sm text-muted-foreground">{props.label}</span>
  534. )}
  535. </div>
  536. );
  537. },
  538. Tooltip: ({ props }) => (
  539. <TooltipProvider>
  540. <TooltipPrimitive>
  541. <TooltipTrigger asChild>
  542. <span className="text-sm underline decoration-dotted cursor-help">
  543. {props.text}
  544. </span>
  545. </TooltipTrigger>
  546. <TooltipContent>
  547. <p>{props.content}</p>
  548. </TooltipContent>
  549. </TooltipPrimitive>
  550. </TooltipProvider>
  551. ),
  552. Popover: ({ props }) => (
  553. <PopoverPrimitive>
  554. <PopoverTrigger asChild>
  555. <Button variant="outline" className="text-sm">
  556. {props.trigger}
  557. </Button>
  558. </PopoverTrigger>
  559. <PopoverContent className="w-64">
  560. <p className="text-sm">{props.content}</p>
  561. </PopoverContent>
  562. </PopoverPrimitive>
  563. ),
  564. Rating: ({ props }) => {
  565. const ratingValue = props.value || 0;
  566. const maxRating = props.max ?? 5;
  567. return (
  568. <div className="space-y-2">
  569. {props.label && (
  570. <Label className="text-sm text-muted-foreground">
  571. {props.label}
  572. </Label>
  573. )}
  574. <div className="flex gap-1">
  575. {Array.from({ length: maxRating }).map((_, i) => (
  576. <span
  577. key={i}
  578. className={`text-lg ${i < ratingValue ? "text-yellow-400" : "text-muted"}`}
  579. >
  580. *
  581. </span>
  582. ))}
  583. </div>
  584. </div>
  585. );
  586. },
  587. // ── Charts ────────────────────────────────────────────────────────
  588. BarGraph: ({ props }) => {
  589. const data = props.data || [];
  590. const maxValue = Math.max(...data.map((d) => d.value), 1);
  591. return (
  592. <div className="space-y-2">
  593. {props.title && (
  594. <div className="text-sm font-medium text-left">{props.title}</div>
  595. )}
  596. <div className="flex gap-2">
  597. {data.map((d, i) => (
  598. <div key={i} className="flex-1 flex flex-col items-center gap-1">
  599. <div className="text-xs text-muted-foreground">{d.value}</div>
  600. <div className="w-full h-24 flex items-end">
  601. <div
  602. className="w-full bg-primary rounded-t transition-all"
  603. style={{
  604. height: `${(d.value / maxValue) * 100}%`,
  605. minHeight: 2,
  606. }}
  607. />
  608. </div>
  609. <div className="text-xs text-muted-foreground truncate w-full text-center">
  610. {d.label}
  611. </div>
  612. </div>
  613. ))}
  614. </div>
  615. </div>
  616. );
  617. },
  618. LineGraph: ({ props }) => {
  619. const data = props.data || [];
  620. const maxValue = Math.max(...data.map((d) => d.value));
  621. const minValue = Math.min(...data.map((d) => d.value));
  622. const range = maxValue - minValue || 1;
  623. const width = 300;
  624. const height = 100;
  625. const padding = { top: 10, right: 10, bottom: 10, left: 10 };
  626. const chartWidth = width - padding.left - padding.right;
  627. const chartHeight = height - padding.top - padding.bottom;
  628. const points = data.map((d, i) => {
  629. const x =
  630. padding.left +
  631. (data.length > 1
  632. ? (i / (data.length - 1)) * chartWidth
  633. : chartWidth / 2);
  634. const y =
  635. padding.top +
  636. chartHeight -
  637. ((d.value - minValue) / range) * chartHeight;
  638. return { x, y, ...d };
  639. });
  640. const pathD =
  641. points.length > 0
  642. ? `M ${points.map((p) => `${p.x} ${p.y}`).join(" L ")}`
  643. : "";
  644. return (
  645. <div className="space-y-2">
  646. {props.title && (
  647. <div className="text-sm font-medium text-left">{props.title}</div>
  648. )}
  649. <div className="relative h-28">
  650. <svg viewBox={`0 0 ${width} ${height}`} className="w-full h-full">
  651. <line
  652. x1={padding.left}
  653. y1={padding.top + chartHeight / 2}
  654. x2={width - padding.right}
  655. y2={padding.top + chartHeight / 2}
  656. stroke="currentColor"
  657. strokeOpacity="0.1"
  658. strokeWidth="1"
  659. />
  660. <line
  661. x1={padding.left}
  662. y1={padding.top}
  663. x2={width - padding.right}
  664. y2={padding.top}
  665. stroke="currentColor"
  666. strokeOpacity="0.1"
  667. strokeWidth="1"
  668. />
  669. <line
  670. x1={padding.left}
  671. y1={height - padding.bottom}
  672. x2={width - padding.right}
  673. y2={height - padding.bottom}
  674. stroke="currentColor"
  675. strokeOpacity="0.1"
  676. strokeWidth="1"
  677. />
  678. {pathD && (
  679. <path
  680. d={pathD}
  681. fill="none"
  682. stroke="currentColor"
  683. strokeWidth="2"
  684. strokeLinecap="round"
  685. strokeLinejoin="round"
  686. className="text-primary"
  687. />
  688. )}
  689. {points.map((p, i) => (
  690. <circle
  691. key={i}
  692. cx={p.x}
  693. cy={p.y}
  694. r="4"
  695. className="fill-primary"
  696. />
  697. ))}
  698. </svg>
  699. </div>
  700. {data.length > 0 && (
  701. <div className="flex justify-between">
  702. {data.map((d, i) => (
  703. <div
  704. key={i}
  705. className="text-xs text-muted-foreground text-center"
  706. style={{ width: `${100 / data.length}%` }}
  707. >
  708. {d.label}
  709. </div>
  710. ))}
  711. </div>
  712. )}
  713. </div>
  714. );
  715. },
  716. // ── Form Inputs ───────────────────────────────────────────────────
  717. Input: ({ props, emit }) => {
  718. const [boundValue, setBoundValue] = props.statePath
  719. ? useStateBinding<string>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  720. : [undefined, undefined];
  721. const [localValue, setLocalValue] = useState("");
  722. const value = props.statePath ? (boundValue ?? "") : localValue;
  723. const setValue = props.statePath ? setBoundValue! : setLocalValue;
  724. return (
  725. <div className="space-y-2">
  726. {props.label && <Label htmlFor={props.name}>{props.label}</Label>}
  727. <Input
  728. id={props.name}
  729. name={props.name}
  730. type={props.type ?? "text"}
  731. placeholder={props.placeholder ?? ""}
  732. value={value}
  733. onChange={(e) => setValue(e.target.value)}
  734. onKeyDown={(e) => {
  735. if (e.key === "Enter") emit?.("submit");
  736. }}
  737. onFocus={() => emit?.("focus")}
  738. onBlur={() => emit?.("blur")}
  739. />
  740. </div>
  741. );
  742. },
  743. Textarea: ({ props }) => {
  744. const [boundValue, setBoundValue] = props.statePath
  745. ? useStateBinding<string>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  746. : [undefined, undefined];
  747. const [localValue, setLocalValue] = useState("");
  748. const value = props.statePath ? (boundValue ?? "") : localValue;
  749. const setValue = props.statePath ? setBoundValue! : setLocalValue;
  750. return (
  751. <div className="space-y-2">
  752. {props.label && <Label htmlFor={props.name}>{props.label}</Label>}
  753. <Textarea
  754. id={props.name}
  755. name={props.name}
  756. placeholder={props.placeholder ?? ""}
  757. rows={props.rows ?? 3}
  758. value={value}
  759. onChange={(e) => setValue(e.target.value)}
  760. />
  761. </div>
  762. );
  763. },
  764. Select: ({ props, emit }) => {
  765. const [boundValue, setBoundValue] = props.statePath
  766. ? useStateBinding<string>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  767. : [undefined, undefined];
  768. const [localValue, setLocalValue] = useState<string>("");
  769. const value = props.statePath ? (boundValue ?? "") : localValue;
  770. const setValue = props.statePath ? setBoundValue! : setLocalValue;
  771. const options = props.options ?? [];
  772. return (
  773. <div className="space-y-2">
  774. <Label>{props.label}</Label>
  775. <Select
  776. value={value}
  777. onValueChange={(v) => {
  778. setValue(v);
  779. emit?.("change");
  780. }}
  781. >
  782. <SelectTrigger className="w-full">
  783. <SelectValue placeholder={props.placeholder ?? "Select..."} />
  784. </SelectTrigger>
  785. <SelectContent>
  786. {options.map((opt) => (
  787. <SelectItem key={opt} value={opt}>
  788. {opt}
  789. </SelectItem>
  790. ))}
  791. </SelectContent>
  792. </Select>
  793. </div>
  794. );
  795. },
  796. Checkbox: ({ props, emit }) => {
  797. const [boundValue, setBoundValue] = props.statePath
  798. ? useStateBinding<boolean>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  799. : [undefined, undefined];
  800. const [localChecked, setLocalChecked] = useState(!!props.checked);
  801. const checked = props.statePath ? (boundValue ?? false) : localChecked;
  802. const setChecked = props.statePath ? setBoundValue! : setLocalChecked;
  803. return (
  804. <div className="flex items-center space-x-2">
  805. <Checkbox
  806. id={props.name}
  807. checked={checked}
  808. onCheckedChange={(c) => {
  809. setChecked(c === true);
  810. emit?.("change");
  811. }}
  812. />
  813. <Label htmlFor={props.name} className="cursor-pointer">
  814. {props.label}
  815. </Label>
  816. </div>
  817. );
  818. },
  819. Radio: ({ props, emit }) => {
  820. const options = props.options ?? [];
  821. const [boundValue, setBoundValue] = props.statePath
  822. ? useStateBinding<string>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  823. : [undefined, undefined];
  824. const [localValue, setLocalValue] = useState(options[0] ?? "");
  825. const value = props.statePath ? (boundValue ?? "") : localValue;
  826. const setValue = props.statePath ? setBoundValue! : setLocalValue;
  827. return (
  828. <div className="space-y-2">
  829. {props.label && <Label>{props.label}</Label>}
  830. <RadioGroup
  831. value={value}
  832. onValueChange={(v) => {
  833. setValue(v);
  834. emit?.("change");
  835. }}
  836. >
  837. {options.map((opt) => (
  838. <div key={opt} className="flex items-center space-x-2">
  839. <RadioGroupItem value={opt} id={`${props.name}-${opt}`} />
  840. <Label
  841. htmlFor={`${props.name}-${opt}`}
  842. className="cursor-pointer"
  843. >
  844. {opt}
  845. </Label>
  846. </div>
  847. ))}
  848. </RadioGroup>
  849. </div>
  850. );
  851. },
  852. Switch: ({ props, emit }) => {
  853. const [boundValue, setBoundValue] = props.statePath
  854. ? useStateBinding<boolean>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  855. : [undefined, undefined];
  856. const [localChecked, setLocalChecked] = useState(!!props.checked);
  857. const checked = props.statePath ? (boundValue ?? false) : localChecked;
  858. const setChecked = props.statePath ? setBoundValue! : setLocalChecked;
  859. return (
  860. <div className="flex items-center justify-between space-x-2">
  861. <Label htmlFor={props.name} className="cursor-pointer">
  862. {props.label}
  863. </Label>
  864. <Switch
  865. id={props.name}
  866. checked={checked}
  867. onCheckedChange={(c) => {
  868. setChecked(c);
  869. emit?.("change");
  870. }}
  871. />
  872. </div>
  873. );
  874. },
  875. Slider: ({ props, emit }) => {
  876. const [boundValue, setBoundValue] = props.statePath
  877. ? useStateBinding<number>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  878. : [undefined, undefined];
  879. const [localValue, setLocalValue] = useState(props.min ?? 0);
  880. const value = props.statePath
  881. ? (boundValue ?? props.min ?? 0)
  882. : localValue;
  883. const setValue = props.statePath ? setBoundValue! : setLocalValue;
  884. return (
  885. <div className="space-y-2">
  886. {props.label && (
  887. <div className="flex justify-between">
  888. <Label className="text-sm">{props.label}</Label>
  889. <span className="text-sm text-muted-foreground">{value}</span>
  890. </div>
  891. )}
  892. <Slider
  893. value={[value]}
  894. min={props.min ?? 0}
  895. max={props.max ?? 100}
  896. step={props.step ?? 1}
  897. onValueChange={(v) => {
  898. setValue(v[0] ?? 0);
  899. emit?.("change");
  900. }}
  901. />
  902. </div>
  903. );
  904. },
  905. // ── Actions ───────────────────────────────────────────────────────
  906. Button: ({ props, emit }) => {
  907. const variant =
  908. props.variant === "danger"
  909. ? "destructive"
  910. : props.variant === "secondary"
  911. ? "secondary"
  912. : "default";
  913. return (
  914. <Button
  915. variant={variant}
  916. disabled={props.disabled ?? false}
  917. onClick={() => emit?.("press")}
  918. >
  919. {props.label}
  920. </Button>
  921. );
  922. },
  923. Link: ({ props, emit }) => (
  924. <Button
  925. variant="link"
  926. className="h-auto p-0"
  927. onClick={() => emit?.("press")}
  928. >
  929. {props.label}
  930. </Button>
  931. ),
  932. DropdownMenu: ({ props, emit }) => {
  933. const items = props.items ?? [];
  934. return (
  935. <DropdownMenuPrimitive>
  936. <DropdownMenuTrigger asChild>
  937. <Button variant="outline">{props.label}</Button>
  938. </DropdownMenuTrigger>
  939. <DropdownMenuContent>
  940. {items.map((item) => (
  941. <DropdownMenuItem
  942. key={item.value}
  943. onClick={() => emit?.("select")}
  944. >
  945. {item.label}
  946. </DropdownMenuItem>
  947. ))}
  948. </DropdownMenuContent>
  949. </DropdownMenuPrimitive>
  950. );
  951. },
  952. Toggle: ({ props, emit }) => {
  953. const [boundValue, setBoundValue] = props.statePath
  954. ? useStateBinding<boolean>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  955. : [undefined, undefined];
  956. const [localPressed, setLocalPressed] = useState(props.pressed ?? false);
  957. const pressed = props.statePath ? (boundValue ?? false) : localPressed;
  958. const setPressed = props.statePath ? setBoundValue! : setLocalPressed;
  959. return (
  960. <Toggle
  961. variant={props.variant ?? "default"}
  962. pressed={pressed}
  963. onPressedChange={(v) => {
  964. setPressed(v);
  965. emit?.("change");
  966. }}
  967. >
  968. {props.label}
  969. </Toggle>
  970. );
  971. },
  972. ToggleGroup: ({ props, emit }) => {
  973. const type = props.type ?? "single";
  974. const items = props.items ?? [];
  975. const [boundValue, setBoundValue] = props.statePath
  976. ? useStateBinding<string>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  977. : [undefined, undefined];
  978. const [localValue, setLocalValue] = useState(items[0]?.value ?? "");
  979. const value = props.statePath ? (boundValue ?? "") : localValue;
  980. const setValue = props.statePath ? setBoundValue! : setLocalValue;
  981. if (type === "multiple") {
  982. return (
  983. <ToggleGroup type="multiple">
  984. {items.map((item) => (
  985. <ToggleGroupItem key={item.value} value={item.value}>
  986. {item.label}
  987. </ToggleGroupItem>
  988. ))}
  989. </ToggleGroup>
  990. );
  991. }
  992. return (
  993. <ToggleGroup
  994. type="single"
  995. value={value}
  996. onValueChange={(v) => {
  997. if (v) {
  998. setValue(v);
  999. emit?.("change");
  1000. }
  1001. }}
  1002. >
  1003. {items.map((item) => (
  1004. <ToggleGroupItem key={item.value} value={item.value}>
  1005. {item.label}
  1006. </ToggleGroupItem>
  1007. ))}
  1008. </ToggleGroup>
  1009. );
  1010. },
  1011. ButtonGroup: ({ props, emit }) => {
  1012. const buttons = props.buttons ?? [];
  1013. const [boundValue, setBoundValue] = props.statePath
  1014. ? useStateBinding<string>(props.statePath) // eslint-disable-line react-hooks/rules-of-hooks
  1015. : [undefined, undefined];
  1016. const [localValue, setLocalValue] = useState(buttons[0]?.value ?? "");
  1017. const value = props.statePath ? (boundValue ?? "") : localValue;
  1018. const setValue = props.statePath ? setBoundValue! : setLocalValue;
  1019. return (
  1020. <div className="inline-flex rounded-md border border-border">
  1021. {buttons.map((btn, i) => (
  1022. <button
  1023. key={btn.value}
  1024. className={`px-3 py-1.5 text-sm transition-colors ${
  1025. value === btn.value
  1026. ? "bg-primary text-primary-foreground"
  1027. : "bg-background hover:bg-muted"
  1028. } ${i > 0 ? "border-l border-border" : ""} ${
  1029. i === 0 ? "rounded-l-md" : ""
  1030. } ${i === buttons.length - 1 ? "rounded-r-md" : ""}`}
  1031. onClick={() => {
  1032. setValue(btn.value);
  1033. emit?.("change");
  1034. }}
  1035. >
  1036. {btn.label}
  1037. </button>
  1038. ))}
  1039. </div>
  1040. );
  1041. },
  1042. Pagination: ({ props, emit }) => {
  1043. const [boundValue, setBoundValue] = useStateBinding<number>(
  1044. props.statePath,
  1045. );
  1046. const currentPage = boundValue ?? 1;
  1047. const pages = Array.from({ length: props.totalPages }, (_, i) => i + 1);
  1048. return (
  1049. <PaginationPrimitive>
  1050. <PaginationContent>
  1051. <PaginationItem>
  1052. <PaginationPrevious
  1053. href="#"
  1054. onClick={(e) => {
  1055. e.preventDefault();
  1056. if (currentPage > 1) {
  1057. setBoundValue(currentPage - 1);
  1058. emit?.("change");
  1059. }
  1060. }}
  1061. />
  1062. </PaginationItem>
  1063. {pages.map((page) => (
  1064. <PaginationItem key={page}>
  1065. <PaginationLink
  1066. href="#"
  1067. isActive={page === currentPage}
  1068. onClick={(e) => {
  1069. e.preventDefault();
  1070. setBoundValue(page);
  1071. emit?.("change");
  1072. }}
  1073. >
  1074. {page}
  1075. </PaginationLink>
  1076. </PaginationItem>
  1077. ))}
  1078. <PaginationItem>
  1079. <PaginationNext
  1080. href="#"
  1081. onClick={(e) => {
  1082. e.preventDefault();
  1083. if (currentPage < props.totalPages) {
  1084. setBoundValue(currentPage + 1);
  1085. emit?.("change");
  1086. }
  1087. }}
  1088. />
  1089. </PaginationItem>
  1090. </PaginationContent>
  1091. </PaginationPrimitive>
  1092. );
  1093. },
  1094. },
  1095. actions: {
  1096. // Built-in state actions — handled by ActionProvider, stubs needed for types
  1097. setState: async () => {},
  1098. pushState: async () => {},
  1099. removeState: async () => {},
  1100. // Demo actions — show toasts
  1101. buttonClick: async (params) => {
  1102. const message = (params?.message as string) || "Button clicked!";
  1103. toast.success(message);
  1104. },
  1105. formSubmit: async (params) => {
  1106. const formName = (params?.formName as string) || "Form";
  1107. toast.success(`${formName} submitted successfully!`);
  1108. },
  1109. linkClick: async (params) => {
  1110. const href = (params?.href as string) || "#";
  1111. toast.info(`Navigating to: ${href}`);
  1112. },
  1113. },
  1114. });
  1115. // Fallback component for unknown types
  1116. export function Fallback({ type }: { type: string }) {
  1117. return <div className="text-xs text-muted-foreground">[{type}]</div>;
  1118. }