registry.tsx 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. "use client";
  2. import { useState } from "react";
  3. import {
  4. defineRegistry,
  5. useBoundProp,
  6. useStateBinding,
  7. useFieldValidation,
  8. } from "@json-render/react";
  9. import { toast } from "sonner";
  10. import { playgroundCatalog } from "./catalog";
  11. // shadcn components
  12. import { Button } from "@/components/ui/button";
  13. import { Input } from "@/components/ui/input";
  14. import { Label } from "@/components/ui/label";
  15. import { Textarea } from "@/components/ui/textarea";
  16. import { Checkbox } from "@/components/ui/checkbox";
  17. import { Switch } from "@/components/ui/switch";
  18. import { Progress } from "@/components/ui/progress";
  19. import { Separator } from "@/components/ui/separator";
  20. import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
  21. import {
  22. Dialog as DialogPrimitive,
  23. DialogContent,
  24. DialogDescription,
  25. DialogHeader,
  26. DialogTitle,
  27. } from "@/components/ui/dialog";
  28. import {
  29. Accordion as AccordionPrimitive,
  30. AccordionContent,
  31. AccordionItem,
  32. AccordionTrigger,
  33. } from "@/components/ui/accordion";
  34. import { Badge } from "@/components/ui/badge";
  35. import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
  36. import {
  37. Select,
  38. SelectContent,
  39. SelectItem,
  40. SelectTrigger,
  41. SelectValue,
  42. } from "@/components/ui/select";
  43. import {
  44. Carousel as CarouselPrimitive,
  45. CarouselContent,
  46. CarouselItem,
  47. CarouselNext,
  48. CarouselPrevious,
  49. } from "@/components/ui/carousel";
  50. import {
  51. Collapsible,
  52. CollapsibleContent,
  53. CollapsibleTrigger,
  54. } from "@/components/ui/collapsible";
  55. import {
  56. Table as TablePrimitive,
  57. TableBody,
  58. TableCaption,
  59. TableCell,
  60. TableHead,
  61. TableHeader,
  62. TableRow,
  63. } from "@/components/ui/table";
  64. import {
  65. Drawer as DrawerPrimitive,
  66. DrawerContent,
  67. DrawerDescription,
  68. DrawerHeader,
  69. DrawerTitle,
  70. } from "@/components/ui/drawer";
  71. import {
  72. DropdownMenu as DropdownMenuPrimitive,
  73. DropdownMenuContent,
  74. DropdownMenuItem,
  75. DropdownMenuTrigger,
  76. } from "@/components/ui/dropdown-menu";
  77. import {
  78. Pagination as PaginationPrimitive,
  79. PaginationContent,
  80. PaginationItem,
  81. PaginationLink,
  82. PaginationNext,
  83. PaginationPrevious,
  84. } from "@/components/ui/pagination";
  85. import {
  86. Popover as PopoverPrimitive,
  87. PopoverContent,
  88. PopoverTrigger,
  89. } from "@/components/ui/popover";
  90. import { Skeleton } from "@/components/ui/skeleton";
  91. import { Slider } from "@/components/ui/slider";
  92. import {
  93. Tabs as TabsPrimitive,
  94. TabsList,
  95. TabsTrigger,
  96. } from "@/components/ui/tabs";
  97. import { Toggle } from "@/components/ui/toggle";
  98. import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
  99. import {
  100. Tooltip as TooltipPrimitive,
  101. TooltipContent,
  102. TooltipProvider,
  103. TooltipTrigger,
  104. } from "@/components/ui/tooltip";
  105. import { icons as lucideIcons } from "lucide-react";
  106. // =============================================================================
  107. // Registry — components + actions, types inferred from catalog
  108. // =============================================================================
  109. export const { registry, executeAction } = defineRegistry(playgroundCatalog, {
  110. components: {
  111. // ── Layout ────────────────────────────────────────────────────────
  112. Card: ({ props, children }) => {
  113. const maxWidthClass =
  114. props.maxWidth === "sm"
  115. ? "max-w-xs sm:min-w-[280px]"
  116. : props.maxWidth === "md"
  117. ? "max-w-sm sm:min-w-[320px]"
  118. : props.maxWidth === "lg"
  119. ? "max-w-md sm:min-w-[360px]"
  120. : "w-full";
  121. const centeredClass = props.centered ? "mx-auto" : "";
  122. return (
  123. <div
  124. className={`border border-border rounded-xl p-5 bg-card text-card-foreground shadow-sm overflow-hidden h-full flex flex-col ${maxWidthClass} ${centeredClass}`}
  125. >
  126. {(props.title || props.description) && (
  127. <div className="mb-4">
  128. {props.title && (
  129. <h3 className="font-semibold text-lg tracking-tight">
  130. {props.title}
  131. </h3>
  132. )}
  133. {props.description && (
  134. <p className="text-sm text-muted-foreground mt-1">
  135. {props.description}
  136. </p>
  137. )}
  138. </div>
  139. )}
  140. <div className="flex-1 flex flex-col gap-4 [&>:last-child]:mt-auto">
  141. {children}
  142. </div>
  143. </div>
  144. );
  145. },
  146. Stack: ({ props, children }) => {
  147. const isHorizontal = props.direction === "horizontal";
  148. const gapClass =
  149. props.gap === "lg"
  150. ? "gap-4"
  151. : props.gap === "md"
  152. ? "gap-3"
  153. : props.gap === "sm"
  154. ? "gap-2"
  155. : props.gap === "none"
  156. ? "gap-0"
  157. : "gap-3";
  158. let alignClass: string;
  159. if (isHorizontal) {
  160. alignClass =
  161. props.align === "center"
  162. ? "items-center"
  163. : props.align === "end"
  164. ? "items-end"
  165. : props.align === "stretch"
  166. ? "items-stretch"
  167. : "items-start";
  168. } else {
  169. // Vertical: items-center/end lets inline elements (Avatar, Badge, Button)
  170. // center/align naturally. Block containers (Grid, Accordion, Table) add
  171. // their own w-full to stretch regardless.
  172. alignClass =
  173. props.align === "center"
  174. ? "items-center"
  175. : props.align === "end"
  176. ? "items-end"
  177. : props.align === "start"
  178. ? "items-start"
  179. : "items-stretch";
  180. }
  181. const justifyClass =
  182. props.justify === "center"
  183. ? "justify-center"
  184. : props.justify === "end"
  185. ? "justify-end"
  186. : props.justify === "between"
  187. ? "justify-between"
  188. : props.justify === "around"
  189. ? "justify-around"
  190. : "";
  191. return (
  192. <div
  193. className={`flex ${isHorizontal ? "flex-row flex-wrap" : "flex-col w-full"} ${gapClass} ${alignClass} ${justifyClass}`}
  194. >
  195. {children}
  196. </div>
  197. );
  198. },
  199. Grid: ({ props, children }) => {
  200. const childCount = Array.isArray(children)
  201. ? children.length
  202. : children
  203. ? 1
  204. : 0;
  205. const n = Math.min(props.columns ?? 1, childCount || 1);
  206. const cols =
  207. n >= 6
  208. ? "grid-cols-6"
  209. : n >= 5
  210. ? "grid-cols-5"
  211. : n >= 4
  212. ? "grid-cols-4"
  213. : n >= 3
  214. ? "grid-cols-3"
  215. : n >= 2
  216. ? "grid-cols-2"
  217. : "grid-cols-1";
  218. const gridGap =
  219. props.gap === "lg" ? "gap-4" : props.gap === "sm" ? "gap-2" : "gap-3";
  220. return <div className={`grid w-full ${cols} ${gridGap}`}>{children}</div>;
  221. },
  222. Separator: ({ props }) => (
  223. <Separator
  224. orientation={props.orientation ?? "horizontal"}
  225. className={
  226. props.orientation === "vertical" ? "h-full mx-3" : "my-4 opacity-50"
  227. }
  228. />
  229. ),
  230. Tabs: ({ props, bindings, emit }) => {
  231. const tabs = props.tabs ?? [];
  232. const [boundValue, setBoundValue] = useBoundProp<string>(
  233. props.value as string | undefined,
  234. bindings?.value,
  235. );
  236. const [localValue, setLocalValue] = useState(
  237. props.defaultValue ?? tabs[0]?.value ?? "",
  238. );
  239. const isBound = !!bindings?.value;
  240. const value = isBound ? (boundValue ?? tabs[0]?.value ?? "") : localValue;
  241. const setValue = isBound ? setBoundValue : setLocalValue;
  242. return (
  243. <TabsPrimitive
  244. value={value}
  245. onValueChange={(v) => {
  246. setValue(v);
  247. emit("change");
  248. }}
  249. >
  250. <TabsList>
  251. {tabs.map((tab) => (
  252. <TabsTrigger key={tab.value} value={tab.value}>
  253. {tab.label}
  254. </TabsTrigger>
  255. ))}
  256. </TabsList>
  257. </TabsPrimitive>
  258. );
  259. },
  260. Accordion: ({ props }) => {
  261. const items = props.items ?? [];
  262. const accordionType = props.type ?? "single";
  263. if (accordionType === "multiple") {
  264. return (
  265. <AccordionPrimitive type="multiple" className="w-full">
  266. {items.map((item, i) => (
  267. <AccordionItem key={i} value={`item-${i}`}>
  268. <AccordionTrigger>{item.title}</AccordionTrigger>
  269. <AccordionContent>{item.content}</AccordionContent>
  270. </AccordionItem>
  271. ))}
  272. </AccordionPrimitive>
  273. );
  274. }
  275. return (
  276. <AccordionPrimitive type="single" collapsible className="w-full">
  277. {items.map((item, i) => (
  278. <AccordionItem key={i} value={`item-${i}`}>
  279. <AccordionTrigger>{item.title}</AccordionTrigger>
  280. <AccordionContent>{item.content}</AccordionContent>
  281. </AccordionItem>
  282. ))}
  283. </AccordionPrimitive>
  284. );
  285. },
  286. Collapsible: ({ props, children }) => {
  287. const [open, setOpen] = useState(props.defaultOpen ?? false);
  288. return (
  289. <Collapsible open={open} onOpenChange={setOpen} className="w-full">
  290. <CollapsibleTrigger asChild>
  291. <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">
  292. {props.title}
  293. <svg
  294. className={`h-4 w-4 transition-transform ${open ? "rotate-180" : ""}`}
  295. fill="none"
  296. viewBox="0 0 24 24"
  297. stroke="currentColor"
  298. strokeWidth={2}
  299. >
  300. <path
  301. strokeLinecap="round"
  302. strokeLinejoin="round"
  303. d="M19 9l-7 7-7-7"
  304. />
  305. </svg>
  306. </button>
  307. </CollapsibleTrigger>
  308. <CollapsibleContent className="pt-2">{children}</CollapsibleContent>
  309. </Collapsible>
  310. );
  311. },
  312. Dialog: ({ props, children }) => {
  313. const [open, setOpen] = useStateBinding<boolean>(props.openPath);
  314. return (
  315. <DialogPrimitive open={open ?? false} onOpenChange={(v) => setOpen(v)}>
  316. <DialogContent>
  317. <DialogHeader>
  318. <DialogTitle>{props.title}</DialogTitle>
  319. {props.description && (
  320. <DialogDescription>{props.description}</DialogDescription>
  321. )}
  322. </DialogHeader>
  323. {children}
  324. </DialogContent>
  325. </DialogPrimitive>
  326. );
  327. },
  328. Drawer: ({ props, children }) => {
  329. const [open, setOpen] = useStateBinding<boolean>(props.openPath);
  330. return (
  331. <DrawerPrimitive open={open ?? false} onOpenChange={(v) => setOpen(v)}>
  332. <DrawerContent>
  333. <DrawerHeader>
  334. <DrawerTitle>{props.title}</DrawerTitle>
  335. {props.description && (
  336. <DrawerDescription>{props.description}</DrawerDescription>
  337. )}
  338. </DrawerHeader>
  339. <div className="p-4">{children}</div>
  340. </DrawerContent>
  341. </DrawerPrimitive>
  342. );
  343. },
  344. Carousel: ({ props }) => {
  345. const items = props.items ?? [];
  346. return (
  347. <CarouselPrimitive className="w-full">
  348. <CarouselContent>
  349. {items.map((item, i) => (
  350. <CarouselItem
  351. key={i}
  352. className="basis-3/4 md:basis-1/2 lg:basis-1/3"
  353. >
  354. <div className="border border-border rounded-lg p-4 bg-card h-full">
  355. {item.title && (
  356. <h4 className="font-semibold text-sm mb-1">{item.title}</h4>
  357. )}
  358. {item.description && (
  359. <p className="text-sm text-muted-foreground">
  360. {item.description}
  361. </p>
  362. )}
  363. </div>
  364. </CarouselItem>
  365. ))}
  366. </CarouselContent>
  367. <CarouselPrevious />
  368. <CarouselNext />
  369. </CarouselPrimitive>
  370. );
  371. },
  372. // ── Data Display ──────────────────────────────────────────────────
  373. Table: ({ props }) => {
  374. const columns = props.columns ?? [];
  375. const rawRows: unknown[] = Array.isArray(props.rows) ? props.rows : [];
  376. const rows = rawRows.map((row) => {
  377. if (Array.isArray(row)) return row.map(String);
  378. if (row && typeof row === "object") {
  379. const obj = row as Record<string, unknown>;
  380. return columns.map((col) =>
  381. String(obj[col] ?? obj[col.toLowerCase()] ?? ""),
  382. );
  383. }
  384. return columns.map(() => "");
  385. });
  386. return (
  387. <div className="w-full rounded-md border border-border overflow-hidden">
  388. <TablePrimitive>
  389. {props.caption && <TableCaption>{props.caption}</TableCaption>}
  390. <TableHeader>
  391. <TableRow>
  392. {columns.map((col) => (
  393. <TableHead key={col}>{col}</TableHead>
  394. ))}
  395. </TableRow>
  396. </TableHeader>
  397. <TableBody>
  398. {rows.map((row, i) => (
  399. <TableRow key={i}>
  400. {row.map((cell, j) => (
  401. <TableCell key={j}>{cell}</TableCell>
  402. ))}
  403. </TableRow>
  404. ))}
  405. </TableBody>
  406. </TablePrimitive>
  407. </div>
  408. );
  409. },
  410. Heading: ({ props }) => {
  411. const level = props.level ?? "h2";
  412. const headingClass =
  413. level === "h1"
  414. ? "text-2xl font-bold tracking-tight"
  415. : level === "h3"
  416. ? "text-base font-semibold tracking-tight"
  417. : level === "h4"
  418. ? "text-sm font-medium uppercase tracking-wider text-muted-foreground"
  419. : "text-xl font-semibold tracking-tight";
  420. if (level === "h1") return <h1 className={headingClass}>{props.text}</h1>;
  421. if (level === "h3") return <h3 className={headingClass}>{props.text}</h3>;
  422. if (level === "h4") return <h4 className={headingClass}>{props.text}</h4>;
  423. return <h2 className={headingClass}>{props.text}</h2>;
  424. },
  425. Text: ({ props }) => {
  426. const textClass =
  427. props.variant === "caption"
  428. ? "text-xs"
  429. : props.variant === "muted"
  430. ? "text-sm text-muted-foreground"
  431. : props.variant === "lead"
  432. ? "text-xl text-muted-foreground"
  433. : props.variant === "code"
  434. ? "font-mono text-sm bg-muted px-1.5 py-0.5 rounded"
  435. : "text-sm";
  436. if (props.variant === "code") {
  437. return <code className={textClass}>{props.text}</code>;
  438. }
  439. return <p className={textClass}>{props.text}</p>;
  440. },
  441. Image: ({ props }) => (
  442. <div
  443. className="w-full bg-muted/50 border border-dashed border-border rounded-lg flex flex-col items-center justify-center gap-2 text-muted-foreground/60 px-4"
  444. style={{
  445. maxWidth: props.width ?? undefined,
  446. height: props.height ?? 120,
  447. minHeight: 80,
  448. }}
  449. >
  450. <svg
  451. width="32"
  452. height="32"
  453. viewBox="0 0 24 24"
  454. fill="none"
  455. stroke="currentColor"
  456. strokeWidth="1.5"
  457. strokeLinecap="round"
  458. strokeLinejoin="round"
  459. className="opacity-50"
  460. >
  461. <rect width="18" height="18" x="3" y="3" rx="2" ry="2" />
  462. <circle cx="9" cy="9" r="2" />
  463. <path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21" />
  464. </svg>
  465. {props.alt && <span className="text-xs">{props.alt}</span>}
  466. </div>
  467. ),
  468. Icon: ({ props }) => {
  469. const IconComponent = lucideIcons[props.name as keyof typeof lucideIcons];
  470. if (!IconComponent) return null;
  471. const sizeMap = { sm: 16, md: 20, lg: 24 } as const;
  472. const px = sizeMap[props.size ?? "md"] ?? 20;
  473. const colorClass =
  474. props.color === "muted"
  475. ? "text-muted-foreground"
  476. : props.color === "primary"
  477. ? "text-primary"
  478. : props.color === "success"
  479. ? "text-green-600 dark:text-green-400"
  480. : props.color === "warning"
  481. ? "text-yellow-600 dark:text-yellow-400"
  482. : props.color === "danger"
  483. ? "text-red-600 dark:text-red-400"
  484. : "";
  485. return <IconComponent size={px} className={`shrink-0 ${colorClass}`} />;
  486. },
  487. Avatar: ({ props }) => {
  488. const name = props.name || "?";
  489. const initials = name
  490. .split(" ")
  491. .map((n) => n[0])
  492. .join("")
  493. .slice(0, 2)
  494. .toUpperCase();
  495. const sizeStyles =
  496. props.size === "lg"
  497. ? { outer: "w-[72px] h-[72px]", text: "text-xl", ring: "ring-[3px]" }
  498. : props.size === "sm"
  499. ? { outer: "w-8 h-8", text: "text-xs", ring: "ring-2" }
  500. : { outer: "w-10 h-10", text: "text-sm", ring: "ring-2" };
  501. return (
  502. <div
  503. className={`${sizeStyles.outer} ${sizeStyles.text} rounded-full bg-gradient-to-br from-muted-foreground/20 to-muted flex items-center justify-center font-semibold tracking-wide ${sizeStyles.ring} ring-background shadow-sm`}
  504. >
  505. {initials}
  506. </div>
  507. );
  508. },
  509. Badge: ({ props }) => {
  510. const variant =
  511. props.variant === "success" || props.variant === "warning"
  512. ? "secondary"
  513. : props.variant === "danger"
  514. ? "destructive"
  515. : "default";
  516. const customClass =
  517. props.variant === "success"
  518. ? "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-100"
  519. : props.variant === "warning"
  520. ? "bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-100"
  521. : "";
  522. const dotColor =
  523. props.variant === "success"
  524. ? "bg-green-500"
  525. : props.variant === "warning"
  526. ? "bg-yellow-500"
  527. : props.variant === "danger"
  528. ? "bg-red-500"
  529. : "";
  530. return (
  531. <Badge variant={variant} className={`${customClass} gap-1.5`}>
  532. {dotColor && (
  533. <span className={`w-1.5 h-1.5 rounded-full ${dotColor} shrink-0`} />
  534. )}
  535. {props.text}
  536. </Badge>
  537. );
  538. },
  539. Alert: ({ props }) => {
  540. const variant = props.type === "error" ? "destructive" : "default";
  541. const customClass =
  542. props.type === "success"
  543. ? "border-green-200 bg-green-50 text-green-900 dark:border-green-800 dark:bg-green-950 dark:text-green-100"
  544. : props.type === "warning"
  545. ? "border-yellow-200 bg-yellow-50 text-yellow-900 dark:border-yellow-800 dark:bg-yellow-950 dark:text-yellow-100"
  546. : props.type === "info"
  547. ? "border-blue-200 bg-blue-50 text-blue-900 dark:border-blue-800 dark:bg-blue-950 dark:text-blue-100"
  548. : "";
  549. const iconProps = {
  550. width: 16,
  551. height: 16,
  552. viewBox: "0 0 24 24",
  553. fill: "none",
  554. stroke: "currentColor",
  555. strokeWidth: 2,
  556. strokeLinecap: "round" as const,
  557. strokeLinejoin: "round" as const,
  558. className: "shrink-0",
  559. };
  560. const icon =
  561. props.type === "success" ? (
  562. <svg {...iconProps}>
  563. <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" />
  564. <polyline points="22 4 12 14.01 9 11.01" />
  565. </svg>
  566. ) : props.type === "warning" ? (
  567. <svg {...iconProps}>
  568. <path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z" />
  569. <line x1="12" y1="9" x2="12" y2="13" />
  570. <line x1="12" y1="17" x2="12.01" y2="17" />
  571. </svg>
  572. ) : props.type === "error" ? (
  573. <svg {...iconProps}>
  574. <circle cx="12" cy="12" r="10" />
  575. <line x1="15" y1="9" x2="9" y2="15" />
  576. <line x1="9" y1="9" x2="15" y2="15" />
  577. </svg>
  578. ) : (
  579. <svg {...iconProps}>
  580. <circle cx="12" cy="12" r="10" />
  581. <line x1="12" y1="16" x2="12" y2="12" />
  582. <line x1="12" y1="8" x2="12.01" y2="8" />
  583. </svg>
  584. );
  585. return (
  586. <Alert variant={variant} className={customClass}>
  587. {icon}
  588. <AlertTitle>{props.title}</AlertTitle>
  589. {props.message && (
  590. <AlertDescription>{props.message}</AlertDescription>
  591. )}
  592. </Alert>
  593. );
  594. },
  595. Progress: ({ props }) => {
  596. const value = Math.min(100, Math.max(0, props.value || 0));
  597. return (
  598. <div className="w-full space-y-2">
  599. {props.label && (
  600. <Label className="text-sm text-muted-foreground">
  601. {props.label}
  602. </Label>
  603. )}
  604. <Progress value={value} />
  605. </div>
  606. );
  607. },
  608. Skeleton: ({ props }) => (
  609. <Skeleton
  610. className={props.rounded ? "rounded-full" : "rounded-md"}
  611. style={{
  612. width: props.width ?? "100%",
  613. height: props.height ?? "1.25rem",
  614. }}
  615. />
  616. ),
  617. Spinner: ({ props }) => {
  618. const sizeClass =
  619. props.size === "lg"
  620. ? "h-8 w-8"
  621. : props.size === "sm"
  622. ? "h-4 w-4"
  623. : "h-6 w-6";
  624. return (
  625. <div className="flex items-center gap-2">
  626. <svg
  627. className={`${sizeClass} animate-spin text-muted-foreground`}
  628. viewBox="0 0 24 24"
  629. fill="none"
  630. >
  631. <circle
  632. className="opacity-25"
  633. cx="12"
  634. cy="12"
  635. r="10"
  636. stroke="currentColor"
  637. strokeWidth="4"
  638. />
  639. <path
  640. className="opacity-75"
  641. fill="currentColor"
  642. d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"
  643. />
  644. </svg>
  645. {props.label && (
  646. <span className="text-sm text-muted-foreground">{props.label}</span>
  647. )}
  648. </div>
  649. );
  650. },
  651. Tooltip: ({ props }) => (
  652. <TooltipProvider>
  653. <TooltipPrimitive>
  654. <TooltipTrigger asChild>
  655. <span className="text-sm underline decoration-dotted cursor-help">
  656. {props.text}
  657. </span>
  658. </TooltipTrigger>
  659. <TooltipContent>
  660. <p>{props.content}</p>
  661. </TooltipContent>
  662. </TooltipPrimitive>
  663. </TooltipProvider>
  664. ),
  665. Popover: ({ props }) => (
  666. <PopoverPrimitive>
  667. <PopoverTrigger asChild>
  668. <Button variant="outline" className="text-sm">
  669. {props.trigger}
  670. </Button>
  671. </PopoverTrigger>
  672. <PopoverContent className="w-64">
  673. <p className="text-sm">{props.content}</p>
  674. </PopoverContent>
  675. </PopoverPrimitive>
  676. ),
  677. Rating: ({ props, bindings, emit }) => {
  678. const [boundValue, setBoundValue] = useBoundProp<number>(
  679. props.value as number | undefined,
  680. bindings?.value,
  681. );
  682. const [localValue, setLocalValue] = useState(props.value || 0);
  683. const isBound = !!bindings?.value;
  684. const ratingValue = isBound ? (boundValue ?? 0) : localValue;
  685. const setValue = isBound ? setBoundValue : setLocalValue;
  686. const maxRating = props.max ?? 5;
  687. const interactive = props.interactive !== false;
  688. const [hoverIndex, setHoverIndex] = useState(-1);
  689. return (
  690. <div className="space-y-2">
  691. {props.label && (
  692. <Label className="text-sm text-muted-foreground">
  693. {props.label}
  694. </Label>
  695. )}
  696. <div
  697. className="flex gap-0.5"
  698. onMouseLeave={() => interactive && setHoverIndex(-1)}
  699. >
  700. {Array.from({ length: maxRating }).map((_, i) => {
  701. const filled =
  702. hoverIndex >= 0 ? i <= hoverIndex : i < ratingValue;
  703. return (
  704. <button
  705. key={i}
  706. type="button"
  707. className={`p-0.5 transition-colors ${interactive ? "cursor-pointer hover:scale-110" : "cursor-default"}`}
  708. onMouseEnter={() => interactive && setHoverIndex(i)}
  709. onClick={() => {
  710. if (!interactive) return;
  711. const newVal = i + 1 === ratingValue ? 0 : i + 1;
  712. setValue(newVal);
  713. emit("change");
  714. }}
  715. >
  716. <svg
  717. width="20"
  718. height="20"
  719. viewBox="0 0 24 24"
  720. fill={filled ? "currentColor" : "none"}
  721. stroke="currentColor"
  722. strokeWidth="1.5"
  723. strokeLinecap="round"
  724. strokeLinejoin="round"
  725. className={
  726. filled ? "text-yellow-400" : "text-muted-foreground/40"
  727. }
  728. >
  729. <polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" />
  730. </svg>
  731. </button>
  732. );
  733. })}
  734. </div>
  735. </div>
  736. );
  737. },
  738. Metric: ({ props }) => {
  739. const changeColor =
  740. props.changeType === "positive"
  741. ? "text-green-600 dark:text-green-400"
  742. : props.changeType === "negative"
  743. ? "text-red-600 dark:text-red-400"
  744. : "text-muted-foreground";
  745. const changeIcon =
  746. props.changeType === "positive"
  747. ? "\u2191"
  748. : props.changeType === "negative"
  749. ? "\u2193"
  750. : "";
  751. return (
  752. <div className="space-y-1">
  753. <div className="text-sm text-muted-foreground">{props.label}</div>
  754. <div className="flex items-baseline gap-1.5">
  755. <span className="text-2xl font-semibold tracking-tight tabular-nums">
  756. {props.prefix}
  757. {props.value}
  758. {props.suffix}
  759. </span>
  760. {props.change && (
  761. <span className={`text-sm font-medium ${changeColor}`}>
  762. {changeIcon}
  763. {props.change}
  764. </span>
  765. )}
  766. </div>
  767. </div>
  768. );
  769. },
  770. // ── Charts ────────────────────────────────────────────────────────
  771. BarGraph: ({ props }) => {
  772. const data = props.data || [];
  773. const maxValue = Math.max(...data.map((d) => d.value), 1);
  774. const barColors = [
  775. "bg-primary",
  776. "bg-primary/80",
  777. "bg-primary/60",
  778. "bg-primary/70",
  779. "bg-primary/90",
  780. "bg-primary/50",
  781. ];
  782. return (
  783. <div className="space-y-3">
  784. {props.title && (
  785. <div className="text-sm font-medium">{props.title}</div>
  786. )}
  787. <div className="flex items-end gap-2" style={{ height: 160 }}>
  788. {data.map((d, i) => (
  789. <div
  790. key={i}
  791. className="flex-1 flex flex-col items-center gap-1.5 h-full justify-end group"
  792. >
  793. <div className="text-[11px] font-medium text-muted-foreground tabular-nums">
  794. {d.value}
  795. </div>
  796. <div className="w-full flex-1 flex items-end">
  797. <div
  798. className={`w-full ${barColors[i % barColors.length]} rounded-t-md transition-all group-hover:opacity-80`}
  799. style={{
  800. height: `${(d.value / maxValue) * 100}%`,
  801. minHeight: 4,
  802. }}
  803. />
  804. </div>
  805. <div className="text-[11px] text-muted-foreground truncate w-full text-center">
  806. {d.label}
  807. </div>
  808. </div>
  809. ))}
  810. </div>
  811. </div>
  812. );
  813. },
  814. LineGraph: ({ props }) => {
  815. const data = props.data || [];
  816. const maxValue = Math.max(...data.map((d) => d.value));
  817. const minValue = Math.min(...data.map((d) => d.value));
  818. const range = maxValue - minValue || 1;
  819. const width = 300;
  820. const height = 140;
  821. const padding = { top: 12, right: 12, bottom: 12, left: 12 };
  822. const chartWidth = width - padding.left - padding.right;
  823. const chartHeight = height - padding.top - padding.bottom;
  824. const points = data.map((d, i) => {
  825. const x =
  826. padding.left +
  827. (data.length > 1
  828. ? (i / (data.length - 1)) * chartWidth
  829. : chartWidth / 2);
  830. const y =
  831. padding.top +
  832. chartHeight -
  833. ((d.value - minValue) / range) * chartHeight;
  834. return { x, y, ...d };
  835. });
  836. // Build smooth cubic bezier curve through points
  837. let smoothPath = "";
  838. let areaPath = "";
  839. if (points.length > 1) {
  840. const first = points[0]!;
  841. const last = points[points.length - 1]!;
  842. smoothPath = `M ${first.x} ${first.y}`;
  843. for (let i = 0; i < points.length - 1; i++) {
  844. const curr = points[i]!;
  845. const next = points[i + 1]!;
  846. const cpx = (curr.x + next.x) / 2;
  847. smoothPath += ` C ${cpx} ${curr.y}, ${cpx} ${next.y}, ${next.x} ${next.y}`;
  848. }
  849. const bottomY = height - padding.bottom;
  850. areaPath = `${smoothPath} L ${last.x} ${bottomY} L ${first.x} ${bottomY} Z`;
  851. } else if (points.length === 1) {
  852. const only = points[0]!;
  853. smoothPath = `M ${only.x} ${only.y}`;
  854. }
  855. const gradientId = `line-gradient-${Math.random().toString(36).slice(2, 8)}`;
  856. return (
  857. <div className="space-y-3">
  858. {props.title && (
  859. <div className="text-sm font-medium">{props.title}</div>
  860. )}
  861. <div className="relative" style={{ height: 160 }}>
  862. <svg
  863. viewBox={`0 0 ${width} ${height}`}
  864. className="w-full h-full"
  865. preserveAspectRatio="none"
  866. >
  867. <defs>
  868. <linearGradient id={gradientId} x1="0" y1="0" x2="0" y2="1">
  869. <stop
  870. offset="0%"
  871. stopColor="currentColor"
  872. stopOpacity="0.15"
  873. />
  874. <stop
  875. offset="100%"
  876. stopColor="currentColor"
  877. stopOpacity="0"
  878. />
  879. </linearGradient>
  880. </defs>
  881. {[0, 0.25, 0.5, 0.75, 1].map((frac) => (
  882. <line
  883. key={frac}
  884. x1={padding.left}
  885. y1={padding.top + chartHeight * frac}
  886. x2={width - padding.right}
  887. y2={padding.top + chartHeight * frac}
  888. stroke="currentColor"
  889. strokeOpacity="0.07"
  890. vectorEffect="non-scaling-stroke"
  891. strokeWidth="1"
  892. />
  893. ))}
  894. {areaPath && (
  895. <path
  896. d={areaPath}
  897. fill={`url(#${gradientId})`}
  898. className="text-primary"
  899. />
  900. )}
  901. {smoothPath && (
  902. <path
  903. d={smoothPath}
  904. fill="none"
  905. stroke="currentColor"
  906. strokeWidth="2"
  907. strokeLinecap="round"
  908. strokeLinejoin="round"
  909. vectorEffect="non-scaling-stroke"
  910. className="text-primary"
  911. />
  912. )}
  913. </svg>
  914. {points.map((p, i) => (
  915. <div
  916. key={i}
  917. className="absolute w-[7px] h-[7px] rounded-full bg-primary -translate-x-1/2 -translate-y-1/2"
  918. style={{
  919. left: `${(p.x / width) * 100}%`,
  920. top: `${(p.y / height) * 100}%`,
  921. }}
  922. />
  923. ))}
  924. </div>
  925. {points.length > 0 && (
  926. <div className="relative h-4">
  927. {points.map((p, i) => (
  928. <span
  929. key={i}
  930. className="absolute text-[11px] text-muted-foreground -translate-x-1/2"
  931. style={{ left: `${(p.x / width) * 100}%` }}
  932. >
  933. {data[i]?.label}
  934. </span>
  935. ))}
  936. </div>
  937. )}
  938. </div>
  939. );
  940. },
  941. // ── Form Inputs ───────────────────────────────────────────────────
  942. Input: ({ props, bindings, emit }) => {
  943. const [boundValue, setBoundValue] = useBoundProp<string>(
  944. props.value as string | undefined,
  945. bindings?.value,
  946. );
  947. const [localValue, setLocalValue] = useState("");
  948. const isBound = !!bindings?.value;
  949. const value = isBound ? (boundValue ?? "") : localValue;
  950. const setValue = isBound ? setBoundValue : setLocalValue;
  951. const hasValidation = !!(bindings?.value && props.checks?.length);
  952. const { errors, validate } = useFieldValidation(
  953. bindings?.value ?? "",
  954. hasValidation ? { checks: props.checks ?? [] } : undefined,
  955. );
  956. return (
  957. <div className="w-full space-y-2">
  958. {props.label && <Label htmlFor={props.name}>{props.label}</Label>}
  959. <Input
  960. id={props.name}
  961. name={props.name}
  962. type={props.type ?? "text"}
  963. placeholder={props.placeholder ?? ""}
  964. value={value}
  965. onChange={(e) => setValue(e.target.value)}
  966. onKeyDown={(e) => {
  967. if (e.key === "Enter") emit("submit");
  968. }}
  969. onFocus={() => emit("focus")}
  970. onBlur={() => {
  971. if (hasValidation) validate();
  972. emit("blur");
  973. }}
  974. />
  975. {errors.length > 0 && (
  976. <p className="text-sm text-destructive">{errors[0]}</p>
  977. )}
  978. </div>
  979. );
  980. },
  981. Textarea: ({ props, bindings }) => {
  982. const [boundValue, setBoundValue] = useBoundProp<string>(
  983. props.value as string | undefined,
  984. bindings?.value,
  985. );
  986. const [localValue, setLocalValue] = useState("");
  987. const isBound = !!bindings?.value;
  988. const value = isBound ? (boundValue ?? "") : localValue;
  989. const setValue = isBound ? setBoundValue : setLocalValue;
  990. const hasValidation = !!(bindings?.value && props.checks?.length);
  991. const { errors, validate } = useFieldValidation(
  992. bindings?.value ?? "",
  993. hasValidation ? { checks: props.checks ?? [] } : undefined,
  994. );
  995. return (
  996. <div className="w-full space-y-2">
  997. {props.label && <Label htmlFor={props.name}>{props.label}</Label>}
  998. <Textarea
  999. id={props.name}
  1000. name={props.name}
  1001. placeholder={props.placeholder ?? ""}
  1002. rows={props.rows ?? 3}
  1003. value={value}
  1004. onChange={(e) => setValue(e.target.value)}
  1005. onBlur={() => {
  1006. if (hasValidation) validate();
  1007. }}
  1008. />
  1009. {errors.length > 0 && (
  1010. <p className="text-sm text-destructive">{errors[0]}</p>
  1011. )}
  1012. </div>
  1013. );
  1014. },
  1015. Select: ({ props, bindings, emit }) => {
  1016. const [boundValue, setBoundValue] = useBoundProp<string>(
  1017. props.value as string | undefined,
  1018. bindings?.value,
  1019. );
  1020. const [localValue, setLocalValue] = useState<string>("");
  1021. const isBound = !!bindings?.value;
  1022. const value = isBound ? (boundValue ?? "") : localValue;
  1023. const setValue = isBound ? setBoundValue : setLocalValue;
  1024. const rawOptions = props.options ?? [];
  1025. // Coerce options to strings – AI may produce objects/numbers instead of
  1026. // plain strings which would cause duplicate `[object Object]` keys.
  1027. const options = rawOptions.map((opt) =>
  1028. typeof opt === "string" ? opt : String(opt ?? ""),
  1029. );
  1030. const hasValidation = !!(bindings?.value && props.checks?.length);
  1031. const { errors, validate } = useFieldValidation(
  1032. bindings?.value ?? "",
  1033. hasValidation ? { checks: props.checks ?? [] } : undefined,
  1034. );
  1035. return (
  1036. <div className="w-full space-y-2">
  1037. <Label>{props.label}</Label>
  1038. <Select
  1039. value={value}
  1040. onValueChange={(v) => {
  1041. setValue(v);
  1042. if (hasValidation) validate();
  1043. emit("change");
  1044. }}
  1045. >
  1046. <SelectTrigger className="w-full">
  1047. <SelectValue placeholder={props.placeholder ?? "Select..."} />
  1048. </SelectTrigger>
  1049. <SelectContent>
  1050. {options.map((opt, idx) => (
  1051. <SelectItem
  1052. key={`${idx}-${opt}`}
  1053. value={opt || `option-${idx}`}
  1054. >
  1055. {opt}
  1056. </SelectItem>
  1057. ))}
  1058. </SelectContent>
  1059. </Select>
  1060. {errors.length > 0 && (
  1061. <p className="text-sm text-destructive">{errors[0]}</p>
  1062. )}
  1063. </div>
  1064. );
  1065. },
  1066. Checkbox: ({ props, bindings, emit }) => {
  1067. const [boundChecked, setBoundChecked] = useBoundProp<boolean>(
  1068. props.checked as boolean | undefined,
  1069. bindings?.checked,
  1070. );
  1071. const [localChecked, setLocalChecked] = useState(!!props.checked);
  1072. const isBound = !!bindings?.checked;
  1073. const checked = isBound ? (boundChecked ?? false) : localChecked;
  1074. const setChecked = isBound ? setBoundChecked : setLocalChecked;
  1075. return (
  1076. <div className="flex items-center space-x-2">
  1077. <Checkbox
  1078. id={props.name}
  1079. checked={checked}
  1080. onCheckedChange={(c) => {
  1081. setChecked(c === true);
  1082. emit("change");
  1083. }}
  1084. />
  1085. <Label htmlFor={props.name} className="cursor-pointer">
  1086. {props.label}
  1087. </Label>
  1088. </div>
  1089. );
  1090. },
  1091. Radio: ({ props, bindings, emit }) => {
  1092. const rawOptions = props.options ?? [];
  1093. const options = rawOptions.map((opt) =>
  1094. typeof opt === "string" ? opt : String(opt ?? ""),
  1095. );
  1096. const [boundValue, setBoundValue] = useBoundProp<string>(
  1097. props.value as string | undefined,
  1098. bindings?.value,
  1099. );
  1100. const [localValue, setLocalValue] = useState(options[0] ?? "");
  1101. const isBound = !!bindings?.value;
  1102. const value = isBound ? (boundValue ?? "") : localValue;
  1103. const setValue = isBound ? setBoundValue : setLocalValue;
  1104. return (
  1105. <div className="w-full space-y-2">
  1106. {props.label && <Label>{props.label}</Label>}
  1107. <RadioGroup
  1108. value={value}
  1109. onValueChange={(v) => {
  1110. setValue(v);
  1111. emit("change");
  1112. }}
  1113. >
  1114. {options.map((opt, idx) => (
  1115. <div
  1116. key={`${idx}-${opt}`}
  1117. className="flex items-center space-x-2"
  1118. >
  1119. <RadioGroupItem
  1120. value={opt || `option-${idx}`}
  1121. id={`${props.name}-${idx}-${opt}`}
  1122. />
  1123. <Label
  1124. htmlFor={`${props.name}-${idx}-${opt}`}
  1125. className="cursor-pointer"
  1126. >
  1127. {opt}
  1128. </Label>
  1129. </div>
  1130. ))}
  1131. </RadioGroup>
  1132. </div>
  1133. );
  1134. },
  1135. Switch: ({ props, bindings, emit }) => {
  1136. const [boundChecked, setBoundChecked] = useBoundProp<boolean>(
  1137. props.checked as boolean | undefined,
  1138. bindings?.checked,
  1139. );
  1140. const [localChecked, setLocalChecked] = useState(!!props.checked);
  1141. const isBound = !!bindings?.checked;
  1142. const checked = isBound ? (boundChecked ?? false) : localChecked;
  1143. const setChecked = isBound ? setBoundChecked : setLocalChecked;
  1144. return (
  1145. <div className="w-full flex items-center justify-between space-x-2">
  1146. <Label htmlFor={props.name} className="cursor-pointer">
  1147. {props.label}
  1148. </Label>
  1149. <Switch
  1150. id={props.name}
  1151. checked={checked}
  1152. onCheckedChange={(c) => {
  1153. setChecked(c);
  1154. emit("change");
  1155. }}
  1156. />
  1157. </div>
  1158. );
  1159. },
  1160. Slider: ({ props, bindings, emit }) => {
  1161. const [boundValue, setBoundValue] = useBoundProp<number>(
  1162. props.value as number | undefined,
  1163. bindings?.value,
  1164. );
  1165. const [localValue, setLocalValue] = useState(props.min ?? 0);
  1166. const isBound = !!bindings?.value;
  1167. const value = isBound ? (boundValue ?? props.min ?? 0) : localValue;
  1168. const setValue = isBound ? setBoundValue : setLocalValue;
  1169. return (
  1170. <div className="w-full space-y-2">
  1171. {props.label && (
  1172. <div className="flex justify-between">
  1173. <Label className="text-sm">{props.label}</Label>
  1174. <span className="text-sm text-muted-foreground">{value}</span>
  1175. </div>
  1176. )}
  1177. <Slider
  1178. value={[value]}
  1179. min={props.min ?? 0}
  1180. max={props.max ?? 100}
  1181. step={props.step ?? 1}
  1182. onValueChange={(v) => {
  1183. setValue(v[0] ?? 0);
  1184. emit("change");
  1185. }}
  1186. />
  1187. </div>
  1188. );
  1189. },
  1190. // ── Actions ───────────────────────────────────────────────────────
  1191. Button: ({ props, emit }) => {
  1192. const variant =
  1193. props.variant === "danger"
  1194. ? "destructive"
  1195. : props.variant === "outline"
  1196. ? "outline"
  1197. : props.variant === "secondary"
  1198. ? "secondary"
  1199. : "default";
  1200. return (
  1201. <Button
  1202. variant={variant}
  1203. disabled={props.disabled ?? false}
  1204. onClick={() => emit("press")}
  1205. >
  1206. {props.label}
  1207. </Button>
  1208. );
  1209. },
  1210. Link: ({ props, emit }) => (
  1211. <Button
  1212. variant="link"
  1213. className="h-auto p-0"
  1214. onClick={() => emit("press")}
  1215. >
  1216. {props.label}
  1217. </Button>
  1218. ),
  1219. DropdownMenu: ({ props, emit }) => {
  1220. const items = props.items ?? [];
  1221. return (
  1222. <DropdownMenuPrimitive>
  1223. <DropdownMenuTrigger asChild>
  1224. <Button variant="outline">{props.label}</Button>
  1225. </DropdownMenuTrigger>
  1226. <DropdownMenuContent>
  1227. {items.map((item) => (
  1228. <DropdownMenuItem key={item.value} onClick={() => emit("select")}>
  1229. {item.label}
  1230. </DropdownMenuItem>
  1231. ))}
  1232. </DropdownMenuContent>
  1233. </DropdownMenuPrimitive>
  1234. );
  1235. },
  1236. Toggle: ({ props, bindings, emit }) => {
  1237. const [boundPressed, setBoundPressed] = useBoundProp<boolean>(
  1238. props.pressed as boolean | undefined,
  1239. bindings?.pressed,
  1240. );
  1241. const [localPressed, setLocalPressed] = useState(props.pressed ?? false);
  1242. const isBound = !!bindings?.pressed;
  1243. const pressed = isBound ? (boundPressed ?? false) : localPressed;
  1244. const setPressed = isBound ? setBoundPressed : setLocalPressed;
  1245. return (
  1246. <Toggle
  1247. variant={props.variant ?? "default"}
  1248. pressed={pressed}
  1249. onPressedChange={(v) => {
  1250. setPressed(v);
  1251. emit("change");
  1252. }}
  1253. >
  1254. {props.label}
  1255. </Toggle>
  1256. );
  1257. },
  1258. ToggleGroup: ({ props, bindings, emit }) => {
  1259. const type = props.type ?? "single";
  1260. const items = props.items ?? [];
  1261. const [boundValue, setBoundValue] = useBoundProp<string>(
  1262. props.value as string | undefined,
  1263. bindings?.value,
  1264. );
  1265. const [localValue, setLocalValue] = useState(items[0]?.value ?? "");
  1266. const isBound = !!bindings?.value;
  1267. const value = isBound ? (boundValue ?? "") : localValue;
  1268. const setValue = isBound ? setBoundValue : setLocalValue;
  1269. if (type === "multiple") {
  1270. return (
  1271. <ToggleGroup type="multiple">
  1272. {items.map((item) => (
  1273. <ToggleGroupItem key={item.value} value={item.value}>
  1274. {item.label}
  1275. </ToggleGroupItem>
  1276. ))}
  1277. </ToggleGroup>
  1278. );
  1279. }
  1280. return (
  1281. <ToggleGroup
  1282. type="single"
  1283. value={value}
  1284. onValueChange={(v) => {
  1285. if (v) {
  1286. setValue(v);
  1287. emit("change");
  1288. }
  1289. }}
  1290. >
  1291. {items.map((item) => (
  1292. <ToggleGroupItem key={item.value} value={item.value}>
  1293. {item.label}
  1294. </ToggleGroupItem>
  1295. ))}
  1296. </ToggleGroup>
  1297. );
  1298. },
  1299. ButtonGroup: ({ props, bindings, emit }) => {
  1300. const buttons = props.buttons ?? [];
  1301. const [boundSelected, setBoundSelected] = useBoundProp<string>(
  1302. props.selected as string | undefined,
  1303. bindings?.selected,
  1304. );
  1305. const [localValue, setLocalValue] = useState(buttons[0]?.value ?? "");
  1306. const isBound = !!bindings?.selected;
  1307. const value = isBound ? (boundSelected ?? "") : localValue;
  1308. const setValue = isBound ? setBoundSelected : setLocalValue;
  1309. return (
  1310. <div className="inline-flex rounded-md border border-border">
  1311. {buttons.map((btn, i) => (
  1312. <button
  1313. key={btn.value}
  1314. className={`px-3 py-1.5 text-sm transition-colors ${
  1315. value === btn.value
  1316. ? "bg-primary text-primary-foreground"
  1317. : "bg-background hover:bg-muted"
  1318. } ${i > 0 ? "border-l border-border" : ""} ${
  1319. i === 0 ? "rounded-l-md" : ""
  1320. } ${i === buttons.length - 1 ? "rounded-r-md" : ""}`}
  1321. onClick={() => {
  1322. setValue(btn.value);
  1323. emit("change");
  1324. }}
  1325. >
  1326. {btn.label}
  1327. </button>
  1328. ))}
  1329. </div>
  1330. );
  1331. },
  1332. Pagination: ({ props, bindings, emit }) => {
  1333. const [boundPage, setBoundPage] = useBoundProp<number>(
  1334. props.page as number | undefined,
  1335. bindings?.page,
  1336. );
  1337. const currentPage = boundPage ?? 1;
  1338. const pages = Array.from({ length: props.totalPages }, (_, i) => i + 1);
  1339. return (
  1340. <PaginationPrimitive>
  1341. <PaginationContent>
  1342. <PaginationItem>
  1343. <PaginationPrevious
  1344. href="#"
  1345. onClick={(e) => {
  1346. e.preventDefault();
  1347. if (currentPage > 1) {
  1348. setBoundPage(currentPage - 1);
  1349. emit("change");
  1350. }
  1351. }}
  1352. />
  1353. </PaginationItem>
  1354. {pages.map((page) => (
  1355. <PaginationItem key={page}>
  1356. <PaginationLink
  1357. href="#"
  1358. isActive={page === currentPage}
  1359. onClick={(e) => {
  1360. e.preventDefault();
  1361. setBoundPage(page);
  1362. emit("change");
  1363. }}
  1364. >
  1365. {page}
  1366. </PaginationLink>
  1367. </PaginationItem>
  1368. ))}
  1369. <PaginationItem>
  1370. <PaginationNext
  1371. href="#"
  1372. onClick={(e) => {
  1373. e.preventDefault();
  1374. if (currentPage < props.totalPages) {
  1375. setBoundPage(currentPage + 1);
  1376. emit("change");
  1377. }
  1378. }}
  1379. />
  1380. </PaginationItem>
  1381. </PaginationContent>
  1382. </PaginationPrimitive>
  1383. );
  1384. },
  1385. },
  1386. actions: {
  1387. // Built-in state actions — handled by ActionProvider, stubs needed for types
  1388. setState: async () => {},
  1389. pushState: async () => {},
  1390. removeState: async () => {},
  1391. // Demo actions — show toasts
  1392. buttonClick: async (params) => {
  1393. const message = (params?.message as string) || "Button clicked!";
  1394. toast.success(message);
  1395. },
  1396. formSubmit: async (params) => {
  1397. const formName = (params?.formName as string) || "Form";
  1398. toast.success(`${formName} submitted successfully!`);
  1399. },
  1400. linkClick: async (params) => {
  1401. const href = (params?.href as string) || "#";
  1402. toast.info(`Navigating to: ${href}`);
  1403. },
  1404. },
  1405. });
  1406. // Fallback component for unknown types
  1407. export function Fallback({ type }: { type: string }) {
  1408. return <div className="text-xs text-muted-foreground">[{type}]</div>;
  1409. }