@vendure-io/ui 2.0.0-beta.2 → 2.0.0-beta.20
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/LICENSE.md +34 -0
- package/README.md +6 -3
- package/package.json +10 -9
- package/src/components/atoms/button.tsx +5 -1
- package/src/components/atoms/card.tsx +39 -4
- package/src/components/atoms/combobox.tsx +19 -4
- package/src/components/atoms/scroll-area.tsx +4 -2
- package/src/components/atoms/sidebar.tsx +27 -12
- package/src/components/atoms/table.tsx +1 -1
- package/src/components/atoms/tabs.tsx +64 -1
- package/src/components/atoms/toggle-group.tsx +1 -1
- package/src/components/atoms/toggle.tsx +5 -2
- package/src/components/molecules/anonymized-token.tsx +2 -2
- package/src/components/molecules/app-shell.tsx +110 -0
- package/src/components/molecules/code-block/file-type-icons.tsx +123 -0
- package/src/components/molecules/code-block/transform-command.tsx +177 -0
- package/src/components/molecules/code-block.tsx +634 -0
- package/src/components/molecules/combobox-free-text.tsx +18 -0
- package/src/components/molecules/copy-feedback-provider.tsx +33 -0
- package/src/components/molecules/copyable-text.tsx +30 -7
- package/src/components/molecules/data-table/data-table-bulk-actions.tsx +10 -9
- package/src/components/molecules/data-table/data-table-types.tsx +50 -2
- package/src/components/molecules/data-table/data-table.tsx +184 -91
- package/src/components/molecules/date-picker.tsx +119 -0
- package/src/components/molecules/date-range-picker.tsx +153 -0
- package/src/components/molecules/date-time-picker.tsx +133 -0
- package/src/components/molecules/file-dropzone.tsx +271 -0
- package/src/components/molecules/id-chip.tsx +1 -1
- package/src/components/molecules/page-header.tsx +7 -3
- package/src/components/molecules/skip-link.tsx +36 -0
- package/src/components/molecules/state-views/empty-state.tsx +12 -1
- package/src/components/molecules/state-views/loading-state.tsx +7 -3
- package/src/lib/date-value.ts +48 -0
- package/src/lib/highlight.ts +174 -0
- package/src/lib/utils.ts +0 -19
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Button } from '@vendure-io/ui/components/atoms/button';
|
|
4
|
+
import { ScrollArea, ScrollBar } from '@vendure-io/ui/components/atoms/scroll-area';
|
|
5
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from '@vendure-io/ui/components/atoms/tooltip';
|
|
6
|
+
import {
|
|
7
|
+
FileTypeIcon,
|
|
8
|
+
matchFileTypeIcon,
|
|
9
|
+
} from '@vendure-io/ui/components/molecules/code-block/file-type-icons';
|
|
10
|
+
import {
|
|
11
|
+
isPackageManagerCommand,
|
|
12
|
+
PACKAGE_MANAGERS,
|
|
13
|
+
type PackageManager,
|
|
14
|
+
transformCommand,
|
|
15
|
+
} from '@vendure-io/ui/components/molecules/code-block/transform-command';
|
|
16
|
+
import { useCopyFeedback } from '@vendure-io/ui/components/molecules/copy-feedback-provider';
|
|
17
|
+
import { useCopy } from '@vendure-io/ui/hooks/use-copy';
|
|
18
|
+
import {
|
|
19
|
+
highlightCode,
|
|
20
|
+
normalizeLanguage,
|
|
21
|
+
type SupportedLanguage,
|
|
22
|
+
supportsNotationComments,
|
|
23
|
+
} from '@vendure-io/ui/lib/highlight';
|
|
24
|
+
import { cn } from '@vendure-io/ui/lib/utils';
|
|
25
|
+
import {
|
|
26
|
+
CheckIcon,
|
|
27
|
+
ChevronDownIcon,
|
|
28
|
+
ChevronUpIcon,
|
|
29
|
+
CodeIcon,
|
|
30
|
+
CopyIcon,
|
|
31
|
+
FileIcon,
|
|
32
|
+
TerminalIcon,
|
|
33
|
+
} from 'lucide-react';
|
|
34
|
+
import { motion } from 'motion/react';
|
|
35
|
+
import { type ComponentProps, type ReactNode, useEffect, useId, useMemo, useState } from 'react';
|
|
36
|
+
|
|
37
|
+
type CodeBlockProps = Omit<ComponentProps<'div'>, 'children'> & {
|
|
38
|
+
/**
|
|
39
|
+
* The code to render, as a plain string. Anything else (elements, numbers)
|
|
40
|
+
* would stringify uselessly, so the type rejects it.
|
|
41
|
+
*/
|
|
42
|
+
children?: string;
|
|
43
|
+
/** Programming language for syntax highlighting */
|
|
44
|
+
language?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Filename shown in the header (overrides a `// filename:` first-line directive).
|
|
47
|
+
* Takes precedence over the package-manager tabs. A block with a filename never
|
|
48
|
+
* shows the switcher, even when `packageManagerSwitcher` is set.
|
|
49
|
+
*/
|
|
50
|
+
filename?: string;
|
|
51
|
+
/** Hide the header bar completely */
|
|
52
|
+
hideHeader?: boolean;
|
|
53
|
+
/** Enable npm→pnpm/yarn/bun tab switching for shell blocks containing npm/npx commands. @default false */
|
|
54
|
+
packageManagerSwitcher?: boolean;
|
|
55
|
+
/** Extra toolbar actions rendered before the built-in copy button. Compose with CodeBlockAction. */
|
|
56
|
+
actions?: ReactNode;
|
|
57
|
+
/**
|
|
58
|
+
* Called after a successful copy. Wire your toast here. The DS never toasts.
|
|
59
|
+
* Function props can't cross an RSC boundary — when rendering from a server
|
|
60
|
+
* component, omit this and mount `CopyFeedbackProvider` instead; it is the
|
|
61
|
+
* fallback when no prop is passed.
|
|
62
|
+
*/
|
|
63
|
+
onCopied?: () => void;
|
|
64
|
+
/** Called when the clipboard write fails. Falls back to `CopyFeedbackProvider`. */
|
|
65
|
+
onCopyError?: (error: Error) => void;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
interface CodeBlockActionProps {
|
|
69
|
+
icon: ReactNode;
|
|
70
|
+
label: string;
|
|
71
|
+
onClick: () => void;
|
|
72
|
+
disabled?: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const STORAGE_KEY = 'vendure-ui-package-manager';
|
|
76
|
+
const STORAGE_EVENT = 'vendure-ui-package-manager-change';
|
|
77
|
+
|
|
78
|
+
const COLLAPSE_THRESHOLD = 40; // Lines needed to trigger collapse
|
|
79
|
+
const COLLAPSED_HEIGHT = 300; // px height when collapsed (roughly 15-20 lines)
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Get the stored package manager preference from localStorage
|
|
83
|
+
*/
|
|
84
|
+
function getStoredPackageManager(): PackageManager {
|
|
85
|
+
if (typeof window === 'undefined') return 'npm';
|
|
86
|
+
try {
|
|
87
|
+
const stored = localStorage.getItem(STORAGE_KEY);
|
|
88
|
+
if (stored && PACKAGE_MANAGERS.includes(stored as PackageManager)) {
|
|
89
|
+
return stored as PackageManager;
|
|
90
|
+
}
|
|
91
|
+
} catch {
|
|
92
|
+
// localStorage might not be available
|
|
93
|
+
}
|
|
94
|
+
return 'npm';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Store the package manager preference in localStorage
|
|
99
|
+
*/
|
|
100
|
+
function setStoredPackageManager(pm: PackageManager): void {
|
|
101
|
+
if (typeof window === 'undefined') return;
|
|
102
|
+
try {
|
|
103
|
+
localStorage.setItem(STORAGE_KEY, pm);
|
|
104
|
+
// Dispatch a custom event so other CodeBlock instances can update
|
|
105
|
+
window.dispatchEvent(new CustomEvent(STORAGE_EVENT, { detail: pm }));
|
|
106
|
+
} catch {
|
|
107
|
+
// localStorage might not be available
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Custom hook to manage package manager state with localStorage persistence
|
|
113
|
+
*/
|
|
114
|
+
function usePackageManager(): [PackageManager, (pm: PackageManager) => void] {
|
|
115
|
+
const [packageManager, setPackageManager] = useState<PackageManager>('npm');
|
|
116
|
+
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
setPackageManager(getStoredPackageManager());
|
|
119
|
+
}, []);
|
|
120
|
+
|
|
121
|
+
// Listen for changes from other CodeBlock instances
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
const handleChange = (e: CustomEvent<PackageManager>) => {
|
|
124
|
+
setPackageManager(e.detail);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
window.addEventListener(STORAGE_EVENT, handleChange as EventListener);
|
|
128
|
+
return () => {
|
|
129
|
+
window.removeEventListener(STORAGE_EVENT, handleChange as EventListener);
|
|
130
|
+
};
|
|
131
|
+
}, []);
|
|
132
|
+
|
|
133
|
+
const updatePackageManager = (pm: PackageManager) => {
|
|
134
|
+
setPackageManager(pm);
|
|
135
|
+
setStoredPackageManager(pm);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return [packageManager, updatePackageManager];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Process code to extract filename directive and strip Shiki notations for unsupported languages.
|
|
143
|
+
* Supports: // filename: path/to/file.ts (must be first line)
|
|
144
|
+
*
|
|
145
|
+
* Note: Line highlighting uses Shiki's native notation:
|
|
146
|
+
* - // [!code highlight] for JS/TS (at end of line)
|
|
147
|
+
* - # [!code highlight] for bash/shell (at end of line)
|
|
148
|
+
* - // [!code ++] and // [!code --] for diff
|
|
149
|
+
* - // [!code focus] for focus mode
|
|
150
|
+
*
|
|
151
|
+
* For languages where the notation would not survive highlighting (see
|
|
152
|
+
* `supportsNotationComments`), these notations are stripped.
|
|
153
|
+
*/
|
|
154
|
+
function processCode(
|
|
155
|
+
code: string,
|
|
156
|
+
language?: string,
|
|
157
|
+
): {
|
|
158
|
+
cleanCode: string;
|
|
159
|
+
extractedFilename?: string;
|
|
160
|
+
} {
|
|
161
|
+
const lines = code.split('\n');
|
|
162
|
+
const cleanLines: string[] = [];
|
|
163
|
+
let extractedFilename: string | undefined;
|
|
164
|
+
|
|
165
|
+
const shouldStripNotations = !supportsNotationComments(language);
|
|
166
|
+
|
|
167
|
+
for (let i = 0; i < lines.length; i++) {
|
|
168
|
+
let line = lines[i] ?? '';
|
|
169
|
+
const trimmedLine = line.trim();
|
|
170
|
+
|
|
171
|
+
// Check for filename directive (must be first non-empty line)
|
|
172
|
+
if (i === 0 || (cleanLines.length === 0 && !extractedFilename)) {
|
|
173
|
+
const filenameMatch = trimmedLine.match(/^\/\/\s*filename:\s*(.+)$/);
|
|
174
|
+
if (filenameMatch) {
|
|
175
|
+
extractedFilename = filenameMatch[1]?.trim();
|
|
176
|
+
continue; // Don't include this directive line
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (shouldStripNotations) {
|
|
181
|
+
line = line.replace(/\s*\/\/\s*\[!code\s+[^\]]+\]\s*$/, '');
|
|
182
|
+
line = line.replace(/\s*#\s*\[!code\s+[^\]]+\]\s*$/, '');
|
|
183
|
+
// Skip lines that are only the notation (e.g., standalone "// [!code highlight]")
|
|
184
|
+
if (trimmedLine.match(/^(\/\/|#)\s*\[!code\s+[^\]]+\]\s*$/)) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
cleanLines.push(line);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
cleanCode: cleanLines.join('\n'),
|
|
194
|
+
extractedFilename,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Escape HTML special characters for safe display (fallback)
|
|
200
|
+
*/
|
|
201
|
+
function escapeHtml(text: string): string {
|
|
202
|
+
return text
|
|
203
|
+
.replace(/&/g, '&')
|
|
204
|
+
.replace(/</g, '<')
|
|
205
|
+
.replace(/>/g, '>')
|
|
206
|
+
.replace(/"/g, '"')
|
|
207
|
+
.replace(/'/g, ''');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const darkModeClassNames = cn(
|
|
211
|
+
'dark:[&_.shiki]:!text-[var(--shiki-dark)]',
|
|
212
|
+
'dark:[&_.shiki]:![font-style:var(--shiki-dark-font-style)]',
|
|
213
|
+
'dark:[&_.shiki]:![font-weight:var(--shiki-dark-font-weight)]',
|
|
214
|
+
'dark:[&_.shiki]:![text-decoration:var(--shiki-dark-text-decoration)]',
|
|
215
|
+
'dark:[&_.shiki_span]:!text-[var(--shiki-dark)]',
|
|
216
|
+
'dark:[&_.shiki_span]:![font-style:var(--shiki-dark-font-style)]',
|
|
217
|
+
'dark:[&_.shiki_span]:![font-weight:var(--shiki-dark-font-weight)]',
|
|
218
|
+
'dark:[&_.shiki_span]:![text-decoration:var(--shiki-dark-text-decoration)]',
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const lineHighlightClassNames = cn(
|
|
222
|
+
'[&_.line.highlighted]:bg-code-highlight',
|
|
223
|
+
'[&_.line.highlighted]:after:bg-code-highlight-accent',
|
|
224
|
+
'[&_.line.highlighted]:after:absolute',
|
|
225
|
+
'[&_.line.highlighted]:after:left-0',
|
|
226
|
+
'[&_.line.highlighted]:after:top-0',
|
|
227
|
+
'[&_.line.highlighted]:after:bottom-0',
|
|
228
|
+
'[&_.line.highlighted]:after:w-0.5',
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
const lineDiffClassNames = cn(
|
|
232
|
+
'[&_.line.diff]:after:absolute',
|
|
233
|
+
'[&_.line.diff]:after:left-0',
|
|
234
|
+
'[&_.line.diff]:after:top-0',
|
|
235
|
+
'[&_.line.diff]:after:bottom-0',
|
|
236
|
+
'[&_.line.diff]:after:w-0.5',
|
|
237
|
+
'[&_.line.diff.add]:bg-code-diff-add',
|
|
238
|
+
'[&_.line.diff.add]:after:bg-code-diff-add-accent',
|
|
239
|
+
'[&_.line.diff.remove]:bg-code-diff-remove',
|
|
240
|
+
'[&_.line.diff.remove]:after:bg-code-diff-remove-accent',
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
const lineFocusedClassNames = cn(
|
|
244
|
+
'[&_code:has(.focused)_.line]:blur-[2px]',
|
|
245
|
+
'[&_code:has(.focused)_.line.focused]:blur-none',
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
const wordHighlightClassNames = cn('[&_.highlighted-word]:bg-code-highlight');
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Tooltip icon-button used for code-block toolbar actions (and the built-in copy
|
|
252
|
+
* button). Compose these into the `actions` slot of {@link CodeBlock}.
|
|
253
|
+
*/
|
|
254
|
+
function CodeBlockAction({ icon, label, onClick, disabled }: CodeBlockActionProps) {
|
|
255
|
+
return (
|
|
256
|
+
<Tooltip>
|
|
257
|
+
<TooltipTrigger
|
|
258
|
+
render={
|
|
259
|
+
<Button
|
|
260
|
+
variant="ghost"
|
|
261
|
+
size="icon"
|
|
262
|
+
onClick={onClick}
|
|
263
|
+
aria-label={label}
|
|
264
|
+
disabled={disabled}
|
|
265
|
+
className={cn('h-7 w-7', disabled && 'cursor-not-allowed opacity-50')}
|
|
266
|
+
/>
|
|
267
|
+
}
|
|
268
|
+
>
|
|
269
|
+
{icon}
|
|
270
|
+
</TooltipTrigger>
|
|
271
|
+
<TooltipContent side="top">{label}</TooltipContent>
|
|
272
|
+
</Tooltip>
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
interface CodeBlockToolbarProps {
|
|
277
|
+
onCopy: () => void;
|
|
278
|
+
isCopied: boolean;
|
|
279
|
+
actions?: ReactNode;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function CodeBlockToolbar({ onCopy, isCopied, actions }: CodeBlockToolbarProps) {
|
|
283
|
+
return (
|
|
284
|
+
<div className="flex items-center gap-0.5">
|
|
285
|
+
{actions}
|
|
286
|
+
<CodeBlockAction
|
|
287
|
+
icon={
|
|
288
|
+
isCopied ? <CheckIcon className="size-4 text-success" /> : <CopyIcon className="size-4" />
|
|
289
|
+
}
|
|
290
|
+
label={isCopied ? 'Copied!' : 'Copy code'}
|
|
291
|
+
onClick={onCopy}
|
|
292
|
+
/>
|
|
293
|
+
</div>
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
interface PackageManagerTabsProps {
|
|
298
|
+
activeManager: PackageManager;
|
|
299
|
+
onSelect: (pm: PackageManager) => void;
|
|
300
|
+
id: string;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function PackageManagerTabs({ activeManager, onSelect, id }: PackageManagerTabsProps) {
|
|
304
|
+
return (
|
|
305
|
+
// clip-path instead of overflow-hidden: overflow clips children at the
|
|
306
|
+
// padding box, so the active-tab underline could never cover the bottom
|
|
307
|
+
// border. Clipping at the border box lets it overlay the border row while
|
|
308
|
+
// the corners stay rounded.
|
|
309
|
+
<div className="bg-background border-border inline-flex rounded-md border text-xs [clip-path:inset(0_round_var(--radius-md))]">
|
|
310
|
+
{PACKAGE_MANAGERS.map((pm, index) => (
|
|
311
|
+
<div key={pm} className="flex items-center">
|
|
312
|
+
{index > 0 && <div className="bg-border h-4 w-px" aria-hidden="true" />}
|
|
313
|
+
<Button
|
|
314
|
+
variant="ghost"
|
|
315
|
+
size="sm"
|
|
316
|
+
className={cn(
|
|
317
|
+
'relative h-auto rounded-none px-3 py-1.5 text-xs font-medium transition-colors',
|
|
318
|
+
activeManager === pm
|
|
319
|
+
? 'bg-muted text-foreground'
|
|
320
|
+
: 'text-muted-foreground hover:text-foreground hover:bg-muted/50',
|
|
321
|
+
)}
|
|
322
|
+
onClick={() => onSelect(pm)}
|
|
323
|
+
>
|
|
324
|
+
{pm}
|
|
325
|
+
{activeManager === pm && (
|
|
326
|
+
<motion.div
|
|
327
|
+
className="bg-brand absolute inset-x-0 -bottom-px mx-auto h-0.5 w-full"
|
|
328
|
+
layoutId={`codeblock-pm-tab-${id}`}
|
|
329
|
+
initial={false}
|
|
330
|
+
transition={{
|
|
331
|
+
type: 'spring',
|
|
332
|
+
stiffness: 500,
|
|
333
|
+
damping: 30,
|
|
334
|
+
}}
|
|
335
|
+
/>
|
|
336
|
+
)}
|
|
337
|
+
</Button>
|
|
338
|
+
</div>
|
|
339
|
+
))}
|
|
340
|
+
</div>
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
interface CodeBlockHeaderProps {
|
|
345
|
+
filename?: string;
|
|
346
|
+
language?: string;
|
|
347
|
+
isCommand?: boolean;
|
|
348
|
+
showPackageManagerTabs?: boolean;
|
|
349
|
+
activeManager?: PackageManager;
|
|
350
|
+
onSelectManager?: (pm: PackageManager) => void;
|
|
351
|
+
toolbar: ReactNode;
|
|
352
|
+
id: string;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function CodeBlockHeader({
|
|
356
|
+
filename,
|
|
357
|
+
language,
|
|
358
|
+
isCommand,
|
|
359
|
+
showPackageManagerTabs,
|
|
360
|
+
activeManager,
|
|
361
|
+
onSelectManager,
|
|
362
|
+
toolbar,
|
|
363
|
+
id,
|
|
364
|
+
}: CodeBlockHeaderProps) {
|
|
365
|
+
const getLabel = () => {
|
|
366
|
+
if (filename) {
|
|
367
|
+
const fileTypeIcon = matchFileTypeIcon(filename);
|
|
368
|
+
return (
|
|
369
|
+
<div className="text-muted-foreground flex items-center gap-1.5 text-sm">
|
|
370
|
+
{fileTypeIcon ? <FileTypeIcon icon={fileTypeIcon} /> : <FileIcon className="size-3.5" />}
|
|
371
|
+
<span>{filename}</span>
|
|
372
|
+
</div>
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (showPackageManagerTabs && activeManager && onSelectManager) {
|
|
377
|
+
return (
|
|
378
|
+
<PackageManagerTabs activeManager={activeManager} onSelect={onSelectManager} id={id} />
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (isCommand) {
|
|
383
|
+
return (
|
|
384
|
+
<div className="text-muted-foreground flex items-center gap-1.5 text-sm">
|
|
385
|
+
<TerminalIcon className="size-3.5" />
|
|
386
|
+
<span>Terminal</span>
|
|
387
|
+
</div>
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return (
|
|
392
|
+
<div className="text-muted-foreground flex items-center gap-1.5 text-sm">
|
|
393
|
+
<CodeIcon className="size-3.5" />
|
|
394
|
+
<span>{language ? language.charAt(0).toUpperCase() + language.slice(1) : 'Code'}</span>
|
|
395
|
+
</div>
|
|
396
|
+
);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
return (
|
|
400
|
+
<div className="bg-muted/50 border-border flex items-center justify-between border-b px-3 py-2">
|
|
401
|
+
<div className="flex items-center gap-2">{getLabel()}</div>
|
|
402
|
+
{toolbar}
|
|
403
|
+
</div>
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
interface FadeOverlayProps {
|
|
408
|
+
visible: boolean;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function FadeOverlay({ visible }: FadeOverlayProps) {
|
|
412
|
+
return (
|
|
413
|
+
<div
|
|
414
|
+
className={cn(
|
|
415
|
+
'from-card pointer-events-none absolute inset-x-0 bottom-0 h-20 bg-gradient-to-t to-transparent transition-opacity duration-300',
|
|
416
|
+
visible ? 'opacity-100' : 'opacity-0',
|
|
417
|
+
)}
|
|
418
|
+
aria-hidden="true"
|
|
419
|
+
/>
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
interface ExpandButtonProps {
|
|
424
|
+
isExpanded: boolean;
|
|
425
|
+
hiddenLineCount: number;
|
|
426
|
+
onToggle: () => void;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function ExpandButton({ isExpanded, hiddenLineCount, onToggle }: ExpandButtonProps) {
|
|
430
|
+
return (
|
|
431
|
+
<div className="border-border bg-muted/30 flex items-center justify-center border-t py-2">
|
|
432
|
+
<Button
|
|
433
|
+
variant="ghost"
|
|
434
|
+
size="sm"
|
|
435
|
+
onClick={onToggle}
|
|
436
|
+
className="text-muted-foreground hover:text-foreground gap-1.5 text-xs"
|
|
437
|
+
>
|
|
438
|
+
{isExpanded ? (
|
|
439
|
+
<>
|
|
440
|
+
<ChevronUpIcon className="size-3.5" />
|
|
441
|
+
Show less
|
|
442
|
+
</>
|
|
443
|
+
) : (
|
|
444
|
+
<>
|
|
445
|
+
<ChevronDownIcon className="size-3.5" />
|
|
446
|
+
Show {hiddenLineCount} more lines
|
|
447
|
+
</>
|
|
448
|
+
)}
|
|
449
|
+
</Button>
|
|
450
|
+
</div>
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const codeBlockContentClassName = cn(
|
|
455
|
+
'bg-transparent text-sm',
|
|
456
|
+
'[&_pre]:py-4',
|
|
457
|
+
'[&_.shiki]:!bg-transparent',
|
|
458
|
+
'[&_code]:w-full',
|
|
459
|
+
'[&_code]:grid',
|
|
460
|
+
'[&_code]:overflow-x-auto',
|
|
461
|
+
'[&_code]:bg-transparent',
|
|
462
|
+
'[&_.line]:px-4',
|
|
463
|
+
'[&_.line]:w-full',
|
|
464
|
+
'[&_.line]:relative',
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
interface SyntaxHighlightedContentProps {
|
|
468
|
+
code: string;
|
|
469
|
+
language: SupportedLanguage;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function SyntaxHighlightedContent({ code, language }: SyntaxHighlightedContentProps) {
|
|
473
|
+
const [html, setHtml] = useState<string | null>(null);
|
|
474
|
+
|
|
475
|
+
useEffect(() => {
|
|
476
|
+
highlightCode(code, language)
|
|
477
|
+
.then(setHtml)
|
|
478
|
+
.catch((error) => {
|
|
479
|
+
console.error('Shiki highlighting failed:', error);
|
|
480
|
+
// Fallback to escaped plain text
|
|
481
|
+
setHtml(`<pre class="shiki"><code>${escapeHtml(code)}</code></pre>`);
|
|
482
|
+
});
|
|
483
|
+
}, [code, language]);
|
|
484
|
+
|
|
485
|
+
if (!html) {
|
|
486
|
+
// Show fallback while loading
|
|
487
|
+
return (
|
|
488
|
+
<pre className="shiki py-4">
|
|
489
|
+
<code className="grid w-full overflow-x-auto bg-transparent">
|
|
490
|
+
{code.split('\n').map((line, i) => (
|
|
491
|
+
<span key={i} className="line relative w-full px-4">
|
|
492
|
+
{line}
|
|
493
|
+
</span>
|
|
494
|
+
))}
|
|
495
|
+
</code>
|
|
496
|
+
</pre>
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return <div dangerouslySetInnerHTML={{ __html: html }} />;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Shiki-highlighted code block with a header, copy button, collapse/expand for
|
|
505
|
+
* long snippets, and an optional npm→pnpm/yarn/bun package-manager switcher.
|
|
506
|
+
* Highlighting is lazy (a shared highlighter singleton + per-snippet cache), and
|
|
507
|
+
* line/diff/focus/word decorations use Shiki's `[!code ...]` notations. The DS
|
|
508
|
+
* never toasts. Wire `onCopied`/`onCopyError` to your own feedback — or, when
|
|
509
|
+
* rendering from server components (where function props can't be passed),
|
|
510
|
+
* mount `CopyFeedbackProvider` once and leave the props off.
|
|
511
|
+
*/
|
|
512
|
+
export function CodeBlock({
|
|
513
|
+
className,
|
|
514
|
+
language,
|
|
515
|
+
filename,
|
|
516
|
+
children,
|
|
517
|
+
hideHeader = false,
|
|
518
|
+
packageManagerSwitcher = false,
|
|
519
|
+
actions,
|
|
520
|
+
onCopied,
|
|
521
|
+
onCopyError,
|
|
522
|
+
...props
|
|
523
|
+
}: CodeBlockProps) {
|
|
524
|
+
const { copied, copy } = useCopy();
|
|
525
|
+
const copyFeedback = useCopyFeedback();
|
|
526
|
+
const [activePackageManager, setActivePackageManager] = usePackageManager();
|
|
527
|
+
const [isExpanded, setIsExpanded] = useState(false);
|
|
528
|
+
const instanceId = useId();
|
|
529
|
+
|
|
530
|
+
const rawCode = children ?? '';
|
|
531
|
+
|
|
532
|
+
const { cleanCode, extractedFilename } = useMemo(
|
|
533
|
+
() => processCode(rawCode, language),
|
|
534
|
+
[rawCode, language],
|
|
535
|
+
);
|
|
536
|
+
|
|
537
|
+
const displayFilename = filename || extractedFilename;
|
|
538
|
+
|
|
539
|
+
const isShellLanguage =
|
|
540
|
+
language?.toLowerCase() === 'bash' ||
|
|
541
|
+
language?.toLowerCase() === 'shell' ||
|
|
542
|
+
language?.toLowerCase() === 'sh';
|
|
543
|
+
const isCommand = packageManagerSwitcher && isShellLanguage && isPackageManagerCommand(cleanCode);
|
|
544
|
+
|
|
545
|
+
const displayCode = useMemo(() => {
|
|
546
|
+
if (isCommand) {
|
|
547
|
+
return transformCommand(cleanCode, activePackageManager);
|
|
548
|
+
}
|
|
549
|
+
return cleanCode;
|
|
550
|
+
}, [cleanCode, isCommand, activePackageManager]);
|
|
551
|
+
|
|
552
|
+
const lineCount = useMemo(() => displayCode.split('\n').length, [displayCode]);
|
|
553
|
+
const isCollapsible = lineCount > COLLAPSE_THRESHOLD;
|
|
554
|
+
const hiddenLineCount = lineCount - Math.floor(COLLAPSED_HEIGHT / 24); // Approximate visible lines based on line height
|
|
555
|
+
|
|
556
|
+
const handleCopy = async () => {
|
|
557
|
+
const ok = await copy(displayCode);
|
|
558
|
+
if (ok) (onCopied ?? copyFeedback.onCopied)?.();
|
|
559
|
+
else (onCopyError ?? copyFeedback.onCopyError)?.(new Error('Failed to copy to the clipboard'));
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
const toolbar = <CodeBlockToolbar onCopy={handleCopy} isCopied={copied} actions={actions} />;
|
|
563
|
+
|
|
564
|
+
const showCollapsed = isCollapsible && !isExpanded;
|
|
565
|
+
|
|
566
|
+
return (
|
|
567
|
+
<div
|
|
568
|
+
data-slot="code-block"
|
|
569
|
+
{...props}
|
|
570
|
+
className={cn(
|
|
571
|
+
'bg-card not-prose border-border relative mb-4 w-full contain-inline-size overflow-hidden rounded-md border text-sm lg:text-base',
|
|
572
|
+
className,
|
|
573
|
+
)}
|
|
574
|
+
>
|
|
575
|
+
{!hideHeader && (
|
|
576
|
+
<CodeBlockHeader
|
|
577
|
+
filename={displayFilename}
|
|
578
|
+
language={language}
|
|
579
|
+
isCommand={isCommand}
|
|
580
|
+
showPackageManagerTabs={isCommand}
|
|
581
|
+
activeManager={activePackageManager}
|
|
582
|
+
onSelectManager={setActivePackageManager}
|
|
583
|
+
toolbar={toolbar}
|
|
584
|
+
id={instanceId}
|
|
585
|
+
/>
|
|
586
|
+
)}
|
|
587
|
+
|
|
588
|
+
<div className="relative">
|
|
589
|
+
<motion.div
|
|
590
|
+
className="overflow-hidden"
|
|
591
|
+
initial={false}
|
|
592
|
+
animate={{
|
|
593
|
+
height: showCollapsed ? COLLAPSED_HEIGHT : 'auto',
|
|
594
|
+
}}
|
|
595
|
+
transition={{
|
|
596
|
+
duration: 0.3,
|
|
597
|
+
ease: [0.4, 0, 0.2, 1],
|
|
598
|
+
}}
|
|
599
|
+
>
|
|
600
|
+
<ScrollArea className="w-full whitespace-nowrap">
|
|
601
|
+
<div
|
|
602
|
+
className={cn(
|
|
603
|
+
codeBlockContentClassName,
|
|
604
|
+
lineHighlightClassNames,
|
|
605
|
+
lineDiffClassNames,
|
|
606
|
+
lineFocusedClassNames,
|
|
607
|
+
wordHighlightClassNames,
|
|
608
|
+
darkModeClassNames,
|
|
609
|
+
)}
|
|
610
|
+
>
|
|
611
|
+
<SyntaxHighlightedContent
|
|
612
|
+
code={displayCode}
|
|
613
|
+
language={normalizeLanguage(isCommand ? 'bash' : language)}
|
|
614
|
+
/>
|
|
615
|
+
</div>
|
|
616
|
+
<ScrollBar orientation="horizontal" />
|
|
617
|
+
</ScrollArea>
|
|
618
|
+
</motion.div>
|
|
619
|
+
<FadeOverlay visible={showCollapsed} />
|
|
620
|
+
</div>
|
|
621
|
+
|
|
622
|
+
{isCollapsible && (
|
|
623
|
+
<ExpandButton
|
|
624
|
+
isExpanded={isExpanded}
|
|
625
|
+
hiddenLineCount={hiddenLineCount}
|
|
626
|
+
onToggle={() => setIsExpanded(!isExpanded)}
|
|
627
|
+
/>
|
|
628
|
+
)}
|
|
629
|
+
</div>
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export { CodeBlockAction, transformCommand, processCode, matchFileTypeIcon };
|
|
634
|
+
export type { CodeBlockProps, CodeBlockActionProps, PackageManager };
|
|
@@ -137,6 +137,24 @@ function ComboboxFreeText<T extends ComboboxFreeTextItem = ComboboxFreeTextItem>
|
|
|
137
137
|
placeholder={placeholder}
|
|
138
138
|
disabled={disabled}
|
|
139
139
|
aria-invalid={invalid || undefined}
|
|
140
|
+
// Base UI only requests open on a keystroke; for pick-or-create
|
|
141
|
+
// fields the suggestions should also show on focus/click so a small
|
|
142
|
+
// known option set is discoverable without typing. The
|
|
143
|
+
// `items.length > 0` gate on `open` keeps an empty popup from
|
|
144
|
+
// flashing when there is nothing to pick.
|
|
145
|
+
onFocus={() => setOpen(true)}
|
|
146
|
+
onClick={() => setOpen(true)}
|
|
147
|
+
// Enter has a local meaning here — commit the highlighted suggestion
|
|
148
|
+
// or keep the typed text — so it must never double as the browser's
|
|
149
|
+
// implicit form submission (Base UI deliberately lets Enter fall
|
|
150
|
+
// through to submit when nothing is highlighted). preventDefault
|
|
151
|
+
// cancels only the native submit; Base UI's own Enter handling runs
|
|
152
|
+
// regardless, since merged handlers ignore defaultPrevented.
|
|
153
|
+
onKeyDown={(event) => {
|
|
154
|
+
if (event.key === 'Enter') {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
}
|
|
157
|
+
}}
|
|
140
158
|
/>
|
|
141
159
|
{loading ? (
|
|
142
160
|
<InputGroupAddon align="inline-end">
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { createContext, type ReactNode, useContext, useMemo } from 'react';
|
|
4
|
+
|
|
5
|
+
// Function props can't cross a server→client boundary, so a copy surface
|
|
6
|
+
// rendered from RSC (e.g. MDX docs) could never receive an `onCopied` callback
|
|
7
|
+
// for toast wiring. This context is the RSC-safe alternative: mount
|
|
8
|
+
// CopyFeedbackProvider once in a client component (wire your toast there — the
|
|
9
|
+
// DS never toasts), and copy surfaces resolve their feedback in a fixed order —
|
|
10
|
+
// explicit prop → this context → nothing beyond the built-in copied icon.
|
|
11
|
+
interface CopyFeedbackContextValue {
|
|
12
|
+
/** Called after a successful copy. Wire your toast here — the DS never toasts. */
|
|
13
|
+
onCopied?: () => void;
|
|
14
|
+
/** Called when the clipboard write fails. */
|
|
15
|
+
onCopyError?: (error: Error) => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const CopyFeedbackContext = createContext<CopyFeedbackContextValue>({});
|
|
19
|
+
|
|
20
|
+
function CopyFeedbackProvider({
|
|
21
|
+
children,
|
|
22
|
+
onCopied,
|
|
23
|
+
onCopyError,
|
|
24
|
+
}: CopyFeedbackContextValue & { children: ReactNode }) {
|
|
25
|
+
const value = useMemo(() => ({ onCopied, onCopyError }), [onCopied, onCopyError]);
|
|
26
|
+
return <CopyFeedbackContext.Provider value={value}>{children}</CopyFeedbackContext.Provider>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function useCopyFeedback(): CopyFeedbackContextValue {
|
|
30
|
+
return useContext(CopyFeedbackContext);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { CopyFeedbackProvider, useCopyFeedback, type CopyFeedbackContextValue };
|