@aereamart/design-system 0.3.6 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. package/dist/cn.d.ts +2 -1
  2. package/dist/cn.js +4 -2
  3. package/dist/hooks/use-mobile.d.ts +1 -0
  4. package/dist/hooks/use-mobile.js +15 -0
  5. package/dist/index.d.ts +48 -37
  6. package/dist/index.js +48 -37
  7. package/dist/ui/accordion.d.ts +7 -0
  8. package/dist/ui/accordion.js +17 -0
  9. package/dist/ui/alert-dialog.d.ts +18 -0
  10. package/dist/ui/alert-dialog.js +42 -0
  11. package/dist/ui/alert.d.ts +9 -0
  12. package/dist/ui/alert.js +24 -0
  13. package/dist/ui/aspect-ratio.d.ts +3 -0
  14. package/dist/ui/aspect-ratio.js +6 -0
  15. package/dist/ui/avatar.d.ts +11 -0
  16. package/dist/ui/avatar.js +23 -0
  17. package/dist/ui/badge.d.ts +9 -0
  18. package/dist/ui/badge.js +24 -0
  19. package/dist/ui/breadcrumb.d.ts +11 -0
  20. package/dist/ui/breadcrumb.js +27 -0
  21. package/dist/ui/button.d.ts +10 -0
  22. package/dist/ui/button.js +35 -0
  23. package/dist/ui/calendar.d.ts +8 -0
  24. package/dist/ui/calendar.js +75 -0
  25. package/dist/ui/card.d.ts +9 -0
  26. package/dist/ui/card.js +24 -0
  27. package/dist/ui/carousel.d.ts +19 -0
  28. package/dist/ui/carousel.js +91 -0
  29. package/dist/ui/chart.d.ts +44 -0
  30. package/dist/ui/chart.js +132 -0
  31. package/dist/ui/checkbox.d.ts +4 -0
  32. package/dist/ui/checkbox.js +8 -0
  33. package/dist/ui/collapsible.d.ts +5 -0
  34. package/dist/ui/collapsible.js +13 -0
  35. package/dist/ui/command.d.ts +18 -0
  36. package/dist/ui/command.js +34 -0
  37. package/dist/ui/context-menu.d.ts +25 -0
  38. package/dist/ui/context-menu.js +51 -0
  39. package/dist/ui/dialog.d.ts +17 -0
  40. package/dist/ui/dialog.js +36 -0
  41. package/dist/ui/drawer.d.ts +13 -0
  42. package/dist/ui/drawer.js +35 -0
  43. package/dist/ui/dropdown-menu.d.ts +25 -0
  44. package/dist/ui/dropdown-menu.js +50 -0
  45. package/dist/ui/form.d.ts +24 -0
  46. package/dist/ui/form.js +58 -0
  47. package/dist/ui/hover-card.d.ts +6 -0
  48. package/dist/ui/hover-card.js +13 -0
  49. package/dist/ui/input-otp.d.ts +11 -0
  50. package/dist/ui/input-otp.js +21 -0
  51. package/dist/ui/input.d.ts +3 -0
  52. package/dist/ui/input.js +6 -0
  53. package/dist/ui/label.d.ts +4 -0
  54. package/dist/ui/label.js +8 -0
  55. package/dist/ui/menubar.d.ts +26 -0
  56. package/dist/ui/menubar.js +53 -0
  57. package/dist/ui/navigation-menu.d.ts +14 -0
  58. package/dist/ui/navigation-menu.js +31 -0
  59. package/dist/ui/pagination.d.ts +13 -0
  60. package/dist/ui/pagination.js +29 -0
  61. package/dist/ui/popover.d.ts +10 -0
  62. package/dist/ui/popover.js +26 -0
  63. package/dist/ui/progress.d.ts +4 -0
  64. package/dist/ui/progress.js +7 -0
  65. package/dist/ui/radio-group.d.ts +5 -0
  66. package/dist/ui/radio-group.js +12 -0
  67. package/dist/ui/resizable.d.ts +7 -0
  68. package/dist/ui/resizable.js +14 -0
  69. package/dist/ui/scroll-area.d.ts +5 -0
  70. package/dist/ui/scroll-area.js +12 -0
  71. package/dist/ui/select.d.ts +15 -0
  72. package/dist/ui/select.js +38 -0
  73. package/dist/ui/separator.d.ts +4 -0
  74. package/dist/ui/separator.js +7 -0
  75. package/dist/ui/sheet.d.ts +14 -0
  76. package/dist/ui/sheet.js +40 -0
  77. package/dist/ui/sidebar.d.ts +69 -0
  78. package/dist/ui/sidebar.js +211 -0
  79. package/dist/ui/skeleton.d.ts +2 -0
  80. package/dist/ui/skeleton.js +6 -0
  81. package/dist/ui/slider.d.ts +4 -0
  82. package/dist/ui/slider.js +13 -0
  83. package/dist/ui/sonner.d.ts +3 -0
  84. package/dist/ui/sonner.js +21 -0
  85. package/dist/ui/switch.d.ts +6 -0
  86. package/dist/ui/switch.js +7 -0
  87. package/dist/ui/table.d.ts +10 -0
  88. package/dist/ui/table.js +28 -0
  89. package/dist/ui/tabs.d.ts +11 -0
  90. package/dist/ui/tabs.js +28 -0
  91. package/dist/ui/textarea.d.ts +3 -0
  92. package/dist/ui/textarea.js +6 -0
  93. package/dist/ui/toggle-group.d.ts +9 -0
  94. package/dist/ui/toggle-group.js +22 -0
  95. package/dist/ui/toggle.d.ts +9 -0
  96. package/dist/ui/toggle.js +26 -0
  97. package/dist/ui/tooltip.d.ts +7 -0
  98. package/dist/ui/tooltip.js +16 -0
  99. package/package.json +28 -4
  100. package/src/cn.ts +5 -2
  101. package/src/hooks/use-mobile.ts +19 -0
  102. package/src/index.css +140 -0
  103. package/src/index.ts +48 -37
  104. package/src/ui/accordion.tsx +63 -0
  105. package/src/ui/alert-dialog.tsx +196 -0
  106. package/src/ui/alert.tsx +65 -0
  107. package/src/ui/aspect-ratio.tsx +9 -0
  108. package/src/ui/avatar.tsx +108 -0
  109. package/src/ui/badge.tsx +47 -0
  110. package/src/ui/breadcrumb.tsx +108 -0
  111. package/src/ui/button.tsx +63 -0
  112. package/src/ui/calendar.tsx +218 -0
  113. package/src/ui/card.tsx +91 -0
  114. package/src/ui/carousel.tsx +241 -0
  115. package/src/ui/chart.tsx +371 -0
  116. package/src/ui/checkbox.tsx +29 -0
  117. package/src/ui/collapsible.tsx +33 -0
  118. package/src/ui/command.tsx +184 -0
  119. package/src/ui/context-menu.tsx +251 -0
  120. package/src/ui/dialog.tsx +156 -0
  121. package/src/ui/drawer.tsx +134 -0
  122. package/src/ui/dropdown-menu.tsx +254 -0
  123. package/src/ui/form.tsx +165 -0
  124. package/src/ui/hover-card.tsx +41 -0
  125. package/src/ui/input-otp.tsx +76 -0
  126. package/src/ui/input.tsx +20 -0
  127. package/src/ui/label.tsx +23 -0
  128. package/src/ui/menubar.tsx +273 -0
  129. package/src/ui/navigation-menu.tsx +167 -0
  130. package/src/ui/pagination.tsx +127 -0
  131. package/src/ui/popover.tsx +88 -0
  132. package/src/ui/progress.tsx +28 -0
  133. package/src/ui/radio-group.tsx +44 -0
  134. package/src/ui/resizable.tsx +50 -0
  135. package/src/ui/scroll-area.tsx +55 -0
  136. package/src/ui/select.tsx +189 -0
  137. package/src/ui/separator.tsx +25 -0
  138. package/src/ui/sheet.tsx +142 -0
  139. package/src/ui/sidebar.tsx +724 -0
  140. package/src/ui/skeleton.tsx +13 -0
  141. package/src/ui/slider.tsx +60 -0
  142. package/src/ui/sonner.tsx +40 -0
  143. package/src/ui/switch.tsx +32 -0
  144. package/src/ui/table.tsx +115 -0
  145. package/src/ui/tabs.tsx +88 -0
  146. package/src/ui/textarea.tsx +17 -0
  147. package/src/ui/toggle-group.tsx +83 -0
  148. package/src/ui/toggle.tsx +46 -0
  149. package/src/ui/tooltip.tsx +54 -0
  150. package/dist/Accordion.d.ts +0 -10
  151. package/dist/Accordion.js +0 -12
  152. package/dist/AlertBanner.d.ts +0 -9
  153. package/dist/AlertBanner.js +0 -21
  154. package/dist/AppBar.d.ts +0 -7
  155. package/dist/AppBar.js +0 -5
  156. package/dist/Avatar.d.ts +0 -6
  157. package/dist/Avatar.js +0 -20
  158. package/dist/Badge.d.ts +0 -6
  159. package/dist/Badge.js +0 -11
  160. package/dist/BottomSheet.d.ts +0 -8
  161. package/dist/BottomSheet.js +0 -24
  162. package/dist/Breadcrumb.d.ts +0 -8
  163. package/dist/Breadcrumb.js +0 -9
  164. package/dist/Button.d.ts +0 -9
  165. package/dist/Button.js +0 -18
  166. package/dist/Card.d.ts +0 -7
  167. package/dist/Card.js +0 -6
  168. package/dist/Checkbox.d.ts +0 -5
  169. package/dist/Checkbox.js +0 -5
  170. package/dist/Chip.d.ts +0 -7
  171. package/dist/Chip.js +0 -15
  172. package/dist/DataTable.d.ts +0 -15
  173. package/dist/DataTable.js +0 -7
  174. package/dist/DatePicker.d.ts +0 -5
  175. package/dist/DatePicker.js +0 -11
  176. package/dist/Divider.d.ts +0 -5
  177. package/dist/Divider.js +0 -11
  178. package/dist/Drawer.d.ts +0 -10
  179. package/dist/Drawer.js +0 -24
  180. package/dist/EmptyState.d.ts +0 -7
  181. package/dist/EmptyState.js +0 -6
  182. package/dist/Input.d.ts +0 -5
  183. package/dist/Input.js +0 -7
  184. package/dist/Link.d.ts +0 -4
  185. package/dist/Link.js +0 -6
  186. package/dist/Menu.d.ts +0 -14
  187. package/dist/Menu.js +0 -29
  188. package/dist/Modal.d.ts +0 -9
  189. package/dist/Modal.js +0 -24
  190. package/dist/NavigationBar.d.ts +0 -14
  191. package/dist/NavigationBar.js +0 -22
  192. package/dist/Pagination.d.ts +0 -7
  193. package/dist/Pagination.js +0 -24
  194. package/dist/Popover.d.ts +0 -7
  195. package/dist/Popover.js +0 -9
  196. package/dist/Progress.d.ts +0 -8
  197. package/dist/Progress.js +0 -15
  198. package/dist/Radio.d.ts +0 -15
  199. package/dist/Radio.js +0 -8
  200. package/dist/SegmentedControl.d.ts +0 -13
  201. package/dist/SegmentedControl.js +0 -18
  202. package/dist/Select.d.ts +0 -5
  203. package/dist/Select.js +0 -7
  204. package/dist/Sidebar.d.ts +0 -16
  205. package/dist/Sidebar.js +0 -19
  206. package/dist/Slider.d.ts +0 -5
  207. package/dist/Slider.js +0 -5
  208. package/dist/Snackbar.d.ts +0 -12
  209. package/dist/Snackbar.js +0 -20
  210. package/dist/Spinner.d.ts +0 -3
  211. package/dist/Spinner.js +0 -5
  212. package/dist/Switch.d.ts +0 -4
  213. package/dist/Switch.js +0 -5
  214. package/dist/Tabs.d.ts +0 -14
  215. package/dist/Tabs.js +0 -24
  216. package/dist/Tag.d.ts +0 -6
  217. package/dist/Tag.js +0 -11
  218. package/dist/Textarea.d.ts +0 -5
  219. package/dist/Textarea.js +0 -7
  220. package/dist/Tooltip.d.ts +0 -7
  221. package/dist/Tooltip.js +0 -5
  222. package/dist/TreeView.d.ts +0 -12
  223. package/dist/TreeView.js +0 -34
  224. package/src/Accordion.tsx +0 -58
  225. package/src/AlertBanner.tsx +0 -62
  226. package/src/AppBar.tsx +0 -29
  227. package/src/Avatar.tsx +0 -42
  228. package/src/Badge.tsx +0 -27
  229. package/src/BottomSheet.tsx +0 -60
  230. package/src/Breadcrumb.tsx +0 -48
  231. package/src/Button.tsx +0 -54
  232. package/src/Card.tsx +0 -39
  233. package/src/Checkbox.tsx +0 -31
  234. package/src/Chip.tsx +0 -44
  235. package/src/DataTable.tsx +0 -64
  236. package/src/DatePicker.tsx +0 -44
  237. package/src/Divider.tsx +0 -33
  238. package/src/Drawer.tsx +0 -71
  239. package/src/EmptyState.tsx +0 -37
  240. package/src/Input.tsx +0 -30
  241. package/src/Link.tsx +0 -22
  242. package/src/Menu.tsx +0 -73
  243. package/src/Modal.tsx +0 -62
  244. package/src/NavigationBar.tsx +0 -68
  245. package/src/Pagination.tsx +0 -69
  246. package/src/Popover.tsx +0 -34
  247. package/src/Progress.tsx +0 -53
  248. package/src/Radio.tsx +0 -49
  249. package/src/SegmentedControl.tsx +0 -63
  250. package/src/Select.tsx +0 -32
  251. package/src/Sidebar.tsx +0 -73
  252. package/src/Slider.tsx +0 -33
  253. package/src/Snackbar.tsx +0 -72
  254. package/src/Spinner.tsx +0 -19
  255. package/src/Switch.tsx +0 -40
  256. package/src/Tabs.tsx +0 -77
  257. package/src/Tag.tsx +0 -29
  258. package/src/Textarea.tsx +0 -30
  259. package/src/Tooltip.tsx +0 -25
  260. package/src/TreeView.tsx +0 -105
@@ -0,0 +1,91 @@
1
+ import * as React from "react"
2
+ import { cn } from "../cn"
3
+
4
+ function Card({ className, ...props }: React.ComponentProps<"div">) {
5
+ return (
6
+ <div
7
+ data-slot="card"
8
+ className={cn(
9
+ "flex flex-col gap-6 rounded-xl border bg-card py-6 text-card-foreground shadow-sm",
10
+ className
11
+ )}
12
+ {...props}
13
+ />
14
+ )
15
+ }
16
+
17
+ function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
18
+ return (
19
+ <div
20
+ data-slot="card-header"
21
+ className={cn(
22
+ "@container/card-header grid auto-rows-min grid-rows-[auto_auto] items-start gap-2 px-6 has-data-[slot=card-action]:grid-cols-[1fr_auto] [.border-b]:pb-6",
23
+ className
24
+ )}
25
+ {...props}
26
+ />
27
+ )
28
+ }
29
+
30
+ function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
31
+ return (
32
+ <div
33
+ data-slot="card-title"
34
+ className={cn("leading-none font-semibold", className)}
35
+ {...props}
36
+ />
37
+ )
38
+ }
39
+
40
+ function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
41
+ return (
42
+ <div
43
+ data-slot="card-description"
44
+ className={cn("text-sm text-muted-foreground", className)}
45
+ {...props}
46
+ />
47
+ )
48
+ }
49
+
50
+ function CardAction({ className, ...props }: React.ComponentProps<"div">) {
51
+ return (
52
+ <div
53
+ data-slot="card-action"
54
+ className={cn(
55
+ "col-start-2 row-span-2 row-start-1 self-start justify-self-end",
56
+ className
57
+ )}
58
+ {...props}
59
+ />
60
+ )
61
+ }
62
+
63
+ function CardContent({ className, ...props }: React.ComponentProps<"div">) {
64
+ return (
65
+ <div
66
+ data-slot="card-content"
67
+ className={cn("px-6", className)}
68
+ {...props}
69
+ />
70
+ )
71
+ }
72
+
73
+ function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
74
+ return (
75
+ <div
76
+ data-slot="card-footer"
77
+ className={cn("flex items-center px-6 [.border-t]:pt-6", className)}
78
+ {...props}
79
+ />
80
+ )
81
+ }
82
+
83
+ export {
84
+ Card,
85
+ CardHeader,
86
+ CardFooter,
87
+ CardTitle,
88
+ CardAction,
89
+ CardDescription,
90
+ CardContent,
91
+ }
@@ -0,0 +1,241 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { cn } from "../cn"
5
+ import useEmblaCarousel, {
6
+ type UseEmblaCarouselType,
7
+ } from "embla-carousel-react"
8
+ import { ArrowLeft, ArrowRight } from "lucide-react"
9
+
10
+ import { Button } from "./button"
11
+
12
+ type CarouselApi = UseEmblaCarouselType[1]
13
+ type UseCarouselParameters = Parameters<typeof useEmblaCarousel>
14
+ type CarouselOptions = UseCarouselParameters[0]
15
+ type CarouselPlugin = UseCarouselParameters[1]
16
+
17
+ type CarouselProps = {
18
+ opts?: CarouselOptions
19
+ plugins?: CarouselPlugin
20
+ orientation?: "horizontal" | "vertical"
21
+ setApi?: (api: CarouselApi) => void
22
+ }
23
+
24
+ type CarouselContextProps = {
25
+ carouselRef: ReturnType<typeof useEmblaCarousel>[0]
26
+ api: ReturnType<typeof useEmblaCarousel>[1]
27
+ scrollPrev: () => void
28
+ scrollNext: () => void
29
+ canScrollPrev: boolean
30
+ canScrollNext: boolean
31
+ } & CarouselProps
32
+
33
+ const CarouselContext = React.createContext<CarouselContextProps | null>(null)
34
+
35
+ function useCarousel() {
36
+ const context = React.useContext(CarouselContext)
37
+
38
+ if (!context) {
39
+ throw new Error("useCarousel must be used within a <Carousel />")
40
+ }
41
+
42
+ return context
43
+ }
44
+
45
+ function Carousel({
46
+ orientation = "horizontal",
47
+ opts,
48
+ setApi,
49
+ plugins,
50
+ className,
51
+ children,
52
+ ...props
53
+ }: React.ComponentProps<"div"> & CarouselProps) {
54
+ const [carouselRef, api] = useEmblaCarousel(
55
+ {
56
+ ...opts,
57
+ axis: orientation === "horizontal" ? "x" : "y",
58
+ },
59
+ plugins
60
+ )
61
+ const [canScrollPrev, setCanScrollPrev] = React.useState(false)
62
+ const [canScrollNext, setCanScrollNext] = React.useState(false)
63
+
64
+ const onSelect = React.useCallback((api: CarouselApi) => {
65
+ if (!api) return
66
+ setCanScrollPrev(api.canScrollPrev())
67
+ setCanScrollNext(api.canScrollNext())
68
+ }, [])
69
+
70
+ const scrollPrev = React.useCallback(() => {
71
+ api?.scrollPrev()
72
+ }, [api])
73
+
74
+ const scrollNext = React.useCallback(() => {
75
+ api?.scrollNext()
76
+ }, [api])
77
+
78
+ const handleKeyDown = React.useCallback(
79
+ (event: React.KeyboardEvent<HTMLDivElement>) => {
80
+ if (event.key === "ArrowLeft") {
81
+ event.preventDefault()
82
+ scrollPrev()
83
+ } else if (event.key === "ArrowRight") {
84
+ event.preventDefault()
85
+ scrollNext()
86
+ }
87
+ },
88
+ [scrollPrev, scrollNext]
89
+ )
90
+
91
+ React.useEffect(() => {
92
+ if (!api || !setApi) return
93
+ setApi(api)
94
+ }, [api, setApi])
95
+
96
+ React.useEffect(() => {
97
+ if (!api) return
98
+ onSelect(api)
99
+ api.on("reInit", onSelect)
100
+ api.on("select", onSelect)
101
+
102
+ return () => {
103
+ api?.off("select", onSelect)
104
+ }
105
+ }, [api, onSelect])
106
+
107
+ return (
108
+ <CarouselContext.Provider
109
+ value={{
110
+ carouselRef,
111
+ api: api,
112
+ opts,
113
+ orientation:
114
+ orientation || (opts?.axis === "y" ? "vertical" : "horizontal"),
115
+ scrollPrev,
116
+ scrollNext,
117
+ canScrollPrev,
118
+ canScrollNext,
119
+ }}
120
+ >
121
+ <div
122
+ onKeyDownCapture={handleKeyDown}
123
+ className={cn("relative", className)}
124
+ role="region"
125
+ aria-roledescription="carousel"
126
+ data-slot="carousel"
127
+ {...props}
128
+ >
129
+ {children}
130
+ </div>
131
+ </CarouselContext.Provider>
132
+ )
133
+ }
134
+
135
+ function CarouselContent({ className, ...props }: React.ComponentProps<"div">) {
136
+ const { carouselRef, orientation } = useCarousel()
137
+
138
+ return (
139
+ <div
140
+ ref={carouselRef}
141
+ className="overflow-hidden"
142
+ data-slot="carousel-content"
143
+ >
144
+ <div
145
+ className={cn(
146
+ "flex",
147
+ orientation === "horizontal" ? "-ml-4" : "-mt-4 flex-col",
148
+ className
149
+ )}
150
+ {...props}
151
+ />
152
+ </div>
153
+ )
154
+ }
155
+
156
+ function CarouselItem({ className, ...props }: React.ComponentProps<"div">) {
157
+ const { orientation } = useCarousel()
158
+
159
+ return (
160
+ <div
161
+ role="group"
162
+ aria-roledescription="slide"
163
+ data-slot="carousel-item"
164
+ className={cn(
165
+ "min-w-0 shrink-0 grow-0 basis-full",
166
+ orientation === "horizontal" ? "pl-4" : "pt-4",
167
+ className
168
+ )}
169
+ {...props}
170
+ />
171
+ )
172
+ }
173
+
174
+ function CarouselPrevious({
175
+ className,
176
+ variant = "outline",
177
+ size = "icon",
178
+ ...props
179
+ }: React.ComponentProps<typeof Button>) {
180
+ const { orientation, scrollPrev, canScrollPrev } = useCarousel()
181
+
182
+ return (
183
+ <Button
184
+ data-slot="carousel-previous"
185
+ variant={variant}
186
+ size={size}
187
+ className={cn(
188
+ "absolute size-8 rounded-full",
189
+ orientation === "horizontal"
190
+ ? "top-1/2 -left-12 -translate-y-1/2"
191
+ : "-top-12 left-1/2 -translate-x-1/2 rotate-90",
192
+ className
193
+ )}
194
+ disabled={!canScrollPrev}
195
+ onClick={scrollPrev}
196
+ {...props}
197
+ >
198
+ <ArrowLeft />
199
+ <span className="sr-only">Previous slide</span>
200
+ </Button>
201
+ )
202
+ }
203
+
204
+ function CarouselNext({
205
+ className,
206
+ variant = "outline",
207
+ size = "icon",
208
+ ...props
209
+ }: React.ComponentProps<typeof Button>) {
210
+ const { orientation, scrollNext, canScrollNext } = useCarousel()
211
+
212
+ return (
213
+ <Button
214
+ data-slot="carousel-next"
215
+ variant={variant}
216
+ size={size}
217
+ className={cn(
218
+ "absolute size-8 rounded-full",
219
+ orientation === "horizontal"
220
+ ? "top-1/2 -right-12 -translate-y-1/2"
221
+ : "-bottom-12 left-1/2 -translate-x-1/2 rotate-90",
222
+ className
223
+ )}
224
+ disabled={!canScrollNext}
225
+ onClick={scrollNext}
226
+ {...props}
227
+ >
228
+ <ArrowRight />
229
+ <span className="sr-only">Next slide</span>
230
+ </Button>
231
+ )
232
+ }
233
+
234
+ export {
235
+ type CarouselApi,
236
+ Carousel,
237
+ CarouselContent,
238
+ CarouselItem,
239
+ CarouselPrevious,
240
+ CarouselNext,
241
+ }
@@ -0,0 +1,371 @@
1
+ import * as React from "react"
2
+ import { cn } from "../cn"
3
+ import * as RechartsPrimitive from "recharts"
4
+ import type { TooltipValueType } from "recharts"
5
+
6
+ // Format: { THEME_NAME: CSS_SELECTOR }
7
+ const THEMES = { light: "", dark: ".dark" } as const
8
+
9
+ const INITIAL_DIMENSION = { width: 320, height: 200 } as const
10
+ type TooltipNameType = number | string
11
+
12
+ export type ChartConfig = Record<
13
+ string,
14
+ {
15
+ label?: React.ReactNode
16
+ icon?: React.ComponentType
17
+ } & (
18
+ | { color?: string; theme?: never }
19
+ | { color?: never; theme: Record<keyof typeof THEMES, string> }
20
+ )
21
+ >
22
+
23
+ type ChartContextProps = {
24
+ config: ChartConfig
25
+ }
26
+
27
+ const ChartContext = React.createContext<ChartContextProps | null>(null)
28
+
29
+ function useChart() {
30
+ const context = React.useContext(ChartContext)
31
+
32
+ if (!context) {
33
+ throw new Error("useChart must be used within a <ChartContainer />")
34
+ }
35
+
36
+ return context
37
+ }
38
+
39
+ function ChartContainer({
40
+ id,
41
+ className,
42
+ children,
43
+ config,
44
+ initialDimension = INITIAL_DIMENSION,
45
+ ...props
46
+ }: React.ComponentProps<"div"> & {
47
+ config: ChartConfig
48
+ children: React.ComponentProps<
49
+ typeof RechartsPrimitive.ResponsiveContainer
50
+ >["children"]
51
+ initialDimension?: {
52
+ width: number
53
+ height: number
54
+ }
55
+ }) {
56
+ const uniqueId = React.useId()
57
+ const chartId = `chart-${id ?? uniqueId.replace(/:/g, "")}`
58
+
59
+ return (
60
+ <ChartContext.Provider value={{ config }}>
61
+ <div
62
+ data-slot="chart"
63
+ data-chart={chartId}
64
+ className={cn(
65
+ "flex aspect-video justify-center text-xs [&_.recharts-cartesian-axis-tick_text]:fill-muted-foreground [&_.recharts-cartesian-grid_line[stroke='#ccc']]:stroke-border/50 [&_.recharts-curve.recharts-tooltip-cursor]:stroke-border [&_.recharts-dot[stroke='#fff']]:stroke-transparent [&_.recharts-layer]:outline-hidden [&_.recharts-polar-grid_[stroke='#ccc']]:stroke-border [&_.recharts-radial-bar-background-sector]:fill-muted [&_.recharts-rectangle.recharts-tooltip-cursor]:fill-muted [&_.recharts-reference-line_[stroke='#ccc']]:stroke-border [&_.recharts-sector]:outline-hidden [&_.recharts-sector[stroke='#fff']]:stroke-transparent [&_.recharts-surface]:outline-hidden",
66
+ className
67
+ )}
68
+ {...props}
69
+ >
70
+ <ChartStyle id={chartId} config={config} />
71
+ <RechartsPrimitive.ResponsiveContainer
72
+ initialDimension={initialDimension}
73
+ >
74
+ {children}
75
+ </RechartsPrimitive.ResponsiveContainer>
76
+ </div>
77
+ </ChartContext.Provider>
78
+ )
79
+ }
80
+
81
+ const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {
82
+ const colorConfig = Object.entries(config).filter(
83
+ ([, config]) => config.theme ?? config.color
84
+ )
85
+
86
+ if (!colorConfig.length) {
87
+ return null
88
+ }
89
+
90
+ return (
91
+ <style
92
+ dangerouslySetInnerHTML={{
93
+ __html: Object.entries(THEMES)
94
+ .map(
95
+ ([theme, prefix]) => `
96
+ ${prefix} [data-chart=${id}] {
97
+ ${colorConfig
98
+ .map(([key, itemConfig]) => {
99
+ const color =
100
+ itemConfig.theme?.[theme as keyof typeof itemConfig.theme] ??
101
+ itemConfig.color
102
+ return color ? ` --color-${key}: ${color};` : null
103
+ })
104
+ .join("\n")}
105
+ }
106
+ `
107
+ )
108
+ .join("\n"),
109
+ }}
110
+ />
111
+ )
112
+ }
113
+
114
+ const ChartTooltip = RechartsPrimitive.Tooltip
115
+
116
+ function ChartTooltipContent({
117
+ active,
118
+ payload,
119
+ className,
120
+ indicator = "dot",
121
+ hideLabel = false,
122
+ hideIndicator = false,
123
+ label,
124
+ labelFormatter,
125
+ labelClassName,
126
+ formatter,
127
+ color,
128
+ nameKey,
129
+ labelKey,
130
+ }: React.ComponentProps<typeof RechartsPrimitive.Tooltip> &
131
+ React.ComponentProps<"div"> & {
132
+ hideLabel?: boolean
133
+ hideIndicator?: boolean
134
+ indicator?: "line" | "dot" | "dashed"
135
+ nameKey?: string
136
+ labelKey?: string
137
+ } & Omit<
138
+ RechartsPrimitive.DefaultTooltipContentProps<
139
+ TooltipValueType,
140
+ TooltipNameType
141
+ >,
142
+ "accessibilityLayer"
143
+ >) {
144
+ const { config } = useChart()
145
+
146
+ const tooltipLabel = React.useMemo(() => {
147
+ if (hideLabel || !payload?.length) {
148
+ return null
149
+ }
150
+
151
+ const [item] = payload
152
+ const key = `${labelKey ?? item?.dataKey ?? item?.name ?? "value"}`
153
+ const itemConfig = getPayloadConfigFromPayload(config, item, key)
154
+ const value =
155
+ !labelKey && typeof label === "string"
156
+ ? (config[label]?.label ?? label)
157
+ : itemConfig?.label
158
+
159
+ if (labelFormatter) {
160
+ return (
161
+ <div className={cn("font-medium", labelClassName)}>
162
+ {labelFormatter(value, payload)}
163
+ </div>
164
+ )
165
+ }
166
+
167
+ if (!value) {
168
+ return null
169
+ }
170
+
171
+ return <div className={cn("font-medium", labelClassName)}>{value}</div>
172
+ }, [
173
+ label,
174
+ labelFormatter,
175
+ payload,
176
+ hideLabel,
177
+ labelClassName,
178
+ config,
179
+ labelKey,
180
+ ])
181
+
182
+ if (!active || !payload?.length) {
183
+ return null
184
+ }
185
+
186
+ const nestLabel = payload.length === 1 && indicator !== "dot"
187
+
188
+ return (
189
+ <div
190
+ className={cn(
191
+ "grid min-w-[8rem] items-start gap-1.5 rounded-lg border border-border/50 bg-background px-2.5 py-1.5 text-xs shadow-xl",
192
+ className
193
+ )}
194
+ >
195
+ {!nestLabel ? tooltipLabel : null}
196
+ <div className="grid gap-1.5">
197
+ {payload
198
+ .filter((item) => item.type !== "none")
199
+ .map((item, index) => {
200
+ const key = `${nameKey ?? item.name ?? item.dataKey ?? "value"}`
201
+ const itemConfig = getPayloadConfigFromPayload(config, item, key)
202
+ const indicatorColor = color ?? item.payload?.fill ?? item.color
203
+
204
+ return (
205
+ <div
206
+ key={index}
207
+ className={cn(
208
+ "flex w-full flex-wrap items-stretch gap-2 [&>svg]:h-2.5 [&>svg]:w-2.5 [&>svg]:text-muted-foreground",
209
+ indicator === "dot" && "items-center"
210
+ )}
211
+ >
212
+ {formatter && item?.value !== undefined && item.name ? (
213
+ formatter(item.value, item.name, item, index, item.payload)
214
+ ) : (
215
+ <>
216
+ {itemConfig?.icon ? (
217
+ <itemConfig.icon />
218
+ ) : (
219
+ !hideIndicator && (
220
+ <div
221
+ className={cn(
222
+ "shrink-0 rounded-[2px] border-(--color-border) bg-(--color-bg)",
223
+ {
224
+ "h-2.5 w-2.5": indicator === "dot",
225
+ "w-1": indicator === "line",
226
+ "w-0 border-[1.5px] border-dashed bg-transparent":
227
+ indicator === "dashed",
228
+ "my-0.5": nestLabel && indicator === "dashed",
229
+ }
230
+ )}
231
+ style={
232
+ {
233
+ "--color-bg": indicatorColor,
234
+ "--color-border": indicatorColor,
235
+ } as React.CSSProperties
236
+ }
237
+ />
238
+ )
239
+ )}
240
+ <div
241
+ className={cn(
242
+ "flex flex-1 justify-between leading-none",
243
+ nestLabel ? "items-end" : "items-center"
244
+ )}
245
+ >
246
+ <div className="grid gap-1.5">
247
+ {nestLabel ? tooltipLabel : null}
248
+ <span className="text-muted-foreground">
249
+ {itemConfig?.label ?? item.name}
250
+ </span>
251
+ </div>
252
+ {item.value != null && (
253
+ <span className="font-mono font-medium text-foreground tabular-nums">
254
+ {typeof item.value === "number"
255
+ ? item.value.toLocaleString()
256
+ : String(item.value)}
257
+ </span>
258
+ )}
259
+ </div>
260
+ </>
261
+ )}
262
+ </div>
263
+ )
264
+ })}
265
+ </div>
266
+ </div>
267
+ )
268
+ }
269
+
270
+ const ChartLegend = RechartsPrimitive.Legend
271
+
272
+ function ChartLegendContent({
273
+ className,
274
+ hideIcon = false,
275
+ payload,
276
+ verticalAlign = "bottom",
277
+ nameKey,
278
+ }: React.ComponentProps<"div"> & {
279
+ hideIcon?: boolean
280
+ nameKey?: string
281
+ } & RechartsPrimitive.DefaultLegendContentProps) {
282
+ const { config } = useChart()
283
+
284
+ if (!payload?.length) {
285
+ return null
286
+ }
287
+
288
+ return (
289
+ <div
290
+ className={cn(
291
+ "flex items-center justify-center gap-4",
292
+ verticalAlign === "top" ? "pb-3" : "pt-3",
293
+ className
294
+ )}
295
+ >
296
+ {payload
297
+ .filter((item) => item.type !== "none")
298
+ .map((item, index) => {
299
+ const key = `${nameKey ?? item.dataKey ?? "value"}`
300
+ const itemConfig = getPayloadConfigFromPayload(config, item, key)
301
+
302
+ return (
303
+ <div
304
+ key={index}
305
+ className={cn(
306
+ "flex items-center gap-1.5 [&>svg]:h-3 [&>svg]:w-3 [&>svg]:text-muted-foreground"
307
+ )}
308
+ >
309
+ {itemConfig?.icon && !hideIcon ? (
310
+ <itemConfig.icon />
311
+ ) : (
312
+ <div
313
+ className="h-2 w-2 shrink-0 rounded-[2px]"
314
+ style={{
315
+ backgroundColor: item.color,
316
+ }}
317
+ />
318
+ )}
319
+ {itemConfig?.label}
320
+ </div>
321
+ )
322
+ })}
323
+ </div>
324
+ )
325
+ }
326
+
327
+ // Helper to extract item config from a payload.
328
+ function getPayloadConfigFromPayload(
329
+ config: ChartConfig,
330
+ payload: unknown,
331
+ key: string
332
+ ) {
333
+ if (typeof payload !== "object" || payload === null) {
334
+ return undefined
335
+ }
336
+
337
+ const payloadPayload =
338
+ "payload" in payload &&
339
+ typeof payload.payload === "object" &&
340
+ payload.payload !== null
341
+ ? payload.payload
342
+ : undefined
343
+
344
+ let configLabelKey: string = key
345
+
346
+ if (
347
+ key in payload &&
348
+ typeof payload[key as keyof typeof payload] === "string"
349
+ ) {
350
+ configLabelKey = payload[key as keyof typeof payload] as string
351
+ } else if (
352
+ payloadPayload &&
353
+ key in payloadPayload &&
354
+ typeof payloadPayload[key as keyof typeof payloadPayload] === "string"
355
+ ) {
356
+ configLabelKey = payloadPayload[
357
+ key as keyof typeof payloadPayload
358
+ ] as string
359
+ }
360
+
361
+ return configLabelKey in config ? config[configLabelKey] : config[key]
362
+ }
363
+
364
+ export {
365
+ ChartContainer,
366
+ ChartTooltip,
367
+ ChartTooltipContent,
368
+ ChartLegend,
369
+ ChartLegendContent,
370
+ ChartStyle,
371
+ }