@viewstem/ui 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.ko.md +47 -0
- package/README.md +60 -2
- package/dist/BASECOAT-LICENSE.md +21 -0
- package/dist/basecoat-contracts.d.ts +2 -0
- package/dist/basecoat-contracts.js +358 -0
- package/dist/basecoat-controllers.js +1612 -0
- package/dist/behaviors.d.ts +29 -0
- package/dist/behaviors.js +1475 -0
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +2 -0
- package/dist/catalog-meta.d.ts +26 -0
- package/dist/catalog-meta.js +130 -0
- package/dist/component-context.d.ts +7 -0
- package/dist/component-context.js +10 -0
- package/dist/components.d.ts +2 -0
- package/dist/components.js +536 -0
- package/dist/data-table.d.ts +2 -0
- package/dist/data-table.js +260 -0
- package/dist/date-calendar.d.ts +4 -0
- package/dist/date-calendar.js +512 -0
- package/dist/dynamic-catalog.d.ts +2 -0
- package/dist/dynamic-catalog.js +118 -0
- package/dist/form-scroll-families.d.ts +7 -0
- package/dist/form-scroll-families.js +895 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +12 -0
- package/dist/markup-families.d.ts +10 -0
- package/dist/markup-families.js +492 -0
- package/dist/menus.d.ts +8 -0
- package/dist/menus.js +427 -0
- package/dist/shadcn.d.ts +13 -0
- package/dist/shadcn.js +8 -0
- package/dist/slider-values.d.ts +9 -0
- package/dist/slider-values.js +43 -0
- package/dist/slider.d.ts +2 -0
- package/dist/slider.js +148 -0
- package/dist/static-catalog.d.ts +2 -0
- package/dist/static-catalog.js +87 -0
- package/dist/style-scope.d.ts +2 -0
- package/dist/style-scope.js +3 -0
- package/dist/styles.css +9573 -0
- package/dist/styles.css.d.ts +1 -0
- package/dist/support.json +1 -0
- package/dist/widget-families.d.ts +6 -0
- package/dist/widget-families.js +739 -0
- package/package.json +59 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ui: import("@viewstem/core").ResourceSet;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { defineResourceSet } from '@viewstem/core';
|
|
2
|
+
import { DOM_CONNECTOR_ID } from '@viewstem/dom';
|
|
3
|
+
import { shadcnContracts } from './shadcn.js';
|
|
4
|
+
import { catalogComponent } from './components.js';
|
|
5
|
+
export const ui = defineResourceSet({
|
|
6
|
+
id: '@viewstem/ui', version: '1',
|
|
7
|
+
components: shadcnContracts.map(contract => ({
|
|
8
|
+
contract,
|
|
9
|
+
connectorId: DOM_CONNECTOR_ID,
|
|
10
|
+
implementation: catalogComponent(contract.id.slice('shadcn.'.length)),
|
|
11
|
+
})),
|
|
12
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { NodeSnapshot } from '@viewstem/core';
|
|
2
|
+
import { type Behavior, type Send } from './behaviors.js';
|
|
3
|
+
import type { Markup } from './dynamic-catalog.js';
|
|
4
|
+
type Props = NodeSnapshot['inputs'];
|
|
5
|
+
export declare const markupCatalog: Record<string, Markup>;
|
|
6
|
+
export declare const markupPorts: Record<string, string>;
|
|
7
|
+
export declare const markupEvents: Record<string, string>;
|
|
8
|
+
export declare function markupTag(name: string, inputs: Props): keyof HTMLElementTagNameMap | undefined;
|
|
9
|
+
export declare function markupBehavior(name: string, element: HTMLElement, send: Send, signal: AbortSignal): Behavior | undefined;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
import { uniqueId } from './behaviors.js';
|
|
2
|
+
import { readInputs } from './component-context.js';
|
|
3
|
+
import { styleScope } from './style-scope.js';
|
|
4
|
+
export const markupCatalog = {
|
|
5
|
+
AspectRatio: ['div', ''],
|
|
6
|
+
'Attachment.Root': ['div', '', { 'data-size': 'default', 'data-orientation': 'horizontal' }],
|
|
7
|
+
'Attachment.Action': ['button', 'btn', { 'data-size': 'icon-xs', 'data-variant': 'ghost' }],
|
|
8
|
+
'Attachment.Actions': ['div', ''],
|
|
9
|
+
'Attachment.Content': ['div', ''],
|
|
10
|
+
'Attachment.Description': ['span', ''],
|
|
11
|
+
'Attachment.Group': ['div', ''],
|
|
12
|
+
'Attachment.Media': ['div', '', { 'data-variant': 'icon' }],
|
|
13
|
+
'Attachment.Title': ['span', ''],
|
|
14
|
+
'Attachment.Trigger': ['button', ''],
|
|
15
|
+
'Bubble.Root': ['div', '', { 'data-align': 'start', 'data-variant': 'default' }],
|
|
16
|
+
'Bubble.Content': ['div', ''],
|
|
17
|
+
'Bubble.Group': ['div', ''],
|
|
18
|
+
'Bubble.Reactions': ['div', '', { 'data-align': 'end' }],
|
|
19
|
+
'CodeBlock.Root': ['div', '', { 'data-size': 'default', 'data-variant': 'default' }],
|
|
20
|
+
'CodeBlock.Actions': ['div', ''],
|
|
21
|
+
'CodeBlock.Content': ['pre', '', { dir: 'ltr', tabindex: '0' }],
|
|
22
|
+
'CodeBlock.CopyButton': ['button', 'btn', { 'data-size': 'icon-xs', 'data-variant': 'ghost' }],
|
|
23
|
+
'CodeBlock.Footer': ['div', ''],
|
|
24
|
+
'CodeBlock.Header': ['div', ''],
|
|
25
|
+
'CodeBlock.Line': ['span', '', { 'data-variant': 'default' }],
|
|
26
|
+
'CodeBlock.Title': ['div', ''],
|
|
27
|
+
'Marker.Root': ['div', '', { 'data-variant': 'default' }],
|
|
28
|
+
'Marker.Content': ['span', ''],
|
|
29
|
+
'Marker.Icon': ['span', '', { 'aria-hidden': 'true' }],
|
|
30
|
+
'Message.Root': ['div', '', { 'data-align': 'start' }],
|
|
31
|
+
'Message.Avatar': ['div', ''],
|
|
32
|
+
'Message.Content': ['div', ''],
|
|
33
|
+
'Message.Footer': ['div', ''],
|
|
34
|
+
'Message.Group': ['div', ''],
|
|
35
|
+
'Message.Header': ['div', ''],
|
|
36
|
+
Toggle: ['button', ''],
|
|
37
|
+
'ToggleGroup.Root': ['div', '', { role: 'group', 'data-orientation': 'horizontal', 'data-variant': 'default', 'data-size': 'default' }],
|
|
38
|
+
'ToggleGroup.Item': ['button', ''],
|
|
39
|
+
'Trace.Root': ['div', ''],
|
|
40
|
+
'Trace.Collapsible': ['div', ''],
|
|
41
|
+
'Trace.CollapsibleContent': ['div', ''],
|
|
42
|
+
'Trace.CollapsibleTrigger': ['button', ''],
|
|
43
|
+
'Trace.Description': ['span', ''],
|
|
44
|
+
'Trace.Icon': ['span', ''],
|
|
45
|
+
'Trace.Line': ['span', ''],
|
|
46
|
+
'Trace.Title': ['span', ''],
|
|
47
|
+
Typography: ['p', ''],
|
|
48
|
+
};
|
|
49
|
+
export const markupPorts = {
|
|
50
|
+
AspectRatio: 'ratio',
|
|
51
|
+
'Attachment.Root': 'orientation size state',
|
|
52
|
+
'Attachment.Action': 'download size variant',
|
|
53
|
+
'Attachment.Media': 'variant',
|
|
54
|
+
'Bubble.Root': 'align variant',
|
|
55
|
+
'Bubble.Reactions': 'align side',
|
|
56
|
+
'CodeBlock.Root': 'size variant',
|
|
57
|
+
'CodeBlock.Content': 'code lineNumbers startLine wrap',
|
|
58
|
+
'CodeBlock.CopyButton': 'value size variant',
|
|
59
|
+
'CodeBlock.Line': 'code variant',
|
|
60
|
+
'Marker.Root': 'variant',
|
|
61
|
+
'Message.Root': 'align',
|
|
62
|
+
Toggle: 'pressed size variant value',
|
|
63
|
+
'ToggleGroup.Root': 'disabled loop orientation rovingFocus size spacing type value variant',
|
|
64
|
+
'ToggleGroup.Item': 'size value variant',
|
|
65
|
+
'Trace.Collapsible': 'disabled open',
|
|
66
|
+
'Trace.CollapsibleContent': 'forceMount hiddenUntilFound',
|
|
67
|
+
'Trace.Line': 'status',
|
|
68
|
+
Typography: 'as variant',
|
|
69
|
+
};
|
|
70
|
+
export const markupEvents = {
|
|
71
|
+
Toggle: 'pressedChange',
|
|
72
|
+
'ToggleGroup.Root': 'valueChange',
|
|
73
|
+
'Trace.Collapsible': 'openChange openChangeComplete',
|
|
74
|
+
};
|
|
75
|
+
const typographyTags = {
|
|
76
|
+
h1: 'h1', h2: 'h2', h3: 'h3', h4: 'h4', p: 'p', blockquote: 'blockquote',
|
|
77
|
+
code: 'code', lead: 'p', large: 'div', small: 'small', muted: 'p',
|
|
78
|
+
};
|
|
79
|
+
const allowedTypographyTags = new Set([
|
|
80
|
+
...Object.values(typographyTags), 'h5', 'h6', 'span', 'strong', 'em',
|
|
81
|
+
]);
|
|
82
|
+
export function markupTag(name, inputs) {
|
|
83
|
+
if (name === 'Slider')
|
|
84
|
+
return inputs.type === 'multiple' ? 'div' : 'input';
|
|
85
|
+
if (name === 'Button' || name === 'Attachment.Action' || name === 'CodeBlock.CopyButton')
|
|
86
|
+
return inputs.href ? 'a' : 'button';
|
|
87
|
+
if (name !== 'Typography')
|
|
88
|
+
return undefined;
|
|
89
|
+
const as = inputs.as;
|
|
90
|
+
if (allowedTypographyTags.has(as))
|
|
91
|
+
return as;
|
|
92
|
+
const variant = inputs.variant;
|
|
93
|
+
return Object.hasOwn(typographyTags, variant) ? typographyTags[variant] : 'p';
|
|
94
|
+
}
|
|
95
|
+
function toggle(element, send, signal) {
|
|
96
|
+
let pressed = false;
|
|
97
|
+
function syncState() {
|
|
98
|
+
element.setAttribute('aria-pressed', String(pressed));
|
|
99
|
+
element.dataset.state = pressed ? 'on' : 'off';
|
|
100
|
+
}
|
|
101
|
+
element.addEventListener('click', () => {
|
|
102
|
+
if (element.matches(':disabled, [aria-disabled="true"]'))
|
|
103
|
+
return;
|
|
104
|
+
pressed = !pressed;
|
|
105
|
+
syncState();
|
|
106
|
+
send('pressedChange', { value: pressed });
|
|
107
|
+
}, { signal });
|
|
108
|
+
return {
|
|
109
|
+
sync(inputs) {
|
|
110
|
+
if (typeof inputs.pressed === 'boolean')
|
|
111
|
+
pressed = inputs.pressed;
|
|
112
|
+
if (typeof inputs.value === 'string' && element.localName === 'button')
|
|
113
|
+
element.value = inputs.value;
|
|
114
|
+
syncState();
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const toggleItemSelector = '[data-vs-component="ToggleGroup.Item"]';
|
|
119
|
+
const toggleGroupSelector = '[data-vs-component="ToggleGroup.Root"]';
|
|
120
|
+
const disabledSelector = ':disabled, [aria-disabled="true"]';
|
|
121
|
+
function toggleGroup(element, send, signal) {
|
|
122
|
+
let props = {};
|
|
123
|
+
let selection = [];
|
|
124
|
+
let tabStop;
|
|
125
|
+
const isOwn = (item) => item.closest(toggleGroupSelector) === element;
|
|
126
|
+
const items = () => Array.from(element.querySelectorAll(toggleItemSelector)).filter(isOwn);
|
|
127
|
+
function render() {
|
|
128
|
+
const all = items();
|
|
129
|
+
for (const item of all) {
|
|
130
|
+
const disabled = props.disabled === true || readInputs(item).disabled === true;
|
|
131
|
+
item.disabled = disabled;
|
|
132
|
+
item.toggleAttribute('data-disabled', disabled);
|
|
133
|
+
item.dataset.orientation = props.orientation === 'vertical' ? 'vertical' : 'horizontal';
|
|
134
|
+
item.dataset.variant = typeof props.variant === 'string' ? props.variant : 'default';
|
|
135
|
+
item.dataset.size = typeof props.size === 'string' ? props.size : 'default';
|
|
136
|
+
const selected = selection.includes(item.dataset.value ?? '');
|
|
137
|
+
item.dataset.state = selected ? 'on' : 'off';
|
|
138
|
+
if (props.type === 'multiple') {
|
|
139
|
+
item.removeAttribute('role');
|
|
140
|
+
item.removeAttribute('aria-checked');
|
|
141
|
+
item.setAttribute('aria-pressed', String(selected));
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
item.setAttribute('role', 'radio');
|
|
145
|
+
item.removeAttribute('aria-pressed');
|
|
146
|
+
item.setAttribute('aria-checked', String(selected));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const enabled = all.filter(item => !item.matches(disabledSelector));
|
|
150
|
+
if (!tabStop || !enabled.includes(tabStop))
|
|
151
|
+
tabStop = enabled[0];
|
|
152
|
+
for (const item of all)
|
|
153
|
+
item.tabIndex = enabled.includes(item) && (props.rovingFocus === false || item === tabStop) ? 0 : -1;
|
|
154
|
+
}
|
|
155
|
+
function activate(item) {
|
|
156
|
+
const value = item.dataset.value;
|
|
157
|
+
if (value === undefined)
|
|
158
|
+
return;
|
|
159
|
+
if (selection.includes(value))
|
|
160
|
+
selection = selection.filter(selected => selected !== value);
|
|
161
|
+
else {
|
|
162
|
+
selection = props.type === 'multiple' ? [...selection, value] : [value];
|
|
163
|
+
tabStop = item;
|
|
164
|
+
}
|
|
165
|
+
render();
|
|
166
|
+
send('valueChange', { value: props.type === 'multiple' ? selection : selection[0] ?? '' });
|
|
167
|
+
}
|
|
168
|
+
element.addEventListener('click', event => {
|
|
169
|
+
const item = event.target?.closest(toggleItemSelector);
|
|
170
|
+
if (item && isOwn(item) && !item.matches(disabledSelector))
|
|
171
|
+
activate(item);
|
|
172
|
+
}, { signal });
|
|
173
|
+
element.addEventListener('keydown', event => {
|
|
174
|
+
const item = event.target?.closest(toggleItemSelector);
|
|
175
|
+
if (!item || !isOwn(item) || item.matches(disabledSelector))
|
|
176
|
+
return;
|
|
177
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
178
|
+
event.preventDefault();
|
|
179
|
+
activate(item);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (props.rovingFocus === false)
|
|
183
|
+
return;
|
|
184
|
+
const enabled = items().filter(candidate => !candidate.matches(disabledSelector));
|
|
185
|
+
const index = enabled.indexOf(item);
|
|
186
|
+
const vertical = props.orientation === 'vertical';
|
|
187
|
+
const rtl = element.ownerDocument.defaultView?.getComputedStyle(element).direction === 'rtl';
|
|
188
|
+
const nextKey = vertical ? 'ArrowDown' : rtl ? 'ArrowLeft' : 'ArrowRight';
|
|
189
|
+
const previousKey = vertical ? 'ArrowUp' : rtl ? 'ArrowRight' : 'ArrowLeft';
|
|
190
|
+
const nextIndex = event.key === nextKey ? index + 1 : event.key === previousKey ? index - 1
|
|
191
|
+
: event.key === 'Home' ? 0 : event.key === 'End' ? enabled.length - 1 : undefined;
|
|
192
|
+
if (nextIndex === undefined)
|
|
193
|
+
return;
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
const next = enabled[props.loop === false ? nextIndex : (nextIndex + enabled.length) % enabled.length];
|
|
196
|
+
if (!next)
|
|
197
|
+
return;
|
|
198
|
+
tabStop = next;
|
|
199
|
+
render();
|
|
200
|
+
next.focus();
|
|
201
|
+
}, { signal });
|
|
202
|
+
return {
|
|
203
|
+
sync(inputs) {
|
|
204
|
+
props = inputs;
|
|
205
|
+
if (typeof inputs.value === 'string')
|
|
206
|
+
selection = inputs.value ? [inputs.value] : [];
|
|
207
|
+
else if (Array.isArray(inputs.value))
|
|
208
|
+
selection = inputs.value.filter((value) => typeof value === 'string');
|
|
209
|
+
const spacing = typeof inputs.spacing === 'number' ? inputs.spacing : 0;
|
|
210
|
+
element.dataset.spacing = String(spacing);
|
|
211
|
+
element.style.setProperty('--vs-toggle-spacing', String(spacing));
|
|
212
|
+
render();
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function traceCollapsible(element, send, signal) {
|
|
217
|
+
let open = false;
|
|
218
|
+
let initialized = false;
|
|
219
|
+
let disabled = false;
|
|
220
|
+
let animation;
|
|
221
|
+
let content = null;
|
|
222
|
+
let trigger = null;
|
|
223
|
+
function render(animate) {
|
|
224
|
+
element.dataset.state = open ? 'open' : 'closed';
|
|
225
|
+
if (trigger) {
|
|
226
|
+
trigger.setAttribute('aria-expanded', String(open));
|
|
227
|
+
trigger.disabled = disabled || readInputs(trigger).disabled === true;
|
|
228
|
+
}
|
|
229
|
+
if (!content)
|
|
230
|
+
return;
|
|
231
|
+
content.dataset.state = element.dataset.state;
|
|
232
|
+
const contentInputs = readInputs(content);
|
|
233
|
+
const finalHidden = () => {
|
|
234
|
+
if (open || (contentInputs.forceMount === true && contentInputs.hiddenUntilFound !== true))
|
|
235
|
+
content?.removeAttribute('hidden');
|
|
236
|
+
else
|
|
237
|
+
content?.setAttribute('hidden', contentInputs.hiddenUntilFound === true ? 'until-found' : '');
|
|
238
|
+
};
|
|
239
|
+
if (!animate) {
|
|
240
|
+
if (!animation)
|
|
241
|
+
finalHidden();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const start = content.hasAttribute('hidden') ? 0 : content.getBoundingClientRect().height;
|
|
245
|
+
animation?.cancel();
|
|
246
|
+
content.removeAttribute('hidden');
|
|
247
|
+
if (element.ownerDocument.defaultView?.matchMedia('(prefers-reduced-motion: reduce)').matches || typeof content.animate !== 'function') {
|
|
248
|
+
animation = undefined;
|
|
249
|
+
finalHidden();
|
|
250
|
+
send('openChangeComplete', { value: open });
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const end = open ? content.getBoundingClientRect().height : 0;
|
|
254
|
+
const current = content.animate([{ height: start + 'px' }, { height: end + 'px' }], {
|
|
255
|
+
duration: 200, easing: 'ease-out',
|
|
256
|
+
});
|
|
257
|
+
animation = current;
|
|
258
|
+
void current.finished.then(() => {
|
|
259
|
+
if (signal.aborted || animation !== current)
|
|
260
|
+
return;
|
|
261
|
+
animation = undefined;
|
|
262
|
+
finalHidden();
|
|
263
|
+
send('openChangeComplete', { value: open });
|
|
264
|
+
}, () => { });
|
|
265
|
+
}
|
|
266
|
+
function change(next) {
|
|
267
|
+
if (open === next || disabled)
|
|
268
|
+
return;
|
|
269
|
+
open = next;
|
|
270
|
+
send('openChange', { value: open });
|
|
271
|
+
render(true);
|
|
272
|
+
}
|
|
273
|
+
element.addEventListener('click', event => {
|
|
274
|
+
if (trigger?.contains(event.target) && !trigger.disabled)
|
|
275
|
+
change(!open);
|
|
276
|
+
}, { signal });
|
|
277
|
+
element.addEventListener('keydown', event => {
|
|
278
|
+
if ((event.key !== 'Enter' && event.key !== ' ') || !trigger?.contains(event.target) || trigger.disabled)
|
|
279
|
+
return;
|
|
280
|
+
event.preventDefault();
|
|
281
|
+
change(!open);
|
|
282
|
+
}, { signal });
|
|
283
|
+
element.addEventListener('beforematch', event => {
|
|
284
|
+
if (event.target === content)
|
|
285
|
+
change(true);
|
|
286
|
+
}, { signal });
|
|
287
|
+
return {
|
|
288
|
+
sync(inputs) {
|
|
289
|
+
disabled = inputs.disabled === true;
|
|
290
|
+
const nextTrigger = element.querySelector(':scope > [data-vs-component="Trace.CollapsibleTrigger"]');
|
|
291
|
+
const nextContent = element.querySelector(':scope > [data-vs-component="Trace.CollapsibleContent"]');
|
|
292
|
+
if (nextContent !== content) {
|
|
293
|
+
animation?.cancel();
|
|
294
|
+
animation = undefined;
|
|
295
|
+
content = nextContent;
|
|
296
|
+
}
|
|
297
|
+
trigger = nextTrigger;
|
|
298
|
+
if (content && trigger) {
|
|
299
|
+
content.id ||= uniqueId();
|
|
300
|
+
trigger.setAttribute('aria-controls', content.id);
|
|
301
|
+
}
|
|
302
|
+
const previous = open;
|
|
303
|
+
if (typeof inputs.open === 'boolean')
|
|
304
|
+
open = inputs.open;
|
|
305
|
+
render(initialized && open !== previous);
|
|
306
|
+
initialized = true;
|
|
307
|
+
},
|
|
308
|
+
dispose() { animation?.cancel(); },
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
const tokenStyles = ['color', 'background-color', 'font-style', 'font-weight', 'text-decoration', 'text-decoration-color'];
|
|
312
|
+
function codeText(element, name) {
|
|
313
|
+
const doc = element.ownerDocument;
|
|
314
|
+
const host = name === 'CodeBlock.Content' ? element.querySelector('code') ?? element : element;
|
|
315
|
+
const generated = styleScope.createElement(doc, 'span');
|
|
316
|
+
generated.dataset.vsCodeText = '';
|
|
317
|
+
const styleParser = doc.createElement('span').style;
|
|
318
|
+
let previous;
|
|
319
|
+
let previousNumbered = false;
|
|
320
|
+
return {
|
|
321
|
+
sync(inputs) {
|
|
322
|
+
const numbered = name === 'CodeBlock.Content' && inputs.lineNumbers === true;
|
|
323
|
+
if (name === 'CodeBlock.Content') {
|
|
324
|
+
element.toggleAttribute('data-numbered', numbered);
|
|
325
|
+
element.toggleAttribute('data-wrap', inputs.wrap === true);
|
|
326
|
+
element.style.counterReset = 'vs-code-line ' + (typeof inputs.startLine === 'number' ? Math.trunc(inputs.startLine) - 1 : 0);
|
|
327
|
+
}
|
|
328
|
+
if (inputs.code === undefined) {
|
|
329
|
+
generated.remove();
|
|
330
|
+
previous = undefined;
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (!generated.isConnected)
|
|
334
|
+
host.append(generated);
|
|
335
|
+
if (inputs.code === previous && numbered === previousNumbered)
|
|
336
|
+
return;
|
|
337
|
+
previous = inputs.code;
|
|
338
|
+
previousNumbered = numbered;
|
|
339
|
+
if (typeof inputs.code === 'string') {
|
|
340
|
+
if (numbered) {
|
|
341
|
+
generated.replaceChildren(...inputs.code.replace(/\n$/, '').split('\n').map(value => {
|
|
342
|
+
const line = styleScope.createElement(doc, 'span');
|
|
343
|
+
line.dataset.vsComponent = 'CodeBlock.Line';
|
|
344
|
+
line.textContent = value || '\n';
|
|
345
|
+
return line;
|
|
346
|
+
}));
|
|
347
|
+
}
|
|
348
|
+
else
|
|
349
|
+
generated.textContent = inputs.code || (name === 'CodeBlock.Line' ? '\n' : '');
|
|
350
|
+
}
|
|
351
|
+
else if (Array.isArray(inputs.code)) {
|
|
352
|
+
generated.replaceChildren(...inputs.code.map(token => {
|
|
353
|
+
const span = styleScope.createElement(doc, 'span');
|
|
354
|
+
span.textContent = token.content;
|
|
355
|
+
if (token.class)
|
|
356
|
+
span.className = token.class;
|
|
357
|
+
if (token.style) {
|
|
358
|
+
styleParser.cssText = token.style;
|
|
359
|
+
for (const property of tokenStyles) {
|
|
360
|
+
const value = styleParser.getPropertyValue(property);
|
|
361
|
+
if (value)
|
|
362
|
+
span.style.setProperty(property, value);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return span;
|
|
366
|
+
}));
|
|
367
|
+
if (!inputs.code.length)
|
|
368
|
+
generated.textContent = '\n';
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
function disableAction(element, signal) {
|
|
374
|
+
element.addEventListener('click', event => {
|
|
375
|
+
if (!element.matches(disabledSelector))
|
|
376
|
+
return;
|
|
377
|
+
event.preventDefault();
|
|
378
|
+
event.stopImmediatePropagation();
|
|
379
|
+
}, { capture: true, signal });
|
|
380
|
+
return (inputs) => {
|
|
381
|
+
if (element.localName !== 'a')
|
|
382
|
+
return;
|
|
383
|
+
if (inputs.disabled === true) {
|
|
384
|
+
element.removeAttribute('href');
|
|
385
|
+
element.setAttribute('role', 'link');
|
|
386
|
+
element.setAttribute('aria-disabled', 'true');
|
|
387
|
+
element.tabIndex = -1;
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
if (typeof inputs.role !== 'string')
|
|
391
|
+
element.removeAttribute('role');
|
|
392
|
+
if (inputs['aria-disabled'] === undefined)
|
|
393
|
+
element.removeAttribute('aria-disabled');
|
|
394
|
+
if (inputs.tabindex === undefined)
|
|
395
|
+
element.removeAttribute('tabindex');
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
function copyButton(element, signal) {
|
|
400
|
+
const syncDisabled = disableAction(element, signal);
|
|
401
|
+
const doc = element.ownerDocument;
|
|
402
|
+
const win = doc.defaultView;
|
|
403
|
+
const status = styleScope.createElement(doc, 'span');
|
|
404
|
+
status.className = 'vs-code-copy-status';
|
|
405
|
+
status.setAttribute('aria-live', 'polite');
|
|
406
|
+
let reset;
|
|
407
|
+
let value = '';
|
|
408
|
+
let customLabel;
|
|
409
|
+
const label = () => element.setAttribute('aria-label', customLabel ?? (element.hasAttribute('data-copied') ? 'Copied' : 'Copy'));
|
|
410
|
+
element.addEventListener('click', () => {
|
|
411
|
+
if (element.matches(disabledSelector))
|
|
412
|
+
return;
|
|
413
|
+
const clipboard = win.navigator.clipboard;
|
|
414
|
+
if (!clipboard)
|
|
415
|
+
return;
|
|
416
|
+
void clipboard.writeText(value).then(() => {
|
|
417
|
+
if (signal.aborted)
|
|
418
|
+
return;
|
|
419
|
+
element.setAttribute('data-copied', '');
|
|
420
|
+
status.textContent = 'Copied';
|
|
421
|
+
label();
|
|
422
|
+
win.clearTimeout(reset);
|
|
423
|
+
reset = win.setTimeout(() => {
|
|
424
|
+
element.removeAttribute('data-copied');
|
|
425
|
+
status.textContent = '';
|
|
426
|
+
label();
|
|
427
|
+
}, 2000);
|
|
428
|
+
}, () => { });
|
|
429
|
+
}, { signal });
|
|
430
|
+
return {
|
|
431
|
+
sync(inputs) {
|
|
432
|
+
if (status.parentElement !== element)
|
|
433
|
+
element.append(status);
|
|
434
|
+
value = typeof inputs.value === 'string' ? inputs.value : '';
|
|
435
|
+
customLabel = typeof inputs['aria-label'] === 'string' ? inputs['aria-label'] : undefined;
|
|
436
|
+
label();
|
|
437
|
+
syncDisabled(inputs);
|
|
438
|
+
},
|
|
439
|
+
dispose() { win.clearTimeout(reset); },
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
export function markupBehavior(name, element, send, signal) {
|
|
443
|
+
switch (name) {
|
|
444
|
+
case 'Button': {
|
|
445
|
+
const syncDisabled = disableAction(element, signal);
|
|
446
|
+
return { sync: syncDisabled };
|
|
447
|
+
}
|
|
448
|
+
case 'AspectRatio':
|
|
449
|
+
return {
|
|
450
|
+
sync(inputs) {
|
|
451
|
+
const ratio = typeof inputs.ratio === 'number' ? inputs.ratio : 1;
|
|
452
|
+
if (element.parentElement?.classList.contains('vs-ui-aspect-frame')) {
|
|
453
|
+
element.parentElement.style.paddingBlockEnd = (ratio > 0 ? 100 / ratio : 0) + '%';
|
|
454
|
+
}
|
|
455
|
+
else
|
|
456
|
+
element.style.aspectRatio = ratio > 0 ? String(ratio) : 'auto';
|
|
457
|
+
},
|
|
458
|
+
};
|
|
459
|
+
case 'Attachment.Root':
|
|
460
|
+
return { sync(inputs) { element.dataset.state = typeof inputs.state === 'string' ? inputs.state : 'done'; } };
|
|
461
|
+
case 'Attachment.Action': {
|
|
462
|
+
const syncDisabled = disableAction(element, signal);
|
|
463
|
+
return { sync(inputs) {
|
|
464
|
+
syncDisabled(inputs);
|
|
465
|
+
element.toggleAttribute('data-vs-labelled', typeof inputs.text === 'string' && inputs.text.trim() !== '');
|
|
466
|
+
if (element.localName !== 'a')
|
|
467
|
+
return;
|
|
468
|
+
if (inputs.download === true || typeof inputs.download === 'string') {
|
|
469
|
+
element.setAttribute('download', inputs.download === true ? '' : inputs.download);
|
|
470
|
+
}
|
|
471
|
+
else
|
|
472
|
+
element.removeAttribute('download');
|
|
473
|
+
} };
|
|
474
|
+
}
|
|
475
|
+
case 'Bubble.Reactions':
|
|
476
|
+
return { sync(inputs) { element.dataset.side = inputs.side === 'top' ? 'top' : 'bottom'; } };
|
|
477
|
+
case 'Trace.Line':
|
|
478
|
+
return { sync(inputs) {
|
|
479
|
+
if (typeof inputs.status === 'string')
|
|
480
|
+
element.dataset.status = inputs.status;
|
|
481
|
+
else
|
|
482
|
+
delete element.dataset.status;
|
|
483
|
+
} };
|
|
484
|
+
case 'Toggle': return toggle(element, send, signal);
|
|
485
|
+
case 'ToggleGroup.Root': return toggleGroup(element, send, signal);
|
|
486
|
+
case 'Trace.Collapsible': return traceCollapsible(element, send, signal);
|
|
487
|
+
case 'CodeBlock.Content':
|
|
488
|
+
case 'CodeBlock.Line': return codeText(element, name);
|
|
489
|
+
case 'CodeBlock.CopyButton': return copyButton(element, signal);
|
|
490
|
+
default: return undefined;
|
|
491
|
+
}
|
|
492
|
+
}
|
package/dist/menus.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Behavior, type Send } from './behaviors.js';
|
|
2
|
+
import type { Markup } from './dynamic-catalog.js';
|
|
3
|
+
type Family = 'DropdownMenu' | 'ContextMenu' | 'Menubar';
|
|
4
|
+
export declare const menuCatalog: Record<string, Markup>;
|
|
5
|
+
export declare const menuPorts: Record<string, string>;
|
|
6
|
+
export declare const menuEvents: Record<string, string>;
|
|
7
|
+
export declare function menus(root: HTMLElement, family: Family, send: Send, signal: AbortSignal): Behavior;
|
|
8
|
+
export {};
|