@aleph-alpha/ui-library 5.9.0 → 5.10.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/config.js +157 -4
- package/dist/system/components/UiFileItem/UiFileItem.vue.d.ts +3 -1
- package/dist/system/components/UiFileType/types.d.ts +2 -1
- package/dist/system/components/UiSwitch/UiSwitch.vue.d.ts +1 -0
- package/dist/system/components/UiSwitch/types.d.ts +12 -0
- package/dist/system/components/core/switch/Switch.vue.d.ts +2 -0
- package/dist/system/components/core/switch/index.d.ts +8 -0
- package/dist/system/index.d.ts +1 -0
- package/dist/system/lib/scrollbar.d.ts +37 -0
- package/dist/system/lib/useAnchorFloat.d.ts +0 -2
- package/dist/system/lib/useIsTruncated.d.ts +15 -0
- package/dist/system/lib.js +15052 -14843
- package/dist/system/patterns/UiFloatActions/types.d.ts +0 -1
- package/dist/system/story-sidebar-icons.json +4 -1
- package/docs/public-docs/foundations.md +17 -15
- package/package.json +3 -3
- package/src/components/UiCodeBlock/UiCodeBlock.stories.ts +54 -0
- package/src/components/UiCodeBlock/UiCodeBlock.vue +84 -32
- package/src/components/UiCodeBlock/__tests__/UiCodeBlock.test.ts +65 -21
- package/src/components/UiFileItem/UiFileItem.vue +17 -4
- package/src/components/UiFileItem/__tests__/UiFileItem.test.ts +49 -90
- package/src/components/UiFileType/UiFileType.stories.ts +13 -2
- package/src/components/UiFileType/UiFileType.vue +8 -0
- package/src/components/UiFileType/__tests__/UiFileType.test.ts +10 -0
- package/src/components/UiFileType/types.ts +3 -1
- package/src/components/UiSnackbar/UiSnackbar.stories.ts +4 -5
- package/src/components/UiSwitch/UiSwitch.stories.ts +658 -7
- package/src/components/UiSwitch/UiSwitch.vue +1 -0
- package/src/components/UiSwitch/__tests__/UiSwitch.test.ts +48 -0
- package/src/components/UiSwitch/types.ts +13 -0
- package/src/components/UiToggleGroup/UiToggleGroup.vue +1 -1
- package/src/components/core/field/FieldLabel.vue +1 -1
- package/src/components/core/label/Label.vue +1 -1
- package/src/components/core/switch/Switch.vue +23 -17
- package/src/components/core/switch/index.ts +36 -0
- package/src/index.ts +3 -0
- package/src/lib/scrollbar.ts +295 -0
- package/src/lib/useAnchorFloat.ts +0 -2
- package/src/lib/useIsTruncated.test.ts +174 -0
- package/src/lib/useIsTruncated.ts +75 -0
- package/src/patterns/UiFileUpload/UiFileItem.stories.ts +1162 -0
- package/src/patterns/UiFileUpload/UiFileUpload.stories.ts +552 -1
- package/src/patterns/UiFileUpload/__tests__/UiFileUpload.test.ts +5 -21
- package/src/patterns/UiFloatActions/UiFloatActionItems.vue +0 -1
- package/src/patterns/UiFloatActions/UiFloatActions.stories.ts +0 -1
- package/src/patterns/UiFloatActions/UiFloatActions.vue +1 -2
- package/src/patterns/UiFloatActions/types.ts +0 -1
- package/src/patterns/UiProfileCard/UiProfileCard.stories.ts +132 -117
- package/src/theme/Background.stories.ts +27 -1
- package/src/theme/BorderRadius.stories.ts +5 -0
- package/src/theme/Overlay.stories.ts +12 -1
- package/src/theme/Scrollbar.stories.ts +260 -0
- package/tokens.json +224 -2
- package/src/components/UiFileItem/UiFileItem.stories.ts +0 -442
|
@@ -89,7 +89,6 @@ export type UiFloatActionItemsValue = string | number;
|
|
|
89
89
|
* Use the root item (`type="toolbar"`) as the container and nest child items
|
|
90
90
|
* for buttons, inputs, text labels, groups, and separators. Implements the
|
|
91
91
|
* WAI-ARIA APG toolbar pattern with roving tabindex.
|
|
92
|
-
*
|
|
93
92
|
* @category Overlays
|
|
94
93
|
* @useCases contextual toolbar, inline actions, pagination controls, filter bar
|
|
95
94
|
* @keywords float, toolbar, pill, actions, keyboard, roving focus
|
|
@@ -21,6 +21,9 @@
|
|
|
21
21
|
"components-uibreadcrumb--accessibility": "accessibility",
|
|
22
22
|
"patterns-uifloatactions--accessibility": "accessibility",
|
|
23
23
|
"patterns-uidatatable--accessibility": "accessibility",
|
|
24
|
+
"patterns-uitopbar--accessibility": "accessibility",
|
|
25
|
+
"patterns-uifileupload-uifileitem--accessibility": "accessibility",
|
|
26
|
+
"patterns-uifileupload-uifileupload--accessibility": "accessibility",
|
|
24
27
|
"patterns-uiprofilecard--accessibility": "accessibility",
|
|
25
|
-
"
|
|
28
|
+
"css-utility-scrollbar--design-notes": "palette"
|
|
26
29
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
The foundations are the design tokens and CSS utilities that every component is built on. They are exposed as UnoCSS classes via [`@aleph-alpha/ui-library/config`](quick-start.md#config-exports-reference) and documented interactively in Storybook.
|
|
4
4
|
|
|
5
5
|
!!! tip "Browse live in Storybook"
|
|
6
|
-
|
|
6
|
+
Every section below has a matching page under **CSS utility/** in [Storybook](https://dev.aleph-alpha.de/ui-library/?path=/docs/css-utility-background--docs) where each token is rendered with its UnoCSS class name. Click a class name in Storybook to copy it.
|
|
7
7
|
|
|
8
8
|
---
|
|
9
9
|
|
|
@@ -22,10 +22,10 @@ Color is delivered through semantic tokens (no raw hex values in app code). Toke
|
|
|
22
22
|
|
|
23
23
|
Use the hover treatment that matches the component's base fill:
|
|
24
24
|
|
|
25
|
-
| Component background
|
|
26
|
-
|
|
|
27
|
-
| No existing background color
|
|
28
|
-
| Existing background color inherited by component
|
|
25
|
+
| Component background | Hover classes |
|
|
26
|
+
| ------------------------------------------------ | -------------------------------------------------------------------------------------------------------- |
|
|
27
|
+
| No existing background color | `hover:bg-background-surface-muted-subtle` |
|
|
28
|
+
| Existing background color inherited by component | `hover-layer hover-layer-subtle`, `hover-layer hover-layer-default`, or `hover-layer hover-layer-strong` |
|
|
29
29
|
|
|
30
30
|
For existing backgrounds, the component keeps its base fill and the hover layer adds the second fill via `overlay-hover-*`. The design team defines the hover strength per component based on the underlying base color, so check the component's existing `hover-layer-*` strength before changing it.
|
|
31
31
|
|
|
@@ -75,15 +75,17 @@ Example:
|
|
|
75
75
|
|
|
76
76
|
Surfaces and shadows together encode the elevation hierarchy. Use the surface token that matches the layer, and add a shadow only at floating / modal level.
|
|
77
77
|
|
|
78
|
-
| Layer | Token
|
|
79
|
-
| ------------------------------------------ |
|
|
80
|
-
| Base app backdrop | `bg-background-surface-primary`
|
|
81
|
-
| Sheet above base | `bg-background-surface-secondary`
|
|
82
|
-
| Sunken / highlighted inner container | `bg-background-surface-muted`
|
|
83
|
-
| Floating overlay (popover, dropdown, menu) | `bg-background-surface-floating`
|
|
84
|
-
| Modal / dialog container | `bg-background-surface-modal`
|
|
85
|
-
| Modal backdrop / scrim | `bg-overlay-surface-modal-overlay`
|
|
86
|
-
|
|
|
78
|
+
| Layer | Token | Notes |
|
|
79
|
+
| ------------------------------------------ | ------------------------------------------ | --------------------------------------------------------- |
|
|
80
|
+
| Base app backdrop | `bg-background-surface-primary` | Sidebar lives on this layer. |
|
|
81
|
+
| Sheet above base | `bg-background-surface-secondary` | Use `shadow-lg`, no border. |
|
|
82
|
+
| Sunken / highlighted inner container | `bg-background-surface-muted` | Subtle non-floating differentiation. |
|
|
83
|
+
| Floating overlay (popover, dropdown, menu) | `bg-background-surface-floating` | `border-default` + `shadow-xs`. |
|
|
84
|
+
| Modal / dialog container | `bg-background-surface-modal` | `border-default` + `shadow-lg`. |
|
|
85
|
+
| Modal backdrop / scrim | `bg-overlay-surface-modal-overlay` | Used by `<UiDialog>` overlay. |
|
|
86
|
+
| Inverse modal backdrop / scrim | `bg-overlay-surface-modal-overlay-inverse` | Lightens instead of dimming, for dark or media backdrops. |
|
|
87
|
+
| Citation highlight surface | `bg-background-surface-citation` | Tints referenced source passages. |
|
|
88
|
+
| Form inputs | `bg-background-input-default` | Text, textarea, file, OTP. |
|
|
87
89
|
|
|
88
90
|
| Topic | Storybook |
|
|
89
91
|
| ------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
|
|
@@ -99,7 +101,7 @@ Icons are rendered with `<UiIcon>`, which wraps the [Lucide](https://lucide.dev/
|
|
|
99
101
|
|
|
100
102
|
```vue
|
|
101
103
|
<script setup lang="ts">
|
|
102
|
-
|
|
104
|
+
import { UiIcon } from '@aleph-alpha/ui-library';
|
|
103
105
|
</script>
|
|
104
106
|
|
|
105
107
|
<template>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aleph-alpha/ui-library",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.10.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/system/lib.js",
|
|
@@ -82,9 +82,9 @@
|
|
|
82
82
|
"vue-router": "4.6.4",
|
|
83
83
|
"vue-tsc": "^2.2.12",
|
|
84
84
|
"wait-on": "9.0.3",
|
|
85
|
-
"@aleph-alpha/eslint-config-frontend": "0.7.1",
|
|
86
85
|
"@aleph-alpha/prettier-config-frontend": "2.1.1",
|
|
87
|
-
"@aleph-alpha/tsconfig-frontend": "0.7.3"
|
|
86
|
+
"@aleph-alpha/tsconfig-frontend": "0.7.3",
|
|
87
|
+
"@aleph-alpha/eslint-config-frontend": "0.7.1"
|
|
88
88
|
},
|
|
89
89
|
"peerDependencies": {
|
|
90
90
|
"@unocss/preset-wind4": ">=66.0.0",
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ref, onMounted, onUnmounted } from 'vue';
|
|
1
2
|
import type { Meta, StoryObj } from '@storybook/vue3-vite';
|
|
2
3
|
import UiCodeBlock from './UiCodeBlock.vue';
|
|
3
4
|
|
|
@@ -366,3 +367,56 @@ export const HtmlWithLineNumbers: Story = {
|
|
|
366
367
|
},
|
|
367
368
|
},
|
|
368
369
|
};
|
|
370
|
+
|
|
371
|
+
const streamingTemplateSource = `<script setup lang="ts">
|
|
372
|
+
import { ref } from 'vue'
|
|
373
|
+
import { UiCodeBlock } from '@aleph-alpha/ui-library'
|
|
374
|
+
|
|
375
|
+
const code = ref('')
|
|
376
|
+
|
|
377
|
+
// Append each chunk as it arrives; UiCodeBlock re-highlights in place
|
|
378
|
+
// instead of falling back to unhighlighted text between updates.
|
|
379
|
+
for await (const chunk of responseStream) {
|
|
380
|
+
code.value += chunk
|
|
381
|
+
}
|
|
382
|
+
</script>
|
|
383
|
+
|
|
384
|
+
<template>
|
|
385
|
+
<UiCodeBlock :code="code" language="typescript" filename="stream.ts" />
|
|
386
|
+
</template>`;
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* The component keeps the previously highlighted output on screen while
|
|
390
|
+
* re-highlighting, so a code prop that grows chunk by chunk (LLM streaming)
|
|
391
|
+
* updates in place instead of flashing back to unhighlighted text.
|
|
392
|
+
*/
|
|
393
|
+
export const Streaming: Story = {
|
|
394
|
+
render: () => ({
|
|
395
|
+
components: { UiCodeBlock },
|
|
396
|
+
setup() {
|
|
397
|
+
const code = ref('');
|
|
398
|
+
let timerId: ReturnType<typeof setInterval> | undefined;
|
|
399
|
+
|
|
400
|
+
onMounted(() => {
|
|
401
|
+
let index = 0;
|
|
402
|
+
timerId = setInterval(() => {
|
|
403
|
+
index = Math.min(index + 3, typescriptExample.length);
|
|
404
|
+
code.value = typescriptExample.slice(0, index);
|
|
405
|
+
if (index === typescriptExample.length) index = 0;
|
|
406
|
+
}, 40);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
onUnmounted(() => clearInterval(timerId));
|
|
410
|
+
|
|
411
|
+
return { code };
|
|
412
|
+
},
|
|
413
|
+
template: `<UiCodeBlock :code="code" language="typescript" filename="stream.ts" />`,
|
|
414
|
+
}),
|
|
415
|
+
parameters: {
|
|
416
|
+
docs: {
|
|
417
|
+
source: {
|
|
418
|
+
code: streamingTemplateSource,
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
},
|
|
422
|
+
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
2
|
import { ref, computed, watch, onMounted, onUnmounted } from 'vue';
|
|
3
|
-
import { useClipboard } from '@vueuse/core';
|
|
4
|
-
import {
|
|
3
|
+
import { useClipboard, useThrottleFn } from '@vueuse/core';
|
|
4
|
+
import { getSingletonHighlighter } from 'shiki';
|
|
5
|
+
import type { BundledLanguage, Highlighter } from 'shiki';
|
|
5
6
|
import { cn } from '@/lib/utils';
|
|
6
7
|
import { UiButton } from '@/components/UiButton';
|
|
7
8
|
import { UiIcon } from '@/components/UiIcon';
|
|
@@ -23,7 +24,6 @@ const props = withDefaults(defineProps<UiCodeBlockProps>(), {
|
|
|
23
24
|
});
|
|
24
25
|
|
|
25
26
|
const highlightedHtml = ref('');
|
|
26
|
-
const isHighlighted = ref(false);
|
|
27
27
|
const isCopied = ref(false);
|
|
28
28
|
|
|
29
29
|
const { copy } = useClipboard();
|
|
@@ -35,12 +35,27 @@ const shikiTokenColorOverrides = {
|
|
|
35
35
|
'#FFAB70': TOKEN_COLOR_CONTENT_ON_SURFACE_PRIMARY,
|
|
36
36
|
} as const;
|
|
37
37
|
|
|
38
|
+
const SHIKI_THEMES = { light: 'github-light', dark: 'github-dark' } as const;
|
|
39
|
+
const FALLBACK_LANGUAGE = 'text';
|
|
40
|
+
/** Languages Shiki renders without loading a grammar. */
|
|
41
|
+
const PLAIN_LANGUAGES = ['text', 'txt', 'plain', 'plaintext'];
|
|
42
|
+
/**
|
|
43
|
+
* Streaming updates `code` on every chunk, and each update re-tokenizes the
|
|
44
|
+
* whole block. Cap how often that happens so long blocks stay cheap.
|
|
45
|
+
*/
|
|
46
|
+
const HIGHLIGHT_THROTTLE_MS = 100;
|
|
47
|
+
|
|
38
48
|
const headerLabel = computed(() => props.filename || props.language);
|
|
39
49
|
const showHeader = computed(
|
|
40
50
|
() => headerLabel.value !== 'text' || props.showCopyButton,
|
|
41
51
|
);
|
|
42
52
|
|
|
43
53
|
let highlightRequestId = 0;
|
|
54
|
+
let highlighter: Highlighter | undefined;
|
|
55
|
+
/** `props.language` that `activeLanguage` was resolved from. */
|
|
56
|
+
let resolvedFor: string | undefined;
|
|
57
|
+
let activeLanguage: string = FALLBACK_LANGUAGE;
|
|
58
|
+
const loadedLanguages = new Set(PLAIN_LANGUAGES);
|
|
44
59
|
|
|
45
60
|
function normalizeShikiTokenColors(html: string) {
|
|
46
61
|
return Object.entries(shikiTokenColorOverrides).reduce(
|
|
@@ -49,41 +64,76 @@ function normalizeShikiTokenColors(html: string) {
|
|
|
49
64
|
);
|
|
50
65
|
}
|
|
51
66
|
|
|
67
|
+
// Renders synchronously, and only ever overwrites `highlightedHtml` with the
|
|
68
|
+
// next result. The previous output stays on screen until then, so a streamed
|
|
69
|
+
// update never falls back to the unhighlighted `<pre>` mid-flight.
|
|
70
|
+
function renderHighlighted(instance: Highlighter, language: string) {
|
|
71
|
+
try {
|
|
72
|
+
highlightedHtml.value = normalizeShikiTokenColors(
|
|
73
|
+
instance.codeToHtml(props.code, {
|
|
74
|
+
lang: language,
|
|
75
|
+
themes: SHIKI_THEMES,
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
} catch {
|
|
79
|
+
highlightedHtml.value = '';
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Loads the grammar on first use, falling back to plain text if unsupported.
|
|
84
|
+
async function resolveLanguage(instance: Highlighter, language: string) {
|
|
85
|
+
if (loadedLanguages.has(language)) return language;
|
|
86
|
+
try {
|
|
87
|
+
await instance.loadLanguage(language as BundledLanguage);
|
|
88
|
+
loadedLanguages.add(language);
|
|
89
|
+
return language;
|
|
90
|
+
} catch {
|
|
91
|
+
return FALLBACK_LANGUAGE;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
52
95
|
async function highlightCode() {
|
|
53
96
|
const requestId = ++highlightRequestId;
|
|
54
|
-
|
|
97
|
+
|
|
55
98
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
light: 'github-light',
|
|
60
|
-
dark: 'github-dark',
|
|
61
|
-
},
|
|
99
|
+
highlighter ??= await getSingletonHighlighter({
|
|
100
|
+
themes: [SHIKI_THEMES.light, SHIKI_THEMES.dark],
|
|
101
|
+
langs: [],
|
|
62
102
|
});
|
|
63
|
-
if (requestId !== highlightRequestId) return;
|
|
64
|
-
highlightedHtml.value = normalizeShikiTokenColors(html);
|
|
65
|
-
isHighlighted.value = true;
|
|
66
103
|
} catch {
|
|
67
|
-
if (requestId
|
|
68
|
-
|
|
69
|
-
const html = await codeToHtml(props.code, {
|
|
70
|
-
lang: 'text',
|
|
71
|
-
themes: {
|
|
72
|
-
light: 'github-light',
|
|
73
|
-
dark: 'github-dark',
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
if (requestId !== highlightRequestId) return;
|
|
77
|
-
highlightedHtml.value = normalizeShikiTokenColors(html);
|
|
78
|
-
isHighlighted.value = true;
|
|
79
|
-
} catch {
|
|
80
|
-
if (requestId !== highlightRequestId) return;
|
|
81
|
-
highlightedHtml.value = '';
|
|
82
|
-
}
|
|
104
|
+
if (requestId === highlightRequestId) highlightedHtml.value = '';
|
|
105
|
+
return;
|
|
83
106
|
}
|
|
107
|
+
if (requestId !== highlightRequestId) return;
|
|
108
|
+
|
|
109
|
+
const language = await resolveLanguage(highlighter, props.language);
|
|
110
|
+
if (requestId !== highlightRequestId) return;
|
|
111
|
+
|
|
112
|
+
activeLanguage = language;
|
|
113
|
+
resolvedFor = props.language;
|
|
114
|
+
renderHighlighted(highlighter, language);
|
|
84
115
|
}
|
|
85
116
|
|
|
86
|
-
|
|
117
|
+
const highlightThrottled = useThrottleFn(
|
|
118
|
+
() => {
|
|
119
|
+
// Grammar already loaded for this language, so no await is needed: the new
|
|
120
|
+
// HTML is set from the pre-flush watcher and lands in the same DOM update
|
|
121
|
+
// as the surrounding text, rather than a render pass later.
|
|
122
|
+
if (highlighter && resolvedFor === props.language) {
|
|
123
|
+
highlightRequestId++;
|
|
124
|
+
renderHighlighted(highlighter, activeLanguage);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
void highlightCode();
|
|
128
|
+
},
|
|
129
|
+
HIGHLIGHT_THROTTLE_MS,
|
|
130
|
+
// Trailing is required: without it the final chunk of a stream is dropped
|
|
131
|
+
// and the block stays stuck on the second-to-last update.
|
|
132
|
+
true,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
watch(() => props.code, highlightThrottled);
|
|
136
|
+
watch(() => props.language, highlightCode);
|
|
87
137
|
onMounted(highlightCode);
|
|
88
138
|
|
|
89
139
|
let copyTimerId: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -102,6 +152,8 @@ async function handleCopy() {
|
|
|
102
152
|
}
|
|
103
153
|
|
|
104
154
|
onUnmounted(() => {
|
|
155
|
+
// Not for safety — a post-unmount ref write is inert. This just drops
|
|
156
|
+
// in-flight highlighting instead of tokenizing for a discarded component.
|
|
105
157
|
highlightRequestId++;
|
|
106
158
|
clearTimeout(copyTimerId);
|
|
107
159
|
});
|
|
@@ -148,7 +200,7 @@ onUnmounted(() => {
|
|
|
148
200
|
-->
|
|
149
201
|
<!-- eslint-disable vue/no-v-html, sonarjs/no-vue-bypass-sanitization -->
|
|
150
202
|
<div
|
|
151
|
-
v-if="
|
|
203
|
+
v-if="highlightedHtml"
|
|
152
204
|
:class="
|
|
153
205
|
cn(
|
|
154
206
|
'ui-code-block__content',
|
|
@@ -160,7 +212,7 @@ onUnmounted(() => {
|
|
|
160
212
|
<!-- eslint-enable vue/no-v-html, sonarjs/no-vue-bypass-sanitization -->
|
|
161
213
|
<pre
|
|
162
214
|
v-else
|
|
163
|
-
class="m-0
|
|
215
|
+
class="m-0 p-4 text-sm leading-relaxed"
|
|
164
216
|
><code>{{ code }}</code></pre>
|
|
165
217
|
</div>
|
|
166
218
|
</div>
|
|
@@ -3,14 +3,22 @@ import userEvent from '@testing-library/user-event';
|
|
|
3
3
|
import { afterEach, describe, expect, test, vi, beforeEach } from 'vitest';
|
|
4
4
|
import UiCodeBlock from '../UiCodeBlock.vue';
|
|
5
5
|
|
|
6
|
+
const shiki = vi.hoisted(() => ({
|
|
7
|
+
codeToHtml: vi.fn(),
|
|
8
|
+
loadLanguage: vi.fn(),
|
|
9
|
+
}));
|
|
10
|
+
|
|
6
11
|
vi.mock('shiki', () => ({
|
|
7
|
-
|
|
8
|
-
.
|
|
9
|
-
.
|
|
10
|
-
|
|
11
|
-
),
|
|
12
|
+
getSingletonHighlighter: vi.fn().mockResolvedValue({
|
|
13
|
+
codeToHtml: shiki.codeToHtml,
|
|
14
|
+
loadLanguage: shiki.loadLanguage,
|
|
15
|
+
}),
|
|
12
16
|
}));
|
|
13
17
|
|
|
18
|
+
function highlightedMarkup(code: string) {
|
|
19
|
+
return `<pre class="shiki shiki-themes github-light github-dark"><code><span class="line">${code}</span></code></pre>`;
|
|
20
|
+
}
|
|
21
|
+
|
|
14
22
|
describe('UiCodeBlock', () => {
|
|
15
23
|
let originalClipboardDescriptor: PropertyDescriptor | undefined;
|
|
16
24
|
let originalExecCommand: typeof document.execCommand;
|
|
@@ -18,6 +26,11 @@ describe('UiCodeBlock', () => {
|
|
|
18
26
|
beforeEach(() => {
|
|
19
27
|
vi.clearAllMocks();
|
|
20
28
|
|
|
29
|
+
shiki.codeToHtml.mockImplementation((code: string) =>
|
|
30
|
+
highlightedMarkup(code),
|
|
31
|
+
);
|
|
32
|
+
shiki.loadLanguage.mockResolvedValue(undefined);
|
|
33
|
+
|
|
21
34
|
originalClipboardDescriptor = Object.getOwnPropertyDescriptor(
|
|
22
35
|
navigator,
|
|
23
36
|
'clipboard',
|
|
@@ -59,8 +72,7 @@ describe('UiCodeBlock', () => {
|
|
|
59
72
|
});
|
|
60
73
|
|
|
61
74
|
test('normalizes low-contrast Shiki token colors to theme tokens', async () => {
|
|
62
|
-
|
|
63
|
-
vi.mocked(codeToHtml).mockResolvedValueOnce(
|
|
75
|
+
shiki.codeToHtml.mockReturnValueOnce(
|
|
64
76
|
'<pre class="shiki shiki-themes github-light github-dark"><code><span class="line"><span style="color:#E36209;--shiki-dark:#FFAB70">columns</span></span></code></pre>',
|
|
65
77
|
);
|
|
66
78
|
|
|
@@ -189,13 +201,7 @@ describe('UiCodeBlock', () => {
|
|
|
189
201
|
});
|
|
190
202
|
|
|
191
203
|
test('falls back to text highlighting when language is unsupported', async () => {
|
|
192
|
-
|
|
193
|
-
const mock = vi.mocked(codeToHtml);
|
|
194
|
-
mock
|
|
195
|
-
.mockRejectedValueOnce(new Error('Unknown language'))
|
|
196
|
-
.mockResolvedValueOnce(
|
|
197
|
-
'<pre class="shiki shiki-themes github-light github-dark"><code><span class="line">code</span></code></pre>',
|
|
198
|
-
);
|
|
204
|
+
shiki.loadLanguage.mockRejectedValueOnce(new Error('Unknown language'));
|
|
199
205
|
|
|
200
206
|
const { container } = render(UiCodeBlock, {
|
|
201
207
|
props: { code: 'code', language: 'unknown-lang' },
|
|
@@ -205,16 +211,16 @@ describe('UiCodeBlock', () => {
|
|
|
205
211
|
expect(container.querySelector('.shiki')).toBeInTheDocument();
|
|
206
212
|
});
|
|
207
213
|
|
|
208
|
-
expect(
|
|
209
|
-
expect(mock).toHaveBeenLastCalledWith(
|
|
214
|
+
expect(shiki.codeToHtml).toHaveBeenLastCalledWith(
|
|
210
215
|
'code',
|
|
211
216
|
expect.objectContaining({ lang: 'text' }),
|
|
212
217
|
);
|
|
213
218
|
});
|
|
214
219
|
|
|
215
220
|
test('shows plain pre/code fallback when all highlighting fails', async () => {
|
|
216
|
-
|
|
217
|
-
|
|
221
|
+
shiki.codeToHtml.mockImplementation(() => {
|
|
222
|
+
throw new Error('Shiki broken');
|
|
223
|
+
});
|
|
218
224
|
|
|
219
225
|
const { container } = render(UiCodeBlock, {
|
|
220
226
|
props: { code: 'fallback content', language: 'typescript' },
|
|
@@ -228,14 +234,12 @@ describe('UiCodeBlock', () => {
|
|
|
228
234
|
});
|
|
229
235
|
|
|
230
236
|
test('calls codeToHtml with correct language and dual themes', async () => {
|
|
231
|
-
const { codeToHtml } = await import('shiki');
|
|
232
|
-
|
|
233
237
|
render(UiCodeBlock, {
|
|
234
238
|
props: { code: 'const x = 1;', language: 'typescript' },
|
|
235
239
|
});
|
|
236
240
|
|
|
237
241
|
await waitFor(() => {
|
|
238
|
-
expect(codeToHtml).toHaveBeenCalledWith('const x = 1;', {
|
|
242
|
+
expect(shiki.codeToHtml).toHaveBeenCalledWith('const x = 1;', {
|
|
239
243
|
lang: 'typescript',
|
|
240
244
|
themes: {
|
|
241
245
|
light: 'github-light',
|
|
@@ -244,4 +248,44 @@ describe('UiCodeBlock', () => {
|
|
|
244
248
|
});
|
|
245
249
|
});
|
|
246
250
|
});
|
|
251
|
+
|
|
252
|
+
test('keeps highlighted output mounted while streamed code updates', async () => {
|
|
253
|
+
const { container, rerender } = render(UiCodeBlock, {
|
|
254
|
+
props: { code: 'const x = 1', language: 'typescript' },
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
await waitFor(() => {
|
|
258
|
+
expect(container.querySelector('.shiki')).toBeInTheDocument();
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// A streamed chunk must not drop back to the unhighlighted fallback, which
|
|
262
|
+
// renders on a different background and so flashes between renders.
|
|
263
|
+
await rerender({ code: 'const x = 12' });
|
|
264
|
+
|
|
265
|
+
expect(container.querySelector('pre:not(.shiki)')).toBeNull();
|
|
266
|
+
expect(container.querySelector('.shiki')).toBeInTheDocument();
|
|
267
|
+
expect(container.querySelector('.shiki')?.textContent).toBe('const x = 12');
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test('throttles re-highlighting but applies the latest streamed code', async () => {
|
|
271
|
+
const { container, rerender } = render(UiCodeBlock, {
|
|
272
|
+
props: { code: 'a', language: 'typescript' },
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
await waitFor(() => {
|
|
276
|
+
expect(container.querySelector('.shiki')).toBeInTheDocument();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
shiki.codeToHtml.mockClear();
|
|
280
|
+
|
|
281
|
+
await rerender({ code: 'ab' });
|
|
282
|
+
await rerender({ code: 'abc' });
|
|
283
|
+
await rerender({ code: 'abcd' });
|
|
284
|
+
|
|
285
|
+
await waitFor(() => {
|
|
286
|
+
expect(container.querySelector('.shiki')?.textContent).toBe('abcd');
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
expect(shiki.codeToHtml.mock.calls.length).toBeLessThan(4);
|
|
290
|
+
});
|
|
247
291
|
});
|
|
@@ -3,8 +3,10 @@ import { UiButton } from '@/components/UiButton';
|
|
|
3
3
|
import { UiFileType } from '@/components/UiFileType';
|
|
4
4
|
import { UiIcon } from '@/components/UiIcon';
|
|
5
5
|
import { UiProgress } from '@/components/UiProgress';
|
|
6
|
+
import { UiTooltip } from '@/components/UiTooltip';
|
|
6
7
|
import { cn } from '@/lib/utils';
|
|
7
|
-
import {
|
|
8
|
+
import { useIsTruncated } from '@/lib/useIsTruncated';
|
|
9
|
+
import { computed, useTemplateRef } from 'vue';
|
|
8
10
|
import type { UiFileItemProps } from './types';
|
|
9
11
|
|
|
10
12
|
defineOptions({
|
|
@@ -96,6 +98,11 @@ const titleClass = computed(() =>
|
|
|
96
98
|
const progressClass = computed(() =>
|
|
97
99
|
cn('w-full', isSmall.value ? 'h-1' : 'h-1.5'),
|
|
98
100
|
);
|
|
101
|
+
|
|
102
|
+
// Long names are clipped to the row width, so the only way to read one in full
|
|
103
|
+
// is a tooltip. Showing it on every row would be noise, hence the measurement.
|
|
104
|
+
const titleRef = useTemplateRef<HTMLElement>('titleRef');
|
|
105
|
+
const isTitleTruncated = useIsTruncated(titleRef, () => displayFilename.value);
|
|
99
106
|
</script>
|
|
100
107
|
|
|
101
108
|
<template>
|
|
@@ -113,9 +120,15 @@ const progressClass = computed(() =>
|
|
|
113
120
|
/>
|
|
114
121
|
|
|
115
122
|
<div data-slot="file-item-content" :class="contentClass">
|
|
116
|
-
<
|
|
117
|
-
|
|
118
|
-
|
|
123
|
+
<UiTooltip
|
|
124
|
+
:content="filename"
|
|
125
|
+
:disabled="!isTitleTruncated"
|
|
126
|
+
align="start"
|
|
127
|
+
>
|
|
128
|
+
<span ref="titleRef" :class="titleClass" :aria-label="filename">
|
|
129
|
+
{{ displayFilename }}
|
|
130
|
+
</span>
|
|
131
|
+
</UiTooltip>
|
|
119
132
|
|
|
120
133
|
<UiProgress
|
|
121
134
|
v-if="isWithProgress"
|