@veluai/velu 0.2.13 → 0.2.15

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 (109) hide show
  1. package/README.md +80 -80
  2. package/dist/cli.js +37 -37
  3. package/package.json +64 -64
  4. package/runtime/velu-ui/base.css +320 -320
  5. package/runtime/velu-ui/components/Accordion.jsx +64 -64
  6. package/runtime/velu-ui/components/ApiClient.jsx +207 -207
  7. package/runtime/velu-ui/components/ApiField.jsx +87 -87
  8. package/runtime/velu-ui/components/ApiPath.jsx +63 -63
  9. package/runtime/velu-ui/components/ApiReferencePage.jsx +384 -384
  10. package/runtime/velu-ui/components/ApiSamples.jsx +36 -36
  11. package/runtime/velu-ui/components/ApiSidebar.jsx +122 -122
  12. package/runtime/velu-ui/components/AskBar.jsx +71 -71
  13. package/runtime/velu-ui/components/Callout.jsx +114 -114
  14. package/runtime/velu-ui/components/Card.jsx +131 -131
  15. package/runtime/velu-ui/components/Chatbot.jsx +885 -885
  16. package/runtime/velu-ui/components/CodeBlock.jsx +375 -375
  17. package/runtime/velu-ui/components/Columns.jsx +56 -56
  18. package/runtime/velu-ui/components/ContextMenu.jsx +298 -273
  19. package/runtime/velu-ui/components/ErrorCard.jsx +138 -138
  20. package/runtime/velu-ui/components/Field.jsx +81 -81
  21. package/runtime/velu-ui/components/Image.jsx +163 -163
  22. package/runtime/velu-ui/components/Logo.jsx +31 -31
  23. package/runtime/velu-ui/components/MethodBadge.jsx +31 -31
  24. package/runtime/velu-ui/components/NavSelect.jsx +108 -108
  25. package/runtime/velu-ui/components/NotFound.jsx +63 -63
  26. package/runtime/velu-ui/components/PageFeedback.jsx +219 -219
  27. package/runtime/velu-ui/components/PageFooter.jsx +145 -145
  28. package/runtime/velu-ui/components/PageHeader.jsx +422 -422
  29. package/runtime/velu-ui/components/PageNav.jsx +77 -77
  30. package/runtime/velu-ui/components/PoweredBy.jsx +51 -51
  31. package/runtime/velu-ui/components/Prompt.jsx +115 -115
  32. package/runtime/velu-ui/components/Search.jsx +460 -460
  33. package/runtime/velu-ui/components/Sidebar.jsx +254 -254
  34. package/runtime/velu-ui/components/SocialLinks.jsx +90 -90
  35. package/runtime/velu-ui/components/Steps.jsx +65 -65
  36. package/runtime/velu-ui/components/ThemeToggle.jsx +48 -48
  37. package/runtime/velu-ui/components/Toc.jsx +537 -537
  38. package/runtime/velu-ui/components/TocBar.jsx +195 -195
  39. package/runtime/velu-ui/components/Tree.jsx +87 -87
  40. package/runtime/velu-ui/components/TryItBar.jsx +102 -102
  41. package/runtime/velu-ui/components/accordion.css +92 -92
  42. package/runtime/velu-ui/components/api-page.css +208 -208
  43. package/runtime/velu-ui/components/api.css +635 -635
  44. package/runtime/velu-ui/components/ask-bar.css +94 -94
  45. package/runtime/velu-ui/components/card.css +105 -105
  46. package/runtime/velu-ui/components/chatbot.css +622 -622
  47. package/runtime/velu-ui/components/code-block.css +263 -263
  48. package/runtime/velu-ui/components/context-menu.css +173 -173
  49. package/runtime/velu-ui/components/docs-layout.css +822 -822
  50. package/runtime/velu-ui/components/field.css +82 -82
  51. package/runtime/velu-ui/components/image.css +237 -237
  52. package/runtime/velu-ui/components/nav-select.css +157 -157
  53. package/runtime/velu-ui/components/not-found.css +94 -94
  54. package/runtime/velu-ui/components/page-feedback.css +241 -241
  55. package/runtime/velu-ui/components/page-footer.css +130 -130
  56. package/runtime/velu-ui/components/page-header.css +558 -558
  57. package/runtime/velu-ui/components/page-nav.css +50 -50
  58. package/runtime/velu-ui/components/powered-by.css +92 -92
  59. package/runtime/velu-ui/components/prompt.css +99 -99
  60. package/runtime/velu-ui/components/search.css +307 -307
  61. package/runtime/velu-ui/components/sidebar.css +205 -205
  62. package/runtime/velu-ui/components/steps.css +77 -77
  63. package/runtime/velu-ui/components/theme-toggle.css +102 -102
  64. package/runtime/velu-ui/components/toc-bar.css +234 -234
  65. package/runtime/velu-ui/components/tree.css +49 -49
  66. package/runtime/velu-ui/index.js +54 -54
  67. package/runtime/velu-ui/lib/api-send.js +92 -92
  68. package/runtime/velu-ui/lib/brand-icons.jsx +103 -103
  69. package/runtime/velu-ui/lib/component-schemas.js +100 -100
  70. package/runtime/velu-ui/lib/copyText.js +64 -64
  71. package/runtime/velu-ui/lib/docs-assistant.js +250 -250
  72. package/runtime/velu-ui/lib/lang-icons.jsx +147 -147
  73. package/runtime/velu-ui/lib/pagefind.js +113 -113
  74. package/runtime/velu-ui/lib/prism-langs.js +957 -957
  75. package/runtime/velu-ui/lib/prism-loader.js +74 -74
  76. package/runtime/velu-ui/lib/resolveIcon.jsx +29 -29
  77. package/runtime/velu-ui/lib/scrollIntoNearestView.js +66 -66
  78. package/runtime/velu-ui/mdx-components.jsx +105 -105
  79. package/runtime/velu-ui/primitives/Cluster.jsx +49 -49
  80. package/runtime/velu-ui/primitives/Stack.jsx +63 -63
  81. package/runtime/velu-ui/primitives/Switcher.jsx +57 -57
  82. package/runtime/velu-ui/primitives/stack.css +3 -3
  83. package/runtime/velu-ui/primitives/switcher.css +25 -25
  84. package/runtime/velu-ui/styles.css +46 -46
  85. package/runtime/velu-ui/tokens.css +4 -4
  86. package/schema/velu.schema.json +423 -423
  87. package/src/lib/extract-mdx-error.js +170 -170
  88. package/src/lib/issues.js +159 -159
  89. package/src/lib/known-components.js +34 -34
  90. package/src/navigation.js +443 -443
  91. package/src/runtime/App.jsx +1669 -1668
  92. package/src/runtime/ErrorBoundary.jsx +54 -54
  93. package/src/runtime/client-entry.jsx +22 -22
  94. package/src/runtime/server-entry.jsx +16 -16
  95. package/src/template.html +48 -48
  96. package/templates/starter/ai-tools/claude-code.mdx +26 -26
  97. package/templates/starter/ai-tools/cursor.mdx +17 -17
  98. package/templates/starter/api-reference/introduction.mdx +43 -43
  99. package/templates/starter/development.mdx +19 -19
  100. package/templates/starter/essentials/code.mdx +29 -29
  101. package/templates/starter/essentials/images.mdx +29 -29
  102. package/templates/starter/essentials/markdown.mdx +25 -25
  103. package/templates/starter/essentials/navigation.mdx +39 -39
  104. package/templates/starter/essentials/settings.mdx +30 -30
  105. package/templates/starter/favicon.svg +6 -6
  106. package/templates/starter/index.mdx +31 -31
  107. package/templates/starter/openapi.json +160 -160
  108. package/templates/starter/quickstart.mdx +31 -31
  109. package/templates/starter/velu.json +41 -41
@@ -1,375 +1,375 @@
1
- import React, {
2
- Children,
3
- isValidElement,
4
- useCallback,
5
- useEffect,
6
- useRef,
7
- useState,
8
- } from 'react';
9
- import { Highlight } from 'prism-react-renderer';
10
- import { ensureLang, hasLang, resolveLang } from '../lib/prism-loader.js';
11
- import { iconForLang } from '../lib/lang-icons.jsx';
12
- import resolveIcon from '../lib/resolveIcon.jsx';
13
- import copyText from '../lib/copyText.js';
14
-
15
- /**
16
- * CodeBlock + CodeGroup — syntax-highlighted code with optional filename
17
- * label or tabbed group of language alternatives.
18
- *
19
- * Standalone (with filename):
20
- * <CodeBlock filename="index.js" language="javascript" lineNumbers
21
- * icon="file-code">
22
- * {`let greeting = function (name) {
23
- * console.log(\`Hello, \${name}!\`);
24
- * };`}
25
- * </CodeBlock>
26
- *
27
- * Grouped (tabbed):
28
- * <CodeGroup>
29
- * <CodeBlock title="Javascript" language="javascript">{js}</CodeBlock>
30
- * <CodeBlock title="Ruby" language="ruby">{rb}</CodeBlock>
31
- * <CodeBlock title="Python" language="python">{py}</CodeBlock>
32
- * </CodeGroup>
33
- *
34
- * Highlighting uses prism-react-renderer with an empty theme — token CSS
35
- * classes (`.token.<type>`) are styled in code-block.css using
36
- * `--syntax-*` tokens that switch per [data-theme]. Languages outside the
37
- * prism-react-renderer default bundle are LAZY-LOADED from prismjs on
38
- * first use via the loader in lib/prism-loader.js — SSR (and the first
39
- * client render) shows plain code for unloaded langs, then upgrades to
40
- * highlighted once the lang's prism file has fetched. No 1.4 MB bundle
41
- * tax for languages the page doesn't actually use.
42
- */
43
-
44
- // Empty theme so prism-react-renderer emits class names but no inline
45
- // colors — all styling lives in code-block.css.
46
- const NO_THEME = { plain: {}, styles: [] };
47
-
48
- function normalize(code) {
49
- if (typeof code !== 'string') return '';
50
- return code.replace(/^\n+/, '').replace(/\n+$/, '');
51
- }
52
-
53
- // Parse a highlight spec into a Set of 1-based line numbers.
54
- // "1-3,5,7-9" → {1,2,3,5,7,8,9}
55
- // [1, 2, 5] → {1,2,5}
56
- // null / '' → null (nothing highlighted)
57
- function parseHighlight(spec) {
58
- if (spec == null || spec === '') return null;
59
- if (Array.isArray(spec)) {
60
- const s = new Set();
61
- for (const n of spec) {
62
- const v = Number(n);
63
- if (Number.isFinite(v)) s.add(v);
64
- }
65
- return s.size ? s : null;
66
- }
67
- if (typeof spec !== 'string') return null;
68
- const s = new Set();
69
- for (const part of spec.split(',')) {
70
- const t = part.trim();
71
- if (!t) continue;
72
- const dash = t.indexOf('-');
73
- if (dash > 0) {
74
- const a = parseInt(t.slice(0, dash), 10);
75
- const b = parseInt(t.slice(dash + 1), 10);
76
- if (Number.isFinite(a) && Number.isFinite(b)) {
77
- const lo = Math.min(a, b);
78
- const hi = Math.max(a, b);
79
- for (let i = lo; i <= hi; i++) s.add(i);
80
- }
81
- } else {
82
- const n = parseInt(t, 10);
83
- if (Number.isFinite(n)) s.add(n);
84
- }
85
- }
86
- return s.size ? s : null;
87
- }
88
-
89
- function TabLabel({ icon, label }) {
90
- return (
91
- <>
92
- {icon != null && (
93
- <span className="velu-code-block__tab-icon" aria-hidden="true">
94
- {typeof icon === 'string' ? resolveIcon(icon, { size: '1em' }) : icon}
95
- </span>
96
- )}
97
- <span className="velu-code-block__tab-label">{label}</span>
98
- </>
99
- );
100
- }
101
-
102
- function Tab({ active, onClick, icon, label }) {
103
- const cls = `velu-code-block__tab${
104
- active ? ' velu-code-block__tab--active' : ''
105
- }`;
106
- if (onClick) {
107
- return (
108
- <button
109
- type="button"
110
- role="tab"
111
- aria-selected={active}
112
- className={cls}
113
- onClick={onClick}
114
- >
115
- <TabLabel icon={icon} label={label} />
116
- </button>
117
- );
118
- }
119
- return (
120
- <div className={cls}>
121
- <TabLabel icon={icon} label={label} />
122
- </div>
123
- );
124
- }
125
-
126
- function CopyButton({ code }) {
127
- const [copied, setCopied] = useState(false);
128
-
129
- function copy() {
130
- copyText(code).then(
131
- () => {
132
- setCopied(true);
133
- setTimeout(() => setCopied(false), 1400);
134
- },
135
- () => {
136
- /* clipboard write rejected (permissions / no focus) — silently noop */
137
- },
138
- );
139
- }
140
-
141
- return (
142
- <button
143
- type="button"
144
- onClick={copy}
145
- className="velu-code-block__copy"
146
- aria-label={copied ? 'Copied' : 'Copy code'}
147
- title={copied ? 'Copied' : 'Copy'}
148
- >
149
- {resolveIcon(copied ? 'check' : 'copy', { size: '1em' })}
150
- </button>
151
- );
152
- }
153
-
154
- function CodeBody({ code, language, lineNumbers, highlight }) {
155
- const canonical = resolveLang(language) || 'text';
156
- const highlightSet = parseHighlight(highlight);
157
-
158
- // Initial render uses 'text' (no highlighting) — this guarantees SSR
159
- // and the first client render produce identical HTML regardless of
160
- // what's in Prism.languages on each side (which may diverge in dev
161
- // mode due to HMR leaving cached grammar mutations behind). After
162
- // hydration, useEffect kicks ensureLang and bumps to the real lang —
163
- // highlighting then upgrades in a normal state-update render.
164
- const [renderLang, setRenderLang] = useState('text');
165
-
166
- useEffect(() => {
167
- let cancelled = false;
168
- ensureLang(canonical).then(() => {
169
- if (!cancelled && hasLang(canonical)) setRenderLang(canonical);
170
- });
171
- return () => {
172
- cancelled = true;
173
- };
174
- }, [canonical]);
175
-
176
- return (
177
- <div className="velu-code-block__body">
178
- <CopyButton code={normalize(code)} />
179
- <Highlight
180
- code={normalize(code)}
181
- language={renderLang}
182
- theme={NO_THEME}
183
- >
184
- {({ tokens, getLineProps, getTokenProps }) => (
185
- <pre className="velu-code-block__pre">
186
- {tokens.map((line, i) => {
187
- const lineProps = getLineProps({ line });
188
- const isHi = highlightSet?.has(i + 1);
189
- return (
190
- <div
191
- key={i}
192
- {...lineProps}
193
- className={`velu-code-block__line ${
194
- lineProps.className || ''
195
- } ${isHi ? 'velu-code-block__line--highlight' : ''}`.trim()}
196
- >
197
- {lineNumbers && (
198
- <span
199
- className="velu-code-block__line-number"
200
- aria-hidden="true"
201
- >
202
- {i + 1}
203
- </span>
204
- )}
205
- <span className="velu-code-block__line-code">
206
- {line.map((token, j) => (
207
- <span key={j} {...getTokenProps({ token })} />
208
- ))}
209
- </span>
210
- </div>
211
- );
212
- })}
213
- </pre>
214
- )}
215
- </Highlight>
216
- </div>
217
- );
218
- }
219
-
220
- // Explicit `icon` always wins. `withIcon` defaults to true, so an icon
221
- // is auto-rendered from `lib/lang-icons.jsx` based on the language;
222
- // pass `withIcon={false}` to suppress.
223
- function pickIcon({ icon, withIcon, language }) {
224
- if (icon != null) return icon;
225
- if (withIcon === false) return null;
226
- return iconForLang(language);
227
- }
228
-
229
- export function CodeBlock({
230
- filename,
231
- title,
232
- language = 'text',
233
- icon,
234
- withIcon = true,
235
- lineNumbers = false,
236
- highlight,
237
- children,
238
- className = '',
239
- ...rest
240
- }) {
241
- const cls = `velu-code-block ${className}`.trim();
242
- const label = filename || title;
243
- const tabIcon = pickIcon({ icon, withIcon, language });
244
- return (
245
- <div className={cls} {...rest}>
246
- {label && (
247
- <div className="velu-code-block__header">
248
- <Tab active icon={tabIcon} label={label} />
249
- </div>
250
- )}
251
- <CodeBody
252
- code={children}
253
- language={language}
254
- lineNumbers={lineNumbers}
255
- highlight={highlight}
256
- />
257
- </div>
258
- );
259
- }
260
- CodeBlock.displayName = 'CodeBlock';
261
-
262
- // Hook that tracks whether a scrollable element can scroll further left
263
- // or right. Resolves layout-driven overflow without media queries.
264
- function useScrollEdges(ref) {
265
- const [canLeft, setCanLeft] = useState(false);
266
- const [canRight, setCanRight] = useState(false);
267
-
268
- const update = useCallback(() => {
269
- const el = ref.current;
270
- if (!el) return;
271
- setCanLeft(el.scrollLeft > 1);
272
- setCanRight(el.scrollLeft + el.clientWidth < el.scrollWidth - 1);
273
- }, [ref]);
274
-
275
- useEffect(() => {
276
- const el = ref.current;
277
- if (!el) return;
278
- update();
279
- const ro = new ResizeObserver(update);
280
- ro.observe(el);
281
- // Also observe each child so adding/removing tabs retriggers update.
282
- for (const c of el.children) ro.observe(c);
283
- el.addEventListener('scroll', update, { passive: true });
284
- return () => {
285
- ro.disconnect();
286
- el.removeEventListener('scroll', update);
287
- };
288
- }, [ref, update]);
289
-
290
- return { canLeft, canRight, update };
291
- }
292
-
293
- export function CodeGroup({
294
- children,
295
- className = '',
296
- defaultIndex = 0,
297
- ...rest
298
- }) {
299
- const blocks = Children.toArray(children).filter(
300
- (c) =>
301
- isValidElement(c) &&
302
- (c.type === CodeBlock || c.type?.displayName === 'CodeBlock'),
303
- );
304
- const [active, setActive] = useState(defaultIndex);
305
- const tabsRef = useRef(null);
306
- const { canLeft, canRight } = useScrollEdges(tabsRef);
307
- const block = blocks[active] || blocks[0];
308
- if (!block) return null;
309
-
310
- const {
311
- language = 'text',
312
- lineNumbers = false,
313
- highlight,
314
- children: code,
315
- } = block.props;
316
- const cls = `velu-code-block velu-code-block--group ${className}`.trim();
317
-
318
- function scrollDir(dir) {
319
- tabsRef.current?.scrollBy({ left: dir * 160, behavior: 'smooth' });
320
- }
321
-
322
- return (
323
- <div className={cls} {...rest}>
324
- <div className="velu-code-block__header">
325
- {canLeft && (
326
- <button
327
- type="button"
328
- className="velu-code-block__nav"
329
- onClick={() => scrollDir(-1)}
330
- aria-label="Scroll tabs left"
331
- >
332
- {resolveIcon('chevron-left', { size: '1em' })}
333
- </button>
334
- )}
335
- <div
336
- ref={tabsRef}
337
- className="velu-code-block__tabs"
338
- role="tablist"
339
- >
340
- {blocks.map((b, i) => {
341
- const { title, icon, withIcon, language } = b.props;
342
- const tabIcon = pickIcon({ icon, withIcon, language });
343
- return (
344
- <Tab
345
- key={i}
346
- active={i === active}
347
- onClick={() => setActive(i)}
348
- icon={tabIcon}
349
- label={title || `Tab ${i + 1}`}
350
- />
351
- );
352
- })}
353
- </div>
354
- {canRight && (
355
- <button
356
- type="button"
357
- className="velu-code-block__nav"
358
- onClick={() => scrollDir(1)}
359
- aria-label="Scroll tabs right"
360
- >
361
- {resolveIcon('chevron-right', { size: '1em' })}
362
- </button>
363
- )}
364
- </div>
365
- <CodeBody
366
- code={code}
367
- language={language}
368
- lineNumbers={lineNumbers}
369
- highlight={highlight}
370
- />
371
- </div>
372
- );
373
- }
374
-
375
- export default CodeBlock;
1
+ import React, {
2
+ Children,
3
+ isValidElement,
4
+ useCallback,
5
+ useEffect,
6
+ useRef,
7
+ useState,
8
+ } from 'react';
9
+ import { Highlight } from 'prism-react-renderer';
10
+ import { ensureLang, hasLang, resolveLang } from '../lib/prism-loader.js';
11
+ import { iconForLang } from '../lib/lang-icons.jsx';
12
+ import resolveIcon from '../lib/resolveIcon.jsx';
13
+ import copyText from '../lib/copyText.js';
14
+
15
+ /**
16
+ * CodeBlock + CodeGroup — syntax-highlighted code with optional filename
17
+ * label or tabbed group of language alternatives.
18
+ *
19
+ * Standalone (with filename):
20
+ * <CodeBlock filename="index.js" language="javascript" lineNumbers
21
+ * icon="file-code">
22
+ * {`let greeting = function (name) {
23
+ * console.log(\`Hello, \${name}!\`);
24
+ * };`}
25
+ * </CodeBlock>
26
+ *
27
+ * Grouped (tabbed):
28
+ * <CodeGroup>
29
+ * <CodeBlock title="Javascript" language="javascript">{js}</CodeBlock>
30
+ * <CodeBlock title="Ruby" language="ruby">{rb}</CodeBlock>
31
+ * <CodeBlock title="Python" language="python">{py}</CodeBlock>
32
+ * </CodeGroup>
33
+ *
34
+ * Highlighting uses prism-react-renderer with an empty theme — token CSS
35
+ * classes (`.token.<type>`) are styled in code-block.css using
36
+ * `--syntax-*` tokens that switch per [data-theme]. Languages outside the
37
+ * prism-react-renderer default bundle are LAZY-LOADED from prismjs on
38
+ * first use via the loader in lib/prism-loader.js — SSR (and the first
39
+ * client render) shows plain code for unloaded langs, then upgrades to
40
+ * highlighted once the lang's prism file has fetched. No 1.4 MB bundle
41
+ * tax for languages the page doesn't actually use.
42
+ */
43
+
44
+ // Empty theme so prism-react-renderer emits class names but no inline
45
+ // colors — all styling lives in code-block.css.
46
+ const NO_THEME = { plain: {}, styles: [] };
47
+
48
+ function normalize(code) {
49
+ if (typeof code !== 'string') return '';
50
+ return code.replace(/^\n+/, '').replace(/\n+$/, '');
51
+ }
52
+
53
+ // Parse a highlight spec into a Set of 1-based line numbers.
54
+ // "1-3,5,7-9" → {1,2,3,5,7,8,9}
55
+ // [1, 2, 5] → {1,2,5}
56
+ // null / '' → null (nothing highlighted)
57
+ function parseHighlight(spec) {
58
+ if (spec == null || spec === '') return null;
59
+ if (Array.isArray(spec)) {
60
+ const s = new Set();
61
+ for (const n of spec) {
62
+ const v = Number(n);
63
+ if (Number.isFinite(v)) s.add(v);
64
+ }
65
+ return s.size ? s : null;
66
+ }
67
+ if (typeof spec !== 'string') return null;
68
+ const s = new Set();
69
+ for (const part of spec.split(',')) {
70
+ const t = part.trim();
71
+ if (!t) continue;
72
+ const dash = t.indexOf('-');
73
+ if (dash > 0) {
74
+ const a = parseInt(t.slice(0, dash), 10);
75
+ const b = parseInt(t.slice(dash + 1), 10);
76
+ if (Number.isFinite(a) && Number.isFinite(b)) {
77
+ const lo = Math.min(a, b);
78
+ const hi = Math.max(a, b);
79
+ for (let i = lo; i <= hi; i++) s.add(i);
80
+ }
81
+ } else {
82
+ const n = parseInt(t, 10);
83
+ if (Number.isFinite(n)) s.add(n);
84
+ }
85
+ }
86
+ return s.size ? s : null;
87
+ }
88
+
89
+ function TabLabel({ icon, label }) {
90
+ return (
91
+ <>
92
+ {icon != null && (
93
+ <span className="velu-code-block__tab-icon" aria-hidden="true">
94
+ {typeof icon === 'string' ? resolveIcon(icon, { size: '1em' }) : icon}
95
+ </span>
96
+ )}
97
+ <span className="velu-code-block__tab-label">{label}</span>
98
+ </>
99
+ );
100
+ }
101
+
102
+ function Tab({ active, onClick, icon, label }) {
103
+ const cls = `velu-code-block__tab${
104
+ active ? ' velu-code-block__tab--active' : ''
105
+ }`;
106
+ if (onClick) {
107
+ return (
108
+ <button
109
+ type="button"
110
+ role="tab"
111
+ aria-selected={active}
112
+ className={cls}
113
+ onClick={onClick}
114
+ >
115
+ <TabLabel icon={icon} label={label} />
116
+ </button>
117
+ );
118
+ }
119
+ return (
120
+ <div className={cls}>
121
+ <TabLabel icon={icon} label={label} />
122
+ </div>
123
+ );
124
+ }
125
+
126
+ function CopyButton({ code }) {
127
+ const [copied, setCopied] = useState(false);
128
+
129
+ function copy() {
130
+ copyText(code).then(
131
+ () => {
132
+ setCopied(true);
133
+ setTimeout(() => setCopied(false), 1400);
134
+ },
135
+ () => {
136
+ /* clipboard write rejected (permissions / no focus) — silently noop */
137
+ },
138
+ );
139
+ }
140
+
141
+ return (
142
+ <button
143
+ type="button"
144
+ onClick={copy}
145
+ className="velu-code-block__copy"
146
+ aria-label={copied ? 'Copied' : 'Copy code'}
147
+ title={copied ? 'Copied' : 'Copy'}
148
+ >
149
+ {resolveIcon(copied ? 'check' : 'copy', { size: '1em' })}
150
+ </button>
151
+ );
152
+ }
153
+
154
+ function CodeBody({ code, language, lineNumbers, highlight }) {
155
+ const canonical = resolveLang(language) || 'text';
156
+ const highlightSet = parseHighlight(highlight);
157
+
158
+ // Initial render uses 'text' (no highlighting) — this guarantees SSR
159
+ // and the first client render produce identical HTML regardless of
160
+ // what's in Prism.languages on each side (which may diverge in dev
161
+ // mode due to HMR leaving cached grammar mutations behind). After
162
+ // hydration, useEffect kicks ensureLang and bumps to the real lang —
163
+ // highlighting then upgrades in a normal state-update render.
164
+ const [renderLang, setRenderLang] = useState('text');
165
+
166
+ useEffect(() => {
167
+ let cancelled = false;
168
+ ensureLang(canonical).then(() => {
169
+ if (!cancelled && hasLang(canonical)) setRenderLang(canonical);
170
+ });
171
+ return () => {
172
+ cancelled = true;
173
+ };
174
+ }, [canonical]);
175
+
176
+ return (
177
+ <div className="velu-code-block__body">
178
+ <CopyButton code={normalize(code)} />
179
+ <Highlight
180
+ code={normalize(code)}
181
+ language={renderLang}
182
+ theme={NO_THEME}
183
+ >
184
+ {({ tokens, getLineProps, getTokenProps }) => (
185
+ <pre className="velu-code-block__pre">
186
+ {tokens.map((line, i) => {
187
+ const lineProps = getLineProps({ line });
188
+ const isHi = highlightSet?.has(i + 1);
189
+ return (
190
+ <div
191
+ key={i}
192
+ {...lineProps}
193
+ className={`velu-code-block__line ${
194
+ lineProps.className || ''
195
+ } ${isHi ? 'velu-code-block__line--highlight' : ''}`.trim()}
196
+ >
197
+ {lineNumbers && (
198
+ <span
199
+ className="velu-code-block__line-number"
200
+ aria-hidden="true"
201
+ >
202
+ {i + 1}
203
+ </span>
204
+ )}
205
+ <span className="velu-code-block__line-code">
206
+ {line.map((token, j) => (
207
+ <span key={j} {...getTokenProps({ token })} />
208
+ ))}
209
+ </span>
210
+ </div>
211
+ );
212
+ })}
213
+ </pre>
214
+ )}
215
+ </Highlight>
216
+ </div>
217
+ );
218
+ }
219
+
220
+ // Explicit `icon` always wins. `withIcon` defaults to true, so an icon
221
+ // is auto-rendered from `lib/lang-icons.jsx` based on the language;
222
+ // pass `withIcon={false}` to suppress.
223
+ function pickIcon({ icon, withIcon, language }) {
224
+ if (icon != null) return icon;
225
+ if (withIcon === false) return null;
226
+ return iconForLang(language);
227
+ }
228
+
229
+ export function CodeBlock({
230
+ filename,
231
+ title,
232
+ language = 'text',
233
+ icon,
234
+ withIcon = true,
235
+ lineNumbers = false,
236
+ highlight,
237
+ children,
238
+ className = '',
239
+ ...rest
240
+ }) {
241
+ const cls = `velu-code-block ${className}`.trim();
242
+ const label = filename || title;
243
+ const tabIcon = pickIcon({ icon, withIcon, language });
244
+ return (
245
+ <div className={cls} {...rest}>
246
+ {label && (
247
+ <div className="velu-code-block__header">
248
+ <Tab active icon={tabIcon} label={label} />
249
+ </div>
250
+ )}
251
+ <CodeBody
252
+ code={children}
253
+ language={language}
254
+ lineNumbers={lineNumbers}
255
+ highlight={highlight}
256
+ />
257
+ </div>
258
+ );
259
+ }
260
+ CodeBlock.displayName = 'CodeBlock';
261
+
262
+ // Hook that tracks whether a scrollable element can scroll further left
263
+ // or right. Resolves layout-driven overflow without media queries.
264
+ function useScrollEdges(ref) {
265
+ const [canLeft, setCanLeft] = useState(false);
266
+ const [canRight, setCanRight] = useState(false);
267
+
268
+ const update = useCallback(() => {
269
+ const el = ref.current;
270
+ if (!el) return;
271
+ setCanLeft(el.scrollLeft > 1);
272
+ setCanRight(el.scrollLeft + el.clientWidth < el.scrollWidth - 1);
273
+ }, [ref]);
274
+
275
+ useEffect(() => {
276
+ const el = ref.current;
277
+ if (!el) return;
278
+ update();
279
+ const ro = new ResizeObserver(update);
280
+ ro.observe(el);
281
+ // Also observe each child so adding/removing tabs retriggers update.
282
+ for (const c of el.children) ro.observe(c);
283
+ el.addEventListener('scroll', update, { passive: true });
284
+ return () => {
285
+ ro.disconnect();
286
+ el.removeEventListener('scroll', update);
287
+ };
288
+ }, [ref, update]);
289
+
290
+ return { canLeft, canRight, update };
291
+ }
292
+
293
+ export function CodeGroup({
294
+ children,
295
+ className = '',
296
+ defaultIndex = 0,
297
+ ...rest
298
+ }) {
299
+ const blocks = Children.toArray(children).filter(
300
+ (c) =>
301
+ isValidElement(c) &&
302
+ (c.type === CodeBlock || c.type?.displayName === 'CodeBlock'),
303
+ );
304
+ const [active, setActive] = useState(defaultIndex);
305
+ const tabsRef = useRef(null);
306
+ const { canLeft, canRight } = useScrollEdges(tabsRef);
307
+ const block = blocks[active] || blocks[0];
308
+ if (!block) return null;
309
+
310
+ const {
311
+ language = 'text',
312
+ lineNumbers = false,
313
+ highlight,
314
+ children: code,
315
+ } = block.props;
316
+ const cls = `velu-code-block velu-code-block--group ${className}`.trim();
317
+
318
+ function scrollDir(dir) {
319
+ tabsRef.current?.scrollBy({ left: dir * 160, behavior: 'smooth' });
320
+ }
321
+
322
+ return (
323
+ <div className={cls} {...rest}>
324
+ <div className="velu-code-block__header">
325
+ {canLeft && (
326
+ <button
327
+ type="button"
328
+ className="velu-code-block__nav"
329
+ onClick={() => scrollDir(-1)}
330
+ aria-label="Scroll tabs left"
331
+ >
332
+ {resolveIcon('chevron-left', { size: '1em' })}
333
+ </button>
334
+ )}
335
+ <div
336
+ ref={tabsRef}
337
+ className="velu-code-block__tabs"
338
+ role="tablist"
339
+ >
340
+ {blocks.map((b, i) => {
341
+ const { title, icon, withIcon, language } = b.props;
342
+ const tabIcon = pickIcon({ icon, withIcon, language });
343
+ return (
344
+ <Tab
345
+ key={i}
346
+ active={i === active}
347
+ onClick={() => setActive(i)}
348
+ icon={tabIcon}
349
+ label={title || `Tab ${i + 1}`}
350
+ />
351
+ );
352
+ })}
353
+ </div>
354
+ {canRight && (
355
+ <button
356
+ type="button"
357
+ className="velu-code-block__nav"
358
+ onClick={() => scrollDir(1)}
359
+ aria-label="Scroll tabs right"
360
+ >
361
+ {resolveIcon('chevron-right', { size: '1em' })}
362
+ </button>
363
+ )}
364
+ </div>
365
+ <CodeBody
366
+ code={code}
367
+ language={language}
368
+ lineNumbers={lineNumbers}
369
+ highlight={highlight}
370
+ />
371
+ </div>
372
+ );
373
+ }
374
+
375
+ export default CodeBlock;