components.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. "use client";
  2. import { useState, type ReactNode } from "react";
  3. import type { z } from "zod";
  4. // shadcn components
  5. import { Button } from "@/components/ui/button";
  6. import { Input } from "@/components/ui/input";
  7. import { Label } from "@/components/ui/label";
  8. import { Textarea } from "@/components/ui/textarea";
  9. import { Checkbox } from "@/components/ui/checkbox";
  10. import { Switch } from "@/components/ui/switch";
  11. import { Progress } from "@/components/ui/progress";
  12. import { Separator } from "@/components/ui/separator";
  13. import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
  14. import { Badge } from "@/components/ui/badge";
  15. import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
  16. import {
  17. Select,
  18. SelectContent,
  19. SelectItem,
  20. SelectTrigger,
  21. SelectValue,
  22. } from "@/components/ui/select";
  23. import { playgroundCatalog } from "../catalog";
  24. // =============================================================================
  25. // Types - Inferred from Catalog
  26. // =============================================================================
  27. type CatalogComponents = typeof playgroundCatalog.data.components;
  28. export type InferProps<K extends keyof CatalogComponents> =
  29. CatalogComponents[K] extends { props: z.ZodType<infer P> } ? P : never;
  30. export interface ComponentContext<K extends keyof CatalogComponents> {
  31. props: InferProps<K>;
  32. children?: ReactNode;
  33. onAction?: (action: {
  34. name: string;
  35. params?: Record<string, unknown>;
  36. }) => void;
  37. loading?: boolean;
  38. }
  39. export type ComponentFn<K extends keyof CatalogComponents> = (
  40. ctx: ComponentContext<K>,
  41. ) => ReactNode;
  42. // =============================================================================
  43. // Components - Type-safe with Catalog using shadcn/ui
  44. // =============================================================================
  45. export const components: { [K in keyof CatalogComponents]: ComponentFn<K> } = {
  46. // Layout Components
  47. Card: ({ props, children }) => {
  48. const maxWidthClass =
  49. props.maxWidth === "sm"
  50. ? "max-w-xs sm:min-w-[280px]"
  51. : props.maxWidth === "md"
  52. ? "max-w-sm sm:min-w-[320px]"
  53. : props.maxWidth === "lg"
  54. ? "max-w-md sm:min-w-[360px]"
  55. : "w-full";
  56. const centeredClass = props.centered ? "mx-auto" : "";
  57. return (
  58. <div
  59. className={`border border-border rounded-lg p-4 bg-card text-card-foreground overflow-hidden ${maxWidthClass} ${centeredClass}`}
  60. >
  61. {props.title && (
  62. <div className="font-semibold text-sm mb-1 text-left">
  63. {props.title}
  64. </div>
  65. )}
  66. {props.description && (
  67. <div className="text-xs text-muted-foreground mb-3 text-left">
  68. {props.description}
  69. </div>
  70. )}
  71. <div className="space-y-3">{children}</div>
  72. </div>
  73. );
  74. },
  75. Stack: ({ props, children }) => {
  76. const isHorizontal = props.direction === "horizontal";
  77. const gapClass =
  78. props.gap === "lg"
  79. ? "gap-4"
  80. : props.gap === "md"
  81. ? "gap-3"
  82. : props.gap === "sm"
  83. ? "gap-2"
  84. : props.gap === "none"
  85. ? "gap-0"
  86. : "gap-3";
  87. const alignClass =
  88. props.align === "center"
  89. ? "items-center"
  90. : props.align === "end"
  91. ? "items-end"
  92. : props.align === "stretch"
  93. ? "items-stretch"
  94. : "items-start";
  95. const justifyClass =
  96. props.justify === "center"
  97. ? "justify-center"
  98. : props.justify === "end"
  99. ? "justify-end"
  100. : props.justify === "between"
  101. ? "justify-between"
  102. : props.justify === "around"
  103. ? "justify-around"
  104. : "";
  105. return (
  106. <div
  107. className={`flex ${isHorizontal ? "flex-row flex-wrap" : "flex-col"} ${gapClass} ${alignClass} ${justifyClass}`}
  108. >
  109. {children}
  110. </div>
  111. );
  112. },
  113. Grid: ({ props, children }) => {
  114. const cols =
  115. props.columns === 6
  116. ? "grid-cols-6"
  117. : props.columns === 5
  118. ? "grid-cols-5"
  119. : props.columns === 4
  120. ? "grid-cols-4"
  121. : props.columns === 3
  122. ? "grid-cols-3"
  123. : props.columns === 2
  124. ? "grid-cols-2"
  125. : "grid-cols-1";
  126. const gridGap =
  127. props.gap === "lg" ? "gap-4" : props.gap === "sm" ? "gap-2" : "gap-3";
  128. return <div className={`grid ${cols} ${gridGap}`}>{children}</div>;
  129. },
  130. Divider: () => <Separator className="my-3" />,
  131. // Form Inputs
  132. Input: ({ props }) => (
  133. <div className="space-y-2">
  134. <Label htmlFor={props.name}>{props.label}</Label>
  135. <Input
  136. id={props.name}
  137. name={props.name}
  138. type={props.type ?? "text"}
  139. placeholder={props.placeholder ?? ""}
  140. />
  141. </div>
  142. ),
  143. Textarea: ({ props }) => (
  144. <div className="space-y-2">
  145. <Label htmlFor={props.name}>{props.label}</Label>
  146. <Textarea
  147. id={props.name}
  148. name={props.name}
  149. placeholder={props.placeholder ?? ""}
  150. rows={props.rows ?? 3}
  151. />
  152. </div>
  153. ),
  154. Select: ({ props }) => {
  155. const [value, setValue] = useState<string>("");
  156. return (
  157. <div className="space-y-2">
  158. <Label>{props.label}</Label>
  159. <Select value={value} onValueChange={setValue}>
  160. <SelectTrigger className="w-full">
  161. <SelectValue placeholder={props.placeholder ?? "Select..."} />
  162. </SelectTrigger>
  163. <SelectContent>
  164. {props.options.map((opt) => (
  165. <SelectItem key={opt} value={opt}>
  166. {opt}
  167. </SelectItem>
  168. ))}
  169. </SelectContent>
  170. </Select>
  171. </div>
  172. );
  173. },
  174. Checkbox: ({ props }) => {
  175. const [checked, setChecked] = useState(!!props.checked);
  176. return (
  177. <div className="flex items-center space-x-2">
  178. <Checkbox
  179. id={props.name}
  180. checked={checked}
  181. onCheckedChange={(c) => setChecked(c === true)}
  182. />
  183. <Label htmlFor={props.name} className="cursor-pointer">
  184. {props.label}
  185. </Label>
  186. </div>
  187. );
  188. },
  189. Radio: ({ props }) => {
  190. const [value, setValue] = useState(props.options[0] ?? "");
  191. return (
  192. <div className="space-y-2">
  193. {props.label && <Label>{props.label}</Label>}
  194. <RadioGroup value={value} onValueChange={setValue}>
  195. {props.options.map((opt) => (
  196. <div key={opt} className="flex items-center space-x-2">
  197. <RadioGroupItem value={opt} id={`${props.name}-${opt}`} />
  198. <Label
  199. htmlFor={`${props.name}-${opt}`}
  200. className="cursor-pointer"
  201. >
  202. {opt}
  203. </Label>
  204. </div>
  205. ))}
  206. </RadioGroup>
  207. </div>
  208. );
  209. },
  210. Switch: ({ props }) => {
  211. const [checked, setChecked] = useState(!!props.checked);
  212. return (
  213. <div className="flex items-center justify-between space-x-2">
  214. <Label htmlFor={props.name} className="cursor-pointer">
  215. {props.label}
  216. </Label>
  217. <Switch
  218. id={props.name}
  219. checked={checked}
  220. onCheckedChange={setChecked}
  221. />
  222. </div>
  223. );
  224. },
  225. // Actions
  226. Button: ({ props, onAction, loading }) => {
  227. const variant =
  228. props.variant === "danger"
  229. ? "destructive"
  230. : props.variant === "secondary"
  231. ? "secondary"
  232. : "default";
  233. return (
  234. <Button
  235. variant={variant}
  236. disabled={loading}
  237. onClick={() =>
  238. onAction?.({
  239. name: props.action ?? "buttonClick",
  240. params: props.actionParams ?? { message: props.label },
  241. })
  242. }
  243. >
  244. {loading ? "..." : props.label}
  245. </Button>
  246. );
  247. },
  248. Link: ({ props, onAction }) => (
  249. <Button
  250. variant="link"
  251. className="h-auto p-0"
  252. onClick={() =>
  253. onAction?.({
  254. name: "linkClick",
  255. params: { href: props.href },
  256. })
  257. }
  258. >
  259. {props.label}
  260. </Button>
  261. ),
  262. // Typography
  263. Heading: ({ props }) => {
  264. const level = props.level ?? "h2";
  265. const headingClass =
  266. level === "h1"
  267. ? "text-2xl font-bold"
  268. : level === "h3"
  269. ? "text-base font-semibold"
  270. : level === "h4"
  271. ? "text-sm font-semibold"
  272. : "text-lg font-semibold";
  273. if (level === "h1")
  274. return <h1 className={`${headingClass} text-left`}>{props.text}</h1>;
  275. if (level === "h3")
  276. return <h3 className={`${headingClass} text-left`}>{props.text}</h3>;
  277. if (level === "h4")
  278. return <h4 className={`${headingClass} text-left`}>{props.text}</h4>;
  279. return <h2 className={`${headingClass} text-left`}>{props.text}</h2>;
  280. },
  281. Text: ({ props }) => {
  282. const textClass =
  283. props.variant === "caption"
  284. ? "text-xs"
  285. : props.variant === "muted"
  286. ? "text-sm text-muted-foreground"
  287. : "text-sm";
  288. return <p className={`${textClass} text-left`}>{props.text}</p>;
  289. },
  290. // Data Display
  291. Image: ({ props }) => {
  292. const imgStyle = {
  293. width: props.width ?? 80,
  294. height: props.height ?? 60,
  295. };
  296. return (
  297. <div
  298. className="bg-muted border border-border rounded flex items-center justify-center text-xs text-muted-foreground aspect-video"
  299. style={imgStyle}
  300. >
  301. {props.alt || "img"}
  302. </div>
  303. );
  304. },
  305. Avatar: ({ props }) => {
  306. const name = props.name || "?";
  307. const initials = name
  308. .split(" ")
  309. .map((n) => n[0])
  310. .join("")
  311. .slice(0, 2)
  312. .toUpperCase();
  313. const avatarSize =
  314. props.size === "lg"
  315. ? "w-12 h-12 text-base"
  316. : props.size === "sm"
  317. ? "w-8 h-8 text-xs"
  318. : "w-10 h-10 text-sm";
  319. return (
  320. <div
  321. className={`${avatarSize} rounded-full bg-muted flex items-center justify-center font-medium`}
  322. >
  323. {initials}
  324. </div>
  325. );
  326. },
  327. Badge: ({ props }) => {
  328. const variant =
  329. props.variant === "success" || props.variant === "warning"
  330. ? "secondary"
  331. : props.variant === "danger"
  332. ? "destructive"
  333. : "default";
  334. // Add custom colors for success/warning
  335. const customClass =
  336. props.variant === "success"
  337. ? "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-100"
  338. : props.variant === "warning"
  339. ? "bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-100"
  340. : "";
  341. return (
  342. <Badge variant={variant} className={customClass}>
  343. {props.text}
  344. </Badge>
  345. );
  346. },
  347. Alert: ({ props }) => {
  348. const variant = props.type === "error" ? "destructive" : "default";
  349. // Custom colors for different alert types
  350. const customClass =
  351. props.type === "success"
  352. ? "border-green-200 bg-green-50 text-green-900 dark:border-green-800 dark:bg-green-950 dark:text-green-100"
  353. : props.type === "warning"
  354. ? "border-yellow-200 bg-yellow-50 text-yellow-900 dark:border-yellow-800 dark:bg-yellow-950 dark:text-yellow-100"
  355. : props.type === "info"
  356. ? "border-blue-200 bg-blue-50 text-blue-900 dark:border-blue-800 dark:bg-blue-950 dark:text-blue-100"
  357. : "";
  358. return (
  359. <Alert variant={variant} className={customClass}>
  360. <AlertTitle>{props.title}</AlertTitle>
  361. {props.message && <AlertDescription>{props.message}</AlertDescription>}
  362. </Alert>
  363. );
  364. },
  365. Progress: ({ props }) => {
  366. const value = Math.min(100, Math.max(0, props.value || 0));
  367. return (
  368. <div className="space-y-2">
  369. {props.label && (
  370. <Label className="text-sm text-muted-foreground">{props.label}</Label>
  371. )}
  372. <Progress value={value} />
  373. </div>
  374. );
  375. },
  376. Rating: ({ props }) => {
  377. const ratingValue = props.value || 0;
  378. const maxRating = props.max ?? 5;
  379. return (
  380. <div className="space-y-2">
  381. {props.label && (
  382. <Label className="text-sm text-muted-foreground">{props.label}</Label>
  383. )}
  384. <div className="flex gap-1">
  385. {Array.from({ length: maxRating }).map((_, i) => (
  386. <span
  387. key={i}
  388. className={`text-lg ${i < ratingValue ? "text-yellow-400" : "text-muted"}`}
  389. >
  390. *
  391. </span>
  392. ))}
  393. </div>
  394. </div>
  395. );
  396. },
  397. // Charts
  398. BarGraph: ({ props }) => {
  399. const data = props.data || [];
  400. const maxValue = Math.max(...data.map((d) => d.value), 1);
  401. return (
  402. <div className="space-y-2">
  403. {props.title && (
  404. <div className="text-sm font-medium text-left">{props.title}</div>
  405. )}
  406. <div className="flex gap-2">
  407. {data.map((d, i) => (
  408. <div key={i} className="flex-1 flex flex-col items-center gap-1">
  409. <div className="text-xs text-muted-foreground">{d.value}</div>
  410. <div className="w-full h-24 flex items-end">
  411. <div
  412. className="w-full bg-primary rounded-t transition-all"
  413. style={{
  414. height: `${(d.value / maxValue) * 100}%`,
  415. minHeight: 2,
  416. }}
  417. />
  418. </div>
  419. <div className="text-xs text-muted-foreground truncate w-full text-center">
  420. {d.label}
  421. </div>
  422. </div>
  423. ))}
  424. </div>
  425. </div>
  426. );
  427. },
  428. LineGraph: ({ props }) => {
  429. const data = props.data || [];
  430. const maxValue = Math.max(...data.map((d) => d.value));
  431. const minValue = Math.min(...data.map((d) => d.value));
  432. const range = maxValue - minValue || 1;
  433. const width = 300;
  434. const height = 100;
  435. const padding = { top: 10, right: 10, bottom: 10, left: 10 };
  436. const chartWidth = width - padding.left - padding.right;
  437. const chartHeight = height - padding.top - padding.bottom;
  438. const points = data.map((d, i) => {
  439. const x =
  440. padding.left +
  441. (data.length > 1
  442. ? (i / (data.length - 1)) * chartWidth
  443. : chartWidth / 2);
  444. const y =
  445. padding.top +
  446. chartHeight -
  447. ((d.value - minValue) / range) * chartHeight;
  448. return { x, y, ...d };
  449. });
  450. const pathD =
  451. points.length > 0
  452. ? `M ${points.map((p) => `${p.x} ${p.y}`).join(" L ")}`
  453. : "";
  454. return (
  455. <div className="space-y-2">
  456. {props.title && (
  457. <div className="text-sm font-medium text-left">{props.title}</div>
  458. )}
  459. <div className="relative h-28">
  460. <svg viewBox={`0 0 ${width} ${height}`} className="w-full h-full">
  461. <line
  462. x1={padding.left}
  463. y1={padding.top + chartHeight / 2}
  464. x2={width - padding.right}
  465. y2={padding.top + chartHeight / 2}
  466. stroke="currentColor"
  467. strokeOpacity="0.1"
  468. strokeWidth="1"
  469. />
  470. <line
  471. x1={padding.left}
  472. y1={padding.top}
  473. x2={width - padding.right}
  474. y2={padding.top}
  475. stroke="currentColor"
  476. strokeOpacity="0.1"
  477. strokeWidth="1"
  478. />
  479. <line
  480. x1={padding.left}
  481. y1={height - padding.bottom}
  482. x2={width - padding.right}
  483. y2={height - padding.bottom}
  484. stroke="currentColor"
  485. strokeOpacity="0.1"
  486. strokeWidth="1"
  487. />
  488. {pathD && (
  489. <path
  490. d={pathD}
  491. fill="none"
  492. stroke="currentColor"
  493. strokeWidth="2"
  494. strokeLinecap="round"
  495. strokeLinejoin="round"
  496. className="text-primary"
  497. />
  498. )}
  499. {points.map((p, i) => (
  500. <circle
  501. key={i}
  502. cx={p.x}
  503. cy={p.y}
  504. r="4"
  505. className="fill-primary"
  506. />
  507. ))}
  508. </svg>
  509. </div>
  510. {data.length > 0 && (
  511. <div className="flex justify-between">
  512. {data.map((d, i) => (
  513. <div
  514. key={i}
  515. className="text-xs text-muted-foreground text-center"
  516. style={{ width: `${100 / data.length}%` }}
  517. >
  518. {d.label}
  519. </div>
  520. ))}
  521. </div>
  522. )}
  523. </div>
  524. );
  525. },
  526. };
  527. // Fallback component for unknown types
  528. export function Fallback({ type }: { type: string }) {
  529. return <div className="text-xs text-muted-foreground">[{type}]</div>;
  530. }