@liberation-data/desk 0.36.1 → 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 +29 -1
- package/dist/css/controls.css +98 -0
- package/dist/css/tokens.css +16 -0
- package/dist/desk.css +114 -0
- package/dist/react/codeBlock.d.ts +48 -0
- package/dist/react/codeBlock.d.ts.map +1 -0
- package/dist/react/codeBlock.js +257 -0
- package/dist/react/codeBlock.js.map +1 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +1 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/popup.d.ts.map +1 -1
- package/dist/react/popup.js +24 -2
- package/dist/react/popup.js.map +1 -1
- package/llms.txt +11 -1
- package/package.json +1 -1
- package/src/css/controls.css +98 -0
- package/src/css/tokens.css +16 -0
- package/src/react/codeBlock.tsx +378 -0
- package/src/react/index.ts +2 -0
- package/src/react/popup.tsx +24 -2
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
import { formatShortcut } from '../core/shortcuts.js'
|
|
4
|
+
import { Button, SegmentedControl } from './controls.js'
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* CODE PEOPLE COPY: a snippet to paste into a program, a command to run, a file to save.
|
|
8
|
+
*
|
|
9
|
+
* It is read exactly, so it keeps its own lines — a long one scrolls sideways rather than wrapping,
|
|
10
|
+
* because a wrapped command reads as two — and the block is a named region that takes focus, so it
|
|
11
|
+
* scrolls from the keyboard. Copy sits in the corner beside the code, never over it, and is always
|
|
12
|
+
* shown: a button that appears on hover is one a keyboard and a touch screen never find.
|
|
13
|
+
*
|
|
14
|
+
* HIGHLIGHTING IS SMALL ON PURPOSE. Comments, strings, keywords, numbers and the keys of YAML and
|
|
15
|
+
* JSON, from a scanner a few rules long per language: enough to read a snippet by, and no
|
|
16
|
+
* dependency. It is a reading aid, not a parser — a regular expression literal or a heredoc is read
|
|
17
|
+
* as code, and a block comment left open colours its own line rather than the rest of the file — and
|
|
18
|
+
* a host with a real highlighter passes `highlight` instead.
|
|
19
|
+
*
|
|
20
|
+
* NOTHING IS PARSED AS HTML. The scanner cuts the code into strings and React renders them, so a
|
|
21
|
+
* `<script>` in a snippet is text in the block, whatever the language or the highlighter.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export type CodeLanguage = 'python' | 'typescript' | 'javascript' | 'kotlin' | 'yaml' | 'shell' | 'json' | 'text'
|
|
25
|
+
|
|
26
|
+
/** One of the forms the same code comes in: the same install with pip or with uv, a client in two languages. */
|
|
27
|
+
export interface CodeVariant {
|
|
28
|
+
readonly value: string
|
|
29
|
+
/** Its tab: "Python", "pip". */
|
|
30
|
+
readonly label: string
|
|
31
|
+
readonly language: CodeLanguage
|
|
32
|
+
readonly code: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface CodeBlockCommon {
|
|
36
|
+
/**
|
|
37
|
+
* What it is: "Install", "config.yaml". It names the region and, without variants, is shown above
|
|
38
|
+
* the code; with them the tabs take that place, and it names them.
|
|
39
|
+
*/
|
|
40
|
+
readonly label?: string
|
|
41
|
+
/** A Copy button in the corner. Default true. */
|
|
42
|
+
readonly copy?: boolean
|
|
43
|
+
/** A highlighter of the host's own, e.g. CodeMirror's `runMode`, used instead of the built-in one. */
|
|
44
|
+
readonly highlight?: (code: string, language: CodeLanguage) => ReactNode
|
|
45
|
+
readonly className?: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface OneCode extends CodeBlockCommon {
|
|
49
|
+
/** The code, exactly as it is copied. One trailing newline is not drawn as an empty line. */
|
|
50
|
+
readonly code: string
|
|
51
|
+
/** How it is highlighted, and the region's name when there is no `label`. Default `text`: none. */
|
|
52
|
+
readonly language?: CodeLanguage
|
|
53
|
+
readonly variants?: never
|
|
54
|
+
readonly value?: never
|
|
55
|
+
readonly onValueChange?: never
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface VariantCode extends CodeBlockCommon {
|
|
59
|
+
/**
|
|
60
|
+
* The same code in several forms, chosen by tabs in a bar across the top of the block: one card,
|
|
61
|
+
* not a control above it. Copy copies the one shown.
|
|
62
|
+
*/
|
|
63
|
+
readonly variants: readonly CodeVariant[]
|
|
64
|
+
/** The variant shown. Leave it out and the block keeps its own choice, starting at the first. */
|
|
65
|
+
readonly value?: string
|
|
66
|
+
readonly onValueChange?: (value: string) => void
|
|
67
|
+
readonly code?: never
|
|
68
|
+
readonly language?: never
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type CodeBlockProps = OneCode | VariantCode
|
|
72
|
+
|
|
73
|
+
type Kind = 'comment' | 'string' | 'keyword' | 'number' | 'key'
|
|
74
|
+
interface Token { readonly kind?: Kind | undefined; readonly text: string }
|
|
75
|
+
|
|
76
|
+
/* A quote: how it opens, whether it may run over lines, and how it says its own quote inside. */
|
|
77
|
+
interface Quote { readonly open: string; readonly lines?: boolean; readonly escape?: 'backslash' | 'double' }
|
|
78
|
+
|
|
79
|
+
interface Grammar {
|
|
80
|
+
readonly line?: string
|
|
81
|
+
/** `#` starts a comment only at the start of a word, so `$#` and `a#b` are not one (shell, YAML). */
|
|
82
|
+
readonly wordComment?: boolean
|
|
83
|
+
/** A quote opens only where a value can start, so an apostrophe inside a word or a sentence is text. */
|
|
84
|
+
readonly quoteAt?: (code: string, at: number) => boolean
|
|
85
|
+
readonly block?: readonly [string, string]
|
|
86
|
+
readonly quotes: readonly Quote[]
|
|
87
|
+
readonly keywords: ReadonlySet<string>
|
|
88
|
+
readonly keys?: 'yaml' | 'json'
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const words = (list: string) => new Set(list.split(' '))
|
|
92
|
+
const C_COMMENTS = { line: '//', block: ['/*', '*/'] } as const
|
|
93
|
+
const B: Quote['escape'] = 'backslash'
|
|
94
|
+
const JS = 'async await break case catch class const continue debugger default delete do else export extends false ' +
|
|
95
|
+
'finally for function if import in instanceof let new null of return static super switch this throw true try ' +
|
|
96
|
+
'typeof undefined var void while with yield'
|
|
97
|
+
|
|
98
|
+
const GRAMMARS: Partial<Record<CodeLanguage, Grammar>> = {
|
|
99
|
+
python: {
|
|
100
|
+
line: '#',
|
|
101
|
+
quotes: [{ open: '"""', lines: true, escape: B }, { open: "'''", lines: true, escape: B }, { open: '"', escape: B }, { open: "'", escape: B }],
|
|
102
|
+
keywords: words('False None True and as assert async await break class continue def del elif else except ' +
|
|
103
|
+
'finally for from global if import in is lambda nonlocal not or pass raise return try while with yield'),
|
|
104
|
+
},
|
|
105
|
+
javascript: {
|
|
106
|
+
...C_COMMENTS,
|
|
107
|
+
quotes: [{ open: '`', lines: true, escape: B }, { open: '"', escape: B }, { open: "'", escape: B }],
|
|
108
|
+
keywords: words(JS),
|
|
109
|
+
},
|
|
110
|
+
typescript: {
|
|
111
|
+
...C_COMMENTS,
|
|
112
|
+
quotes: [{ open: '`', lines: true, escape: B }, { open: '"', escape: B }, { open: "'", escape: B }],
|
|
113
|
+
keywords: words(`${JS} abstract as declare enum implements interface keyof namespace private protected public readonly satisfies`),
|
|
114
|
+
},
|
|
115
|
+
kotlin: {
|
|
116
|
+
...C_COMMENTS,
|
|
117
|
+
quotes: [{ open: '"""', lines: true }, { open: '"', escape: B }, { open: "'", escape: B }],
|
|
118
|
+
keywords: words('abstract as break by class companion const constructor continue data do else enum false for fun ' +
|
|
119
|
+
'if import in init interface internal is lateinit null object open override package private protected public ' +
|
|
120
|
+
'return sealed super suspend this throw true try typealias val var when while'),
|
|
121
|
+
},
|
|
122
|
+
shell: {
|
|
123
|
+
line: '#',
|
|
124
|
+
wordComment: true,
|
|
125
|
+
quotes: [{ open: '"', lines: true, escape: B }, { open: "'", lines: true }],
|
|
126
|
+
// `Don't panic` in a heredoc or an unquoted echo is not the start of a string.
|
|
127
|
+
quoteAt: (code, at) => !/[\w]/.test(code[at - 1] ?? ''),
|
|
128
|
+
keywords: words('case do done elif else esac export fi for function if in local readonly return select then until while'),
|
|
129
|
+
},
|
|
130
|
+
yaml: {
|
|
131
|
+
line: '#',
|
|
132
|
+
wordComment: true,
|
|
133
|
+
quotes: [{ open: '"', lines: true, escape: B }, { open: "'", lines: true, escape: 'double' }],
|
|
134
|
+
keywords: words('true false null'),
|
|
135
|
+
quoteAt: (code, at) => yamlValueStarts(code, at),
|
|
136
|
+
keys: 'yaml',
|
|
137
|
+
},
|
|
138
|
+
json: {
|
|
139
|
+
quotes: [{ open: '"', escape: B }],
|
|
140
|
+
keywords: words('true false null'),
|
|
141
|
+
keys: 'json',
|
|
142
|
+
},
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const NUMBER = /0[xX][\da-fA-F_]+|\d[\d_]*(?:\.\d[\d_]*)?(?:[eE][+-]?\d+)?[fFLn]?/y
|
|
146
|
+
const WORD = /[A-Za-z_$][\w$]*/y
|
|
147
|
+
const JSON_COLON = /[ \t]*:/y
|
|
148
|
+
/* Indentation and list dashes, a key — plain or quoted — and its colon, which ends at a space or the line. */
|
|
149
|
+
const YAML_KEY = /([ \t]*(?:-[ \t]+)*)([^\s#'"{}[\],:-](?:[^:\n#]*[^\s:#])?|"(?:[^"\\\n]|\\.)*"|'(?:[^'\n]|'')*')([ \t]*:)(?=[ \t]|$)/my
|
|
150
|
+
/* What a number or keyword cannot touch, so `v1`, `1.2.3`, `obj.if` and `--if-exists` stay text. */
|
|
151
|
+
const JOINED = /[\w$.-]/
|
|
152
|
+
|
|
153
|
+
function sticky(pattern: RegExp, code: string, at: number): RegExpExecArray | null {
|
|
154
|
+
pattern.lastIndex = at
|
|
155
|
+
return pattern.exec(code)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Where a quote that opens at `at` closes: past its closing quote, or at the line or text's end. */
|
|
159
|
+
function closeOf(code: string, at: number, quote: Quote): number {
|
|
160
|
+
let i = at + quote.open.length
|
|
161
|
+
while (i < code.length) {
|
|
162
|
+
if (quote.escape === 'backslash' && code[i] === '\\') i += 2
|
|
163
|
+
else if (code.startsWith(quote.open, i)) {
|
|
164
|
+
if (quote.escape === 'double' && code[i + 1] === quote.open) i += 2
|
|
165
|
+
else {
|
|
166
|
+
// A raw string may end in its own quote: `"""a""""` closes at the last of the run.
|
|
167
|
+
let end = i + quote.open.length
|
|
168
|
+
while (quote.open.length === 3 && code[end] === quote.open[0]) end++
|
|
169
|
+
return end
|
|
170
|
+
}
|
|
171
|
+
} else if (code[i] === '\n' && !quote.lines) return i
|
|
172
|
+
else i++
|
|
173
|
+
}
|
|
174
|
+
return code.length
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/* YAML opens a quote only where a value starts: `title: Don't stop` is a plain sentence. */
|
|
178
|
+
function yamlValueStarts(code: string, at: number): boolean {
|
|
179
|
+
let i = at - 1
|
|
180
|
+
while (code[i] === ' ' || code[i] === '\t') i--
|
|
181
|
+
return i < 0 || ':-[{,\n'.includes(code[i] ?? '')
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function tokenize(code: string, grammar: Grammar): Token[] {
|
|
185
|
+
const out: Token[] = []
|
|
186
|
+
const push = (text = '', kind?: Kind) => {
|
|
187
|
+
if (!text) return
|
|
188
|
+
const last = out[out.length - 1]
|
|
189
|
+
if (!kind && last && !last.kind) out[out.length - 1] = { text: last.text + text }
|
|
190
|
+
else out.push({ kind, text })
|
|
191
|
+
}
|
|
192
|
+
let i = 0
|
|
193
|
+
while (i < code.length) {
|
|
194
|
+
const before = code[i - 1]
|
|
195
|
+
const lineStart = i === 0 || before === '\n'
|
|
196
|
+
if (lineStart && grammar.keys === 'yaml') {
|
|
197
|
+
const key = sticky(YAML_KEY, code, i)
|
|
198
|
+
if (key) {
|
|
199
|
+
push(key[1])
|
|
200
|
+
push(key[2], 'key')
|
|
201
|
+
push(key[3])
|
|
202
|
+
i += key[0].length
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (grammar.block && code.startsWith(grammar.block[0], i)) {
|
|
207
|
+
const end = code.indexOf(grammar.block[1], i + 2)
|
|
208
|
+
// Never closed: most likely a `/*` inside a regular expression, so it colours one line, not the file.
|
|
209
|
+
const open = code.indexOf('\n', i)
|
|
210
|
+
const stop = end >= 0 ? end + grammar.block[1].length : open < 0 ? code.length : open
|
|
211
|
+
push(code.slice(i, stop), 'comment')
|
|
212
|
+
i = stop
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
if (grammar.line && code.startsWith(grammar.line, i) && (!grammar.wordComment || lineStart || /\s/.test(before ?? ''))) {
|
|
216
|
+
const end = code.indexOf('\n', i)
|
|
217
|
+
const stop = end < 0 ? code.length : end
|
|
218
|
+
push(code.slice(i, stop), 'comment')
|
|
219
|
+
i = stop
|
|
220
|
+
continue
|
|
221
|
+
}
|
|
222
|
+
const quote = grammar.quotes.find(q => code.startsWith(q.open, i))
|
|
223
|
+
if (quote && (!grammar.quoteAt || grammar.quoteAt(code, i))) {
|
|
224
|
+
const stop = closeOf(code, i, quote)
|
|
225
|
+
const isKey = grammar.keys === 'json' && sticky(JSON_COLON, code, stop) !== null
|
|
226
|
+
push(code.slice(i, stop), isKey ? 'key' : 'string')
|
|
227
|
+
i = stop
|
|
228
|
+
continue
|
|
229
|
+
}
|
|
230
|
+
const loose = before === undefined || !JOINED.test(before)
|
|
231
|
+
// A minus is a sign where nothing it could join sits before it: `-1`, but not the 10 of `2026-10`.
|
|
232
|
+
const signed = before === '-' && !JOINED.test(code[i - 2] ?? ' ')
|
|
233
|
+
const number = loose || signed ? sticky(NUMBER, code, i) : null
|
|
234
|
+
if (number && !JOINED.test(code[i + number[0].length] ?? '')) {
|
|
235
|
+
push(number[0], 'number')
|
|
236
|
+
i += number[0].length
|
|
237
|
+
continue
|
|
238
|
+
}
|
|
239
|
+
const word = sticky(WORD, code, i)
|
|
240
|
+
if (word) {
|
|
241
|
+
const keyword = loose && grammar.keywords.has(word[0]) && code[i + word[0].length] !== '-'
|
|
242
|
+
push(word[0], keyword ? 'keyword' : undefined)
|
|
243
|
+
i += word[0].length
|
|
244
|
+
continue
|
|
245
|
+
}
|
|
246
|
+
push(code[i])
|
|
247
|
+
i++
|
|
248
|
+
}
|
|
249
|
+
return out
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function highlighted(code: string, language: CodeLanguage): ReactNode {
|
|
253
|
+
const grammar = GRAMMARS[language]
|
|
254
|
+
if (!grammar) return code
|
|
255
|
+
return tokenize(code, grammar).map((token, n) =>
|
|
256
|
+
token.kind ? <span key={n} className={`desk-code-${token.kind}`}>{token.text}</span> : token.text,
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const NAMES: Record<CodeLanguage, string> = {
|
|
261
|
+
python: 'Python', typescript: 'TypeScript', javascript: 'JavaScript', kotlin: 'Kotlin', yaml: 'YAML',
|
|
262
|
+
shell: 'Shell', json: 'JSON', text: '',
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** How long "Copied" stays before the button says Copy again. */
|
|
266
|
+
const COPIED_MS = 2000
|
|
267
|
+
|
|
268
|
+
type Copying = 'idle' | 'copied' | 'select'
|
|
269
|
+
|
|
270
|
+
/** A framed block of code to copy, highlighted for its language, with a Copy button in its corner. */
|
|
271
|
+
export function CodeBlock(props: CodeBlockProps) {
|
|
272
|
+
const { label, copy = true, highlight, className, variants, value, onValueChange } = props
|
|
273
|
+
const [own, setOwn] = useState(variants?.[0]?.value)
|
|
274
|
+
const variant = variants && (variants.find(v => v.value === (value ?? own)) ?? variants[0])
|
|
275
|
+
const choose = (next: string) => {
|
|
276
|
+
if (value === undefined) setOwn(next)
|
|
277
|
+
onValueChange?.(next)
|
|
278
|
+
}
|
|
279
|
+
const code = (variants ? variant?.code : props.code) ?? ''
|
|
280
|
+
const language: CodeLanguage = (variants ? variant?.language : props.language) ?? 'text'
|
|
281
|
+
const body = useRef<HTMLElement>(null)
|
|
282
|
+
const [copying, setCopying] = useState<Copying>('idle')
|
|
283
|
+
const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
|
|
284
|
+
const alive = useRef(true)
|
|
285
|
+
const copied = useRef(code)
|
|
286
|
+
const shown = code.endsWith('\n') ? code.slice(0, -1) : code
|
|
287
|
+
const content = useMemo(() => (highlight ?? highlighted)(shown, language), [shown, language, highlight])
|
|
288
|
+
|
|
289
|
+
useEffect(() => {
|
|
290
|
+
alive.current = true
|
|
291
|
+
return () => {
|
|
292
|
+
alive.current = false
|
|
293
|
+
clearTimeout(timer.current)
|
|
294
|
+
}
|
|
295
|
+
}, [])
|
|
296
|
+
// Copied or selected says something about this code; new code has been neither. Put back while
|
|
297
|
+
// rendering, so not one frame says Copied over code nobody copied.
|
|
298
|
+
if (copied.current !== code) {
|
|
299
|
+
copied.current = code
|
|
300
|
+
clearTimeout(timer.current)
|
|
301
|
+
if (copying !== 'idle') setCopying('idle')
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const settle = (next: Copying) => {
|
|
305
|
+
// A copy that finishes after the block has gone has nothing left to say it in.
|
|
306
|
+
if (!alive.current) return
|
|
307
|
+
clearTimeout(timer.current)
|
|
308
|
+
setCopying(next)
|
|
309
|
+
if (next === 'copied') timer.current = setTimeout(() => setCopying('idle'), COPIED_MS)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/*
|
|
313
|
+
* THE CLIPBOARD IS NOT ALWAYS THERE: a page over plain http has none, and a browser may refuse
|
|
314
|
+
* it. Then the code is selected and the older copy command tried; where that fails too, the
|
|
315
|
+
* selection stays and the button says which keys copy it, so the person is one keystroke away.
|
|
316
|
+
* The selection is the code as drawn: without the trailing newline, and with whatever a host's
|
|
317
|
+
* `highlight` adds to it.
|
|
318
|
+
*/
|
|
319
|
+
const onCopy = async () => {
|
|
320
|
+
try {
|
|
321
|
+
await navigator.clipboard.writeText(code)
|
|
322
|
+
settle('copied')
|
|
323
|
+
} catch {
|
|
324
|
+
const selection = window.getSelection()
|
|
325
|
+
if (body.current && selection) selection.selectAllChildren(body.current)
|
|
326
|
+
let copied = false
|
|
327
|
+
try {
|
|
328
|
+
copied = document.execCommand('copy')
|
|
329
|
+
} catch {
|
|
330
|
+
// Unsupported: the selection is the way.
|
|
331
|
+
}
|
|
332
|
+
settle(copied ? 'copied' : 'select')
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const keys = formatShortcut('mod+c')
|
|
337
|
+
const name = variant
|
|
338
|
+
? label ? `${label}, ${variant.label}` : `${variant.label} code`
|
|
339
|
+
: label ?? (NAMES[language] ? `${NAMES[language]} code` : 'Code')
|
|
340
|
+
const actions = copy && (
|
|
341
|
+
<div className="desk-code-actions">
|
|
342
|
+
<Button intent="quiet" size="small" onClick={onCopy}>
|
|
343
|
+
{copying === 'copied' ? 'Copied' : copying === 'select' ? `Press ${keys}` : 'Copy'}
|
|
344
|
+
</Button>
|
|
345
|
+
<span className="desk-code-status" role="status">
|
|
346
|
+
{copying === 'copied' ? 'Copied' : copying === 'select' ? `Selected. Press ${keys} to copy.` : ''}
|
|
347
|
+
</span>
|
|
348
|
+
</div>
|
|
349
|
+
)
|
|
350
|
+
return (
|
|
351
|
+
<div
|
|
352
|
+
className={['desk-code', className].filter(Boolean).join(' ')}
|
|
353
|
+
data-language={language}
|
|
354
|
+
{...(variants ? { 'data-variants': '' } : {})}
|
|
355
|
+
>
|
|
356
|
+
{variants && (
|
|
357
|
+
// The tabs are the frame's top edge, as on a phone: one card, not a control above a box.
|
|
358
|
+
<div className="desk-code-head">
|
|
359
|
+
<SegmentedControl
|
|
360
|
+
label={label ?? 'Code'}
|
|
361
|
+
size="small"
|
|
362
|
+
options={variants.map(v => ({ value: v.value, label: v.label }))}
|
|
363
|
+
value={variant?.value ?? ''}
|
|
364
|
+
onChange={choose}
|
|
365
|
+
/>
|
|
366
|
+
{actions}
|
|
367
|
+
</div>
|
|
368
|
+
)}
|
|
369
|
+
<div className="desk-code-main">
|
|
370
|
+
{label && !variants && <div className="desk-code-label" aria-hidden="true">{label}</div>}
|
|
371
|
+
<pre className="desk-code-body" role="region" aria-label={name} tabIndex={0}>
|
|
372
|
+
<code ref={body}>{content}</code>
|
|
373
|
+
</pre>
|
|
374
|
+
</div>
|
|
375
|
+
{!variants && actions}
|
|
376
|
+
</div>
|
|
377
|
+
)
|
|
378
|
+
}
|
package/src/react/index.ts
CHANGED
|
@@ -62,6 +62,8 @@ export type { ChecklistProps, Task, TaskProgress, Tasks, TaskState } from './tas
|
|
|
62
62
|
export { PopUpButton } from './popup.js'
|
|
63
63
|
export { Chip, Disclosure } from './disclosure.js'
|
|
64
64
|
export type { ChipProps, DisclosureProps } from './disclosure.js'
|
|
65
|
+
export { CodeBlock } from './codeBlock.js'
|
|
66
|
+
export type { CodeBlockProps, CodeLanguage, CodeVariant } from './codeBlock.js'
|
|
65
67
|
export type { PopUpButtonProps, PopUpOption } from './popup.js'
|
|
66
68
|
export { Tabs } from './tabs.js'
|
|
67
69
|
export type { Tab, TabsProps } from './tabs.js'
|
package/src/react/popup.tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { useEffect, useId, useRef, useState } from 'react'
|
|
1
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from 'react'
|
|
2
|
+
import { WINDOW_ATTRIBUTE } from '../core/commands.js'
|
|
2
3
|
import type { KeyboardEvent, ReactNode } from 'react'
|
|
3
4
|
|
|
4
5
|
/*
|
|
@@ -27,6 +28,9 @@ export interface PopUpButtonProps<T extends string> {
|
|
|
27
28
|
readonly size?: 'regular' | 'small'
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
/** How far the list keeps from its window's edges. */
|
|
32
|
+
const LIST_MARGIN = 8
|
|
33
|
+
|
|
30
34
|
export function PopUpButton<T extends string>({
|
|
31
35
|
value,
|
|
32
36
|
onChange,
|
|
@@ -76,6 +80,24 @@ export function PopUpButton<T extends string>({
|
|
|
76
80
|
return () => document.removeEventListener('pointerdown', onPointerDown, true)
|
|
77
81
|
}, [open])
|
|
78
82
|
|
|
83
|
+
/*
|
|
84
|
+
* THE LIST IS AS WIDE AS ITS WIDEST OPTION, as a menu is, and not as narrow as the button: a short
|
|
85
|
+
* button over longer options wrapped every label in two. Wider than the button, it can reach past
|
|
86
|
+
* the window's right edge, so it moves left until it fits, never past the window's left edge.
|
|
87
|
+
*/
|
|
88
|
+
useLayoutEffect(() => {
|
|
89
|
+
const element = list.current
|
|
90
|
+
const anchor = element?.parentElement
|
|
91
|
+
if (!open || !element || !anchor) return
|
|
92
|
+
element.style.left = ''
|
|
93
|
+
const frame = anchor.closest(`[${WINDOW_ATTRIBUTE}]`)?.getBoundingClientRect()
|
|
94
|
+
const right = (frame?.right ?? innerWidth) - LIST_MARGIN
|
|
95
|
+
const left = (frame?.left ?? 0) + LIST_MARGIN
|
|
96
|
+
const box = element.getBoundingClientRect()
|
|
97
|
+
const over = box.right - right
|
|
98
|
+
if (over > 0) element.style.left = `${-Math.min(over, Math.max(box.left - left, 0))}px`
|
|
99
|
+
}, [open])
|
|
100
|
+
|
|
79
101
|
const step = (from: number, delta: 1 | -1) => {
|
|
80
102
|
for (let i = 1; i <= options.length; i++) {
|
|
81
103
|
const index = (from + delta * i + options.length * 2) % options.length
|
|
@@ -163,7 +185,7 @@ export function PopUpButton<T extends string>({
|
|
|
163
185
|
>
|
|
164
186
|
<span className="desk-popup-check" aria-hidden="true">{option.value === value ? '✓' : ''}</span>
|
|
165
187
|
<span className="desk-popup-text">
|
|
166
|
-
<span>{option.label}</span>
|
|
188
|
+
<span className="desk-popup-label">{option.label}</span>
|
|
167
189
|
{option.description && <span className="desk-help">{option.description}</span>}
|
|
168
190
|
</span>
|
|
169
191
|
</li>
|