@veluai/velu 0.2.24 → 0.2.27

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 (114) hide show
  1. package/README.md +80 -80
  2. package/dist/cli.js +44 -44
  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 +125 -125
  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/ChangelogFilters.jsx +75 -75
  16. package/runtime/velu-ui/components/Chatbot.jsx +972 -972
  17. package/runtime/velu-ui/components/CodeBlock.jsx +375 -375
  18. package/runtime/velu-ui/components/Columns.jsx +56 -56
  19. package/runtime/velu-ui/components/ContextMenu.jsx +327 -327
  20. package/runtime/velu-ui/components/ErrorCard.jsx +138 -138
  21. package/runtime/velu-ui/components/Field.jsx +81 -81
  22. package/runtime/velu-ui/components/Image.jsx +163 -163
  23. package/runtime/velu-ui/components/Logo.jsx +31 -31
  24. package/runtime/velu-ui/components/MethodBadge.jsx +31 -31
  25. package/runtime/velu-ui/components/NavSelect.jsx +108 -108
  26. package/runtime/velu-ui/components/NotFound.jsx +63 -63
  27. package/runtime/velu-ui/components/PageFeedback.jsx +219 -219
  28. package/runtime/velu-ui/components/PageFooter.jsx +145 -145
  29. package/runtime/velu-ui/components/PageHeader.jsx +422 -422
  30. package/runtime/velu-ui/components/PageNav.jsx +77 -77
  31. package/runtime/velu-ui/components/PoweredBy.jsx +51 -51
  32. package/runtime/velu-ui/components/Prompt.jsx +115 -115
  33. package/runtime/velu-ui/components/Search.jsx +460 -460
  34. package/runtime/velu-ui/components/Sidebar.jsx +246 -246
  35. package/runtime/velu-ui/components/SocialLinks.jsx +90 -90
  36. package/runtime/velu-ui/components/Steps.jsx +65 -65
  37. package/runtime/velu-ui/components/ThemeToggle.jsx +48 -48
  38. package/runtime/velu-ui/components/Toc.jsx +537 -537
  39. package/runtime/velu-ui/components/TocBar.jsx +195 -195
  40. package/runtime/velu-ui/components/Tree.jsx +87 -87
  41. package/runtime/velu-ui/components/TryItBar.jsx +102 -102
  42. package/runtime/velu-ui/components/Update.jsx +92 -92
  43. package/runtime/velu-ui/components/accordion.css +92 -92
  44. package/runtime/velu-ui/components/api-page.css +208 -208
  45. package/runtime/velu-ui/components/api.css +635 -635
  46. package/runtime/velu-ui/components/ask-bar.css +103 -94
  47. package/runtime/velu-ui/components/card.css +105 -105
  48. package/runtime/velu-ui/components/changelog-filters.css +43 -43
  49. package/runtime/velu-ui/components/chatbot.css +687 -687
  50. package/runtime/velu-ui/components/code-block.css +263 -263
  51. package/runtime/velu-ui/components/context-menu.css +199 -199
  52. package/runtime/velu-ui/components/docs-layout.css +822 -822
  53. package/runtime/velu-ui/components/field.css +82 -82
  54. package/runtime/velu-ui/components/image.css +237 -237
  55. package/runtime/velu-ui/components/nav-select.css +157 -157
  56. package/runtime/velu-ui/components/not-found.css +94 -94
  57. package/runtime/velu-ui/components/page-feedback.css +261 -261
  58. package/runtime/velu-ui/components/page-footer.css +130 -130
  59. package/runtime/velu-ui/components/page-header.css +558 -558
  60. package/runtime/velu-ui/components/page-nav.css +50 -50
  61. package/runtime/velu-ui/components/powered-by.css +92 -92
  62. package/runtime/velu-ui/components/prompt.css +99 -99
  63. package/runtime/velu-ui/components/search.css +307 -307
  64. package/runtime/velu-ui/components/sidebar.css +205 -205
  65. package/runtime/velu-ui/components/steps.css +77 -77
  66. package/runtime/velu-ui/components/theme-toggle.css +102 -102
  67. package/runtime/velu-ui/components/toc-bar.css +234 -234
  68. package/runtime/velu-ui/components/tree.css +49 -49
  69. package/runtime/velu-ui/components/update.css +111 -111
  70. package/runtime/velu-ui/index.js +58 -58
  71. package/runtime/velu-ui/lib/api-send.js +118 -118
  72. package/runtime/velu-ui/lib/brand-icons.jsx +103 -103
  73. package/runtime/velu-ui/lib/component-schemas.js +100 -100
  74. package/runtime/velu-ui/lib/copyText.js +64 -64
  75. package/runtime/velu-ui/lib/docs-assistant.js +273 -273
  76. package/runtime/velu-ui/lib/lang-icons.jsx +147 -147
  77. package/runtime/velu-ui/lib/page-feedback.js +72 -72
  78. package/runtime/velu-ui/lib/pagefind.js +116 -116
  79. package/runtime/velu-ui/lib/prism-langs.js +957 -957
  80. package/runtime/velu-ui/lib/prism-loader.js +74 -74
  81. package/runtime/velu-ui/lib/resolveIcon.jsx +29 -29
  82. package/runtime/velu-ui/lib/scrollIntoNearestView.js +66 -66
  83. package/runtime/velu-ui/mdx-components.jsx +107 -107
  84. package/runtime/velu-ui/primitives/Cluster.jsx +49 -49
  85. package/runtime/velu-ui/primitives/Stack.jsx +63 -63
  86. package/runtime/velu-ui/primitives/Switcher.jsx +57 -57
  87. package/runtime/velu-ui/primitives/stack.css +3 -3
  88. package/runtime/velu-ui/primitives/switcher.css +25 -25
  89. package/runtime/velu-ui/styles.css +48 -48
  90. package/runtime/velu-ui/tokens.css +4 -4
  91. package/schema/velu.schema.json +419 -419
  92. package/src/lib/extract-mdx-error.js +170 -170
  93. package/src/lib/issues.js +159 -159
  94. package/src/lib/known-components.js +35 -35
  95. package/src/navigation.js +459 -459
  96. package/src/runtime/App.jsx +1754 -1754
  97. package/src/runtime/ErrorBoundary.jsx +54 -54
  98. package/src/runtime/client-entry.jsx +27 -27
  99. package/src/runtime/server-entry.jsx +24 -24
  100. package/src/template.html +48 -48
  101. package/templates/starter/ai-tools/claude-code.mdx +26 -26
  102. package/templates/starter/ai-tools/cursor.mdx +17 -17
  103. package/templates/starter/api-reference/introduction.mdx +43 -43
  104. package/templates/starter/development.mdx +19 -19
  105. package/templates/starter/essentials/code.mdx +29 -29
  106. package/templates/starter/essentials/images.mdx +29 -29
  107. package/templates/starter/essentials/markdown.mdx +25 -25
  108. package/templates/starter/essentials/navigation.mdx +39 -39
  109. package/templates/starter/essentials/settings.mdx +30 -30
  110. package/templates/starter/favicon.svg +6 -6
  111. package/templates/starter/index.mdx +31 -31
  112. package/templates/starter/openapi.json +160 -160
  113. package/templates/starter/quickstart.mdx +31 -31
  114. package/templates/starter/velu.json +40 -40
@@ -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;