@tuturuuu/ui 0.0.4
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.
- package/.checksum +1 -0
- package/README.md +46 -0
- package/components.json +20 -0
- package/eslint.config.mjs +20 -0
- package/jsr.json +10 -0
- package/package.json +120 -0
- package/postcss.config.mjs +8 -0
- package/rollup.config.js +40 -0
- package/src/components/ui/accordion.tsx +70 -0
- package/src/components/ui/alert-dialog.tsx +156 -0
- package/src/components/ui/alert.tsx +58 -0
- package/src/components/ui/aspect-ratio.tsx +11 -0
- package/src/components/ui/avatar.tsx +52 -0
- package/src/components/ui/badge.tsx +49 -0
- package/src/components/ui/breadcrumb.tsx +108 -0
- package/src/components/ui/button.tsx +61 -0
- package/src/components/ui/calendar.tsx +212 -0
- package/src/components/ui/card.tsx +74 -0
- package/src/components/ui/carousel.tsx +240 -0
- package/src/components/ui/chart.tsx +365 -0
- package/src/components/ui/checkbox.tsx +31 -0
- package/src/components/ui/codeblock.tsx +161 -0
- package/src/components/ui/collapsible.tsx +33 -0
- package/src/components/ui/color-picker.tsx +143 -0
- package/src/components/ui/command.tsx +176 -0
- package/src/components/ui/context-menu.tsx +251 -0
- package/src/components/ui/custom/autosize-textarea.tsx +111 -0
- package/src/components/ui/custom/calendar/core.tsx +61 -0
- package/src/components/ui/custom/calendar/day-cell.tsx +74 -0
- package/src/components/ui/custom/calendar/month-header.tsx +59 -0
- package/src/components/ui/custom/calendar/month-view.tsx +110 -0
- package/src/components/ui/custom/calendar/utils.ts +76 -0
- package/src/components/ui/custom/calendar/year-calendar.tsx +64 -0
- package/src/components/ui/custom/calendar/year-view.tsx +58 -0
- package/src/components/ui/custom/combobox.tsx +197 -0
- package/src/components/ui/custom/common-footer.tsx +215 -0
- package/src/components/ui/custom/compared-date-range-picker.tsx +561 -0
- package/src/components/ui/custom/date-input.tsx +279 -0
- package/src/components/ui/custom/empty-card.tsx +39 -0
- package/src/components/ui/custom/feature-summary.tsx +135 -0
- package/src/components/ui/custom/file-uploader.tsx +349 -0
- package/src/components/ui/custom/input-field.tsx +29 -0
- package/src/components/ui/custom/loading-indicator.tsx +28 -0
- package/src/components/ui/custom/modifiable-dialog-trigger.tsx +83 -0
- package/src/components/ui/custom/month-picker.tsx +157 -0
- package/src/components/ui/custom/report-preview.tsx +175 -0
- package/src/components/ui/custom/search-bar.tsx +56 -0
- package/src/components/ui/custom/select-field.tsx +78 -0
- package/src/components/ui/custom/tables/data-table-column-header.tsx +72 -0
- package/src/components/ui/custom/tables/data-table-create-button.tsx +31 -0
- package/src/components/ui/custom/tables/data-table-faceted-filter.tsx +142 -0
- package/src/components/ui/custom/tables/data-table-pagination.tsx +243 -0
- package/src/components/ui/custom/tables/data-table-refresh-button.tsx +45 -0
- package/src/components/ui/custom/tables/data-table-toolbar.tsx +133 -0
- package/src/components/ui/custom/tables/data-table-view-options.tsx +112 -0
- package/src/components/ui/custom/tables/data-table.tsx +228 -0
- package/src/components/ui/custom/uploaded-files-card.tsx +50 -0
- package/src/components/ui/dialog.tsx +137 -0
- package/src/components/ui/drawer.tsx +131 -0
- package/src/components/ui/dropdown-menu.tsx +256 -0
- package/src/components/ui/form.tsx +167 -0
- package/src/components/ui/hover-card.tsx +41 -0
- package/src/components/ui/icons.tsx +506 -0
- package/src/components/ui/input-otp.tsx +78 -0
- package/src/components/ui/input.tsx +18 -0
- package/src/components/ui/label.tsx +23 -0
- package/src/components/ui/markdown.tsx +7 -0
- package/src/components/ui/menubar.tsx +275 -0
- package/src/components/ui/navigation-menu.tsx +169 -0
- package/src/components/ui/pagination.tsx +126 -0
- package/src/components/ui/popover.tsx +47 -0
- package/src/components/ui/progress.tsx +30 -0
- package/src/components/ui/radio-group.tsx +44 -0
- package/src/components/ui/resizable.tsx +55 -0
- package/src/components/ui/scroll-area.tsx +57 -0
- package/src/components/ui/select.tsx +180 -0
- package/src/components/ui/separator.tsx +27 -0
- package/src/components/ui/sheet.tsx +138 -0
- package/src/components/ui/sidebar.tsx +734 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/slider.tsx +62 -0
- package/src/components/ui/sonner.tsx +29 -0
- package/src/components/ui/switch.tsx +30 -0
- package/src/components/ui/table.tsx +112 -0
- package/src/components/ui/tabs.tsx +68 -0
- package/src/components/ui/tag-input.tsx +141 -0
- package/src/components/ui/textarea.tsx +17 -0
- package/src/components/ui/time-picker-input.tsx +117 -0
- package/src/components/ui/time-picker-utils.tsx +146 -0
- package/src/components/ui/toast.tsx +128 -0
- package/src/components/ui/toaster.tsx +35 -0
- package/src/components/ui/toggle-group.tsx +72 -0
- package/src/components/ui/toggle.tsx +46 -0
- package/src/components/ui/tooltip.tsx +60 -0
- package/src/globals.css +252 -0
- package/src/hooks/use-callback-ref.ts +28 -0
- package/src/hooks/use-controllable-state.ts +68 -0
- package/src/hooks/use-copy-to-clipboard.ts +46 -0
- package/src/hooks/use-form.ts +23 -0
- package/src/hooks/use-forwarded-ref.ts +17 -0
- package/src/hooks/use-mobile.tsx +21 -0
- package/src/hooks/use-toast.ts +191 -0
- package/src/resolvers.ts +3 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { cn } from '@tuturuuu/utils/format';
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import * as RechartsPrimitive from 'recharts';
|
|
6
|
+
|
|
7
|
+
// Format: { THEME_NAME: CSS_SELECTOR }
|
|
8
|
+
const THEMES = { light: '', dark: '.dark' } as const;
|
|
9
|
+
|
|
10
|
+
export type ChartConfig = {
|
|
11
|
+
[k in string]: {
|
|
12
|
+
label?: React.ReactNode;
|
|
13
|
+
icon?: React.ComponentType;
|
|
14
|
+
} & (
|
|
15
|
+
| { color?: string; theme?: never }
|
|
16
|
+
| { color?: never; theme: Record<keyof typeof THEMES, string> }
|
|
17
|
+
);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type ChartContextProps = {
|
|
21
|
+
config: ChartConfig;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const ChartContext = React.createContext<ChartContextProps | null>(null);
|
|
25
|
+
|
|
26
|
+
function useChart() {
|
|
27
|
+
const context = React.useContext(ChartContext);
|
|
28
|
+
|
|
29
|
+
if (!context) {
|
|
30
|
+
throw new Error('useChart must be used within a <ChartContainer />');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return context;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const ChartContainer = React.forwardRef<
|
|
37
|
+
HTMLDivElement,
|
|
38
|
+
React.ComponentProps<'div'> & {
|
|
39
|
+
config: ChartConfig;
|
|
40
|
+
children: React.ComponentProps<
|
|
41
|
+
typeof RechartsPrimitive.ResponsiveContainer
|
|
42
|
+
>['children'];
|
|
43
|
+
}
|
|
44
|
+
>(({ id, className, children, config, ...props }, ref) => {
|
|
45
|
+
const uniqueId = React.useId();
|
|
46
|
+
const chartId = `chart-${id || uniqueId.replace(/:/g, '')}`;
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<ChartContext.Provider value={{ config }}>
|
|
50
|
+
<div
|
|
51
|
+
data-slot="chart"
|
|
52
|
+
data-chart={chartId}
|
|
53
|
+
ref={ref}
|
|
54
|
+
className={cn(
|
|
55
|
+
"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",
|
|
56
|
+
className
|
|
57
|
+
)}
|
|
58
|
+
{...props}
|
|
59
|
+
>
|
|
60
|
+
<ChartStyle id={chartId} config={config} />
|
|
61
|
+
<RechartsPrimitive.ResponsiveContainer>
|
|
62
|
+
{children}
|
|
63
|
+
</RechartsPrimitive.ResponsiveContainer>
|
|
64
|
+
</div>
|
|
65
|
+
</ChartContext.Provider>
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
ChartContainer.displayName = 'Chart';
|
|
69
|
+
|
|
70
|
+
const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {
|
|
71
|
+
const colorConfig = Object.entries(config).filter(
|
|
72
|
+
([, config]) => config.theme || config.color
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
if (!colorConfig.length) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<style
|
|
81
|
+
dangerouslySetInnerHTML={{
|
|
82
|
+
__html: Object.entries(THEMES)
|
|
83
|
+
.map(
|
|
84
|
+
([theme, prefix]) => `
|
|
85
|
+
${prefix} [data-chart=${id}] {
|
|
86
|
+
${colorConfig
|
|
87
|
+
.map(([key, itemConfig]) => {
|
|
88
|
+
const color =
|
|
89
|
+
itemConfig.theme?.[theme as keyof typeof itemConfig.theme] ||
|
|
90
|
+
itemConfig.color;
|
|
91
|
+
return color ? ` --color-${key}: ${color};` : null;
|
|
92
|
+
})
|
|
93
|
+
.join('\n')}
|
|
94
|
+
}
|
|
95
|
+
`
|
|
96
|
+
)
|
|
97
|
+
.join('\n'),
|
|
98
|
+
}}
|
|
99
|
+
/>
|
|
100
|
+
);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const ChartTooltip = RechartsPrimitive.Tooltip;
|
|
104
|
+
|
|
105
|
+
const ChartTooltipContent = React.forwardRef<
|
|
106
|
+
HTMLDivElement,
|
|
107
|
+
React.ComponentProps<typeof RechartsPrimitive.Tooltip> &
|
|
108
|
+
React.ComponentProps<'div'> & {
|
|
109
|
+
hideLabel?: boolean;
|
|
110
|
+
hideIndicator?: boolean;
|
|
111
|
+
indicator?: 'line' | 'dot' | 'dashed';
|
|
112
|
+
nameKey?: string;
|
|
113
|
+
labelKey?: string;
|
|
114
|
+
}
|
|
115
|
+
>(
|
|
116
|
+
(
|
|
117
|
+
{
|
|
118
|
+
active,
|
|
119
|
+
payload,
|
|
120
|
+
className,
|
|
121
|
+
indicator = 'dot',
|
|
122
|
+
hideLabel = false,
|
|
123
|
+
hideIndicator = false,
|
|
124
|
+
label,
|
|
125
|
+
labelFormatter,
|
|
126
|
+
labelClassName,
|
|
127
|
+
formatter,
|
|
128
|
+
color,
|
|
129
|
+
nameKey,
|
|
130
|
+
labelKey,
|
|
131
|
+
},
|
|
132
|
+
ref
|
|
133
|
+
) => {
|
|
134
|
+
const { config } = useChart();
|
|
135
|
+
|
|
136
|
+
const tooltipLabel = React.useMemo(() => {
|
|
137
|
+
if (hideLabel || !payload?.length) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const [item] = payload;
|
|
142
|
+
const key = `${labelKey || item?.dataKey || item?.name || 'value'}`;
|
|
143
|
+
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
144
|
+
const value =
|
|
145
|
+
!labelKey && typeof label === 'string'
|
|
146
|
+
? config[label as keyof typeof config]?.label || label
|
|
147
|
+
: itemConfig?.label;
|
|
148
|
+
|
|
149
|
+
if (labelFormatter) {
|
|
150
|
+
return (
|
|
151
|
+
<div className={cn('font-medium', labelClassName)}>
|
|
152
|
+
{labelFormatter(value, payload)}
|
|
153
|
+
</div>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!value) {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return <div className={cn('font-medium', labelClassName)}>{value}</div>;
|
|
162
|
+
}, [
|
|
163
|
+
label,
|
|
164
|
+
labelFormatter,
|
|
165
|
+
payload,
|
|
166
|
+
hideLabel,
|
|
167
|
+
labelClassName,
|
|
168
|
+
config,
|
|
169
|
+
labelKey,
|
|
170
|
+
]);
|
|
171
|
+
|
|
172
|
+
if (!active || !payload?.length) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const nestLabel = payload.length === 1 && indicator !== 'dot';
|
|
177
|
+
|
|
178
|
+
return (
|
|
179
|
+
<div
|
|
180
|
+
ref={ref}
|
|
181
|
+
className={cn(
|
|
182
|
+
'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',
|
|
183
|
+
className
|
|
184
|
+
)}
|
|
185
|
+
>
|
|
186
|
+
{!nestLabel ? tooltipLabel : null}
|
|
187
|
+
<div className="grid gap-1.5">
|
|
188
|
+
{payload.map((item, index) => {
|
|
189
|
+
const key = `${nameKey || item.name || item.dataKey || 'value'}`;
|
|
190
|
+
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
191
|
+
const indicatorColor = color || item.payload.fill || item.color;
|
|
192
|
+
|
|
193
|
+
return (
|
|
194
|
+
<div
|
|
195
|
+
key={item.dataKey}
|
|
196
|
+
className={cn(
|
|
197
|
+
'flex w-full flex-wrap items-stretch gap-2 [&>svg]:h-2.5 [&>svg]:w-2.5 [&>svg]:text-muted-foreground',
|
|
198
|
+
indicator === 'dot' && 'items-center'
|
|
199
|
+
)}
|
|
200
|
+
>
|
|
201
|
+
{formatter && item?.value !== undefined && item.name ? (
|
|
202
|
+
formatter(item.value, item.name, item, index, item.payload)
|
|
203
|
+
) : (
|
|
204
|
+
<>
|
|
205
|
+
{itemConfig?.icon ? (
|
|
206
|
+
<itemConfig.icon />
|
|
207
|
+
) : (
|
|
208
|
+
!hideIndicator && (
|
|
209
|
+
<div
|
|
210
|
+
className={cn(
|
|
211
|
+
'shrink-0 rounded-[2px] border-(--color-border) bg-(--color-bg)',
|
|
212
|
+
{
|
|
213
|
+
'h-2.5 w-2.5': indicator === 'dot',
|
|
214
|
+
'w-1': indicator === 'line',
|
|
215
|
+
'w-0 border-[1.5px] border-dashed bg-transparent':
|
|
216
|
+
indicator === 'dashed',
|
|
217
|
+
'my-0.5': nestLabel && indicator === 'dashed',
|
|
218
|
+
}
|
|
219
|
+
)}
|
|
220
|
+
style={
|
|
221
|
+
{
|
|
222
|
+
'--color-bg': indicatorColor,
|
|
223
|
+
'--color-border': indicatorColor,
|
|
224
|
+
} as React.CSSProperties
|
|
225
|
+
}
|
|
226
|
+
/>
|
|
227
|
+
)
|
|
228
|
+
)}
|
|
229
|
+
<div
|
|
230
|
+
className={cn(
|
|
231
|
+
'flex flex-1 justify-between leading-none',
|
|
232
|
+
nestLabel ? 'items-end' : 'items-center'
|
|
233
|
+
)}
|
|
234
|
+
>
|
|
235
|
+
<div className="grid gap-1.5">
|
|
236
|
+
{nestLabel ? tooltipLabel : null}
|
|
237
|
+
<span className="text-muted-foreground">
|
|
238
|
+
{itemConfig?.label || item.name}
|
|
239
|
+
</span>
|
|
240
|
+
</div>
|
|
241
|
+
{item.value && (
|
|
242
|
+
<span className="font-mono font-medium text-foreground tabular-nums">
|
|
243
|
+
{item.value.toLocaleString()}
|
|
244
|
+
</span>
|
|
245
|
+
)}
|
|
246
|
+
</div>
|
|
247
|
+
</>
|
|
248
|
+
)}
|
|
249
|
+
</div>
|
|
250
|
+
);
|
|
251
|
+
})}
|
|
252
|
+
</div>
|
|
253
|
+
</div>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
);
|
|
257
|
+
ChartTooltipContent.displayName = 'ChartTooltip';
|
|
258
|
+
|
|
259
|
+
const ChartLegend = RechartsPrimitive.Legend;
|
|
260
|
+
|
|
261
|
+
const ChartLegendContent = React.forwardRef<
|
|
262
|
+
HTMLDivElement,
|
|
263
|
+
React.ComponentProps<'div'> &
|
|
264
|
+
Pick<RechartsPrimitive.LegendProps, 'payload' | 'verticalAlign'> & {
|
|
265
|
+
hideIcon?: boolean;
|
|
266
|
+
nameKey?: string;
|
|
267
|
+
}
|
|
268
|
+
>(
|
|
269
|
+
(
|
|
270
|
+
{ className, hideIcon = false, payload, verticalAlign = 'bottom', nameKey },
|
|
271
|
+
ref
|
|
272
|
+
) => {
|
|
273
|
+
const { config } = useChart();
|
|
274
|
+
|
|
275
|
+
if (!payload?.length) {
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return (
|
|
280
|
+
<div
|
|
281
|
+
ref={ref}
|
|
282
|
+
className={cn(
|
|
283
|
+
'flex items-center justify-center gap-4',
|
|
284
|
+
verticalAlign === 'top' ? 'pb-3' : 'pt-3',
|
|
285
|
+
className
|
|
286
|
+
)}
|
|
287
|
+
>
|
|
288
|
+
{payload.map((item) => {
|
|
289
|
+
const key = `${nameKey || item.dataKey || 'value'}`;
|
|
290
|
+
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
291
|
+
|
|
292
|
+
return (
|
|
293
|
+
<div
|
|
294
|
+
key={item.value}
|
|
295
|
+
className={cn(
|
|
296
|
+
'flex items-center gap-1.5 [&>svg]:h-3 [&>svg]:w-3 [&>svg]:text-muted-foreground'
|
|
297
|
+
)}
|
|
298
|
+
>
|
|
299
|
+
{itemConfig?.icon && !hideIcon ? (
|
|
300
|
+
<itemConfig.icon />
|
|
301
|
+
) : (
|
|
302
|
+
<div
|
|
303
|
+
className="h-2 w-2 shrink-0 rounded-[2px]"
|
|
304
|
+
style={{
|
|
305
|
+
backgroundColor: item.color,
|
|
306
|
+
}}
|
|
307
|
+
/>
|
|
308
|
+
)}
|
|
309
|
+
{itemConfig?.label}
|
|
310
|
+
</div>
|
|
311
|
+
);
|
|
312
|
+
})}
|
|
313
|
+
</div>
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
);
|
|
317
|
+
ChartLegendContent.displayName = 'ChartLegend';
|
|
318
|
+
|
|
319
|
+
// Helper to extract item config from a payload.
|
|
320
|
+
function getPayloadConfigFromPayload(
|
|
321
|
+
config: ChartConfig,
|
|
322
|
+
payload: unknown,
|
|
323
|
+
key: string
|
|
324
|
+
) {
|
|
325
|
+
if (typeof payload !== 'object' || payload === null) {
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const payloadPayload =
|
|
330
|
+
'payload' in payload &&
|
|
331
|
+
typeof payload.payload === 'object' &&
|
|
332
|
+
payload.payload !== null
|
|
333
|
+
? payload.payload
|
|
334
|
+
: undefined;
|
|
335
|
+
|
|
336
|
+
let configLabelKey: string = key;
|
|
337
|
+
|
|
338
|
+
if (
|
|
339
|
+
key in payload &&
|
|
340
|
+
typeof payload[key as keyof typeof payload] === 'string'
|
|
341
|
+
) {
|
|
342
|
+
configLabelKey = payload[key as keyof typeof payload] as string;
|
|
343
|
+
} else if (
|
|
344
|
+
payloadPayload &&
|
|
345
|
+
key in payloadPayload &&
|
|
346
|
+
typeof payloadPayload[key as keyof typeof payloadPayload] === 'string'
|
|
347
|
+
) {
|
|
348
|
+
configLabelKey = payloadPayload[
|
|
349
|
+
key as keyof typeof payloadPayload
|
|
350
|
+
] as string;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return configLabelKey in config
|
|
354
|
+
? config[configLabelKey]
|
|
355
|
+
: config[key as keyof typeof config];
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export {
|
|
359
|
+
ChartContainer,
|
|
360
|
+
ChartLegend,
|
|
361
|
+
ChartLegendContent,
|
|
362
|
+
ChartStyle,
|
|
363
|
+
ChartTooltip,
|
|
364
|
+
ChartTooltipContent,
|
|
365
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import * as CheckboxPrimitive from '@radix-ui/react-checkbox';
|
|
4
|
+
import { cn } from '@tuturuuu/utils/format';
|
|
5
|
+
import { CheckIcon } from 'lucide-react';
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
|
|
8
|
+
function Checkbox({
|
|
9
|
+
className,
|
|
10
|
+
...props
|
|
11
|
+
}: React.ComponentProps<typeof CheckboxPrimitive.Root>) {
|
|
12
|
+
return (
|
|
13
|
+
<CheckboxPrimitive.Root
|
|
14
|
+
data-slot="checkbox"
|
|
15
|
+
className={cn(
|
|
16
|
+
'peer size-4 shrink-0 rounded-[4px] border border-input shadow-xs ring-ring/10 outline-ring/50 transition-[color,box-shadow] focus-visible:ring-4 focus-visible:outline-1 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:focus-visible:ring-0 data-[state=checked]:border-primary data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground dark:ring-ring/20 dark:outline-ring/40',
|
|
17
|
+
className
|
|
18
|
+
)}
|
|
19
|
+
{...props}
|
|
20
|
+
>
|
|
21
|
+
<CheckboxPrimitive.Indicator
|
|
22
|
+
data-slot="checkbox-indicator"
|
|
23
|
+
className="flex items-center justify-center text-current"
|
|
24
|
+
>
|
|
25
|
+
<CheckIcon className="size-3.5" />
|
|
26
|
+
</CheckboxPrimitive.Indicator>
|
|
27
|
+
</CheckboxPrimitive.Root>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { Checkbox };
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useCopyToClipboard } from '../../hooks/use-copy-to-clipboard';
|
|
4
|
+
import { Button } from './button';
|
|
5
|
+
import { IconCheck, IconCopy, IconDownload } from './icons';
|
|
6
|
+
import { useTheme } from 'next-themes';
|
|
7
|
+
import { FC, memo } from 'react';
|
|
8
|
+
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
|
|
9
|
+
import {
|
|
10
|
+
coldarkCold,
|
|
11
|
+
coldarkDark,
|
|
12
|
+
} from 'react-syntax-highlighter/dist/cjs/styles/prism';
|
|
13
|
+
|
|
14
|
+
// Inspired by Chatbot-UI and modified to fit the needs of this project
|
|
15
|
+
// @see https://github.com/mckaywrigley/chatbot-ui/blob/main/components/Markdown/CodeBlock.tsx
|
|
16
|
+
|
|
17
|
+
interface Props {
|
|
18
|
+
language: string;
|
|
19
|
+
value: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface languageMap {
|
|
23
|
+
[key: string]: string | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const programmingLanguages: languageMap = {
|
|
27
|
+
javascript: '',
|
|
28
|
+
python: '.py',
|
|
29
|
+
java: '.java',
|
|
30
|
+
c: '.c',
|
|
31
|
+
cpp: '.cpp',
|
|
32
|
+
'c++': '.cpp',
|
|
33
|
+
'c#': '.cs',
|
|
34
|
+
ruby: '.rb',
|
|
35
|
+
php: '.php',
|
|
36
|
+
swift: '.swift',
|
|
37
|
+
'objective-c': '.m',
|
|
38
|
+
kotlin: '.kt',
|
|
39
|
+
typescript: '.ts',
|
|
40
|
+
go: '.go',
|
|
41
|
+
perl: '.pl',
|
|
42
|
+
rust: '.rs',
|
|
43
|
+
scala: '.scala',
|
|
44
|
+
haskell: '.hs',
|
|
45
|
+
lua: '.lua',
|
|
46
|
+
shell: '.sh',
|
|
47
|
+
sql: '.sql',
|
|
48
|
+
html: '.html',
|
|
49
|
+
css: '.css',
|
|
50
|
+
json: '.json',
|
|
51
|
+
yaml: '.yaml',
|
|
52
|
+
markdown: '.md',
|
|
53
|
+
xml: '.xml',
|
|
54
|
+
// add more file extensions here, make sure the key is same as language prop in CodeBlock.tsx component
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const generateRandomString = (length: number, lowercase = false) => {
|
|
58
|
+
const chars = 'ABCDEFGHJKLMNPQRSTUVWXY3456789'; // excluding similar looking characters like Z, 2, I, 1, O, 0
|
|
59
|
+
let result = '';
|
|
60
|
+
for (let i = 0; i < length; i++) {
|
|
61
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
62
|
+
}
|
|
63
|
+
return lowercase ? result.toLowerCase() : result;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const CodeBlock: FC<Props> = memo(({ language, value }) => {
|
|
67
|
+
const { resolvedTheme } = useTheme();
|
|
68
|
+
const isDark = resolvedTheme?.startsWith('dark') ?? true;
|
|
69
|
+
|
|
70
|
+
const { isCopied, copyToClipboard } = useCopyToClipboard({ timeout: 2000 });
|
|
71
|
+
|
|
72
|
+
const downloadAsFile = () => {
|
|
73
|
+
if (typeof window === 'undefined') {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const fileExtension = programmingLanguages[language] || '.file';
|
|
77
|
+
const suggestedFileName = `file-${generateRandomString(
|
|
78
|
+
3,
|
|
79
|
+
true
|
|
80
|
+
)}${fileExtension}`;
|
|
81
|
+
const fileName = window.prompt('Enter file name', suggestedFileName);
|
|
82
|
+
|
|
83
|
+
if (!fileName) {
|
|
84
|
+
// User pressed cancel on prompt.
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const blob = new Blob([value], { type: 'text/plain' });
|
|
89
|
+
const url = URL.createObjectURL(blob);
|
|
90
|
+
const link = document.createElement('a');
|
|
91
|
+
link.download = fileName;
|
|
92
|
+
link.href = url;
|
|
93
|
+
link.style.display = 'none';
|
|
94
|
+
document.body.appendChild(link);
|
|
95
|
+
link.click();
|
|
96
|
+
document.body.removeChild(link);
|
|
97
|
+
URL.revokeObjectURL(url);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const onCopy = () => {
|
|
101
|
+
if (isCopied) return;
|
|
102
|
+
copyToClipboard(value);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<div className="codeblock relative w-full rounded font-sans">
|
|
107
|
+
<div className="flex w-full items-center justify-between rounded border bg-foreground/10 px-4 py-1 pr-4 text-foreground">
|
|
108
|
+
<span className="text-xs font-semibold capitalize">{language}</span>
|
|
109
|
+
<div className="flex items-center space-x-1">
|
|
110
|
+
<Button
|
|
111
|
+
variant="ghost"
|
|
112
|
+
className="hover:bg-foreground/5 focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-0"
|
|
113
|
+
onClick={downloadAsFile}
|
|
114
|
+
size="icon"
|
|
115
|
+
>
|
|
116
|
+
<IconDownload />
|
|
117
|
+
<span className="sr-only">Download</span>
|
|
118
|
+
</Button>
|
|
119
|
+
<Button
|
|
120
|
+
variant="ghost"
|
|
121
|
+
size="icon"
|
|
122
|
+
className="text-xs hover:bg-foreground/5 focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-0"
|
|
123
|
+
onClick={onCopy}
|
|
124
|
+
>
|
|
125
|
+
{isCopied ? <IconCheck /> : <IconCopy />}
|
|
126
|
+
<span className="sr-only">Copy code</span>
|
|
127
|
+
</Button>
|
|
128
|
+
</div>
|
|
129
|
+
</div>
|
|
130
|
+
<SyntaxHighlighter
|
|
131
|
+
language={language}
|
|
132
|
+
style={isDark ? coldarkDark : coldarkCold}
|
|
133
|
+
PreTag="div"
|
|
134
|
+
customStyle={{
|
|
135
|
+
margin: 0,
|
|
136
|
+
marginTop: '0.5rem',
|
|
137
|
+
width: '100%',
|
|
138
|
+
padding: '1.5rem 1rem',
|
|
139
|
+
borderRadius: '0.25rem',
|
|
140
|
+
background: 'transparent',
|
|
141
|
+
}}
|
|
142
|
+
lineNumberStyle={{
|
|
143
|
+
// Disable line number selection
|
|
144
|
+
userSelect: 'none',
|
|
145
|
+
}}
|
|
146
|
+
codeTagProps={{
|
|
147
|
+
style: {
|
|
148
|
+
fontSize: '0.9rem',
|
|
149
|
+
fontFamily: 'var(--font-mono)',
|
|
150
|
+
},
|
|
151
|
+
}}
|
|
152
|
+
showLineNumbers
|
|
153
|
+
>
|
|
154
|
+
{value}
|
|
155
|
+
</SyntaxHighlighter>
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
CodeBlock.displayName = 'CodeBlock';
|
|
160
|
+
|
|
161
|
+
export { CodeBlock };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import * as CollapsiblePrimitive from '@radix-ui/react-collapsible';
|
|
4
|
+
|
|
5
|
+
function Collapsible({
|
|
6
|
+
...props
|
|
7
|
+
}: React.ComponentProps<typeof CollapsiblePrimitive.Root>) {
|
|
8
|
+
return <CollapsiblePrimitive.Root data-slot="collapsible" {...props} />;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function CollapsibleTrigger({
|
|
12
|
+
...props
|
|
13
|
+
}: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleTrigger>) {
|
|
14
|
+
return (
|
|
15
|
+
<CollapsiblePrimitive.CollapsibleTrigger
|
|
16
|
+
data-slot="collapsible-trigger"
|
|
17
|
+
{...props}
|
|
18
|
+
/>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function CollapsibleContent({
|
|
23
|
+
...props
|
|
24
|
+
}: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleContent>) {
|
|
25
|
+
return (
|
|
26
|
+
<CollapsiblePrimitive.CollapsibleContent
|
|
27
|
+
data-slot="collapsible-content"
|
|
28
|
+
{...props}
|
|
29
|
+
/>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { Collapsible, CollapsibleContent, CollapsibleTrigger };
|