-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-block.d.ts
More file actions
470 lines (411 loc) · 11.3 KB
/
code-block.d.ts
File metadata and controls
470 lines (411 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
* Code Block Web Component with Syntax Highlighting and Copy Button
* Uses highlight.js for syntax highlighting (limited language bundle)
*/
export declare class CodeBlock extends HTMLElement {
constructor();
/**
* Called when the element is added to the DOM
*/
connectedCallback(): void;
/**
* List of attributes to observe for changes
*/
static readonly observedAttributes: readonly ['language', 'label', 'theme', 'show-lines', 'start-line', 'end-line', 'filename', 'highlight-lines', 'collapsed', 'max-lines', 'max-height', 'wrap', 'copy-text', 'copied-text', 'show-share', 'show-download', 'lazy', 'focus-mode', 'src'];
/**
* Called when an observed attribute changes
*/
attributeChangedCallback(
name: string,
oldValue: string | null,
newValue: string | null
): void;
/**
* The programming language for syntax highlighting.
* Supported: 'html', 'css', 'javascript', 'js', 'json', 'yaml', 'yml', 'php', 'http', 'plaintext', 'text', 'txt', 'csv'
* @default 'plaintext'
*/
readonly language: string;
/**
* The label displayed in the header.
* Falls back to filename if set, otherwise language name.
* @default language.toUpperCase()
*/
readonly label: string;
/**
* The color theme for the code block
* @default 'light'
*/
readonly theme: 'light' | 'dark';
/**
* Whether to display line numbers
* Set via the `show-lines` attribute
* @default false
*/
readonly showLines: boolean;
/**
* Starting line number when `show-lines` is enabled.
* Set via the `start-line` attribute. Useful for displaying snippets
* from a larger source where the original line numbers matter.
* @default 1
*/
readonly startLine: number;
/**
* End line number — when set, only lines [startLine..endLine] of the
* source are rendered. Line numbers in the gutter remain absolute.
* Set via the `end-line` attribute.
*/
readonly endLine: number | null;
/**
* The filename displayed in the header (optional)
* Shows language badge + filename when set
*/
readonly filename: string;
/**
* Set of line numbers to highlight
* Parsed from `highlight-lines` attribute
* Supports individual lines (1,3,5) and ranges (1-5)
*/
readonly highlightLines: Set<number>;
/**
* Whether the code block is collapsed
* Set via the `collapsed` attribute
* @default false
*/
readonly collapsed: boolean;
/**
* Maximum number of lines to show when collapsed
* Set via the `max-lines` attribute
* @default 10
*/
readonly maxLines: number;
/**
* Maximum height of the code container with scrolling
* Set via the `max-height` attribute (e.g., "300px")
*/
readonly maxHeight: string;
/**
* Whether to wrap long lines instead of horizontal scrolling
* Set via the `wrap` attribute
* @default false
*/
readonly wrap: boolean;
/**
* Custom text for the copy button
* Set via the `copy-text` attribute
* @default 'Copy'
*/
readonly copyText: string;
/**
* Custom text shown after successful copy
* Set via the `copied-text` attribute
* @default 'Copied!'
*/
readonly copiedText: string;
/**
* Whether to show the share button
* Set via the `show-share` attribute
* @default false
*/
readonly showShare: boolean;
/**
* Whether to show the download button
* Set via the `show-download` attribute
* @default false
*/
readonly showDownload: boolean;
/**
* Whether to use lazy highlighting (defer until visible)
* Set via the `lazy` attribute
* @default false
*/
readonly lazy: boolean;
/**
* Whether to dim/blur non-highlighted lines for focus
* Set via the `focus-mode` attribute
* Works in conjunction with highlight-lines
* @default false
*/
readonly focusMode: boolean;
/**
* URL to load code content from
* Set via the `src` attribute
* When set, fetches code from the URL instead of using textContent
* Language is auto-detected from file extension if not specified
*/
readonly src: string;
/**
* Copies the code content to clipboard.
* Shows visual feedback on success or failure.
*/
copyCode(): Promise<void>;
/**
* Downloads the code as a file with appropriate extension
*/
downloadCode(): void;
/**
* Opens the code in CodePen
*/
openInCodePen(): void;
/**
* Share via Web Share API (if available)
*/
shareViaWebAPI(): Promise<void>;
/**
* Toggle share menu visibility
*/
toggleShareMenu(): void;
/**
* Renders the component with syntax highlighting
*/
render(): void;
/**
* Renders a placeholder without syntax highlighting (for lazy loading)
*/
renderPlaceholder(): void;
/**
* Toggle collapsed state
*/
toggleCollapsed(): void;
/**
* Escapes HTML special characters
*/
escapeHtml(text: string): string;
/**
* Update the code content programmatically
*/
setCode(code: string): void;
/**
* Get the current code content
*/
getCode(): string;
/**
* Get list of supported languages
*/
static getSupportedLanguages(): string[];
}
/**
* CSS Custom Properties for theming
*
* Layout:
* --cb-margin: Component margin (default: 1rem 0)
* --cb-border-radius: Border radius (default: 8px)
* --cb-border-color: Border color
* --cb-font-family: Code font family
* --cb-font-size: Code font size (default: 0.875rem)
*
* Colors:
* --cb-bg: Component background
* --cb-header-bg: Header background
* --cb-code-bg: Code area background
* --cb-text-color: Default text color
* --cb-label-color: Label text color
* --cb-filename-color: Filename text color
*
* Line Numbers:
* --cb-line-numbers-bg: Line numbers background
* --cb-line-numbers-color: Line numbers text color
* --cb-line-numbers-highlight-color: Highlighted line number color
*
* Line Highlighting:
* --cb-highlight-bg: Highlighted line background
* --cb-highlight-border: Highlighted line left border
* --cb-highlight-gutter: Highlighted line number background
*
* Focus Mode:
* --cb-focus-dim-opacity: Opacity of non-highlighted lines (default: 0.4)
* --cb-focus-blur: Blur amount for non-highlighted lines (default: 0.5px)
*
* Button:
* --cb-button-bg: Copy button background
* --cb-button-border: Copy button border
* --cb-button-color: Copy button text color
* --cb-button-hover-bg: Copy button hover background
* --cb-focus-color: Focus outline color
* --cb-success-color: Success state color
* --cb-error-color: Error state color
*
* Syntax Highlighting:
* --cb-comment: Comment color
* --cb-keyword: Keyword color
* --cb-string: String color
* --cb-number: Number color
* --cb-function: Function name color
* --cb-attribute: Attribute color
* --cb-tag: HTML tag color
* --cb-meta: Meta/operator color
* --cb-builtin: Built-in function color
*
* Diff Colors:
* --cb-diff-add-bg: Added line background
* --cb-diff-add-border: Added line left border
* --cb-diff-add-gutter: Added line number background
* --cb-diff-add-color: Added line number color
* --cb-diff-add-text: Added text color
* --cb-diff-remove-bg: Removed line background
* --cb-diff-remove-border: Removed line left border
* --cb-diff-remove-gutter: Removed line number background
* --cb-diff-remove-color: Removed line number color
* --cb-diff-remove-text: Removed text color
*
* Collapsible:
* --cb-expand-bg: Expand button background
* --cb-expand-color: Expand button text color
* --cb-expand-hover-bg: Expand button hover background
*
* Scrollbar (max-height):
* --cb-scrollbar-track: Scrollbar track background
* --cb-scrollbar-thumb: Scrollbar thumb color
* --cb-scrollbar-thumb-hover: Scrollbar thumb hover color
*/
/**
* Code Block Group - Tabbed interface for multiple code blocks
*/
export declare class CodeBlockGroup extends HTMLElement {
constructor();
/**
* Called when the element is added to the DOM
*/
connectedCallback(): void;
/**
* Called when the element is removed from the DOM
*/
disconnectedCallback(): void;
/**
* List of attributes to observe for changes
*/
static readonly observedAttributes: readonly ['theme', 'show-share', 'show-download'];
/**
* Called when an observed attribute changes
*/
attributeChangedCallback(
name: string,
oldValue: string | null,
newValue: string | null
): void;
/**
* The color theme for the code block group
* @default 'light'
*/
readonly theme: 'light' | 'dark';
/**
* Whether to show the share button
* Set via the `show-share` attribute
* @default false
*/
readonly showShare: boolean;
/**
* Whether to show the download button
* Set via the `show-download` attribute
* @default false
*/
readonly showDownload: boolean;
/**
* Get all child code-block elements
*/
readonly codeBlocks: CodeBlock[];
/**
* Get/set the currently active tab index
*/
activeIndex: number;
/**
* Renders the component
*/
render(): void;
/**
* Programmatically select a tab by index
*/
selectTab(index: number): void;
/**
* Get the currently active code block
*/
getActiveBlock(): CodeBlock | undefined;
/**
* Toggle share menu visibility
*/
toggleShareMenu(): void;
/**
* Downloads the code from the active block as a file
*/
downloadCode(): void;
/**
* Opens all blocks' code in CodePen (aggregates HTML, CSS, JS)
*/
openInCodePen(): void;
/**
* Share all blocks' code via Web Share API (if available)
*/
shareViaWebAPI(): Promise<void>;
/**
* Escapes HTML special characters
*/
escapeHtml(text: string): string;
}
/**
* Event detail for tab-change event
*/
export interface TabChangeEventDetail {
index: number;
block: CodeBlock;
}
/**
* Event detail for code-loaded event (fired when src content loads successfully)
*/
export interface CodeLoadedEventDetail {
url: string;
code: string;
}
/**
* Event detail for code-load-error event (fired when src content fails to load)
*/
export interface CodeLoadErrorEventDetail {
url: string;
error: string;
}
declare global {
interface HTMLElementTagNameMap {
'code-block': CodeBlock;
'code-block-group': CodeBlockGroup;
}
interface HTMLElementEventMap {
'tab-change': CustomEvent<TabChangeEventDetail>;
'code-loaded': CustomEvent<CodeLoadedEventDetail>;
'code-load-error': CustomEvent<CodeLoadErrorEventDetail>;
}
namespace JSX {
interface IntrinsicElements {
'code-block': React.DetailedHTMLProps<
React.HTMLAttributes<HTMLElement> & {
language?: string;
label?: string;
theme?: 'light' | 'dark';
'show-lines'?: boolean;
'start-line'?: number | string;
'end-line'?: number | string;
filename?: string;
'highlight-lines'?: string;
collapsed?: boolean;
'max-lines'?: number | string;
'max-height'?: string;
wrap?: boolean;
'copy-text'?: string;
'copied-text'?: string;
'show-share'?: boolean;
'show-download'?: boolean;
lazy?: boolean;
'focus-mode'?: boolean;
src?: string;
},
HTMLElement
>;
'code-block-group': React.DetailedHTMLProps<
React.HTMLAttributes<HTMLElement> & {
theme?: 'light' | 'dark';
'show-share'?: boolean;
'show-download'?: boolean;
},
HTMLElement
>;
}
}
}