@veluai/velu 0.1.11 → 0.1.12

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.
@@ -1,70 +1,101 @@
1
- /* ThemeToggle — circular button with a sun/moon glyph; click flips
2
- the theme. Geometry is a 32 × 32 circle with an 18 × 18 glyph
3
- centred inside. The pill-with-slider design was replaced earlier;
4
- only the circle survives now.
5
-
6
- In light mode: --page-bg circle, sun glyph.
7
- In dark mode: --page-bg circle, moon glyph.
8
-
9
- Visibility model: BOTH icons are always rendered, stacked at the
10
- same `position: absolute` slot inside the button. Opacity controls
11
- which one is visible. We use opacity (not display: none) because:
12
-
13
- 1. Stacked icons render at the exact same point — no layout
14
- shift when the theme flips.
15
- 2. Opacity is not touched anywhere else for these elements, so
16
- the cascade is trivial — no risk of a stray rule (preflight,
17
- utility class, dev-time fallback) overriding `display: none`
18
- and revealing the wrong icon.
19
- 3. Identical markup on server + client → SSR-safe, no hydration
20
- mismatch and no icon flash.
21
-
22
- All values are tokens. */
23
-
24
- .velu-theme-toggle {
25
- /* `relative` so the absolutely-positioned icons inside are
26
- anchored to this button, not the page. */
27
- position: relative;
28
- display: inline-flex;
29
- align-items: center;
30
- justify-content: center;
31
- inline-size: 2rem;
32
- block-size: 2rem;
33
- border: var(--border-width) solid var(--border-color);
34
- border-radius: 999px;
35
- padding: 0;
36
- background: var(--page-bg);
37
- cursor: pointer;
38
- transition: border-color 0.12s ease, background 0.12s ease;
39
- }
40
- .velu-theme-toggle:hover {
41
- border-color: var(--accent-color);
42
- }
43
-
44
- /* Both icons stack here, centred. Default opacity 0 — the per-theme
45
- rules below switch one on. */
46
- .velu-theme-toggle__icon {
47
- position: absolute;
48
- inset-block-start: 50%;
49
- inset-inline-start: 50%;
50
- inline-size: 1.125rem;
51
- block-size: 1.125rem;
52
- margin-block-start: -0.5625rem; /* half of block-size */
53
- margin-inline-start: -0.5625rem; /* half of inline-size */
54
- color: var(--text-color);
55
- opacity: 0;
56
- transition: opacity 0.2s ease;
57
- pointer-events: none;
58
- }
59
-
60
- /* Default (light) sun on. */
61
- .velu-theme-toggle__icon--sun {
62
- opacity: 1;
63
- }
64
- /* Dark theme → moon on, sun off. */
65
- [data-theme='dark'] .velu-theme-toggle__icon--sun {
66
- opacity: 0;
67
- }
68
- [data-theme='dark'] .velu-theme-toggle__icon--moon {
69
- opacity: 1;
70
- }
1
+ /* ThemeToggle — circular button with a sun/moon glyph; click flips
2
+ the theme. Geometry is a 32 × 32 circle with an 18 × 18 glyph
3
+ centred inside. The pill-with-slider design was replaced earlier;
4
+ only the circle survives now.
5
+
6
+ In light mode: --page-bg circle, sun glyph.
7
+ In dark mode: --page-bg circle, moon glyph.
8
+
9
+ Visibility model: BOTH icons are always rendered, stacked at the
10
+ same `position: absolute` slot inside the button. Opacity controls
11
+ which one is visible. We use opacity (not display: none) because:
12
+
13
+ 1. Stacked icons render at the exact same point — no layout
14
+ shift when the theme flips.
15
+ 2. Opacity is not touched anywhere else for these elements, so
16
+ the cascade is trivial — no risk of a stray rule (preflight,
17
+ utility class, dev-time fallback) overriding `display: none`
18
+ and revealing the wrong icon.
19
+ 3. Identical markup on server + client → SSR-safe, no hydration
20
+ mismatch and no icon flash.
21
+
22
+ All values are tokens. */
23
+
24
+ .velu-theme-toggle {
25
+ /* `relative` so the absolutely-positioned icons inside are
26
+ anchored to this button, not the page. */
27
+ position: relative;
28
+ display: inline-flex;
29
+ align-items: center;
30
+ justify-content: center;
31
+ inline-size: 2rem;
32
+ block-size: 2rem;
33
+ border: var(--border-width) solid var(--border-color);
34
+ border-radius: 999px;
35
+ padding: 0;
36
+ background: var(--page-bg);
37
+ cursor: pointer;
38
+ transition: border-color 0.12s ease, background 0.12s ease,
39
+ transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1);
40
+ }
41
+ .velu-theme-toggle:hover {
42
+ border-color: var(--accent-color);
43
+ }
44
+ /* Tactile press feedback on click. */
45
+ .velu-theme-toggle:active {
46
+ transform: scale(0.9);
47
+ }
48
+
49
+ /* Both icons stack here, centred. Each animates in/out on a theme flip:
50
+ the hidden glyph is faded, shrunk, and rotated off its axis; the active
51
+ one springs to full size at 0deg. Centring is done with margins (above),
52
+ so `transform` is free to drive the rotate/scale animation. */
53
+ .velu-theme-toggle__icon {
54
+ position: absolute;
55
+ inset-block-start: 50%;
56
+ inset-inline-start: 50%;
57
+ inline-size: 1.125rem;
58
+ block-size: 1.125rem;
59
+ margin-block-start: -0.5625rem; /* half of block-size */
60
+ margin-inline-start: -0.5625rem; /* half of inline-size */
61
+ color: var(--text-color);
62
+ opacity: 0;
63
+ transition: opacity 0.28s ease,
64
+ transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
65
+ pointer-events: none;
66
+ }
67
+
68
+ /* Hidden state per glyph — sun spins out clockwise, moon anti-clockwise,
69
+ so the swap reads as one rotating through the other. */
70
+ .velu-theme-toggle__icon--sun {
71
+ transform: rotate(90deg) scale(0.4);
72
+ }
73
+ .velu-theme-toggle__icon--moon {
74
+ transform: rotate(-90deg) scale(0.4);
75
+ }
76
+
77
+ /* Exactly one glyph is shown — the sun in light, the moon in dark — at full
78
+ size, upright. Higher specificity than the hidden defaults above, so it
79
+ wins for whichever glyph the active theme selects. */
80
+ :root:not([data-theme='dark']) .velu-theme-toggle__icon--sun,
81
+ [data-theme='dark'] .velu-theme-toggle__icon--moon {
82
+ opacity: 1;
83
+ transform: rotate(0deg) scale(1);
84
+ }
85
+
86
+ /* Honour reduced-motion: keep a plain crossfade — no spin, scale, or press. */
87
+ @media (prefers-reduced-motion: reduce) {
88
+ .velu-theme-toggle {
89
+ transition: border-color 0.12s ease, background 0.12s ease;
90
+ }
91
+ .velu-theme-toggle:active {
92
+ transform: none;
93
+ }
94
+ .velu-theme-toggle__icon {
95
+ transition: opacity 0.2s ease;
96
+ }
97
+ .velu-theme-toggle__icon--sun,
98
+ .velu-theme-toggle__icon--moon {
99
+ transform: none;
100
+ }
101
+ }
@@ -1,46 +1,46 @@
1
- export { default as Stack } from './primitives/Stack.jsx';
2
- export { default as Switcher } from './primitives/Switcher.jsx';
3
- export { default as Cluster } from './primitives/Cluster.jsx';
4
- export { default as ThemeToggle } from './components/ThemeToggle.jsx';
5
- export { default as Sidebar } from './components/Sidebar.jsx';
6
- export { default as NavSelect } from './components/NavSelect.jsx';
7
- export { default as Toc } from './components/Toc.jsx';
8
- export { default as TocBar } from './components/TocBar.jsx';
9
- export { default as Callout } from './components/Callout.jsx';
10
- export { default as ErrorCard } from './components/ErrorCard.jsx';
11
- export {
12
- default as Accordion,
13
- AccordionGroup,
14
- } from './components/Accordion.jsx';
15
- export { default as Card, CardGroup } from './components/Card.jsx';
16
- export { default as Columns } from './components/Columns.jsx';
17
- export { default as Field } from './components/Field.jsx';
18
- export { default as Prompt } from './components/Prompt.jsx';
19
- export { default as Steps, Step } from './components/Steps.jsx';
20
- export { default as Tree, Folder, File } from './components/Tree.jsx';
21
- export { default as MethodBadge } from './components/MethodBadge.jsx';
22
- export { default as ApiPath } from './components/ApiPath.jsx';
23
- export { default as TryItBar } from './components/TryItBar.jsx';
24
- export { default as ApiField } from './components/ApiField.jsx';
25
- export { default as ApiClient } from './components/ApiClient.jsx';
26
- export { default as ApiSidebar } from './components/ApiSidebar.jsx';
27
- export { default as AskBar } from './components/AskBar.jsx';
28
- export { default as Chatbot } from './components/Chatbot.jsx';
29
- export { default as PageFeedback } from './components/PageFeedback.jsx';
30
- export { default as PageNav } from './components/PageNav.jsx';
31
- export { default as PageFooter } from './components/PageFooter.jsx';
32
- export { default as PoweredBy } from './components/PoweredBy.jsx';
33
- export { default as resolveIcon } from './lib/resolveIcon.jsx';
34
- export {
35
- default as PageHeader,
36
- VeluMark,
37
- } from './components/PageHeader.jsx';
38
- export { default as Search } from './components/Search.jsx';
39
- export { default as Image } from './components/Image.jsx';
40
- export {
41
- default as CodeBlock,
42
- CodeGroup,
43
- } from './components/CodeBlock.jsx';
44
- export {
45
- default as defaultMdxComponents,
46
- } from './mdx-components.jsx';
1
+ export { default as Stack } from './primitives/Stack.jsx';
2
+ export { default as Switcher } from './primitives/Switcher.jsx';
3
+ export { default as Cluster } from './primitives/Cluster.jsx';
4
+ export { default as ThemeToggle } from './components/ThemeToggle.jsx';
5
+ export { default as Sidebar } from './components/Sidebar.jsx';
6
+ export { default as NavSelect } from './components/NavSelect.jsx';
7
+ export { default as Toc } from './components/Toc.jsx';
8
+ export { default as TocBar } from './components/TocBar.jsx';
9
+ export { default as Callout } from './components/Callout.jsx';
10
+ export { default as ErrorCard } from './components/ErrorCard.jsx';
11
+ export {
12
+ default as Accordion,
13
+ AccordionGroup,
14
+ } from './components/Accordion.jsx';
15
+ export { default as Card, CardGroup } from './components/Card.jsx';
16
+ export { default as Columns } from './components/Columns.jsx';
17
+ export { default as Field } from './components/Field.jsx';
18
+ export { default as Prompt } from './components/Prompt.jsx';
19
+ export { default as Steps, Step } from './components/Steps.jsx';
20
+ export { default as Tree, Folder, File } from './components/Tree.jsx';
21
+ export { default as MethodBadge } from './components/MethodBadge.jsx';
22
+ export { default as ApiPath } from './components/ApiPath.jsx';
23
+ export { default as TryItBar } from './components/TryItBar.jsx';
24
+ export { default as ApiField } from './components/ApiField.jsx';
25
+ export { default as ApiClient } from './components/ApiClient.jsx';
26
+ export { default as ApiSidebar } from './components/ApiSidebar.jsx';
27
+ export { default as AskBar } from './components/AskBar.jsx';
28
+ export { default as Chatbot } from './components/Chatbot.jsx';
29
+ export { default as PageFeedback } from './components/PageFeedback.jsx';
30
+ export { default as PageNav } from './components/PageNav.jsx';
31
+ export { default as PageFooter } from './components/PageFooter.jsx';
32
+ export { default as PoweredBy } from './components/PoweredBy.jsx';
33
+ export { default as resolveIcon } from './lib/resolveIcon.jsx';
34
+ export {
35
+ default as PageHeader,
36
+ VeluMark,
37
+ } from './components/PageHeader.jsx';
38
+ export { default as Search } from './components/Search.jsx';
39
+ export { default as Image } from './components/Image.jsx';
40
+ export {
41
+ default as CodeBlock,
42
+ CodeGroup,
43
+ } from './components/CodeBlock.jsx';
44
+ export {
45
+ default as defaultMdxComponents,
46
+ } from './mdx-components.jsx';
@@ -1,100 +1,100 @@
1
- // Per-component option schemas — the props whose wrong value would otherwise
2
- // render incorrectly *silently*. Deliberately small and high-signal; extend a
3
- // component's entry here when it gains a new constrained option.
4
- //
5
- // Pure data + a pure validator (no React, no other imports) so BOTH sides can
6
- // use it: the live MDX registry (velu-ui/mdx-components.jsx) validates at
7
- // render time, and `velu validate` (velu-cli) imports this same module to
8
- // statically check literal attributes — one source of truth, no drift.
9
-
10
- // Canonical accepted values (must match the components):
11
- // Callout — VARIANTS keys; the component is CASE-SENSITIVE (VARIANTS[type]).
12
- // MethodBadge — METHODS; the component lower-cases the input first.
13
- export const CALLOUT_TYPES = ['note', 'warning', 'info', 'tip', 'check', 'danger', 'callout'];
14
- export const METHOD_NAMES = ['get', 'post', 'put', 'patch', 'delete'];
15
-
16
- // schema: { enums: { prop: { values, caseSensitive } }, required: [prop, …] }
17
- const SCHEMAS = {
18
- Callout: { enums: { type: { values: CALLOUT_TYPES, caseSensitive: true } } },
19
- MethodBadge: { enums: { method: { values: METHOD_NAMES, caseSensitive: false } } },
20
- ApiPath: { enums: { method: { values: METHOD_NAMES, caseSensitive: false } } },
21
- Image: { required: ['src'] },
22
- };
23
-
24
- // Smallest edit distance match within a set (for "did you mean"). Tiny inputs.
25
- function closest(value, values) {
26
- const v = String(value).toLowerCase();
27
- let best = null;
28
- let bestD = Infinity;
29
- for (const cand of values) {
30
- const a = v;
31
- const b = cand.toLowerCase();
32
- // inline Levenshtein
33
- const m = a.length;
34
- const n = b.length;
35
- const d = Array.from({ length: n + 1 }, (_, i) => i);
36
- for (let i = 1; i <= m; i++) {
37
- let prev = d[0];
38
- d[0] = i;
39
- for (let j = 1; j <= n; j++) {
40
- const tmp = d[j];
41
- d[j] = Math.min(d[j] + 1, d[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
42
- prev = tmp;
43
- }
44
- }
45
- if (d[n] < bestD) {
46
- bestD = d[n];
47
- best = cand;
48
- }
49
- }
50
- return bestD <= 3 ? best : null;
51
- }
52
-
53
- /**
54
- * Validate a component's props against its schema.
55
- * @param {string} name component tag name
56
- * @param {object} props props passed to it
57
- * @param {{ checkRequired?: boolean }} [opts] static callers pass
58
- * checkRequired:false because a prop supplied via a {expression}
59
- * isn't a literal they can see, so "missing required" would be a
60
- * false positive.
61
- * @returns an issue-shaped object (category 'invalid-props') or null if valid.
62
- * { category, title, detail, hint, suggestion }
63
- */
64
- export function validateProps(name, props = {}, opts = {}) {
65
- const { checkRequired = true } = opts;
66
- const schema = SCHEMAS[name];
67
- if (!schema) return null;
68
-
69
- for (const req of checkRequired ? schema.required || [] : []) {
70
- const v = props[req];
71
- if (v == null || v === '') {
72
- return {
73
- category: 'invalid-props',
74
- title: `<${name}> is missing the required "${req}" prop`,
75
- hint: `Add ${req}="…" to the component.`,
76
- };
77
- }
78
- }
79
-
80
- for (const [prop, rule] of Object.entries(schema.enums || {})) {
81
- if (!(prop in props) || props[prop] == null) continue;
82
- const raw = props[prop];
83
- const ok = rule.caseSensitive
84
- ? rule.values.includes(raw)
85
- : rule.values.map((s) => s.toLowerCase()).includes(String(raw).toLowerCase());
86
- if (!ok) {
87
- const guess = closest(raw, rule.values);
88
- return {
89
- category: 'invalid-props',
90
- title: `Invalid "${prop}" on <${name}>: "${raw}"`,
91
- detail: `Expected one of: ${rule.values.join(', ')}.`,
92
- hint: guess ? `Did you mean "${guess}"?` : '',
93
- };
94
- }
95
- }
96
-
97
- return null;
98
- }
99
-
100
- export default validateProps;
1
+ // Per-component option schemas — the props whose wrong value would otherwise
2
+ // render incorrectly *silently*. Deliberately small and high-signal; extend a
3
+ // component's entry here when it gains a new constrained option.
4
+ //
5
+ // Pure data + a pure validator (no React, no other imports) so BOTH sides can
6
+ // use it: the live MDX registry (velu-ui/mdx-components.jsx) validates at
7
+ // render time, and `velu validate` (velu-cli) imports this same module to
8
+ // statically check literal attributes — one source of truth, no drift.
9
+
10
+ // Canonical accepted values (must match the components):
11
+ // Callout — VARIANTS keys; the component is CASE-SENSITIVE (VARIANTS[type]).
12
+ // MethodBadge — METHODS; the component lower-cases the input first.
13
+ export const CALLOUT_TYPES = ['note', 'warning', 'info', 'tip', 'check', 'danger', 'callout'];
14
+ export const METHOD_NAMES = ['get', 'post', 'put', 'patch', 'delete'];
15
+
16
+ // schema: { enums: { prop: { values, caseSensitive } }, required: [prop, …] }
17
+ const SCHEMAS = {
18
+ Callout: { enums: { type: { values: CALLOUT_TYPES, caseSensitive: true } } },
19
+ MethodBadge: { enums: { method: { values: METHOD_NAMES, caseSensitive: false } } },
20
+ ApiPath: { enums: { method: { values: METHOD_NAMES, caseSensitive: false } } },
21
+ Image: { required: ['src'] },
22
+ };
23
+
24
+ // Smallest edit distance match within a set (for "did you mean"). Tiny inputs.
25
+ function closest(value, values) {
26
+ const v = String(value).toLowerCase();
27
+ let best = null;
28
+ let bestD = Infinity;
29
+ for (const cand of values) {
30
+ const a = v;
31
+ const b = cand.toLowerCase();
32
+ // inline Levenshtein
33
+ const m = a.length;
34
+ const n = b.length;
35
+ const d = Array.from({ length: n + 1 }, (_, i) => i);
36
+ for (let i = 1; i <= m; i++) {
37
+ let prev = d[0];
38
+ d[0] = i;
39
+ for (let j = 1; j <= n; j++) {
40
+ const tmp = d[j];
41
+ d[j] = Math.min(d[j] + 1, d[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
42
+ prev = tmp;
43
+ }
44
+ }
45
+ if (d[n] < bestD) {
46
+ bestD = d[n];
47
+ best = cand;
48
+ }
49
+ }
50
+ return bestD <= 3 ? best : null;
51
+ }
52
+
53
+ /**
54
+ * Validate a component's props against its schema.
55
+ * @param {string} name component tag name
56
+ * @param {object} props props passed to it
57
+ * @param {{ checkRequired?: boolean }} [opts] static callers pass
58
+ * checkRequired:false because a prop supplied via a {expression}
59
+ * isn't a literal they can see, so "missing required" would be a
60
+ * false positive.
61
+ * @returns an issue-shaped object (category 'invalid-props') or null if valid.
62
+ * { category, title, detail, hint, suggestion }
63
+ */
64
+ export function validateProps(name, props = {}, opts = {}) {
65
+ const { checkRequired = true } = opts;
66
+ const schema = SCHEMAS[name];
67
+ if (!schema) return null;
68
+
69
+ for (const req of checkRequired ? schema.required || [] : []) {
70
+ const v = props[req];
71
+ if (v == null || v === '') {
72
+ return {
73
+ category: 'invalid-props',
74
+ title: `<${name}> is missing the required "${req}" prop`,
75
+ hint: `Add ${req}="…" to the component.`,
76
+ };
77
+ }
78
+ }
79
+
80
+ for (const [prop, rule] of Object.entries(schema.enums || {})) {
81
+ if (!(prop in props) || props[prop] == null) continue;
82
+ const raw = props[prop];
83
+ const ok = rule.caseSensitive
84
+ ? rule.values.includes(raw)
85
+ : rule.values.map((s) => s.toLowerCase()).includes(String(raw).toLowerCase());
86
+ if (!ok) {
87
+ const guess = closest(raw, rule.values);
88
+ return {
89
+ category: 'invalid-props',
90
+ title: `Invalid "${prop}" on <${name}>: "${raw}"`,
91
+ detail: `Expected one of: ${rule.values.join(', ')}.`,
92
+ hint: guess ? `Did you mean "${guess}"?` : '',
93
+ };
94
+ }
95
+ }
96
+
97
+ return null;
98
+ }
99
+
100
+ export default validateProps;
@@ -1,64 +1,64 @@
1
- /**
2
- * copyText — copy a string to the clipboard, working in BOTH secure and
3
- * insecure contexts.
4
- *
5
- * `navigator.clipboard` only exists in a secure context (HTTPS or
6
- * localhost). On a plain-HTTP origin — e.g. previewing the dev server
7
- * over the LAN at http://<ip>:8358 on a phone — it's `undefined`, so a
8
- * `if (!navigator.clipboard) return` guard makes copy buttons silently
9
- * no-op. This util falls back to the legacy `execCommand('copy')` via a
10
- * hidden textarea so copy still works on those origins.
11
- *
12
- * Must be called from a user gesture (click) — both paths require it.
13
- * Returns a Promise: resolves on success, rejects on failure.
14
- */
15
- export default function copyText(text) {
16
- const str = String(text ?? '');
17
-
18
- // Preferred path — async Clipboard API (secure contexts only).
19
- if (
20
- typeof navigator !== 'undefined' &&
21
- navigator.clipboard &&
22
- typeof window !== 'undefined' &&
23
- window.isSecureContext
24
- ) {
25
- return navigator.clipboard.writeText(str);
26
- }
27
-
28
- // Fallback — hidden <textarea> + execCommand, for insecure origins.
29
- return new Promise((resolve, reject) => {
30
- if (typeof document === 'undefined') {
31
- reject(new Error('copyText: no document'));
32
- return;
33
- }
34
- const ta = document.createElement('textarea');
35
- ta.value = str;
36
- ta.setAttribute('readonly', '');
37
- // Keep it out of view and inert, but still selectable.
38
- ta.style.position = 'fixed';
39
- ta.style.top = '0';
40
- ta.style.left = '0';
41
- ta.style.inlineSize = '1px';
42
- ta.style.blockSize = '1px';
43
- ta.style.padding = '0';
44
- ta.style.border = 'none';
45
- ta.style.opacity = '0';
46
- document.body.appendChild(ta);
47
- ta.focus();
48
- ta.select();
49
- try {
50
- ta.setSelectionRange(0, str.length);
51
- } catch {
52
- /* some browsers reject setSelectionRange on a readonly field */
53
- }
54
- let ok = false;
55
- try {
56
- ok = document.execCommand('copy');
57
- } catch {
58
- ok = false;
59
- }
60
- document.body.removeChild(ta);
61
- if (ok) resolve();
62
- else reject(new Error('copyText: execCommand copy failed'));
63
- });
64
- }
1
+ /**
2
+ * copyText — copy a string to the clipboard, working in BOTH secure and
3
+ * insecure contexts.
4
+ *
5
+ * `navigator.clipboard` only exists in a secure context (HTTPS or
6
+ * localhost). On a plain-HTTP origin — e.g. previewing the dev server
7
+ * over the LAN at http://<ip>:8358 on a phone — it's `undefined`, so a
8
+ * `if (!navigator.clipboard) return` guard makes copy buttons silently
9
+ * no-op. This util falls back to the legacy `execCommand('copy')` via a
10
+ * hidden textarea so copy still works on those origins.
11
+ *
12
+ * Must be called from a user gesture (click) — both paths require it.
13
+ * Returns a Promise: resolves on success, rejects on failure.
14
+ */
15
+ export default function copyText(text) {
16
+ const str = String(text ?? '');
17
+
18
+ // Preferred path — async Clipboard API (secure contexts only).
19
+ if (
20
+ typeof navigator !== 'undefined' &&
21
+ navigator.clipboard &&
22
+ typeof window !== 'undefined' &&
23
+ window.isSecureContext
24
+ ) {
25
+ return navigator.clipboard.writeText(str);
26
+ }
27
+
28
+ // Fallback — hidden <textarea> + execCommand, for insecure origins.
29
+ return new Promise((resolve, reject) => {
30
+ if (typeof document === 'undefined') {
31
+ reject(new Error('copyText: no document'));
32
+ return;
33
+ }
34
+ const ta = document.createElement('textarea');
35
+ ta.value = str;
36
+ ta.setAttribute('readonly', '');
37
+ // Keep it out of view and inert, but still selectable.
38
+ ta.style.position = 'fixed';
39
+ ta.style.top = '0';
40
+ ta.style.left = '0';
41
+ ta.style.inlineSize = '1px';
42
+ ta.style.blockSize = '1px';
43
+ ta.style.padding = '0';
44
+ ta.style.border = 'none';
45
+ ta.style.opacity = '0';
46
+ document.body.appendChild(ta);
47
+ ta.focus();
48
+ ta.select();
49
+ try {
50
+ ta.setSelectionRange(0, str.length);
51
+ } catch {
52
+ /* some browsers reject setSelectionRange on a readonly field */
53
+ }
54
+ let ok = false;
55
+ try {
56
+ ok = document.execCommand('copy');
57
+ } catch {
58
+ ok = false;
59
+ }
60
+ document.body.removeChild(ta);
61
+ if (ok) resolve();
62
+ else reject(new Error('copyText: execCommand copy failed'));
63
+ });
64
+ }