@aleph-alpha/ui-library 1.20.0 → 1.21.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/dist/system/index.d.ts +64 -0
- package/dist/system/lib.js +12723 -12534
- package/package.json +2 -1
- package/src/components/UiCodeBlock/UiCodeBlock.stories.ts +352 -0
- package/src/components/UiCodeBlock/UiCodeBlock.vue +168 -0
- package/src/components/UiCodeBlock/__tests__/UiCodeBlock.test.ts +210 -0
- package/src/components/UiCodeBlock/index.ts +2 -0
- package/src/components/UiCodeBlock/types.ts +60 -0
- package/src/components/index.ts +1 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { render, waitFor } from '@testing-library/vue';
|
|
2
|
+
import userEvent from '@testing-library/user-event';
|
|
3
|
+
import { afterEach, describe, expect, test, vi, beforeEach } from 'vitest';
|
|
4
|
+
import UiCodeBlock from '../UiCodeBlock.vue';
|
|
5
|
+
|
|
6
|
+
vi.mock('shiki', () => ({
|
|
7
|
+
codeToHtml: vi
|
|
8
|
+
.fn()
|
|
9
|
+
.mockResolvedValue(
|
|
10
|
+
'<pre class="shiki shiki-themes github-light github-dark"><code><span class="line">const x = 1;</span></code></pre>',
|
|
11
|
+
),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
describe('UiCodeBlock', () => {
|
|
15
|
+
let originalClipboardDescriptor: PropertyDescriptor | undefined;
|
|
16
|
+
let originalExecCommand: typeof document.execCommand;
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
vi.clearAllMocks();
|
|
20
|
+
|
|
21
|
+
originalClipboardDescriptor = Object.getOwnPropertyDescriptor(navigator, 'clipboard');
|
|
22
|
+
originalExecCommand = document.execCommand;
|
|
23
|
+
|
|
24
|
+
Object.defineProperty(navigator, 'clipboard', {
|
|
25
|
+
value: {
|
|
26
|
+
writeText: vi.fn().mockResolvedValue(undefined),
|
|
27
|
+
readText: vi.fn().mockResolvedValue(''),
|
|
28
|
+
},
|
|
29
|
+
writable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
});
|
|
32
|
+
document.execCommand = vi.fn().mockReturnValue(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
if (originalClipboardDescriptor) {
|
|
37
|
+
Object.defineProperty(navigator, 'clipboard', originalClipboardDescriptor);
|
|
38
|
+
} else {
|
|
39
|
+
delete (navigator as never)['clipboard'];
|
|
40
|
+
}
|
|
41
|
+
document.execCommand = originalExecCommand;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('renders Shiki highlighted output after async load', async () => {
|
|
45
|
+
const { container } = render(UiCodeBlock, {
|
|
46
|
+
props: { code: 'const x = 1;', language: 'typescript' },
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
await waitFor(() => {
|
|
50
|
+
expect(container.querySelector('.shiki')).toBeInTheDocument();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('shows language label in header', async () => {
|
|
55
|
+
const { getByText } = render(UiCodeBlock, {
|
|
56
|
+
props: { code: 'print("hi")', language: 'python' },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
await waitFor(() => {
|
|
60
|
+
expect(getByText('python')).toBeInTheDocument();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('shows filename in header and takes precedence over language label', async () => {
|
|
65
|
+
const { getByText, queryByText } = render(UiCodeBlock, {
|
|
66
|
+
props: { code: '{}', language: 'json', filename: 'config.json' },
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
await waitFor(() => {
|
|
70
|
+
expect(getByText('config.json')).toBeInTheDocument();
|
|
71
|
+
expect(queryByText('json')).not.toBeInTheDocument();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('shows copy button by default', async () => {
|
|
76
|
+
const { getByRole } = render(UiCodeBlock, {
|
|
77
|
+
props: { code: 'code', language: 'typescript' },
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
await waitFor(() => {
|
|
81
|
+
expect(getByRole('button', { name: 'Copy code' })).toBeInTheDocument();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('hides copy button when showCopyButton is false', async () => {
|
|
86
|
+
const { queryByRole } = render(UiCodeBlock, {
|
|
87
|
+
props: { code: 'code', language: 'typescript', showCopyButton: false },
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
await waitFor(() => {
|
|
91
|
+
expect(queryByRole('button', { name: 'Copy code' })).not.toBeInTheDocument();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('changes button label to "Copied" after clicking copy', async () => {
|
|
96
|
+
const user = userEvent.setup();
|
|
97
|
+
|
|
98
|
+
const { getByRole } = render(UiCodeBlock, {
|
|
99
|
+
props: { code: 'const x = 1;', language: 'typescript' },
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
await waitFor(() => {
|
|
103
|
+
expect(getByRole('button', { name: 'Copy code' })).toBeInTheDocument();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
await user.click(getByRole('button', { name: 'Copy code' }));
|
|
107
|
+
|
|
108
|
+
await waitFor(() => {
|
|
109
|
+
expect(getByRole('button', { name: 'Copied' })).toBeInTheDocument();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('applies line-numbers class when showLineNumbers is true', async () => {
|
|
114
|
+
const { container } = render(UiCodeBlock, {
|
|
115
|
+
props: { code: 'code', language: 'typescript', showLineNumbers: true },
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
await waitFor(() => {
|
|
119
|
+
expect(container.querySelector('.ui-code-block__line-numbers')).toBeInTheDocument();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('applies maxHeight style when provided', async () => {
|
|
124
|
+
const { container } = render(UiCodeBlock, {
|
|
125
|
+
props: { code: 'code', language: 'typescript', maxHeight: '200px' },
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
await waitFor(() => {
|
|
129
|
+
const scrollContainer = container.querySelector('.overflow-auto');
|
|
130
|
+
expect(scrollContainer).toHaveStyle({ maxHeight: '200px' });
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('uses custom copy labels when provided', async () => {
|
|
135
|
+
const user = userEvent.setup();
|
|
136
|
+
|
|
137
|
+
const { getByRole } = render(UiCodeBlock, {
|
|
138
|
+
props: {
|
|
139
|
+
code: 'x = 1',
|
|
140
|
+
language: 'python',
|
|
141
|
+
copyLabel: 'Code kopieren',
|
|
142
|
+
copiedLabel: 'Kopiert',
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
await waitFor(() => {
|
|
147
|
+
expect(getByRole('button', { name: 'Code kopieren' })).toBeInTheDocument();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
await user.click(getByRole('button', { name: 'Code kopieren' }));
|
|
151
|
+
|
|
152
|
+
await waitFor(() => {
|
|
153
|
+
expect(getByRole('button', { name: 'Kopiert' })).toBeInTheDocument();
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('falls back to text highlighting when language is unsupported', async () => {
|
|
158
|
+
const { codeToHtml } = await import('shiki');
|
|
159
|
+
const mock = vi.mocked(codeToHtml);
|
|
160
|
+
mock
|
|
161
|
+
.mockRejectedValueOnce(new Error('Unknown language'))
|
|
162
|
+
.mockResolvedValueOnce(
|
|
163
|
+
'<pre class="shiki shiki-themes github-light github-dark"><code><span class="line">code</span></code></pre>',
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
const { container } = render(UiCodeBlock, {
|
|
167
|
+
props: { code: 'code', language: 'unknown-lang' },
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
await waitFor(() => {
|
|
171
|
+
expect(container.querySelector('.shiki')).toBeInTheDocument();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expect(mock).toHaveBeenCalledTimes(2);
|
|
175
|
+
expect(mock).toHaveBeenLastCalledWith('code', expect.objectContaining({ lang: 'text' }));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test('shows plain pre/code fallback when all highlighting fails', async () => {
|
|
179
|
+
const { codeToHtml } = await import('shiki');
|
|
180
|
+
vi.mocked(codeToHtml).mockRejectedValue(new Error('Shiki broken'));
|
|
181
|
+
|
|
182
|
+
const { container } = render(UiCodeBlock, {
|
|
183
|
+
props: { code: 'fallback content', language: 'typescript' },
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
await waitFor(() => {
|
|
187
|
+
const codeEl = container.querySelector('pre code');
|
|
188
|
+
expect(codeEl).toBeInTheDocument();
|
|
189
|
+
expect(codeEl?.textContent).toBe('fallback content');
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('calls codeToHtml with correct language and dual themes', async () => {
|
|
194
|
+
const { codeToHtml } = await import('shiki');
|
|
195
|
+
|
|
196
|
+
render(UiCodeBlock, {
|
|
197
|
+
props: { code: 'const x = 1;', language: 'typescript' },
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
await waitFor(() => {
|
|
201
|
+
expect(codeToHtml).toHaveBeenCalledWith('const x = 1;', {
|
|
202
|
+
lang: 'typescript',
|
|
203
|
+
themes: {
|
|
204
|
+
light: 'github-light',
|
|
205
|
+
dark: 'github-dark',
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A code block component for displaying source code with syntax highlighting,
|
|
3
|
+
* line numbers, and copy-to-clipboard functionality. Powered by Shiki.
|
|
4
|
+
* @category Content
|
|
5
|
+
* @useCases code display, code snippet, syntax highlighting, API response, terminal output
|
|
6
|
+
* @keywords code, codeblock, syntax, highlight, copy, pre, snippet
|
|
7
|
+
* @related UiKbd
|
|
8
|
+
*/
|
|
9
|
+
export interface UiCodeBlockProps {
|
|
10
|
+
/**
|
|
11
|
+
* The source code to display.
|
|
12
|
+
*/
|
|
13
|
+
code: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The programming language for syntax highlighting.
|
|
17
|
+
* Supports all languages available in Shiki (e.g., 'typescript', 'python', 'json', 'bash').
|
|
18
|
+
* Use 'text' for plain text without highlighting.
|
|
19
|
+
* @default 'text'
|
|
20
|
+
*/
|
|
21
|
+
language?: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Optional filename to display in the header.
|
|
25
|
+
* When provided, takes precedence over the language label.
|
|
26
|
+
* @example 'app.config.ts'
|
|
27
|
+
*/
|
|
28
|
+
filename?: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Whether to show line numbers alongside the code.
|
|
32
|
+
* @default false
|
|
33
|
+
*/
|
|
34
|
+
showLineNumbers?: boolean;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether to show the copy-to-clipboard button in the header.
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
showCopyButton?: boolean;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Maximum height of the code area before scrolling.
|
|
44
|
+
* Accepts any valid CSS height value.
|
|
45
|
+
* @example '300px'
|
|
46
|
+
*/
|
|
47
|
+
maxHeight?: string;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Aria label for the copy button in its default state.
|
|
51
|
+
* @default 'Copy code'
|
|
52
|
+
*/
|
|
53
|
+
copyLabel?: string;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Aria label for the copy button after copying.
|
|
57
|
+
* @default 'Copied'
|
|
58
|
+
*/
|
|
59
|
+
copiedLabel?: string;
|
|
60
|
+
}
|
package/src/components/index.ts
CHANGED