From 0ae21e2c8929651b4483b174720b92f69961301b Mon Sep 17 00:00:00 2001 From: Suhail Kakar Date: Wed, 17 Jul 2024 17:36:23 +0530 Subject: [PATCH 1/5] ui: add card, chart and select component --- packages/www/components/ui/card.tsx | 82 ++++++ packages/www/components/ui/chart.tsx | 357 ++++++++++++++++++++++++++ packages/www/components/ui/select.tsx | 158 ++++++++++++ packages/www/package.json | 2 + 4 files changed, 599 insertions(+) create mode 100644 packages/www/components/ui/card.tsx create mode 100644 packages/www/components/ui/chart.tsx create mode 100644 packages/www/components/ui/select.tsx diff --git a/packages/www/components/ui/card.tsx b/packages/www/components/ui/card.tsx new file mode 100644 index 000000000..3c0ac8edd --- /dev/null +++ b/packages/www/components/ui/card.tsx @@ -0,0 +1,82 @@ +import * as React from "react"; + +import { cn } from "lib/cn"; + +const Card = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)); +Card.displayName = "Card"; + +const CardHeader = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)); +CardHeader.displayName = "CardHeader"; + +const CardTitle = React.forwardRef< + HTMLParagraphElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +

+)); +CardTitle.displayName = "CardTitle"; + +const CardDescription = React.forwardRef< + HTMLParagraphElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +

+)); +CardDescription.displayName = "CardDescription"; + +const CardContent = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +

+)); +CardContent.displayName = "CardContent"; + +const CardFooter = React.forwardRef< + HTMLDivElement, + React.HTMLAttributes +>(({ className, ...props }, ref) => ( +
+)); +CardFooter.displayName = "CardFooter"; + +export { + Card, + CardHeader, + CardFooter, + CardTitle, + CardDescription, + CardContent, +}; diff --git a/packages/www/components/ui/chart.tsx b/packages/www/components/ui/chart.tsx new file mode 100644 index 000000000..33e4ae267 --- /dev/null +++ b/packages/www/components/ui/chart.tsx @@ -0,0 +1,357 @@ +import * as React from "react"; +import * as RechartsPrimitive from "recharts"; + +import { cn } from "lib/cn"; + +// Format: { THEME_NAME: CSS_SELECTOR } +const THEMES = { light: "", dark: ".dark" } as const; + +export type ChartConfig = { + [k in string]: { + label?: React.ReactNode; + icon?: React.ComponentType; + } & ( + | { color?: string; theme?: never } + | { color?: never; theme: Record } + ); +}; + +type ChartContextProps = { + config: ChartConfig; +}; + +const ChartContext = React.createContext(null); + +function useChart() { + const context = React.useContext(ChartContext); + + if (!context) { + throw new Error("useChart must be used within a "); + } + + return context; +} + +const ChartContainer = React.forwardRef< + HTMLDivElement, + React.ComponentProps<"div"> & { + config: ChartConfig; + children: React.ComponentProps< + typeof RechartsPrimitive.ResponsiveContainer + >["children"]; + } +>(({ id, className, children, config, ...props }, ref) => { + const uniqueId = React.useId(); + const chartId = `chart-${id || uniqueId.replace(/:/g, "")}`; + + return ( + +
+ + + {children} + +
+
+ ); +}); +ChartContainer.displayName = "Chart"; + +const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => { + const colorConfig = Object.entries(config).filter( + ([_, config]) => config.theme || config.color + ); + + if (!colorConfig.length) { + return null; + } + + return ( +