@liberation-data/desk 0.36.0 → 0.37.0

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/README.md CHANGED
@@ -333,6 +333,34 @@ are set to: a chip for each one off its default, so the folded line is never a m
333
333
  mounted while folded, so a field keeps its value. A chip is one thing that is set or chosen; with `onRemove`
334
334
  it ends in a × that takes it away, named by `removeLabel`.
335
335
 
336
+ ```tsx
337
+ <CodeBlock language="shell" label="Install" code="npm install @liberation-data/desk" />
338
+ <CodeBlock language="python" code={snippet} highlight={(code, language) => <Highlighted code={code} language={language} />} />
339
+ ```
340
+
341
+ A code block is code people copy: a snippet to paste into a program, a command to run. It keeps its lines
342
+ and scrolls sideways, since a wrapped command reads as two, and it is a named region that takes focus, so it
343
+ scrolls from the keyboard. Copy sits in its corner and says Copied for a moment, politely announced; where
344
+ the page has no clipboard it selects the code and says which keys copy it. `language` is `python`,
345
+ `typescript`, `javascript`, `kotlin`, `yaml`, `shell`, `json` or `text` (the default), and the built-in
346
+ highlighting marks comments, strings, keywords, numbers and the keys of YAML and JSON, in
347
+ `--desk-code-comment`, `-string`, `-keyword`, `-number` and `-key`, each 4.5:1 on the block's ground in
348
+ light and dark. It is a reading aid, not a parser, and it builds React nodes, so code is always text, never
349
+ HTML. A host with a highlighter of its own passes `highlight`, which is used instead.
350
+
351
+ ```tsx
352
+ <CodeBlock label="Install" variants={[
353
+ { value: 'pip', label: 'pip', language: 'shell', code: 'pip install desk' },
354
+ { value: 'uv', label: 'uv', language: 'shell', code: 'uv add desk' },
355
+ ]} />
356
+ ```
357
+
358
+ The same code in several forms (an install with pip or uv, a client in two languages) is one block with
359
+ `variants`. Their tabs, a small segmented control, sit in a bar across the top of the frame with Copy at its
360
+ other end, so the choice and the code read as one card rather than a control above a box. The tabs are one
361
+ tab stop and the arrow keys move between them; Copy copies the variant shown. The block keeps its own choice
362
+ unless `value` and `onValueChange` hold it, and `label` then names the tabs instead of being drawn.
363
+
336
364
  ## Waiting
337
365
 
338
366
  ```tsx
@@ -829,6 +857,8 @@ A route is where a window is inside itself: Settings at a section, a list at one
829
857
  and the window says what it means. Opening a window that is already open with a route moves it there as it
830
858
  comes forward, so one rule serves a link, a command and another window, and the window reading
831
859
  `useWindowRoute` follows all of them. Opening without a route leaves it where it was; closing forgets it.
860
+ Outside a window — the same view on a page of its own, or in a test — the route is the component's own
861
+ state, starting at `''`.
832
862
 
833
863
  The hash carries it after the window: `#w=notes,settings~/agents&f=settings`. A route is what someone meant
834
864
  to point at, not how their screen was arranged, so like minimizing it travels. Slashes stay readable —
@@ -888,7 +918,7 @@ import '@liberation-data/desk/css/tokens.css' // always: every other part rea
888
918
  import '@liberation-data/desk/css/windows.css' // the stage, windows, panes and toolbars
889
919
  import '@liberation-data/desk/css/dock.css'
890
920
  import '@liberation-data/desk/css/menus.css' // menu bar and context menus
891
- import '@liberation-data/desk/css/controls.css' // buttons, fields, table, sidebar, icon view
921
+ import '@liberation-data/desk/css/controls.css' // buttons, fields, table, sidebar, icon view, code
892
922
  import '@liberation-data/desk/css/overlays.css' // popovers, sheets, alerts, toasts, the (i)
893
923
  import '@liberation-data/desk/css/conversation.css'
894
924
  import '@liberation-data/desk/css/search.css'
@@ -706,7 +706,11 @@
706
706
  z-index: 1200;
707
707
  top: calc(100% + 4px);
708
708
  left: 0;
709
+ /* As wide as the widest option, as a menu is; never narrower than the button, nor past the screen. */
710
+ width: max-content;
709
711
  min-width: 100%;
712
+ max-width: min(360px, calc(100vw - 32px));
713
+ box-sizing: border-box;
710
714
  max-height: 40vh;
711
715
  margin: 0;
712
716
  padding: 4px;
@@ -755,6 +759,12 @@
755
759
  .desk-popup-text {
756
760
  display: flex;
757
761
  flex-direction: column;
762
+ min-width: 0;
763
+ }
764
+
765
+ /* An option is one line; its description may wrap inside the list's widest. */
766
+ .desk-popup-label {
767
+ white-space: nowrap;
758
768
  }
759
769
 
760
770
  /* ── Tabs ── */
@@ -1485,3 +1495,91 @@
1485
1495
  transition: none;
1486
1496
  }
1487
1497
  }
1498
+
1499
+ /*
1500
+ * CODE TO COPY (`CodeBlock`). On the page's ground, which is what the code tokens are measured
1501
+ * against, with Copy in a column of its own so it never sits over a line. Lines keep their length
1502
+ * and the block scrolls sideways: a wrapped command reads as two.
1503
+ */
1504
+ .desk-code {
1505
+ display: flex;
1506
+ align-items: flex-start;
1507
+ min-width: 0;
1508
+ border: 1px solid var(--desk-rule);
1509
+ border-radius: var(--desk-control-radius);
1510
+ background: color-mix(in srgb, var(--desk-accent) var(--desk-page-tint), var(--desk-page));
1511
+ color: var(--desk-ink);
1512
+ }
1513
+
1514
+ .desk-code-main {
1515
+ flex: 1;
1516
+ min-width: 0;
1517
+ }
1518
+
1519
+ .desk-code-label {
1520
+ padding: var(--desk-space-2) var(--desk-space-3) 0;
1521
+ color: var(--desk-muted);
1522
+ font-family: var(--desk-font);
1523
+ font-size: var(--desk-text-caption);
1524
+ font-weight: 500;
1525
+ }
1526
+
1527
+ .desk-code-body {
1528
+ margin: 0;
1529
+ padding: var(--desk-space-2) var(--desk-space-3);
1530
+ overflow-x: auto;
1531
+ border-radius: var(--desk-control-radius);
1532
+ font-family: var(--desk-font-mono);
1533
+ font-size: var(--desk-text-small);
1534
+ line-height: 1.6;
1535
+ white-space: pre;
1536
+ tab-size: 4;
1537
+ }
1538
+
1539
+ .desk-code-body:focus-visible {
1540
+ outline: 2px solid var(--desk-accent);
1541
+ outline-offset: -2px;
1542
+ }
1543
+
1544
+ .desk-code-actions {
1545
+ flex: none;
1546
+ padding: var(--desk-space-1);
1547
+ }
1548
+
1549
+ /* With variants the block is a column: a bar of tabs and Copy across its top, the code under it. */
1550
+ .desk-code[data-variants] {
1551
+ flex-direction: column;
1552
+ align-items: stretch;
1553
+ }
1554
+
1555
+ .desk-code-head {
1556
+ display: flex;
1557
+ align-items: center;
1558
+ flex-wrap: wrap;
1559
+ justify-content: space-between;
1560
+ gap: var(--desk-space-2);
1561
+ padding: var(--desk-space-1);
1562
+ border-bottom: 1px solid var(--desk-rule);
1563
+ border-radius: var(--desk-control-radius) var(--desk-control-radius) 0 0;
1564
+ background: color-mix(in srgb, var(--desk-ink) 4%, transparent);
1565
+ }
1566
+
1567
+ .desk-code-head .desk-code-actions {
1568
+ padding: 0;
1569
+ }
1570
+
1571
+ .desk-code-status {
1572
+ position: absolute;
1573
+ width: 1px;
1574
+ height: 1px;
1575
+ margin: -1px;
1576
+ overflow: hidden;
1577
+ clip-path: inset(50%);
1578
+ white-space: nowrap;
1579
+ }
1580
+
1581
+ .desk-code-comment { color: var(--desk-code-comment); font-style: italic; }
1582
+ .desk-code-string { color: var(--desk-code-string); }
1583
+ .desk-code-keyword { color: var(--desk-code-keyword); }
1584
+ .desk-code-number { color: var(--desk-code-number); }
1585
+ .desk-code-key { color: var(--desk-code-key); }
@@ -68,6 +68,12 @@
68
68
  /* How much of the accent is mixed into it, so the sheet belongs to the theme it is read in.
69
69
  0% is the neutral alone. */
70
70
  --desk-page-tint: 4%;
71
+ /* Code (`CodeBlock`), on the page's ground: each at least 4.5:1 against it, tinted or not. */
72
+ --desk-code-comment: #5d6676;
73
+ --desk-code-string: #136c2e;
74
+ --desk-code-keyword: #8034c8;
75
+ --desk-code-number: #a34400;
76
+ --desk-code-key: #0b5cad;
71
77
  --desk-on-accent: #ffffff;
72
78
  --desk-ease: cubic-bezier(.32, .72, 0, 1);
73
79
  --desk-dock-size: 48px;
@@ -107,6 +113,11 @@
107
113
  --desk-field-bg: rgb(0 0 0 / 35%);
108
114
  --desk-page: #26282c;
109
115
  --desk-page-tint: 5%;
116
+ --desk-code-comment: #9aa3b2;
117
+ --desk-code-string: #7ee2a0;
118
+ --desk-code-keyword: #d2a8ff;
119
+ --desk-code-number: #ffab70;
120
+ --desk-code-key: #7cc4ff;
110
121
  }
111
122
  }
112
123
 
@@ -133,4 +144,9 @@
133
144
  --desk-field-bg: rgb(0 0 0 / 35%);
134
145
  --desk-page: #26282c;
135
146
  --desk-page-tint: 5%;
147
+ --desk-code-comment: #9aa3b2;
148
+ --desk-code-string: #7ee2a0;
149
+ --desk-code-keyword: #d2a8ff;
150
+ --desk-code-number: #ffab70;
151
+ --desk-code-key: #7cc4ff;
136
152
  }
package/dist/desk.css CHANGED
@@ -68,6 +68,12 @@
68
68
  /* How much of the accent is mixed into it, so the sheet belongs to the theme it is read in.
69
69
  0% is the neutral alone. */
70
70
  --desk-page-tint: 4%;
71
+ /* Code (`CodeBlock`), on the page's ground: each at least 4.5:1 against it, tinted or not. */
72
+ --desk-code-comment: #5d6676;
73
+ --desk-code-string: #136c2e;
74
+ --desk-code-keyword: #8034c8;
75
+ --desk-code-number: #a34400;
76
+ --desk-code-key: #0b5cad;
71
77
  --desk-on-accent: #ffffff;
72
78
  --desk-ease: cubic-bezier(.32, .72, 0, 1);
73
79
  --desk-dock-size: 48px;
@@ -107,6 +113,11 @@
107
113
  --desk-field-bg: rgb(0 0 0 / 35%);
108
114
  --desk-page: #26282c;
109
115
  --desk-page-tint: 5%;
116
+ --desk-code-comment: #9aa3b2;
117
+ --desk-code-string: #7ee2a0;
118
+ --desk-code-keyword: #d2a8ff;
119
+ --desk-code-number: #ffab70;
120
+ --desk-code-key: #7cc4ff;
110
121
  }
111
122
  }
112
123
 
@@ -133,6 +144,11 @@
133
144
  --desk-field-bg: rgb(0 0 0 / 35%);
134
145
  --desk-page: #26282c;
135
146
  --desk-page-tint: 5%;
147
+ --desk-code-comment: #9aa3b2;
148
+ --desk-code-string: #7ee2a0;
149
+ --desk-code-keyword: #d2a8ff;
150
+ --desk-code-number: #ffab70;
151
+ --desk-code-key: #7cc4ff;
136
152
  }
137
153
 
138
154
  /* @liberation-data/desk — windows.css */
@@ -2027,7 +2043,11 @@
2027
2043
  z-index: 1200;
2028
2044
  top: calc(100% + 4px);
2029
2045
  left: 0;
2046
+ /* As wide as the widest option, as a menu is; never narrower than the button, nor past the screen. */
2047
+ width: max-content;
2030
2048
  min-width: 100%;
2049
+ max-width: min(360px, calc(100vw - 32px));
2050
+ box-sizing: border-box;
2031
2051
  max-height: 40vh;
2032
2052
  margin: 0;
2033
2053
  padding: 4px;
@@ -2076,6 +2096,12 @@
2076
2096
  .desk-popup-text {
2077
2097
  display: flex;
2078
2098
  flex-direction: column;
2099
+ min-width: 0;
2100
+ }
2101
+
2102
+ /* An option is one line; its description may wrap inside the list's widest. */
2103
+ .desk-popup-label {
2104
+ white-space: nowrap;
2079
2105
  }
2080
2106
 
2081
2107
  /* ── Tabs ── */
@@ -2807,6 +2833,94 @@
2807
2833
  }
2808
2834
  }
2809
2835
 
2836
+ /*
2837
+ * CODE TO COPY (`CodeBlock`). On the page's ground, which is what the code tokens are measured
2838
+ * against, with Copy in a column of its own so it never sits over a line. Lines keep their length
2839
+ * and the block scrolls sideways: a wrapped command reads as two.
2840
+ */
2841
+ .desk-code {
2842
+ display: flex;
2843
+ align-items: flex-start;
2844
+ min-width: 0;
2845
+ border: 1px solid var(--desk-rule);
2846
+ border-radius: var(--desk-control-radius);
2847
+ background: color-mix(in srgb, var(--desk-accent) var(--desk-page-tint), var(--desk-page));
2848
+ color: var(--desk-ink);
2849
+ }
2850
+
2851
+ .desk-code-main {
2852
+ flex: 1;
2853
+ min-width: 0;
2854
+ }
2855
+
2856
+ .desk-code-label {
2857
+ padding: var(--desk-space-2) var(--desk-space-3) 0;
2858
+ color: var(--desk-muted);
2859
+ font-family: var(--desk-font);
2860
+ font-size: var(--desk-text-caption);
2861
+ font-weight: 500;
2862
+ }
2863
+
2864
+ .desk-code-body {
2865
+ margin: 0;
2866
+ padding: var(--desk-space-2) var(--desk-space-3);
2867
+ overflow-x: auto;
2868
+ border-radius: var(--desk-control-radius);
2869
+ font-family: var(--desk-font-mono);
2870
+ font-size: var(--desk-text-small);
2871
+ line-height: 1.6;
2872
+ white-space: pre;
2873
+ tab-size: 4;
2874
+ }
2875
+
2876
+ .desk-code-body:focus-visible {
2877
+ outline: 2px solid var(--desk-accent);
2878
+ outline-offset: -2px;
2879
+ }
2880
+
2881
+ .desk-code-actions {
2882
+ flex: none;
2883
+ padding: var(--desk-space-1);
2884
+ }
2885
+
2886
+ /* With variants the block is a column: a bar of tabs and Copy across its top, the code under it. */
2887
+ .desk-code[data-variants] {
2888
+ flex-direction: column;
2889
+ align-items: stretch;
2890
+ }
2891
+
2892
+ .desk-code-head {
2893
+ display: flex;
2894
+ align-items: center;
2895
+ flex-wrap: wrap;
2896
+ justify-content: space-between;
2897
+ gap: var(--desk-space-2);
2898
+ padding: var(--desk-space-1);
2899
+ border-bottom: 1px solid var(--desk-rule);
2900
+ border-radius: var(--desk-control-radius) var(--desk-control-radius) 0 0;
2901
+ background: color-mix(in srgb, var(--desk-ink) 4%, transparent);
2902
+ }
2903
+
2904
+ .desk-code-head .desk-code-actions {
2905
+ padding: 0;
2906
+ }
2907
+
2908
+ .desk-code-status {
2909
+ position: absolute;
2910
+ width: 1px;
2911
+ height: 1px;
2912
+ margin: -1px;
2913
+ overflow: hidden;
2914
+ clip-path: inset(50%);
2915
+ white-space: nowrap;
2916
+ }
2917
+
2918
+ .desk-code-comment { color: var(--desk-code-comment); font-style: italic; }
2919
+ .desk-code-string { color: var(--desk-code-string); }
2920
+ .desk-code-keyword { color: var(--desk-code-keyword); }
2921
+ .desk-code-number { color: var(--desk-code-number); }
2922
+ .desk-code-key { color: var(--desk-code-key); }
2923
+
2810
2924
  /* @liberation-data/desk — overlays.css */
2811
2925
  /* ── Overlays ── */
2812
2926
 
@@ -0,0 +1,48 @@
1
+ import type { ReactNode } from 'react';
2
+ export type CodeLanguage = 'python' | 'typescript' | 'javascript' | 'kotlin' | 'yaml' | 'shell' | 'json' | 'text';
3
+ /** One of the forms the same code comes in: the same install with pip or with uv, a client in two languages. */
4
+ export interface CodeVariant {
5
+ readonly value: string;
6
+ /** Its tab: "Python", "pip". */
7
+ readonly label: string;
8
+ readonly language: CodeLanguage;
9
+ readonly code: string;
10
+ }
11
+ interface CodeBlockCommon {
12
+ /**
13
+ * What it is: "Install", "config.yaml". It names the region and, without variants, is shown above
14
+ * the code; with them the tabs take that place, and it names them.
15
+ */
16
+ readonly label?: string;
17
+ /** A Copy button in the corner. Default true. */
18
+ readonly copy?: boolean;
19
+ /** A highlighter of the host's own, e.g. CodeMirror's `runMode`, used instead of the built-in one. */
20
+ readonly highlight?: (code: string, language: CodeLanguage) => ReactNode;
21
+ readonly className?: string;
22
+ }
23
+ interface OneCode extends CodeBlockCommon {
24
+ /** The code, exactly as it is copied. One trailing newline is not drawn as an empty line. */
25
+ readonly code: string;
26
+ /** How it is highlighted, and the region's name when there is no `label`. Default `text`: none. */
27
+ readonly language?: CodeLanguage;
28
+ readonly variants?: never;
29
+ readonly value?: never;
30
+ readonly onValueChange?: never;
31
+ }
32
+ interface VariantCode extends CodeBlockCommon {
33
+ /**
34
+ * The same code in several forms, chosen by tabs in a bar across the top of the block: one card,
35
+ * not a control above it. Copy copies the one shown.
36
+ */
37
+ readonly variants: readonly CodeVariant[];
38
+ /** The variant shown. Leave it out and the block keeps its own choice, starting at the first. */
39
+ readonly value?: string;
40
+ readonly onValueChange?: (value: string) => void;
41
+ readonly code?: never;
42
+ readonly language?: never;
43
+ }
44
+ export type CodeBlockProps = OneCode | VariantCode;
45
+ /** A framed block of code to copy, highlighted for its language, with a Copy button in its corner. */
46
+ export declare function CodeBlock(props: CodeBlockProps): import("react").JSX.Element;
47
+ export {};
48
+ //# sourceMappingURL=codeBlock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codeBlock.d.ts","sourceRoot":"","sources":["../../src/react/codeBlock.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAsBtC,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,YAAY,GAAG,YAAY,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,CAAA;AAEjH,gHAAgH;AAChH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,gCAAgC;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAA;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CACtB;AAED,UAAU,eAAe;IACvB;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB,iDAAiD;IACjD,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAA;IACvB,sGAAsG;IACtG,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,KAAK,SAAS,CAAA;IACxE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAC5B;AAED,UAAU,OAAQ,SAAQ,eAAe;IACvC,6FAA6F;IAC7F,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,mGAAmG;IACnG,QAAQ,CAAC,QAAQ,CAAC,EAAE,YAAY,CAAA;IAChC,QAAQ,CAAC,QAAQ,CAAC,EAAE,KAAK,CAAA;IACzB,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAA;IACtB,QAAQ,CAAC,aAAa,CAAC,EAAE,KAAK,CAAA;CAC/B;AAED,UAAU,WAAY,SAAQ,eAAe;IAC3C;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,SAAS,WAAW,EAAE,CAAA;IACzC,iGAAiG;IACjG,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IAChD,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,CAAA;IACrB,QAAQ,CAAC,QAAQ,CAAC,EAAE,KAAK,CAAA;CAC1B;AAED,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,WAAW,CAAA;AAuMlD,sGAAsG;AACtG,wBAAgB,SAAS,CAAC,KAAK,EAAE,cAAc,+BA2G9C"}
@@ -0,0 +1,257 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useMemo, useRef, useState } from 'react';
3
+ import { formatShortcut } from '../core/shortcuts.js';
4
+ import { Button, SegmentedControl } from './controls.js';
5
+ const words = (list) => new Set(list.split(' '));
6
+ const C_COMMENTS = { line: '//', block: ['/*', '*/'] };
7
+ const B = 'backslash';
8
+ const JS = 'async await break case catch class const continue debugger default delete do else export extends false ' +
9
+ 'finally for function if import in instanceof let new null of return static super switch this throw true try ' +
10
+ 'typeof undefined var void while with yield';
11
+ const GRAMMARS = {
12
+ python: {
13
+ line: '#',
14
+ quotes: [{ open: '"""', lines: true, escape: B }, { open: "'''", lines: true, escape: B }, { open: '"', escape: B }, { open: "'", escape: B }],
15
+ keywords: words('False None True and as assert async await break class continue def del elif else except ' +
16
+ 'finally for from global if import in is lambda nonlocal not or pass raise return try while with yield'),
17
+ },
18
+ javascript: {
19
+ ...C_COMMENTS,
20
+ quotes: [{ open: '`', lines: true, escape: B }, { open: '"', escape: B }, { open: "'", escape: B }],
21
+ keywords: words(JS),
22
+ },
23
+ typescript: {
24
+ ...C_COMMENTS,
25
+ quotes: [{ open: '`', lines: true, escape: B }, { open: '"', escape: B }, { open: "'", escape: B }],
26
+ keywords: words(`${JS} abstract as declare enum implements interface keyof namespace private protected public readonly satisfies`),
27
+ },
28
+ kotlin: {
29
+ ...C_COMMENTS,
30
+ quotes: [{ open: '"""', lines: true }, { open: '"', escape: B }, { open: "'", escape: B }],
31
+ keywords: words('abstract as break by class companion const constructor continue data do else enum false for fun ' +
32
+ 'if import in init interface internal is lateinit null object open override package private protected public ' +
33
+ 'return sealed super suspend this throw true try typealias val var when while'),
34
+ },
35
+ shell: {
36
+ line: '#',
37
+ wordComment: true,
38
+ quotes: [{ open: '"', lines: true, escape: B }, { open: "'", lines: true }],
39
+ // `Don't panic` in a heredoc or an unquoted echo is not the start of a string.
40
+ quoteAt: (code, at) => !/[\w]/.test(code[at - 1] ?? ''),
41
+ keywords: words('case do done elif else esac export fi for function if in local readonly return select then until while'),
42
+ },
43
+ yaml: {
44
+ line: '#',
45
+ wordComment: true,
46
+ quotes: [{ open: '"', lines: true, escape: B }, { open: "'", lines: true, escape: 'double' }],
47
+ keywords: words('true false null'),
48
+ quoteAt: (code, at) => yamlValueStarts(code, at),
49
+ keys: 'yaml',
50
+ },
51
+ json: {
52
+ quotes: [{ open: '"', escape: B }],
53
+ keywords: words('true false null'),
54
+ keys: 'json',
55
+ },
56
+ };
57
+ const NUMBER = /0[xX][\da-fA-F_]+|\d[\d_]*(?:\.\d[\d_]*)?(?:[eE][+-]?\d+)?[fFLn]?/y;
58
+ const WORD = /[A-Za-z_$][\w$]*/y;
59
+ const JSON_COLON = /[ \t]*:/y;
60
+ /* Indentation and list dashes, a key — plain or quoted — and its colon, which ends at a space or the line. */
61
+ const YAML_KEY = /([ \t]*(?:-[ \t]+)*)([^\s#'"{}[\],:-](?:[^:\n#]*[^\s:#])?|"(?:[^"\\\n]|\\.)*"|'(?:[^'\n]|'')*')([ \t]*:)(?=[ \t]|$)/my;
62
+ /* What a number or keyword cannot touch, so `v1`, `1.2.3`, `obj.if` and `--if-exists` stay text. */
63
+ const JOINED = /[\w$.-]/;
64
+ function sticky(pattern, code, at) {
65
+ pattern.lastIndex = at;
66
+ return pattern.exec(code);
67
+ }
68
+ /** Where a quote that opens at `at` closes: past its closing quote, or at the line or text's end. */
69
+ function closeOf(code, at, quote) {
70
+ let i = at + quote.open.length;
71
+ while (i < code.length) {
72
+ if (quote.escape === 'backslash' && code[i] === '\\')
73
+ i += 2;
74
+ else if (code.startsWith(quote.open, i)) {
75
+ if (quote.escape === 'double' && code[i + 1] === quote.open)
76
+ i += 2;
77
+ else {
78
+ // A raw string may end in its own quote: `"""a""""` closes at the last of the run.
79
+ let end = i + quote.open.length;
80
+ while (quote.open.length === 3 && code[end] === quote.open[0])
81
+ end++;
82
+ return end;
83
+ }
84
+ }
85
+ else if (code[i] === '\n' && !quote.lines)
86
+ return i;
87
+ else
88
+ i++;
89
+ }
90
+ return code.length;
91
+ }
92
+ /* YAML opens a quote only where a value starts: `title: Don't stop` is a plain sentence. */
93
+ function yamlValueStarts(code, at) {
94
+ let i = at - 1;
95
+ while (code[i] === ' ' || code[i] === '\t')
96
+ i--;
97
+ return i < 0 || ':-[{,\n'.includes(code[i] ?? '');
98
+ }
99
+ function tokenize(code, grammar) {
100
+ const out = [];
101
+ const push = (text = '', kind) => {
102
+ if (!text)
103
+ return;
104
+ const last = out[out.length - 1];
105
+ if (!kind && last && !last.kind)
106
+ out[out.length - 1] = { text: last.text + text };
107
+ else
108
+ out.push({ kind, text });
109
+ };
110
+ let i = 0;
111
+ while (i < code.length) {
112
+ const before = code[i - 1];
113
+ const lineStart = i === 0 || before === '\n';
114
+ if (lineStart && grammar.keys === 'yaml') {
115
+ const key = sticky(YAML_KEY, code, i);
116
+ if (key) {
117
+ push(key[1]);
118
+ push(key[2], 'key');
119
+ push(key[3]);
120
+ i += key[0].length;
121
+ continue;
122
+ }
123
+ }
124
+ if (grammar.block && code.startsWith(grammar.block[0], i)) {
125
+ const end = code.indexOf(grammar.block[1], i + 2);
126
+ // Never closed: most likely a `/*` inside a regular expression, so it colours one line, not the file.
127
+ const open = code.indexOf('\n', i);
128
+ const stop = end >= 0 ? end + grammar.block[1].length : open < 0 ? code.length : open;
129
+ push(code.slice(i, stop), 'comment');
130
+ i = stop;
131
+ continue;
132
+ }
133
+ if (grammar.line && code.startsWith(grammar.line, i) && (!grammar.wordComment || lineStart || /\s/.test(before ?? ''))) {
134
+ const end = code.indexOf('\n', i);
135
+ const stop = end < 0 ? code.length : end;
136
+ push(code.slice(i, stop), 'comment');
137
+ i = stop;
138
+ continue;
139
+ }
140
+ const quote = grammar.quotes.find(q => code.startsWith(q.open, i));
141
+ if (quote && (!grammar.quoteAt || grammar.quoteAt(code, i))) {
142
+ const stop = closeOf(code, i, quote);
143
+ const isKey = grammar.keys === 'json' && sticky(JSON_COLON, code, stop) !== null;
144
+ push(code.slice(i, stop), isKey ? 'key' : 'string');
145
+ i = stop;
146
+ continue;
147
+ }
148
+ const loose = before === undefined || !JOINED.test(before);
149
+ // A minus is a sign where nothing it could join sits before it: `-1`, but not the 10 of `2026-10`.
150
+ const signed = before === '-' && !JOINED.test(code[i - 2] ?? ' ');
151
+ const number = loose || signed ? sticky(NUMBER, code, i) : null;
152
+ if (number && !JOINED.test(code[i + number[0].length] ?? '')) {
153
+ push(number[0], 'number');
154
+ i += number[0].length;
155
+ continue;
156
+ }
157
+ const word = sticky(WORD, code, i);
158
+ if (word) {
159
+ const keyword = loose && grammar.keywords.has(word[0]) && code[i + word[0].length] !== '-';
160
+ push(word[0], keyword ? 'keyword' : undefined);
161
+ i += word[0].length;
162
+ continue;
163
+ }
164
+ push(code[i]);
165
+ i++;
166
+ }
167
+ return out;
168
+ }
169
+ function highlighted(code, language) {
170
+ const grammar = GRAMMARS[language];
171
+ if (!grammar)
172
+ return code;
173
+ return tokenize(code, grammar).map((token, n) => token.kind ? _jsx("span", { className: `desk-code-${token.kind}`, children: token.text }, n) : token.text);
174
+ }
175
+ const NAMES = {
176
+ python: 'Python', typescript: 'TypeScript', javascript: 'JavaScript', kotlin: 'Kotlin', yaml: 'YAML',
177
+ shell: 'Shell', json: 'JSON', text: '',
178
+ };
179
+ /** How long "Copied" stays before the button says Copy again. */
180
+ const COPIED_MS = 2000;
181
+ /** A framed block of code to copy, highlighted for its language, with a Copy button in its corner. */
182
+ export function CodeBlock(props) {
183
+ const { label, copy = true, highlight, className, variants, value, onValueChange } = props;
184
+ const [own, setOwn] = useState(variants?.[0]?.value);
185
+ const variant = variants && (variants.find(v => v.value === (value ?? own)) ?? variants[0]);
186
+ const choose = (next) => {
187
+ if (value === undefined)
188
+ setOwn(next);
189
+ onValueChange?.(next);
190
+ };
191
+ const code = (variants ? variant?.code : props.code) ?? '';
192
+ const language = (variants ? variant?.language : props.language) ?? 'text';
193
+ const body = useRef(null);
194
+ const [copying, setCopying] = useState('idle');
195
+ const timer = useRef(undefined);
196
+ const alive = useRef(true);
197
+ const copied = useRef(code);
198
+ const shown = code.endsWith('\n') ? code.slice(0, -1) : code;
199
+ const content = useMemo(() => (highlight ?? highlighted)(shown, language), [shown, language, highlight]);
200
+ useEffect(() => {
201
+ alive.current = true;
202
+ return () => {
203
+ alive.current = false;
204
+ clearTimeout(timer.current);
205
+ };
206
+ }, []);
207
+ // Copied or selected says something about this code; new code has been neither. Put back while
208
+ // rendering, so not one frame says Copied over code nobody copied.
209
+ if (copied.current !== code) {
210
+ copied.current = code;
211
+ clearTimeout(timer.current);
212
+ if (copying !== 'idle')
213
+ setCopying('idle');
214
+ }
215
+ const settle = (next) => {
216
+ // A copy that finishes after the block has gone has nothing left to say it in.
217
+ if (!alive.current)
218
+ return;
219
+ clearTimeout(timer.current);
220
+ setCopying(next);
221
+ if (next === 'copied')
222
+ timer.current = setTimeout(() => setCopying('idle'), COPIED_MS);
223
+ };
224
+ /*
225
+ * THE CLIPBOARD IS NOT ALWAYS THERE: a page over plain http has none, and a browser may refuse
226
+ * it. Then the code is selected and the older copy command tried; where that fails too, the
227
+ * selection stays and the button says which keys copy it, so the person is one keystroke away.
228
+ * The selection is the code as drawn: without the trailing newline, and with whatever a host's
229
+ * `highlight` adds to it.
230
+ */
231
+ const onCopy = async () => {
232
+ try {
233
+ await navigator.clipboard.writeText(code);
234
+ settle('copied');
235
+ }
236
+ catch {
237
+ const selection = window.getSelection();
238
+ if (body.current && selection)
239
+ selection.selectAllChildren(body.current);
240
+ let copied = false;
241
+ try {
242
+ copied = document.execCommand('copy');
243
+ }
244
+ catch {
245
+ // Unsupported: the selection is the way.
246
+ }
247
+ settle(copied ? 'copied' : 'select');
248
+ }
249
+ };
250
+ const keys = formatShortcut('mod+c');
251
+ const name = variant
252
+ ? label ? `${label}, ${variant.label}` : `${variant.label} code`
253
+ : label ?? (NAMES[language] ? `${NAMES[language]} code` : 'Code');
254
+ const actions = copy && (_jsxs("div", { className: "desk-code-actions", children: [_jsx(Button, { intent: "quiet", size: "small", onClick: onCopy, children: copying === 'copied' ? 'Copied' : copying === 'select' ? `Press ${keys}` : 'Copy' }), _jsx("span", { className: "desk-code-status", role: "status", children: copying === 'copied' ? 'Copied' : copying === 'select' ? `Selected. Press ${keys} to copy.` : '' })] }));
255
+ return (_jsxs("div", { className: ['desk-code', className].filter(Boolean).join(' '), "data-language": language, ...(variants ? { 'data-variants': '' } : {}), children: [variants && (_jsxs("div", { className: "desk-code-head", children: [_jsx(SegmentedControl, { label: label ?? 'Code', size: "small", options: variants.map(v => ({ value: v.value, label: v.label })), value: variant?.value ?? '', onChange: choose }), actions] })), _jsxs("div", { className: "desk-code-main", children: [label && !variants && _jsx("div", { className: "desk-code-label", "aria-hidden": "true", children: label }), _jsx("pre", { className: "desk-code-body", role: "region", "aria-label": name, tabIndex: 0, children: _jsx("code", { ref: body, children: content }) })] }), !variants && actions] }));
256
+ }
257
+ //# sourceMappingURL=codeBlock.js.map