@aceshooting/lyra-ui 1.2.0 → 2.0.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 +11 -0
- package/custom-elements.json +23341 -11202
- package/dist/components/app-rail/app-rail.d.ts +171 -0
- package/dist/components/app-rail/app-rail.js +483 -0
- package/dist/components/app-rail/app-rail.styles.d.ts +1 -0
- package/dist/components/app-rail/app-rail.styles.js +125 -0
- package/dist/components/attachment-chip/attachment-chip.d.ts +114 -0
- package/dist/components/attachment-chip/attachment-chip.js +312 -0
- package/dist/components/attachment-chip/attachment-chip.styles.d.ts +1 -0
- package/dist/components/attachment-chip/attachment-chip.styles.js +184 -0
- package/dist/components/attachment-trigger/attachment-trigger.d.ts +83 -0
- package/dist/components/attachment-trigger/attachment-trigger.js +258 -0
- package/dist/components/attachment-trigger/attachment-trigger.styles.d.ts +1 -0
- package/dist/components/attachment-trigger/attachment-trigger.styles.js +68 -0
- package/dist/components/chart/box-plot.js +11 -1
- package/dist/components/chart/chart-loader.d.ts +44 -13
- package/dist/components/chart/chart-loader.js +65 -18
- package/dist/components/chart/chart.d.ts +1 -2
- package/dist/components/chart/chart.js +67 -12
- package/dist/components/chart/histogram-bin.d.ts +4 -1
- package/dist/components/chart/histogram-bin.js +24 -14
- package/dist/components/chart/lite-chart.d.ts +54 -0
- package/dist/components/chart/lite-chart.js +140 -22
- package/dist/components/chart/lite-chart.styles.js +9 -0
- package/dist/components/chat-composer/chat-composer.d.ts +92 -0
- package/dist/components/chat-composer/chat-composer.js +285 -0
- package/dist/components/chat-composer/chat-composer.styles.d.ts +1 -0
- package/dist/components/chat-composer/chat-composer.styles.js +135 -0
- package/dist/components/chat-message/chat-message.d.ts +113 -0
- package/dist/components/chat-message/chat-message.js +291 -0
- package/dist/components/chat-message/chat-message.styles.d.ts +1 -0
- package/dist/components/chat-message/chat-message.styles.js +203 -0
- package/dist/components/checkbox/checkbox.d.ts +59 -0
- package/dist/components/checkbox/checkbox.js +230 -0
- package/dist/components/checkbox/checkbox.styles.d.ts +1 -0
- package/dist/components/checkbox/checkbox.styles.js +70 -0
- package/dist/components/chip/chip-group.d.ts +56 -0
- package/dist/components/chip/chip-group.js +124 -0
- package/dist/components/chip/chip-group.styles.d.ts +1 -0
- package/dist/components/chip/chip-group.styles.js +57 -0
- package/dist/components/chip/chip.d.ts +64 -0
- package/dist/components/chip/chip.js +115 -0
- package/dist/components/chip/chip.styles.d.ts +1 -0
- package/dist/components/chip/chip.styles.js +115 -0
- package/dist/components/citation-badge/citation-badge.d.ts +113 -0
- package/dist/components/citation-badge/citation-badge.js +300 -0
- package/dist/components/citation-badge/citation-badge.styles.d.ts +1 -0
- package/dist/components/citation-badge/citation-badge.styles.js +96 -0
- package/dist/components/code-block/code-block.d.ts +89 -0
- package/dist/components/code-block/code-block.js +317 -0
- package/dist/components/code-block/code-block.styles.d.ts +1 -0
- package/dist/components/code-block/code-block.styles.js +187 -0
- package/dist/components/code-block/code-loader.d.ts +44 -0
- package/dist/components/code-block/code-loader.js +79 -0
- package/dist/components/combobox/combobox.d.ts +19 -1
- package/dist/components/combobox/combobox.js +84 -14
- package/dist/components/combobox/option.d.ts +5 -1
- package/dist/components/combobox/option.js +35 -0
- package/dist/components/context-meter/context-meter.d.ts +53 -0
- package/dist/components/context-meter/context-meter.js +150 -0
- package/dist/components/context-meter/context-meter.styles.d.ts +1 -0
- package/dist/components/context-meter/context-meter.styles.js +120 -0
- package/dist/components/conversation-item/conversation-item.d.ts +128 -0
- package/dist/components/conversation-item/conversation-item.js +322 -0
- package/dist/components/conversation-item/conversation-item.styles.d.ts +1 -0
- package/dist/components/conversation-item/conversation-item.styles.js +155 -0
- package/dist/components/date-picker/date-input.d.ts +24 -0
- package/dist/components/date-picker/date-input.js +134 -22
- package/dist/components/date-picker/date-picker.d.ts +7 -0
- package/dist/components/date-picker/date-picker.js +85 -16
- package/dist/components/dialog/confirm.d.ts +40 -0
- package/dist/components/dialog/confirm.js +79 -0
- package/dist/components/dialog/dialog.d.ts +85 -0
- package/dist/components/dialog/dialog.js +272 -0
- package/dist/components/dialog/dialog.styles.d.ts +1 -0
- package/dist/components/dialog/dialog.styles.js +52 -0
- package/dist/components/dock-panel/dock-panel.d.ts +134 -0
- package/dist/components/dock-panel/dock-panel.js +345 -0
- package/dist/components/dock-panel/dock-panel.styles.d.ts +1 -0
- package/dist/components/dock-panel/dock-panel.styles.js +145 -0
- package/dist/components/document-preview/document-preview.d.ts +135 -0
- package/dist/components/document-preview/document-preview.js +372 -0
- package/dist/components/document-preview/document-preview.styles.d.ts +1 -0
- package/dist/components/document-preview/document-preview.styles.js +191 -0
- package/dist/components/export-button/csv.js +1 -1
- package/dist/components/export-button/export-button.js +2 -0
- package/dist/components/flag/flag.d.ts +29 -13
- package/dist/components/flag/flag.js +39 -13
- package/dist/components/gauge/gauge.js +19 -10
- package/dist/components/generation-status/generation-status.d.ts +109 -0
- package/dist/components/generation-status/generation-status.js +273 -0
- package/dist/components/generation-status/generation-status.styles.d.ts +1 -0
- package/dist/components/generation-status/generation-status.styles.js +84 -0
- package/dist/components/heatmap/calendar-grid.d.ts +4 -1
- package/dist/components/heatmap/calendar-grid.js +25 -12
- package/dist/components/heatmap/heatmap-scale.d.ts +6 -0
- package/dist/components/heatmap/heatmap-scale.js +7 -1
- package/dist/components/heatmap/heatmap.d.ts +74 -0
- package/dist/components/heatmap/heatmap.js +135 -23
- package/dist/components/json-viewer/json-viewer.d.ts +53 -0
- package/dist/components/json-viewer/json-viewer.js +283 -0
- package/dist/components/json-viewer/json-viewer.styles.d.ts +1 -0
- package/dist/components/json-viewer/json-viewer.styles.js +176 -0
- package/dist/components/kbd/kbd.d.ts +77 -0
- package/dist/components/kbd/kbd.js +214 -0
- package/dist/components/kbd/kbd.styles.d.ts +1 -0
- package/dist/components/kbd/kbd.styles.js +52 -0
- package/dist/components/live-region/live-region.d.ts +81 -0
- package/dist/components/live-region/live-region.js +153 -0
- package/dist/components/live-region/live-region.styles.d.ts +1 -0
- package/dist/components/live-region/live-region.styles.js +10 -0
- package/dist/components/map/map.d.ts +8 -6
- package/dist/components/map/map.js +44 -8
- package/dist/components/markdown/markdown-loader.d.ts +34 -0
- package/dist/components/markdown/markdown-loader.js +43 -0
- package/dist/components/markdown/markdown.d.ts +81 -0
- package/dist/components/markdown/markdown.js +284 -0
- package/dist/components/markdown/markdown.styles.d.ts +1 -0
- package/dist/components/markdown/markdown.styles.js +82 -0
- package/dist/components/media-card/media-card.d.ts +122 -0
- package/dist/components/media-card/media-card.js +295 -0
- package/dist/components/media-card/media-card.styles.d.ts +1 -0
- package/dist/components/media-card/media-card.styles.js +132 -0
- package/dist/components/mention-popover/mention-popover.d.ts +200 -0
- package/dist/components/mention-popover/mention-popover.js +475 -0
- package/dist/components/mention-popover/mention-popover.styles.d.ts +1 -0
- package/dist/components/mention-popover/mention-popover.styles.js +101 -0
- package/dist/components/menu/menu-item.d.ts +62 -0
- package/dist/components/menu/menu-item.js +119 -0
- package/dist/components/menu/menu-item.styles.d.ts +1 -0
- package/dist/components/menu/menu-item.styles.js +67 -0
- package/dist/components/menu/menu.d.ts +143 -0
- package/dist/components/menu/menu.js +365 -0
- package/dist/components/menu/menu.styles.d.ts +1 -0
- package/dist/components/menu/menu.styles.js +66 -0
- package/dist/components/model-select/model-select.d.ts +125 -0
- package/dist/components/model-select/model-select.js +490 -0
- package/dist/components/model-select/model-select.styles.d.ts +1 -0
- package/dist/components/model-select/model-select.styles.js +189 -0
- package/dist/components/model-settings-panel/model-settings-panel.d.ts +69 -0
- package/dist/components/model-settings-panel/model-settings-panel.js +145 -0
- package/dist/components/model-settings-panel/model-settings-panel.styles.d.ts +1 -0
- package/dist/components/model-settings-panel/model-settings-panel.styles.js +82 -0
- package/dist/components/playback/playback.d.ts +13 -1
- package/dist/components/playback/playback.js +47 -14
- package/dist/components/responsive-panel/responsive-panel.d.ts +150 -0
- package/dist/components/responsive-panel/responsive-panel.js +365 -0
- package/dist/components/responsive-panel/responsive-panel.styles.d.ts +1 -0
- package/dist/components/responsive-panel/responsive-panel.styles.js +103 -0
- package/dist/components/result-card/result-card.d.ts +44 -0
- package/dist/components/result-card/result-card.js +83 -0
- package/dist/components/result-card/result-card.styles.d.ts +1 -0
- package/dist/components/result-card/result-card.styles.js +50 -0
- package/dist/components/result-card/result-field.d.ts +36 -0
- package/dist/components/result-card/result-field.js +87 -0
- package/dist/components/result-card/result-field.styles.d.ts +1 -0
- package/dist/components/result-card/result-field.styles.js +32 -0
- package/dist/components/select/select.d.ts +43 -2
- package/dist/components/select/select.js +150 -17
- package/dist/components/slider/slider.d.ts +89 -0
- package/dist/components/slider/slider.js +352 -0
- package/dist/components/slider/slider.styles.d.ts +1 -0
- package/dist/components/slider/slider.styles.js +87 -0
- package/dist/components/source-card/source-card.d.ts +92 -0
- package/dist/components/source-card/source-card.js +148 -0
- package/dist/components/source-card/source-card.styles.d.ts +1 -0
- package/dist/components/source-card/source-card.styles.js +69 -0
- package/dist/components/source-list/source-list.d.ts +63 -0
- package/dist/components/source-list/source-list.js +122 -0
- package/dist/components/source-list/source-list.styles.d.ts +1 -0
- package/dist/components/source-list/source-list.styles.js +59 -0
- package/dist/components/sparkline/sparkline.js +27 -13
- package/dist/components/split/split.d.ts +93 -1
- package/dist/components/split/split.js +284 -15
- package/dist/components/split/split.styles.js +30 -0
- package/dist/components/stat/stat.d.ts +6 -1
- package/dist/components/stat/stat.js +8 -2
- package/dist/components/stream-status/stream-status.d.ts +110 -0
- package/dist/components/stream-status/stream-status.js +210 -0
- package/dist/components/stream-status/stream-status.styles.d.ts +1 -0
- package/dist/components/stream-status/stream-status.styles.js +103 -0
- package/dist/components/streaming-text/streaming-text.d.ts +98 -0
- package/dist/components/streaming-text/streaming-text.js +209 -0
- package/dist/components/streaming-text/streaming-text.styles.d.ts +1 -0
- package/dist/components/streaming-text/streaming-text.styles.js +62 -0
- package/dist/components/switch/switch.d.ts +58 -0
- package/dist/components/switch/switch.js +179 -0
- package/dist/components/switch/switch.styles.d.ts +1 -0
- package/dist/components/switch/switch.styles.js +75 -0
- package/dist/components/table/table.d.ts +73 -4
- package/dist/components/table/table.js +169 -20
- package/dist/components/table/table.styles.js +6 -1
- package/dist/components/tabs/tabs.d.ts +51 -0
- package/dist/components/tabs/tabs.js +190 -0
- package/dist/components/tabs/tabs.styles.d.ts +1 -0
- package/dist/components/tabs/tabs.styles.js +63 -0
- package/dist/components/thinking-panel/thinking-panel.d.ts +121 -0
- package/dist/components/thinking-panel/thinking-panel.js +235 -0
- package/dist/components/thinking-panel/thinking-panel.styles.d.ts +1 -0
- package/dist/components/thinking-panel/thinking-panel.styles.js +117 -0
- package/dist/components/toast/toast-item.d.ts +1 -0
- package/dist/components/toast/toast-item.js +31 -6
- package/dist/components/tool-approval-dialog/tool-approval-dialog.d.ts +130 -0
- package/dist/components/tool-approval-dialog/tool-approval-dialog.js +403 -0
- package/dist/components/tool-approval-dialog/tool-approval-dialog.styles.d.ts +1 -0
- package/dist/components/tool-approval-dialog/tool-approval-dialog.styles.js +160 -0
- package/dist/components/tool-call-chip/tool-call-chip.d.ts +89 -0
- package/dist/components/tool-call-chip/tool-call-chip.js +288 -0
- package/dist/components/tool-call-chip/tool-call-chip.styles.d.ts +1 -0
- package/dist/components/tool-call-chip/tool-call-chip.styles.js +203 -0
- package/dist/components/tool-param-form/tool-param-form.d.ts +145 -0
- package/dist/components/tool-param-form/tool-param-form.js +366 -0
- package/dist/components/tool-param-form/tool-param-form.styles.d.ts +1 -0
- package/dist/components/tool-param-form/tool-param-form.styles.js +75 -0
- package/dist/components/tool-result-dialog/tool-result-dialog.d.ts +102 -0
- package/dist/components/tool-result-dialog/tool-result-dialog.js +393 -0
- package/dist/components/tool-result-dialog/tool-result-dialog.styles.d.ts +1 -0
- package/dist/components/tool-result-dialog/tool-result-dialog.styles.js +176 -0
- package/dist/components/tool-result-view/registry.d.ts +81 -0
- package/dist/components/tool-result-view/registry.js +84 -0
- package/dist/components/tool-result-view/tool-result-view.d.ts +55 -0
- package/dist/components/tool-result-view/tool-result-view.js +138 -0
- package/dist/components/tool-result-view/tool-result-view.styles.d.ts +1 -0
- package/dist/components/tool-result-view/tool-result-view.styles.js +13 -0
- package/dist/components/tool-select-dialog/tool-select-dialog.d.ts +151 -0
- package/dist/components/tool-select-dialog/tool-select-dialog.js +439 -0
- package/dist/components/tool-select-dialog/tool-select-dialog.styles.d.ts +1 -0
- package/dist/components/tool-select-dialog/tool-select-dialog.styles.js +182 -0
- package/dist/components/tree/tree-node.d.ts +11 -2
- package/dist/components/tree/tree-node.js +16 -6
- package/dist/components/tree/tree.d.ts +8 -2
- package/dist/components/tree/tree.js +18 -11
- package/dist/components/typing-indicator/typing-indicator.d.ts +60 -0
- package/dist/components/typing-indicator/typing-indicator.js +96 -0
- package/dist/components/typing-indicator/typing-indicator.styles.d.ts +1 -0
- package/dist/components/typing-indicator/typing-indicator.styles.js +132 -0
- package/dist/components/virtual-list/virtual-list.d.ts +173 -0
- package/dist/components/virtual-list/virtual-list.js +461 -0
- package/dist/components/virtual-list/virtual-list.styles.d.ts +1 -0
- package/dist/components/virtual-list/virtual-list.styles.js +44 -0
- package/dist/components/word-cloud/word-cloud-layout.js +14 -1
- package/dist/components/word-cloud/word-cloud.d.ts +6 -0
- package/dist/components/word-cloud/word-cloud.js +25 -14
- package/dist/components/word-cloud/word-cloud.styles.js +3 -2
- package/dist/internal/announcer.d.ts +57 -0
- package/dist/internal/announcer.js +71 -0
- package/dist/internal/form-associated.d.ts +1 -0
- package/dist/internal/form-associated.js +55 -11
- package/dist/internal/group-by-recency.d.ts +50 -0
- package/dist/internal/group-by-recency.js +80 -0
- package/dist/internal/rtl.d.ts +23 -0
- package/dist/internal/rtl.js +28 -0
- package/dist/internal/scroll-lock.d.ts +10 -5
- package/dist/internal/scroll-lock.js +30 -14
- package/dist/internal/tokens.styles.js +6 -0
- package/dist/lyra.d.ts +130 -31
- package/dist/lyra.js +94 -26
- package/llms-full.txt +3836 -17
- package/llms.txt +16 -1
- package/package.json +23 -5
- package/dist/components/chart/bar-chart.stories.d.ts +0 -5
- package/dist/components/chart/bar-chart.stories.js +0 -20
- package/dist/components/chart/box-plot.stories.d.ts +0 -5
- package/dist/components/chart/box-plot.stories.js +0 -29
- package/dist/components/chart/bubble-chart.stories.d.ts +0 -5
- package/dist/components/chart/bubble-chart.stories.js +0 -26
- package/dist/components/chart/chart.stories.d.ts +0 -32
- package/dist/components/chart/chart.stories.js +0 -141
- package/dist/components/chart/doughnut-chart.stories.d.ts +0 -5
- package/dist/components/chart/doughnut-chart.stories.js +0 -26
- package/dist/components/chart/histogram.stories.d.ts +0 -5
- package/dist/components/chart/histogram.stories.js +0 -17
- package/dist/components/chart/line-chart.stories.d.ts +0 -22
- package/dist/components/chart/line-chart.stories.js +0 -99
- package/dist/components/chart/lite-chart.stories.d.ts +0 -14
- package/dist/components/chart/lite-chart.stories.js +0 -114
- package/dist/components/chart/pie-chart.stories.d.ts +0 -5
- package/dist/components/chart/pie-chart.stories.js +0 -26
- package/dist/components/chart/polar-area-chart.stories.d.ts +0 -5
- package/dist/components/chart/polar-area-chart.stories.js +0 -20
- package/dist/components/chart/radar-chart.stories.d.ts +0 -5
- package/dist/components/chart/radar-chart.stories.js +0 -20
- package/dist/components/chart/scatter-chart.stories.d.ts +0 -5
- package/dist/components/chart/scatter-chart.stories.js +0 -31
- package/dist/components/combobox/combobox.stories.d.ts +0 -24
- package/dist/components/combobox/combobox.stories.js +0 -102
- package/dist/components/date-picker/date-input.stories.d.ts +0 -10
- package/dist/components/date-picker/date-input.stories.js +0 -48
- package/dist/components/date-picker/date-picker.stories.d.ts +0 -10
- package/dist/components/date-picker/date-picker.stories.js +0 -25
- package/dist/components/empty/empty.stories.d.ts +0 -7
- package/dist/components/empty/empty.stories.js +0 -34
- package/dist/components/export-button/export-button.stories.d.ts +0 -7
- package/dist/components/export-button/export-button.stories.js +0 -37
- package/dist/components/file-input/file-input.stories.d.ts +0 -7
- package/dist/components/file-input/file-input.stories.js +0 -19
- package/dist/components/flag/flag.stories.d.ts +0 -6
- package/dist/components/flag/flag.stories.js +0 -31
- package/dist/components/gauge/gauge.stories.d.ts +0 -8
- package/dist/components/gauge/gauge.stories.js +0 -19
- package/dist/components/graph/graph.stories.d.ts +0 -8
- package/dist/components/graph/graph.stories.js +0 -68
- package/dist/components/heatmap/heatmap.stories.d.ts +0 -27
- package/dist/components/heatmap/heatmap.stories.js +0 -179
- package/dist/components/map/map.stories.d.ts +0 -15
- package/dist/components/map/map.stories.js +0 -135
- package/dist/components/playback/playback.stories.d.ts +0 -7
- package/dist/components/playback/playback.stories.js +0 -16
- package/dist/components/select/select.stories.d.ts +0 -21
- package/dist/components/select/select.stories.js +0 -91
- package/dist/components/skeleton/skeleton.stories.d.ts +0 -5
- package/dist/components/skeleton/skeleton.stories.js +0 -16
- package/dist/components/sparkline/sparkline.stories.d.ts +0 -11
- package/dist/components/sparkline/sparkline.stories.js +0 -39
- package/dist/components/split/split.stories.d.ts +0 -7
- package/dist/components/split/split.stories.js +0 -36
- package/dist/components/stat/stat.stories.d.ts +0 -9
- package/dist/components/stat/stat.stories.js +0 -81
- package/dist/components/table/table.stories.d.ts +0 -12
- package/dist/components/table/table.stories.js +0 -67
- package/dist/components/time-range/time-range.stories.d.ts +0 -8
- package/dist/components/time-range/time-range.stories.js +0 -26
- package/dist/components/toast/toast.stories.d.ts +0 -8
- package/dist/components/toast/toast.stories.js +0 -72
- package/dist/components/tree/tree.stories.d.ts +0 -9
- package/dist/components/tree/tree.stories.js +0 -52
- package/dist/components/widget/widget.stories.d.ts +0 -9
- package/dist/components/widget/widget.stories.js +0 -74
- package/dist/components/word-cloud/word-cloud.stories.d.ts +0 -10
- package/dist/components/word-cloud/word-cloud.stories.js +0 -55
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { html, nothing } from 'lit';
|
|
8
|
+
import { property, state } from 'lit/decorators.js';
|
|
9
|
+
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
|
|
10
|
+
import { LyraElement } from '../../internal/lyra-element.js';
|
|
11
|
+
import { defineElement } from '../../internal/prefix.js';
|
|
12
|
+
import { nextId } from '../../internal/a11y.js';
|
|
13
|
+
import { chevronIcon } from '../../internal/icons.js';
|
|
14
|
+
import { loadShikiHighlighter, loadShikiLanguage, SHIKI_THEMES } from './code-loader.js';
|
|
15
|
+
import { styles } from './code-block.styles.js';
|
|
16
|
+
import '../skeleton/skeleton.js';
|
|
17
|
+
/** How long the copy button's confirmation state lasts before reverting. */
|
|
18
|
+
const COPY_CONFIRM_MS = 1500;
|
|
19
|
+
/**
|
|
20
|
+
* Rewrites shiki's generated `<pre>`/`<code>` hast nodes so they carry this
|
|
21
|
+
* component's own `part="pre"`/`part="code"` hooks — the same single-pass
|
|
22
|
+
* "inject part attributes while rendering, not a second DOM walk after
|
|
23
|
+
* insertion" approach `<lyra-markdown>`'s renderer override uses, applied
|
|
24
|
+
* here via shiki's own transformer API instead of a custom marked renderer.
|
|
25
|
+
* Also strips shiki's own default `tabindex="0"` from the `<pre>` — this
|
|
26
|
+
* component's `[part="body"]` wrapper (see `render()`) is the single
|
|
27
|
+
* scrollable/focusable region for the code area, and leaving shiki's own
|
|
28
|
+
* tabindex in place would add a second, redundant tab stop over the same
|
|
29
|
+
* scrollable content.
|
|
30
|
+
*/
|
|
31
|
+
const partTransformer = {
|
|
32
|
+
name: 'lyra-code-block-parts',
|
|
33
|
+
pre(node) {
|
|
34
|
+
node.properties.part = ['pre'];
|
|
35
|
+
delete node.properties.tabindex;
|
|
36
|
+
},
|
|
37
|
+
code(node) {
|
|
38
|
+
node.properties.part = ['code'];
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* `<lyra-code-block>` — fenced code display with optional lazy syntax
|
|
43
|
+
* highlighting and a copy button. No highlighting grammar ships in this
|
|
44
|
+
* component itself: it lazy-loads the optional peer dependency `shiki` (see
|
|
45
|
+
* `code-loader.ts`) for the actual tokenizing, and degrades to a plain
|
|
46
|
+
* `<pre><code>` when that peer isn't installed or `language` is unset/
|
|
47
|
+
* unrecognized — the exact same optional-peer shape `<lyra-markdown>` and
|
|
48
|
+
* `<lyra-chart>` already establish. That fallback is the *default* rendering
|
|
49
|
+
* path, not a degraded one: unhighlighted code is perfectly usable, and it's
|
|
50
|
+
* what every instance renders at zero extra bytes until shiki resolves.
|
|
51
|
+
*
|
|
52
|
+
* A `<lyra-skeleton>` placeholder stands in only while shiki itself is
|
|
53
|
+
* loading for the very first time on the page (cached — see
|
|
54
|
+
* `loadShikiHighlighter()`) and `language` is set. It's deliberately *not*
|
|
55
|
+
* shown again for a subsequent per-language grammar load (e.g. a second
|
|
56
|
+
* `<lyra-code-block>` requesting a language no earlier instance has used
|
|
57
|
+
* yet) — that grammar fetch is typically fast, and the plain-text fallback
|
|
58
|
+
* is already a perfectly readable placeholder for it, so a second
|
|
59
|
+
* loading-chrome state would add complexity for little practical benefit.
|
|
60
|
+
*
|
|
61
|
+
* @customElement lyra-code-block
|
|
62
|
+
* @event lyra-copy - The copy button was activated. `detail: { text }` is
|
|
63
|
+
* always the raw `code` value (never the highlighted HTML), and always
|
|
64
|
+
* fires regardless of whether the actual OS clipboard write succeeded —
|
|
65
|
+
* same convention as `<lyra-json-viewer>`'s own copy button.
|
|
66
|
+
* @csspart base - The outer container.
|
|
67
|
+
* @csspart header - The row above the code (filename/language/copy/toggle),
|
|
68
|
+
* present whenever there's anything to put in it.
|
|
69
|
+
* @csspart filename - The `filename` text, when set.
|
|
70
|
+
* @csspart language - The `language` badge, when set, so the language is
|
|
71
|
+
* exposed to assistive tech as visible text rather than only a `language`
|
|
72
|
+
* attribute a screen reader would never announce.
|
|
73
|
+
* @csspart copy-button - The copy-to-clipboard button, when `copyable`.
|
|
74
|
+
* @csspart toggle - The collapse/expand chevron button, when `collapsible`.
|
|
75
|
+
* @csspart body - The scrollable region wrapping the code (or the loading
|
|
76
|
+
* skeleton); respects `max-height`, `hidden` while `collapsible` and
|
|
77
|
+
* `collapsed`.
|
|
78
|
+
* @csspart pre - The rendered `<pre>` — shiki's own in the highlighted path,
|
|
79
|
+
* this component's own plain one in the fallback path.
|
|
80
|
+
* @csspart code - The rendered `<code>`, same split as `pre` above.
|
|
81
|
+
*/
|
|
82
|
+
export class LyraCodeBlock extends LyraElement {
|
|
83
|
+
constructor() {
|
|
84
|
+
super(...arguments);
|
|
85
|
+
/** The raw source text. */
|
|
86
|
+
this.code = '';
|
|
87
|
+
/** A shiki-recognized language id or alias (e.g. `"javascript"`,
|
|
88
|
+
* `"python"`, `"json"`). When unset, or when shiki doesn't recognize it,
|
|
89
|
+
* the code renders as plain unhighlighted text regardless of whether
|
|
90
|
+
* shiki itself is available. */
|
|
91
|
+
this.language = '';
|
|
92
|
+
/** Shown in the header above the code, when set. */
|
|
93
|
+
this.filename = '';
|
|
94
|
+
/** Whether the code region can be collapsed via a header toggle. */
|
|
95
|
+
this.collapsible = false;
|
|
96
|
+
/** Whether the code region is currently hidden. Only has a visible effect
|
|
97
|
+
* while `collapsible` is also true. */
|
|
98
|
+
this.collapsed = false;
|
|
99
|
+
/** Shows a copy-to-clipboard button in the header. */
|
|
100
|
+
this.copyable = true;
|
|
101
|
+
/** A CSS length (e.g. `"20rem"`); once set, the code scrolls internally
|
|
102
|
+
* past this height instead of growing the page. */
|
|
103
|
+
this.maxHeight = '';
|
|
104
|
+
// `null` covers every reason the plain-text fallback is showing: shiki
|
|
105
|
+
// isn't installed, `language` is unset, or `language` isn't shiki-
|
|
106
|
+
// recognized -- `render()` doesn't need to (and can't usefully) tell these
|
|
107
|
+
// apart, same rationale as <lyra-markdown>'s identically-shaped field.
|
|
108
|
+
this.highlightedHtml = null;
|
|
109
|
+
// Becomes true once the shared shiki-loading promise has settled, whether
|
|
110
|
+
// or not it actually resolved to a highlighter -- gates the skeleton (see
|
|
111
|
+
// the class doc's second paragraph), not the highlighting itself.
|
|
112
|
+
this.shikiReady = false;
|
|
113
|
+
this.justCopied = false;
|
|
114
|
+
// Guards the async per-language load in syncHighlight() against a
|
|
115
|
+
// `code`/`language` change that arrives before a previous load resolves --
|
|
116
|
+
// only the result matching the *current* token is ever applied.
|
|
117
|
+
this.highlightToken = 0;
|
|
118
|
+
this.bodyId = nextId('code-block-body');
|
|
119
|
+
this.copy = () => {
|
|
120
|
+
this.writeClipboard(this.code);
|
|
121
|
+
this.emit('lyra-copy', { text: this.code });
|
|
122
|
+
this.justCopied = true;
|
|
123
|
+
clearTimeout(this.copyTimeoutId);
|
|
124
|
+
this.copyTimeoutId = setTimeout(() => {
|
|
125
|
+
this.justCopied = false;
|
|
126
|
+
}, COPY_CONFIRM_MS);
|
|
127
|
+
};
|
|
128
|
+
this.toggleCollapsed = () => {
|
|
129
|
+
this.collapsed = !this.collapsed;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
static { this.styles = [LyraElement.styles, styles]; }
|
|
133
|
+
connectedCallback() {
|
|
134
|
+
super.connectedCallback();
|
|
135
|
+
void loadShikiHighlighter().then((hl) => {
|
|
136
|
+
this.highlighter = hl;
|
|
137
|
+
this.shikiReady = true;
|
|
138
|
+
this.syncHighlight();
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
disconnectedCallback() {
|
|
142
|
+
super.disconnectedCallback();
|
|
143
|
+
clearTimeout(this.copyTimeoutId);
|
|
144
|
+
}
|
|
145
|
+
// Mutating `highlightedHtml` here (rather than in `updated()`) absorbs the
|
|
146
|
+
// synchronous case -- language already loaded, see `syncHighlight()` --
|
|
147
|
+
// into this same update cycle instead of scheduling a second one, Lit's
|
|
148
|
+
// documented pattern for deriving one reactive property from a change to
|
|
149
|
+
// others (same approach <lyra-markdown>'s `willUpdate` takes).
|
|
150
|
+
willUpdate(changed) {
|
|
151
|
+
if (this.shikiReady && (changed.has('code') || changed.has('language'))) {
|
|
152
|
+
this.syncHighlight();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
updated() {
|
|
156
|
+
const showingSkeleton = !this.shikiReady && !!this.language;
|
|
157
|
+
if (showingSkeleton)
|
|
158
|
+
this.setAttribute('aria-busy', 'true');
|
|
159
|
+
else
|
|
160
|
+
this.removeAttribute('aria-busy');
|
|
161
|
+
}
|
|
162
|
+
syncHighlight() {
|
|
163
|
+
const hl = this.highlighter;
|
|
164
|
+
const lang = this.language;
|
|
165
|
+
if (!hl || !lang) {
|
|
166
|
+
this.highlightedHtml = null;
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (hl.getLoadedLanguages().includes(lang)) {
|
|
170
|
+
this.highlightedHtml = this.tokenize(hl, lang);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
// Grammar not loaded yet -- show the plain-text fallback in the
|
|
174
|
+
// meantime rather than leaving a *previous* code/language value's stale
|
|
175
|
+
// highlighted markup on screen while this one loads.
|
|
176
|
+
this.highlightedHtml = null;
|
|
177
|
+
const token = ++this.highlightToken;
|
|
178
|
+
void loadShikiLanguage(hl, lang).then((ok) => {
|
|
179
|
+
if (token !== this.highlightToken)
|
|
180
|
+
return; // superseded by a newer code/language change
|
|
181
|
+
this.highlightedHtml = ok ? this.tokenize(hl, lang) : null;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
tokenize(hl, lang) {
|
|
185
|
+
try {
|
|
186
|
+
return hl.codeToHtml(this.code, {
|
|
187
|
+
lang,
|
|
188
|
+
// shiki's generated per-token colors are theme-specific inline
|
|
189
|
+
// styles/CSS variables (from SHIKI_THEMES's github-light/github-dark
|
|
190
|
+
// theme data), not this library's --lyra-* design tokens -- the one
|
|
191
|
+
// deliberate exception to every other color in this component being
|
|
192
|
+
// a --lyra-* token. See the dark-mode override in
|
|
193
|
+
// code-block.styles.ts for how the dark half of this activates.
|
|
194
|
+
themes: SHIKI_THEMES,
|
|
195
|
+
transformers: [partTransformer],
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
// Malformed input for this grammar, or any other shiki-internal
|
|
200
|
+
// failure -- fall back to plain text rather than a blank code block.
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
writeClipboard(text) {
|
|
205
|
+
try {
|
|
206
|
+
// navigator.clipboard is absent in insecure contexts / older browsers,
|
|
207
|
+
// and some engines throw synchronously rather than rejecting -- either
|
|
208
|
+
// way this is best-effort; copy() below always emits lyra-copy
|
|
209
|
+
// regardless of whether the OS clipboard was actually reached. Same
|
|
210
|
+
// precedent as <lyra-json-viewer>'s own copy button.
|
|
211
|
+
void navigator.clipboard?.writeText(text)?.catch(() => { });
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
// see above
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
renderHeader() {
|
|
218
|
+
return html `
|
|
219
|
+
<div part="header">
|
|
220
|
+
${this.collapsible
|
|
221
|
+
? html `
|
|
222
|
+
<button
|
|
223
|
+
part="toggle"
|
|
224
|
+
type="button"
|
|
225
|
+
aria-expanded=${this.collapsed ? 'false' : 'true'}
|
|
226
|
+
aria-controls=${this.bodyId}
|
|
227
|
+
aria-label=${this.collapsed ? 'Expand code' : 'Collapse code'}
|
|
228
|
+
@click=${this.toggleCollapsed}
|
|
229
|
+
>
|
|
230
|
+
<span class="chevron" aria-hidden="true">${chevronIcon()}</span>
|
|
231
|
+
</button>
|
|
232
|
+
`
|
|
233
|
+
: nothing}
|
|
234
|
+
${this.filename ? html `<span part="filename">${this.filename}</span>` : nothing}
|
|
235
|
+
${this.language ? html `<span part="language">${this.language}</span>` : nothing}
|
|
236
|
+
${this.copyable
|
|
237
|
+
? html `
|
|
238
|
+
<button
|
|
239
|
+
part="copy-button"
|
|
240
|
+
type="button"
|
|
241
|
+
aria-label=${this.justCopied ? 'Copied to clipboard' : 'Copy code'}
|
|
242
|
+
@click=${this.copy}
|
|
243
|
+
>
|
|
244
|
+
${this.justCopied ? 'Copied!' : 'Copy'}
|
|
245
|
+
</button>
|
|
246
|
+
`
|
|
247
|
+
: nothing}
|
|
248
|
+
</div>
|
|
249
|
+
`;
|
|
250
|
+
}
|
|
251
|
+
// `[part="body"]` is `tabindex="0"` because it's the scrollable region for
|
|
252
|
+
// arbitrarily-wide/tall code (max-height, plus horizontal overflow from
|
|
253
|
+
// long lines) -- a scrollable region with no other focusable content of
|
|
254
|
+
// its own is otherwise unreachable by keyboard, same reasoning
|
|
255
|
+
// lyra-virtual-list's own `[part="base"]` documents for its identical
|
|
256
|
+
// tabindex. `role="group"` (rather than e.g. `region`, a page landmark
|
|
257
|
+
// role) plus `aria-label` gives it an accessible name without claiming
|
|
258
|
+
// landmark/navigation significance for what is, structurally, just one
|
|
259
|
+
// small piece of a larger document.
|
|
260
|
+
render() {
|
|
261
|
+
const hasHeader = !!this.filename || !!this.language || this.copyable || this.collapsible;
|
|
262
|
+
const showSkeleton = !this.shikiReady && !!this.language;
|
|
263
|
+
const bodyHidden = this.collapsible && this.collapsed;
|
|
264
|
+
const bodyLabel = this.filename || (this.language ? `${this.language} code` : 'Code');
|
|
265
|
+
return html `
|
|
266
|
+
<div part="base">
|
|
267
|
+
${hasHeader ? this.renderHeader() : nothing}
|
|
268
|
+
<div
|
|
269
|
+
part="body"
|
|
270
|
+
id=${this.bodyId}
|
|
271
|
+
role="group"
|
|
272
|
+
aria-label=${bodyLabel}
|
|
273
|
+
tabindex="0"
|
|
274
|
+
?hidden=${bodyHidden}
|
|
275
|
+
style=${this.maxHeight ? `--lyra-code-block-max-height:${this.maxHeight}` : nothing}
|
|
276
|
+
>
|
|
277
|
+
${showSkeleton
|
|
278
|
+
? html `<lyra-skeleton variant="rect"></lyra-skeleton>`
|
|
279
|
+
: this.highlightedHtml !== null
|
|
280
|
+
? unsafeHTML(this.highlightedHtml)
|
|
281
|
+
: html `<pre part="pre"><code part="code">${this.code}</code></pre>`}
|
|
282
|
+
</div>
|
|
283
|
+
</div>
|
|
284
|
+
`;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
__decorate([
|
|
288
|
+
property()
|
|
289
|
+
], LyraCodeBlock.prototype, "code", void 0);
|
|
290
|
+
__decorate([
|
|
291
|
+
property()
|
|
292
|
+
], LyraCodeBlock.prototype, "language", void 0);
|
|
293
|
+
__decorate([
|
|
294
|
+
property()
|
|
295
|
+
], LyraCodeBlock.prototype, "filename", void 0);
|
|
296
|
+
__decorate([
|
|
297
|
+
property({ type: Boolean, reflect: true })
|
|
298
|
+
], LyraCodeBlock.prototype, "collapsible", void 0);
|
|
299
|
+
__decorate([
|
|
300
|
+
property({ type: Boolean, reflect: true })
|
|
301
|
+
], LyraCodeBlock.prototype, "collapsed", void 0);
|
|
302
|
+
__decorate([
|
|
303
|
+
property({ type: Boolean, reflect: true })
|
|
304
|
+
], LyraCodeBlock.prototype, "copyable", void 0);
|
|
305
|
+
__decorate([
|
|
306
|
+
property({ attribute: 'max-height' })
|
|
307
|
+
], LyraCodeBlock.prototype, "maxHeight", void 0);
|
|
308
|
+
__decorate([
|
|
309
|
+
state()
|
|
310
|
+
], LyraCodeBlock.prototype, "highlightedHtml", void 0);
|
|
311
|
+
__decorate([
|
|
312
|
+
state()
|
|
313
|
+
], LyraCodeBlock.prototype, "shikiReady", void 0);
|
|
314
|
+
__decorate([
|
|
315
|
+
state()
|
|
316
|
+
], LyraCodeBlock.prototype, "justCopied", void 0);
|
|
317
|
+
defineElement('code-block', LyraCodeBlock);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const styles: import("lit").CSSResult;
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { css } from 'lit';
|
|
2
|
+
export const styles = css `
|
|
3
|
+
:host {
|
|
4
|
+
display: block;
|
|
5
|
+
/* Consumer-tunable scroll cap -- 'none' means the block grows with its
|
|
6
|
+
content, matching every other block-level component in this library
|
|
7
|
+
until a caller opts in via the max-height attribute (same convention
|
|
8
|
+
as --lyra-json-viewer-max-height). */
|
|
9
|
+
--lyra-code-block-max-height: none;
|
|
10
|
+
/* Contained here rather than left as a bare font-family literal so a
|
|
11
|
+
host page can retheme it -- same rationale as --lyra-markdown-font-mono
|
|
12
|
+
and --lyra-json-viewer-font, no shared --wa-*/--lyra-* monospace token
|
|
13
|
+
exists to resolve through. */
|
|
14
|
+
--lyra-code-block-font: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
15
|
+
font-size: 0.8125rem;
|
|
16
|
+
}
|
|
17
|
+
[part='base'] {
|
|
18
|
+
border: 1px solid var(--lyra-color-border);
|
|
19
|
+
border-radius: var(--lyra-radius);
|
|
20
|
+
background: var(--lyra-color-surface);
|
|
21
|
+
overflow: hidden;
|
|
22
|
+
}
|
|
23
|
+
[part='header'] {
|
|
24
|
+
display: flex;
|
|
25
|
+
align-items: center;
|
|
26
|
+
gap: var(--lyra-space-xs);
|
|
27
|
+
padding: var(--lyra-space-xs) var(--lyra-space-s);
|
|
28
|
+
border-block-end: 1px solid var(--lyra-color-border);
|
|
29
|
+
/* --lyra-color-surface (not -brand-quiet) -- pairing
|
|
30
|
+
--lyra-color-text-quiet (the toggle/copy-button color below) against
|
|
31
|
+
--lyra-color-brand-quiet fails WCAG AA contrast in this token
|
|
32
|
+
palette; --lyra-color-surface is the same header background
|
|
33
|
+
lyra-json-viewer's own toolbar already uses with that same text
|
|
34
|
+
color. */
|
|
35
|
+
background: var(--lyra-color-surface);
|
|
36
|
+
font-family: var(--lyra-font);
|
|
37
|
+
}
|
|
38
|
+
[part='toggle'] {
|
|
39
|
+
/* Deliberately smaller than the shared --lyra-icon-button-size (2.5rem,
|
|
40
|
+
meant for a standalone icon-only button) -- this sits inline in a
|
|
41
|
+
dense header row, same reasoning as lyra-json-viewer's own toggle. */
|
|
42
|
+
inline-size: 1.25rem;
|
|
43
|
+
block-size: 1.25rem;
|
|
44
|
+
flex: 0 0 auto;
|
|
45
|
+
display: inline-flex;
|
|
46
|
+
align-items: center;
|
|
47
|
+
justify-content: center;
|
|
48
|
+
padding: 0;
|
|
49
|
+
border: none;
|
|
50
|
+
background: none;
|
|
51
|
+
color: var(--lyra-color-text-quiet);
|
|
52
|
+
border-radius: var(--lyra-radius);
|
|
53
|
+
cursor: pointer;
|
|
54
|
+
}
|
|
55
|
+
[part='toggle']:hover {
|
|
56
|
+
background: var(--lyra-color-brand-quiet);
|
|
57
|
+
color: var(--lyra-color-brand);
|
|
58
|
+
}
|
|
59
|
+
[part='toggle'] .chevron {
|
|
60
|
+
display: inline-flex;
|
|
61
|
+
transition: transform var(--lyra-transition-fast);
|
|
62
|
+
}
|
|
63
|
+
/* Chevron points at the content: rotated (pointing down) while expanded,
|
|
64
|
+
resting (pointing right) while collapsed -- same rotation direction as
|
|
65
|
+
lyra-thinking-panel's and lyra-json-viewer's own toggles. */
|
|
66
|
+
:host(:not([collapsed])) [part='toggle'] .chevron {
|
|
67
|
+
transform: rotate(90deg);
|
|
68
|
+
}
|
|
69
|
+
[part='filename'] {
|
|
70
|
+
flex: 1 1 auto;
|
|
71
|
+
min-inline-size: 0;
|
|
72
|
+
overflow: hidden;
|
|
73
|
+
text-overflow: ellipsis;
|
|
74
|
+
white-space: nowrap;
|
|
75
|
+
font-family: var(--lyra-code-block-font);
|
|
76
|
+
font-size: 0.75rem;
|
|
77
|
+
font-weight: 600;
|
|
78
|
+
color: var(--lyra-color-text);
|
|
79
|
+
}
|
|
80
|
+
[part='language'] {
|
|
81
|
+
flex: 0 0 auto;
|
|
82
|
+
padding: 0.0625rem 0.4375rem;
|
|
83
|
+
border-radius: 999px;
|
|
84
|
+
/* --lyra-color-brand + -brand-quiet (not -text-quiet + -surface) --
|
|
85
|
+
this pill needs to read as distinct from the [part="header"]
|
|
86
|
+
background it sits on (also -surface as of the comment above), and
|
|
87
|
+
-brand on -brand-quiet is a pairing already relied on elsewhere in
|
|
88
|
+
this library (e.g. hover states below) that passes contrast, unlike
|
|
89
|
+
-text-quiet on -brand-quiet. */
|
|
90
|
+
background: var(--lyra-color-brand-quiet);
|
|
91
|
+
color: var(--lyra-color-brand);
|
|
92
|
+
font-size: 0.6875rem;
|
|
93
|
+
line-height: 1.4;
|
|
94
|
+
text-transform: uppercase;
|
|
95
|
+
letter-spacing: 0.02em;
|
|
96
|
+
}
|
|
97
|
+
/* Pushed to the end of the header whether or not filename/language
|
|
98
|
+
precede it -- margin-inline-start:auto works whether this is the first
|
|
99
|
+
flex child (copyable alone) or the last of several. */
|
|
100
|
+
[part='copy-button'] {
|
|
101
|
+
flex: 0 0 auto;
|
|
102
|
+
margin-inline-start: auto;
|
|
103
|
+
border: none;
|
|
104
|
+
background: none;
|
|
105
|
+
color: var(--lyra-color-text-quiet);
|
|
106
|
+
font: inherit;
|
|
107
|
+
font-size: 0.75rem;
|
|
108
|
+
line-height: 1;
|
|
109
|
+
padding: 0.1875rem var(--lyra-space-xs);
|
|
110
|
+
border-radius: var(--lyra-radius);
|
|
111
|
+
cursor: pointer;
|
|
112
|
+
}
|
|
113
|
+
[part='copy-button']:hover {
|
|
114
|
+
background: var(--lyra-color-brand-quiet);
|
|
115
|
+
color: var(--lyra-color-brand);
|
|
116
|
+
}
|
|
117
|
+
[part='toggle']:focus-visible,
|
|
118
|
+
[part='copy-button']:focus-visible {
|
|
119
|
+
outline: var(--lyra-focus-ring-width) solid var(--lyra-focus-ring-color);
|
|
120
|
+
outline-offset: var(--lyra-focus-ring-offset);
|
|
121
|
+
}
|
|
122
|
+
[part='body'] {
|
|
123
|
+
display: block;
|
|
124
|
+
max-block-size: var(--lyra-code-block-max-height);
|
|
125
|
+
overflow: auto;
|
|
126
|
+
}
|
|
127
|
+
[part='body'][hidden] {
|
|
128
|
+
display: none;
|
|
129
|
+
}
|
|
130
|
+
[part='body']:focus-visible {
|
|
131
|
+
outline: var(--lyra-focus-ring-width) solid var(--lyra-focus-ring-color);
|
|
132
|
+
/* Negative (inward) so the ring isn't clipped by this element's own
|
|
133
|
+
overflow:auto -- same reasoning as lyra-virtual-list's [part="base"]. */
|
|
134
|
+
outline-offset: calc(-1 * var(--lyra-focus-ring-offset));
|
|
135
|
+
}
|
|
136
|
+
[part='body'] lyra-skeleton {
|
|
137
|
+
display: block;
|
|
138
|
+
padding: var(--lyra-space-s) var(--lyra-space-m);
|
|
139
|
+
--lyra-skeleton-w: 100%;
|
|
140
|
+
--lyra-skeleton-h: 8rem;
|
|
141
|
+
}
|
|
142
|
+
[part='pre'] {
|
|
143
|
+
margin: 0;
|
|
144
|
+
padding: var(--lyra-space-s) var(--lyra-space-m);
|
|
145
|
+
/* A default background so the plain-fallback path still reads as a
|
|
146
|
+
proper code block -- shiki's own inline background-color (part of the
|
|
147
|
+
generated-token-colors exception documented in code-block.ts's
|
|
148
|
+
tokenize()) silently overrides this the moment highlighting succeeds,
|
|
149
|
+
since an element's own style attribute always wins over an external
|
|
150
|
+
stylesheet rule at equal or lower specificity. */
|
|
151
|
+
background: var(--lyra-color-surface);
|
|
152
|
+
font-family: var(--lyra-code-block-font);
|
|
153
|
+
font-size: inherit;
|
|
154
|
+
line-height: 1.5;
|
|
155
|
+
white-space: pre;
|
|
156
|
+
}
|
|
157
|
+
[part='code'] {
|
|
158
|
+
font-family: inherit;
|
|
159
|
+
}
|
|
160
|
+
/*
|
|
161
|
+
* Activates shiki's "dual themes" dark variant. codeToHtml() (see
|
|
162
|
+
* tokenize() in code-block.ts) renders every token with its *light* color
|
|
163
|
+
* as a plain inline color/background-color and its *dark* color stashed in
|
|
164
|
+
* the --shiki-dark/--shiki-dark-bg custom properties shiki itself defines
|
|
165
|
+
* inline on the same elements -- shiki's own documented pattern for
|
|
166
|
+
* toggling them is exactly this: an external stylesheet rule that
|
|
167
|
+
* reassigns color/background-color from those variables. This requires
|
|
168
|
+
* !important because an inline style="..." attribute always outranks an
|
|
169
|
+
* external stylesheet at any selector specificity short of !important --
|
|
170
|
+
* there's no other way for a page-level rule to override an element's own
|
|
171
|
+
* style attribute. This is the one legitimate exception to "every color is
|
|
172
|
+
* a --lyra-* token" in this file: these values come from shiki's theme
|
|
173
|
+
* data, not this library's design tokens.
|
|
174
|
+
*/
|
|
175
|
+
@media (prefers-color-scheme: dark) {
|
|
176
|
+
[part='pre'],
|
|
177
|
+
[part='pre'] span {
|
|
178
|
+
color: var(--shiki-dark, inherit) !important;
|
|
179
|
+
background-color: var(--shiki-dark-bg, transparent) !important;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
@media (prefers-reduced-motion: reduce) {
|
|
183
|
+
[part='toggle'] .chevron {
|
|
184
|
+
transition: none !important;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
`;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Highlighter, BundledTheme } from 'shiki';
|
|
2
|
+
/** Re-exported under a component-scoped name so importers don't need their
|
|
3
|
+
* own `import type { Highlighter } from 'shiki'`. */
|
|
4
|
+
export type ShikiHighlighter = Highlighter;
|
|
5
|
+
/**
|
|
6
|
+
* The two bundled themes every highlighter instance is seeded with, so a
|
|
7
|
+
* single `codeToHtml()` call can emit a light *and* dark rendering at once
|
|
8
|
+
* (shiki's "dual themes" feature — https://shiki.style/guide/dual-themes —
|
|
9
|
+
* rather than maintaining two separately-highlighted copies). `github-light`/
|
|
10
|
+
* `github-dark` are well-known, reasonably neutral, and already part of
|
|
11
|
+
* shiki's bundled theme set, so picking them costs no extra install. See the
|
|
12
|
+
* `prefers-color-scheme` block in `code-block.styles.ts` for how the dark
|
|
13
|
+
* variant actually activates.
|
|
14
|
+
*/
|
|
15
|
+
export declare const SHIKI_LIGHT_THEME: BundledTheme;
|
|
16
|
+
export declare const SHIKI_DARK_THEME: BundledTheme;
|
|
17
|
+
/** Passed directly as `codeToHtml()`'s `themes` option — see `tokenize()` in `code-block.ts`. */
|
|
18
|
+
export declare const SHIKI_THEMES: Record<'light' | 'dark', BundledTheme>;
|
|
19
|
+
/**
|
|
20
|
+
* Lazily loads the optional peer dependency `shiki` once per page and builds
|
|
21
|
+
* (and caches) a single `Highlighter` instance seeded with `SHIKI_THEMES` and
|
|
22
|
+
* *zero* language grammars. Creating the highlighter — compiling its regex
|
|
23
|
+
* engine, parsing the seed themes — is the expensive part, not the dynamic
|
|
24
|
+
* `import()` itself, so the created instance is what's cached here rather
|
|
25
|
+
* than just the resolved module (one level deeper than `map-loader.ts`'s
|
|
26
|
+
* single-dependency cached-promise shape, which this otherwise mirrors).
|
|
27
|
+
* Resolves to `null` (with a one-time `console.warn`) if shiki isn't
|
|
28
|
+
* installed — `<lyra-code-block>` falls back to plain unhighlighted text in
|
|
29
|
+
* that case, which is a fully supported default, not a degraded mode. No
|
|
30
|
+
* language grammar is loaded up front; `loadShikiLanguage()` below loads each
|
|
31
|
+
* one incrementally the first time a `language` value actually requests it.
|
|
32
|
+
*/
|
|
33
|
+
export declare function loadShikiHighlighter(): Promise<ShikiHighlighter | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Ensures `lang` is loaded into `hl`, loading it on demand the first time
|
|
36
|
+
* it's requested rather than bundling every possible grammar up front —
|
|
37
|
+
* shiki supports this incrementally via `Highlighter.loadLanguage()`, which
|
|
38
|
+
* is what keeps this lazy instead of defeating the point of lazy-loading the
|
|
39
|
+
* peer at all. Resolves `false` (and remembers not to retry) when `lang`
|
|
40
|
+
* isn't a shiki-recognized grammar id or alias at all, so a caller can fall
|
|
41
|
+
* back to plain-text rendering instead of retrying an id that can never
|
|
42
|
+
* succeed on every future render.
|
|
43
|
+
*/
|
|
44
|
+
export declare function loadShikiLanguage(hl: ShikiHighlighter, lang: string): Promise<boolean>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two bundled themes every highlighter instance is seeded with, so a
|
|
3
|
+
* single `codeToHtml()` call can emit a light *and* dark rendering at once
|
|
4
|
+
* (shiki's "dual themes" feature — https://shiki.style/guide/dual-themes —
|
|
5
|
+
* rather than maintaining two separately-highlighted copies). `github-light`/
|
|
6
|
+
* `github-dark` are well-known, reasonably neutral, and already part of
|
|
7
|
+
* shiki's bundled theme set, so picking them costs no extra install. See the
|
|
8
|
+
* `prefers-color-scheme` block in `code-block.styles.ts` for how the dark
|
|
9
|
+
* variant actually activates.
|
|
10
|
+
*/
|
|
11
|
+
export const SHIKI_LIGHT_THEME = 'github-light';
|
|
12
|
+
export const SHIKI_DARK_THEME = 'github-dark';
|
|
13
|
+
/** Passed directly as `codeToHtml()`'s `themes` option — see `tokenize()` in `code-block.ts`. */
|
|
14
|
+
export const SHIKI_THEMES = {
|
|
15
|
+
light: SHIKI_LIGHT_THEME,
|
|
16
|
+
dark: SHIKI_DARK_THEME,
|
|
17
|
+
};
|
|
18
|
+
let highlighter;
|
|
19
|
+
/** Language ids that have already failed `loadLanguage()` once — avoids
|
|
20
|
+
* retrying (and re-throwing on) the same unrecognized `language` value on
|
|
21
|
+
* every re-render of every `<lyra-code-block>` that requests it. Shared
|
|
22
|
+
* across every highlighter instance the page ever creates (there's only
|
|
23
|
+
* ever one, see `loadShikiHighlighter()` below), so this never needs
|
|
24
|
+
* resetting alongside it. */
|
|
25
|
+
const unsupportedLanguages = new Set();
|
|
26
|
+
/**
|
|
27
|
+
* Lazily loads the optional peer dependency `shiki` once per page and builds
|
|
28
|
+
* (and caches) a single `Highlighter` instance seeded with `SHIKI_THEMES` and
|
|
29
|
+
* *zero* language grammars. Creating the highlighter — compiling its regex
|
|
30
|
+
* engine, parsing the seed themes — is the expensive part, not the dynamic
|
|
31
|
+
* `import()` itself, so the created instance is what's cached here rather
|
|
32
|
+
* than just the resolved module (one level deeper than `map-loader.ts`'s
|
|
33
|
+
* single-dependency cached-promise shape, which this otherwise mirrors).
|
|
34
|
+
* Resolves to `null` (with a one-time `console.warn`) if shiki isn't
|
|
35
|
+
* installed — `<lyra-code-block>` falls back to plain unhighlighted text in
|
|
36
|
+
* that case, which is a fully supported default, not a degraded mode. No
|
|
37
|
+
* language grammar is loaded up front; `loadShikiLanguage()` below loads each
|
|
38
|
+
* one incrementally the first time a `language` value actually requests it.
|
|
39
|
+
*/
|
|
40
|
+
export function loadShikiHighlighter() {
|
|
41
|
+
if (!highlighter) {
|
|
42
|
+
highlighter = import('shiki')
|
|
43
|
+
.then((mod) => mod.createHighlighter({ themes: [SHIKI_LIGHT_THEME, SHIKI_DARK_THEME], langs: [] }))
|
|
44
|
+
.catch((err) => {
|
|
45
|
+
console.warn('<lyra-code-block> needs the optional peer dependency `shiki` for syntax highlighting — install it ' +
|
|
46
|
+
'with `pnpm add shiki`. Code still renders, just unhighlighted, without it:', err);
|
|
47
|
+
return null;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return highlighter;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Ensures `lang` is loaded into `hl`, loading it on demand the first time
|
|
54
|
+
* it's requested rather than bundling every possible grammar up front —
|
|
55
|
+
* shiki supports this incrementally via `Highlighter.loadLanguage()`, which
|
|
56
|
+
* is what keeps this lazy instead of defeating the point of lazy-loading the
|
|
57
|
+
* peer at all. Resolves `false` (and remembers not to retry) when `lang`
|
|
58
|
+
* isn't a shiki-recognized grammar id or alias at all, so a caller can fall
|
|
59
|
+
* back to plain-text rendering instead of retrying an id that can never
|
|
60
|
+
* succeed on every future render.
|
|
61
|
+
*/
|
|
62
|
+
export async function loadShikiLanguage(hl, lang) {
|
|
63
|
+
if (hl.getLoadedLanguages().includes(lang))
|
|
64
|
+
return true;
|
|
65
|
+
if (unsupportedLanguages.has(lang))
|
|
66
|
+
return false;
|
|
67
|
+
try {
|
|
68
|
+
await hl.loadLanguage(lang);
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// Not a shiki-recognized grammar id/alias, or the grammar failed to
|
|
73
|
+
// load for some other reason — either way, <lyra-code-block> treats an
|
|
74
|
+
// unrecognized language the same as an unset one (plain text), and
|
|
75
|
+
// there's nothing more specific a caller could do with the reason.
|
|
76
|
+
unsupportedLanguages.add(lang);
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|