@thedatagrid/docs-kit 0.1.0 → 0.2.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.
@@ -1,13 +1,23 @@
1
1
  /**
2
2
  * CodeMirror 6 for demo editing. Only imported (dynamically) when someone
3
- * clicks Edit, so pages that are just read never download it. Colors come
4
- * from `--dk-*` CSS variables (see styles.css): tokens get `tok-*` classes.
3
+ * clicks Edit, so pages that are just read never download it.
4
+ *
5
+ * Every color, and the font, comes from a `--dk-editor-*` CSS variable, so a
6
+ * site restyles the editor from its own CSS (see "Theming" in the README).
7
+ * The variables a site doesn't set fall back to the card's `--dk-*` ones
8
+ * (styles.css), so the editor matches the cards by default.
5
9
  */
6
10
  import { autocompletion, closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete';
7
11
  import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands';
8
12
  import { css } from '@codemirror/lang-css';
9
13
  import { javascript } from '@codemirror/lang-javascript';
10
- import { bracketMatching, indentOnInput, indentUnit, syntaxHighlighting } from '@codemirror/language';
14
+ import {
15
+ bracketMatching,
16
+ HighlightStyle,
17
+ indentOnInput,
18
+ indentUnit,
19
+ syntaxHighlighting,
20
+ } from '@codemirror/language';
11
21
  import { highlightSelectionMatches, searchKeymap } from '@codemirror/search';
12
22
  import { EditorState, type Extension } from '@codemirror/state';
13
23
  import {
@@ -18,7 +28,113 @@ import {
18
28
  keymap,
19
29
  lineNumbers,
20
30
  } from '@codemirror/view';
21
- import { classHighlighter } from '@lezer/highlight';
31
+ import { tags as t } from '@lezer/highlight';
32
+
33
+ /** `var(--dk-editor-<name>, <fallback>)` */
34
+ const v = (name: string, fallback: string) => `var(--dk-editor-${name}, ${fallback})`;
35
+
36
+ const fg = v('fg', 'var(--dk-fg)');
37
+ const bg = v('bg', 'var(--dk-code-bg)');
38
+ const activeLineBg = v('active-line-bg', 'var(--dk-accent-soft)');
39
+ const gutterFg = v('gutter-fg', 'var(--dk-line-number, var(--dk-faint))');
40
+ const border = v('border', 'var(--dk-border)');
41
+ const surface = v('panel-bg', 'var(--dk-surface)');
42
+
43
+ /**
44
+ * Editor chrome. Rules in an `EditorView.theme` come after CodeMirror's base
45
+ * theme, so they win over its light defaults without `!important`.
46
+ */
47
+ export const editorTheme = EditorView.theme({
48
+ '&': {
49
+ height: '100%',
50
+ color: fg,
51
+ backgroundColor: bg,
52
+ fontSize: v('font-size', 'var(--dk-code-font-size, 13.5px)'),
53
+ },
54
+ '&.cm-focused': { outline: 'none' },
55
+ '.cm-scroller': {
56
+ fontFamily: v('font-family', 'var(--dk-font-mono)'),
57
+ lineHeight: v('line-height', 'var(--dk-code-line-height, 1.6)'),
58
+ },
59
+ '.cm-content': { caretColor: v('cursor', fg) },
60
+ '.cm-cursor, .cm-dropCursor': { borderLeftColor: v('cursor', fg) },
61
+ '&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection':
62
+ { backgroundColor: v('selection-bg', 'var(--dk-accent-soft)') },
63
+ '.cm-activeLine': { backgroundColor: activeLineBg },
64
+ '.cm-gutters': {
65
+ backgroundColor: v('gutter-bg', bg),
66
+ color: gutterFg,
67
+ border: 'none',
68
+ borderRight: `1px solid ${v('gutter-border', 'transparent')}`,
69
+ },
70
+ '.cm-activeLineGutter': {
71
+ backgroundColor: v('active-line-gutter-bg', activeLineBg),
72
+ color: v('active-line-gutter-fg', gutterFg),
73
+ },
74
+ '.cm-selectionMatch': { backgroundColor: v('selection-match-bg', 'var(--dk-accent-soft)') },
75
+ '&.cm-focused .cm-matchingBracket': {
76
+ backgroundColor: v('bracket-match-bg', 'var(--dk-accent-soft)'),
77
+ outline: `1px solid ${v('bracket-match-outline', 'var(--dk-accent)')}`,
78
+ },
79
+ '&.cm-focused .cm-nonmatchingBracket': {
80
+ backgroundColor: 'transparent',
81
+ outline: `1px solid ${v('invalid', 'var(--dk-error)')}`,
82
+ },
83
+ '.cm-searchMatch': {
84
+ backgroundColor: v('search-match-bg', 'var(--dk-highlight-bg)'),
85
+ outline: `1px solid ${v('search-match-outline', 'var(--dk-highlight-border)')}`,
86
+ },
87
+ '.cm-searchMatch.cm-searchMatch-selected': {
88
+ backgroundColor: v('search-match-selected-bg', 'var(--dk-accent-soft)'),
89
+ },
90
+ '.cm-tooltip': {
91
+ border: `1px solid ${border}`,
92
+ backgroundColor: v('tooltip-bg', surface),
93
+ color: v('tooltip-fg', fg),
94
+ },
95
+ '.cm-tooltip-autocomplete > ul > li[aria-selected]': {
96
+ backgroundColor: v('tooltip-selected-bg', 'var(--dk-accent-soft)'),
97
+ color: v('tooltip-selected-fg', fg),
98
+ },
99
+ '.cm-panels': { backgroundColor: surface, color: v('panel-fg', fg) },
100
+ '.cm-panels.cm-panels-top': { borderBottom: `1px solid ${border}` },
101
+ '.cm-panels.cm-panels-bottom': { borderTop: `1px solid ${border}` },
102
+ '.cm-textfield, .cm-button': {
103
+ border: `1px solid ${border}`,
104
+ borderRadius: '4px',
105
+ backgroundColor: bg,
106
+ backgroundImage: 'none',
107
+ color: fg,
108
+ },
109
+ });
110
+
111
+ /** Syntax colors: one variable per token group (README → Theming). */
112
+ export const editorHighlightStyle = HighlightStyle.define([
113
+ { tag: t.keyword, color: v('keyword', 'var(--dk-fg)') },
114
+ { tag: [t.self, t.null, t.bool, t.atom], color: v('constant', v('keyword', 'var(--dk-fg)')) },
115
+ { tag: [t.number, t.unit, t.color], color: v('number', 'var(--dk-fg)') },
116
+ { tag: [t.string, t.special(t.string), t.character], color: v('string', 'var(--dk-fg)') },
117
+ { tag: [t.regexp, t.escape], color: v('regexp', v('string', 'var(--dk-fg)')) },
118
+ {
119
+ tag: t.comment,
120
+ color: v('comment', 'var(--dk-faint)'),
121
+ fontStyle: v('comment-font-style', 'italic'),
122
+ },
123
+ { tag: [t.typeName, t.className, t.namespace], color: v('type', 'var(--dk-fg)') },
124
+ {
125
+ tag: [t.function(t.variableName), t.function(t.propertyName)],
126
+ color: v('function', 'var(--dk-fg)'),
127
+ },
128
+ { tag: t.definition(t.variableName), color: v('definition', v('function', 'var(--dk-fg)')) },
129
+ { tag: t.variableName, color: v('variable', fg) },
130
+ { tag: t.propertyName, color: v('property', 'var(--dk-fg)') },
131
+ { tag: t.tagName, color: v('tag', 'var(--dk-fg)') },
132
+ { tag: t.attributeName, color: v('attribute', 'var(--dk-fg)') },
133
+ { tag: t.operator, color: v('operator', fg) },
134
+ { tag: [t.punctuation, t.bracket, t.separator], color: v('punctuation', fg) },
135
+ { tag: t.meta, color: v('meta', v('comment', 'var(--dk-faint)')) },
136
+ { tag: t.invalid, color: v('invalid', 'var(--dk-error)') },
137
+ ]);
22
138
 
23
139
  function languageFor(fileName: string): Extension {
24
140
  if (/\.css$/.test(fileName)) return css();
@@ -47,7 +163,8 @@ export function createCodeEditor(
47
163
  closeBrackets(),
48
164
  autocompletion({ activateOnTyping: false }),
49
165
  highlightSelectionMatches(),
50
- syntaxHighlighting(classHighlighter),
166
+ editorTheme,
167
+ syntaxHighlighting(editorHighlightStyle),
51
168
  languageFor(options.fileName),
52
169
  keymap.of([...closeBracketsKeymap, ...defaultKeymap, ...searchKeymap, ...historyKeymap, indentWithTab]),
53
170
  EditorView.updateListener.of((update) => {
@@ -2,9 +2,34 @@
2
2
  * docs-kit demo cards. Plain CSS on custom properties: sites restyle by
3
3
  * overriding the --dk-* variables (light values on :root, dark ones under
4
4
  * [data-theme='dark']), or any .dk-* rule.
5
+ *
6
+ * The code editor reads --dk-editor-* variables (README → Theming). Only the
7
+ * syntax colors have defaults here; every other --dk-editor-* variable falls
8
+ * back to a card variable:
9
+ * --dk-editor-bg --dk-code-bg
10
+ * --dk-editor-fg --dk-fg
11
+ * --dk-editor-font-family --dk-font-mono
12
+ * --dk-editor-font-size --dk-code-font-size
13
+ * --dk-editor-line-height --dk-code-line-height
14
+ * --dk-editor-cursor --dk-editor-fg
15
+ * --dk-editor-selection-bg (set below)
16
+ * --dk-editor-active-line-bg --dk-accent-soft
17
+ * --dk-editor-gutter-bg --dk-editor-bg
18
+ * --dk-editor-gutter-fg --dk-line-number
19
+ * --dk-editor-gutter-border transparent
20
+ * --dk-editor-active-line-gutter-bg / -fg active line bg / gutter fg
21
+ * --dk-editor-selection-match-bg --dk-accent-soft
22
+ * --dk-editor-bracket-match-bg / -outline --dk-accent-soft / --dk-accent
23
+ * --dk-editor-search-match-bg / -outline --dk-highlight-bg / -border
24
+ * --dk-editor-search-match-selected-bg --dk-accent-soft
25
+ * --dk-editor-tooltip-bg / -fg --dk-editor-panel-bg / -fg
26
+ * --dk-editor-tooltip-selected-bg / -fg --dk-accent-soft / -fg
27
+ * --dk-editor-panel-bg / -fg --dk-surface / -fg (search panel)
28
+ * --dk-editor-border --dk-border
5
29
  */
6
30
  :root {
7
31
  --dk-font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
32
+ --dk-font-sans: system-ui, -apple-system, 'Segoe UI', sans-serif;
8
33
  --dk-radius: 12px;
9
34
  --dk-border: rgb(0 0 0 / 0.12);
10
35
  --dk-surface: #ffffff;
@@ -18,21 +43,29 @@
18
43
  --dk-accent-soft: rgb(9 105 218 / 0.1);
19
44
  --dk-highlight-bg: rgb(9 105 218 / 0.1);
20
45
  --dk-highlight-border: #0969da;
46
+ /* `#highlight label` pills, line numbers */
47
+ --dk-label-bg: rgb(9 105 218 / 0.12);
48
+ --dk-label-fg: #0550ae;
49
+ --dk-line-number: #818b98;
21
50
  --dk-error: #cf222e;
22
51
  --dk-ok: #1a7f37;
23
52
  --dk-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
24
53
  --dk-code-max-height: 560px;
25
- /* editor tokens */
26
- --dk-tok-keyword: #cf222e;
27
- --dk-tok-string: #0a3069;
28
- --dk-tok-number: #0550ae;
29
- --dk-tok-comment: #6e7781;
30
- --dk-tok-type: #953800;
31
- --dk-tok-function: #8250df;
32
- --dk-tok-property: #0550ae;
33
- --dk-tok-tag: #116329;
34
- --dk-tok-attribute: #0550ae;
35
- --dk-selection: rgb(9 105 218 / 0.2);
54
+ --dk-code-font-size: 13.5px;
55
+ --dk-code-line-height: 1.6;
56
+ /* editor syntax colors (github-light, like the static code) */
57
+ --dk-editor-keyword: #cf222e;
58
+ --dk-editor-constant: #0550ae;
59
+ --dk-editor-string: #0a3069;
60
+ --dk-editor-number: #0550ae;
61
+ --dk-editor-comment: #6e7781;
62
+ --dk-editor-type: #953800;
63
+ --dk-editor-function: #8250df;
64
+ --dk-editor-property: #0550ae;
65
+ --dk-editor-tag: #116329;
66
+ --dk-editor-attribute: #0550ae;
67
+ --dk-editor-operator: #cf222e;
68
+ --dk-editor-selection-bg: rgb(9 105 218 / 0.2);
36
69
  }
37
70
 
38
71
  [data-theme='dark'] {
@@ -48,30 +81,37 @@
48
81
  --dk-accent-soft: rgb(88 166 255 / 0.14);
49
82
  --dk-highlight-bg: rgb(122 162 247 / 0.14);
50
83
  --dk-highlight-border: #7aa2f7;
84
+ --dk-label-bg: rgb(122 162 247 / 0.28);
85
+ --dk-label-fg: #e3eaff;
86
+ --dk-line-number: #6e7681;
51
87
  --dk-error: #ff7b72;
52
88
  --dk-ok: #3fb950;
53
- --dk-tok-keyword: #bb9af7;
54
- --dk-tok-string: #9ece6a;
55
- --dk-tok-number: #ff9e64;
56
- --dk-tok-comment: #565f89;
57
- --dk-tok-type: #2ac3de;
58
- --dk-tok-function: #7aa2f7;
59
- --dk-tok-property: #73daca;
60
- --dk-tok-tag: #f7768e;
61
- --dk-tok-attribute: #e0af68;
62
- --dk-selection: rgb(122 162 247 / 0.25);
89
+ /* editor syntax colors (tokyo-night, like the static code) */
90
+ --dk-editor-keyword: #bb9af7;
91
+ --dk-editor-constant: #ff9e64;
92
+ --dk-editor-string: #9ece6a;
93
+ --dk-editor-number: #ff9e64;
94
+ --dk-editor-comment: #565f89;
95
+ --dk-editor-type: #2ac3de;
96
+ --dk-editor-function: #7aa2f7;
97
+ --dk-editor-property: #73daca;
98
+ --dk-editor-tag: #f7768e;
99
+ --dk-editor-attribute: #e0af68;
100
+ --dk-editor-operator: #89ddff;
101
+ --dk-editor-punctuation: #9abdf5;
102
+ --dk-editor-selection-bg: rgb(122 162 247 / 0.25);
63
103
  }
64
104
 
65
- /* shiki dual themes (defaultColor: false) */
105
+ /* shiki dual themes (defaultColor: false). Only the text color: the code
106
+ background is --dk-code-bg, and a background on the token spans would paint
107
+ boxes over highlighted lines. */
66
108
  .dk-demo .shiki,
67
109
  .dk-demo .shiki span {
68
110
  color: var(--shiki-light);
69
- background-color: var(--shiki-light-bg);
70
111
  }
71
112
  [data-theme='dark'] .dk-demo .shiki,
72
113
  [data-theme='dark'] .dk-demo .shiki span {
73
114
  color: var(--shiki-dark);
74
- background-color: var(--shiki-dark-bg);
75
115
  }
76
116
 
77
117
  .dk-demo {
@@ -274,8 +314,8 @@
274
314
  border-radius: 0;
275
315
  overflow: visible;
276
316
  font-family: var(--dk-font-mono);
277
- font-size: 13.5px;
278
- line-height: 1.6;
317
+ font-size: var(--dk-code-font-size);
318
+ line-height: var(--dk-code-line-height);
279
319
  tab-size: 2;
280
320
  background-color: var(--dk-code-bg) !important;
281
321
  }
@@ -296,13 +336,28 @@
296
336
  display: flex;
297
337
  gap: 16px;
298
338
  }
339
+ /* `#highlight label`: a pill at the end of the line, left out of text selections */
299
340
  .dk-demo pre.shiki .line-label {
341
+ align-self: center;
300
342
  margin-left: auto;
301
- padding-left: 16px;
343
+ padding: 0 8px;
344
+ border-radius: 999px;
345
+ background: var(--dk-label-bg);
346
+ color: var(--dk-label-fg);
347
+ font-family: var(--dk-font-sans);
302
348
  font-size: 12px;
303
- font-style: italic;
304
- color: var(--dk-accent);
349
+ font-weight: 500;
350
+ line-height: 1.6;
305
351
  white-space: nowrap;
352
+ user-select: none;
353
+ }
354
+ /* a label on a line of its own (`// #highlight label` above the code) */
355
+ .dk-demo pre.shiki .line.label-only {
356
+ gap: 0;
357
+ user-select: none;
358
+ }
359
+ .dk-demo pre.shiki .line.label-only .line-label {
360
+ margin-left: 0;
306
361
  }
307
362
  .dk-demo pre.shiki.has-line-numbers .line::before {
308
363
  content: attr(data-line);
@@ -310,7 +365,7 @@
310
365
  width: 2.5em;
311
366
  margin-right: 16px;
312
367
  text-align: right;
313
- color: var(--dk-faint);
368
+ color: var(--dk-line-number);
314
369
  user-select: none;
315
370
  }
316
371
  .dk-demo .dk-file-link {
@@ -370,87 +425,10 @@
370
425
  border-bottom: 1px solid var(--dk-border);
371
426
  }
372
427
  }
428
+ /* the editor's colors and font: --dk-editor-* (editor.ts) */
373
429
  .dk-demo-editor {
374
430
  height: 100%;
375
- background: var(--dk-code-bg);
376
- }
377
- .dk-demo-editor .cm-editor {
378
- height: 100%;
379
- font-size: 13.5px;
380
- background: var(--dk-code-bg);
381
- color: var(--dk-fg);
382
- }
383
- .dk-demo-editor .cm-editor.cm-focused {
384
- outline: none;
385
- }
386
- .dk-demo-editor .cm-scroller {
387
- font-family: var(--dk-font-mono);
388
- line-height: 1.6;
389
- }
390
- .dk-demo-editor .cm-gutters {
391
- border: 0;
392
- background: var(--dk-code-bg);
393
- color: var(--dk-faint);
394
- }
395
- .dk-demo-editor .cm-activeLine,
396
- .dk-demo-editor .cm-activeLineGutter {
397
- background: var(--dk-accent-soft);
398
- }
399
- .dk-demo-editor .cm-content {
400
- caret-color: var(--dk-fg);
401
- }
402
- .dk-demo-editor .cm-cursor {
403
- border-left-color: var(--dk-fg);
404
- }
405
- .dk-demo-editor .cm-selectionBackground,
406
- .dk-demo-editor .cm-focused .cm-selectionBackground,
407
- .dk-demo-editor ::selection {
408
- background: var(--dk-selection) !important;
409
- }
410
- .dk-demo-editor .cm-matchingBracket {
411
- background: var(--dk-accent-soft);
412
- outline: 1px solid var(--dk-accent);
413
- }
414
- .dk-demo-editor .cm-tooltip {
415
- border: 1px solid var(--dk-border);
416
- background: var(--dk-surface);
417
- color: var(--dk-fg);
418
- }
419
- .dk-demo-editor .tok-keyword,
420
- .dk-demo-editor .tok-operatorKeyword,
421
- .dk-demo-editor .tok-modifier {
422
- color: var(--dk-tok-keyword);
423
- }
424
- .dk-demo-editor .tok-string,
425
- .dk-demo-editor .tok-string2 {
426
- color: var(--dk-tok-string);
427
- }
428
- .dk-demo-editor .tok-number,
429
- .dk-demo-editor .tok-bool,
430
- .dk-demo-editor .tok-atom {
431
- color: var(--dk-tok-number);
432
- }
433
- .dk-demo-editor .tok-comment {
434
- color: var(--dk-tok-comment);
435
- font-style: italic;
436
- }
437
- .dk-demo-editor .tok-typeName,
438
- .dk-demo-editor .tok-className,
439
- .dk-demo-editor .tok-namespace {
440
- color: var(--dk-tok-type);
441
- }
442
- .dk-demo-editor .tok-variableName.tok-definition,
443
- .dk-demo-editor .tok-function {
444
- color: var(--dk-tok-function);
445
- }
446
- .dk-demo-editor .tok-propertyName {
447
- color: var(--dk-tok-property);
448
- }
449
- .dk-demo-editor .tok-tagName {
450
- color: var(--dk-tok-tag);
451
- }
452
- .dk-demo-editor .tok-attributeName {
453
- color: var(--dk-tok-attribute);
431
+ background: var(--dk-editor-bg, var(--dk-code-bg));
454
432
  }
455
433
  .dk-demo-editor-error {
456
434
  margin: 0;