@haiyue/ui 0.2.1 → 0.3.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.
Files changed (139) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/LICENSE +27 -0
  3. package/README.md +707 -5
  4. package/dist/adapters/web/controls.js +28 -5
  5. package/dist/adapters/web/renderer.js +11 -10
  6. package/dist/adapters/web/styles.d.ts +2 -3
  7. package/dist/adapters/web/styles.js +42 -18
  8. package/dist/alert.d.ts +39 -0
  9. package/dist/alert.js +144 -0
  10. package/dist/anchor.d.ts +60 -0
  11. package/dist/anchor.js +274 -0
  12. package/dist/auto-complete.d.ts +83 -0
  13. package/dist/auto-complete.js +267 -0
  14. package/dist/avatar.d.ts +45 -0
  15. package/dist/avatar.js +135 -0
  16. package/dist/badge.js +5 -5
  17. package/dist/border-beam.js +1 -1
  18. package/dist/breadcrumb.js +5 -5
  19. package/dist/button-group.js +8 -8
  20. package/dist/button.js +10 -10
  21. package/dist/card.d.ts +29 -0
  22. package/dist/card.js +101 -0
  23. package/dist/carousel.d.ts +92 -0
  24. package/dist/carousel.js +509 -0
  25. package/dist/cascader.js +12 -12
  26. package/dist/checkbox.d.ts +12 -0
  27. package/dist/checkbox.js +22 -6
  28. package/dist/collapse.d.ts +13 -0
  29. package/dist/collapse.js +100 -13
  30. package/dist/context-menu.js +3 -3
  31. package/dist/core/types.d.ts +22 -3
  32. package/dist/core/validation.js +9 -4
  33. package/dist/date-picker.js +13 -13
  34. package/dist/descriptions.d.ts +44 -0
  35. package/dist/descriptions.js +169 -0
  36. package/dist/dialog.js +11 -11
  37. package/dist/divider.js +2 -2
  38. package/dist/draggable.js +1 -1
  39. package/dist/drawer.js +12 -12
  40. package/dist/dropdown.js +3 -3
  41. package/dist/empty.d.ts +20 -0
  42. package/dist/empty.js +58 -0
  43. package/dist/expandable.js +4 -4
  44. package/dist/form-item.d.ts +60 -0
  45. package/dist/form-item.js +212 -0
  46. package/dist/form.d.ts +41 -0
  47. package/dist/form.js +154 -0
  48. package/dist/history-controls.js +5 -5
  49. package/dist/image-preview.d.ts +22 -0
  50. package/dist/image-preview.js +38 -0
  51. package/dist/image.d.ts +52 -0
  52. package/dist/image.js +152 -0
  53. package/dist/index.d.ts +56 -0
  54. package/dist/index.js +84 -0
  55. package/dist/input.d.ts +11 -0
  56. package/dist/input.js +29 -15
  57. package/dist/internal/anchored-popup.d.ts +19 -0
  58. package/dist/internal/anchored-popup.js +89 -0
  59. package/dist/internal/form-control.d.ts +22 -0
  60. package/dist/internal/form-control.js +52 -0
  61. package/dist/internal/image-viewer.d.ts +51 -0
  62. package/dist/internal/image-viewer.js +210 -0
  63. package/dist/internal/pagination.d.ts +35 -0
  64. package/dist/internal/pagination.js +169 -0
  65. package/dist/internal/popover-base.d.ts +87 -0
  66. package/dist/internal/popover-base.js +365 -0
  67. package/dist/internal/presentation.d.ts +2 -0
  68. package/dist/internal/presentation.js +28 -0
  69. package/dist/internal/qrcodegen.d.ts +94 -0
  70. package/dist/internal/qrcodegen.js +856 -0
  71. package/dist/masonry.d.ts +36 -0
  72. package/dist/masonry.js +171 -0
  73. package/dist/mentions.d.ts +115 -0
  74. package/dist/mentions.js +485 -0
  75. package/dist/menu-shared.d.ts +1 -1
  76. package/dist/menu-shared.js +3 -3
  77. package/dist/menu.d.ts +63 -0
  78. package/dist/menu.js +339 -0
  79. package/dist/modal.d.ts +73 -0
  80. package/dist/modal.js +361 -0
  81. package/dist/notification.js +9 -9
  82. package/dist/pagination.d.ts +34 -0
  83. package/dist/pagination.js +73 -0
  84. package/dist/popconfirm.d.ts +6 -0
  85. package/dist/popconfirm.js +7 -0
  86. package/dist/popover.d.ts +6 -0
  87. package/dist/popover.js +7 -0
  88. package/dist/portable/icons.d.ts +5 -0
  89. package/dist/portable/icons.js +24 -0
  90. package/dist/portable/index.d.ts +3 -0
  91. package/dist/portable/index.js +2 -0
  92. package/dist/portable/responsive.d.ts +20 -0
  93. package/dist/portable/responsive.js +15 -0
  94. package/dist/portable/theme.d.ts +6 -1
  95. package/dist/portable/theme.js +23 -3
  96. package/dist/progress.d.ts +30 -0
  97. package/dist/progress.js +102 -0
  98. package/dist/qr-code.d.ts +62 -0
  99. package/dist/qr-code.js +224 -0
  100. package/dist/radio.d.ts +10 -0
  101. package/dist/radio.js +18 -5
  102. package/dist/range.js +7 -7
  103. package/dist/rate.js +3 -3
  104. package/dist/resizable.js +4 -4
  105. package/dist/result.d.ts +22 -0
  106. package/dist/result.js +88 -0
  107. package/dist/ribbon.d.ts +19 -0
  108. package/dist/ribbon.js +106 -0
  109. package/dist/segmented.d.ts +39 -0
  110. package/dist/segmented.js +192 -0
  111. package/dist/select.d.ts +12 -0
  112. package/dist/select.js +31 -13
  113. package/dist/skeleton.js +10 -10
  114. package/dist/spin.d.ts +26 -0
  115. package/dist/spin.js +80 -0
  116. package/dist/steps.js +13 -13
  117. package/dist/switch.d.ts +44 -0
  118. package/dist/switch.js +104 -0
  119. package/dist/table.d.ts +0 -2
  120. package/dist/table.js +46 -87
  121. package/dist/tabs.js +7 -7
  122. package/dist/tag.js +8 -8
  123. package/dist/time-picker.js +10 -10
  124. package/dist/timeline.js +12 -12
  125. package/dist/toast.js +9 -9
  126. package/dist/tooltip.js +4 -4
  127. package/dist/tour.js +11 -11
  128. package/dist/tree-node.js +2 -2
  129. package/dist/tree-select.d.ts +94 -0
  130. package/dist/tree-select.js +398 -0
  131. package/dist/tree.js +6 -5
  132. package/dist/upload.d.ts +107 -0
  133. package/dist/upload.js +353 -0
  134. package/dist/virtual-keyboard.js +5 -5
  135. package/dist/virtual-list.js +4 -4
  136. package/dist/virtual-mouse.js +5 -5
  137. package/package.json +99 -1
  138. package/themes/haiyue-dark.css +51 -0
  139. package/themes/haiyue-light.css +51 -0
@@ -0,0 +1,398 @@
1
+ import { HYAnchoredPopup, HY_COMBOBOX_STYLES } from './internal/anchored-popup.js';
2
+ let treeSelectId = 0;
3
+ export class HYTreeSelect extends HTMLElement {
4
+ static formAssociated = true;
5
+ _internals = this.attachInternals();
6
+ _control = document.createElement('div');
7
+ _tags = document.createElement('span');
8
+ _input = document.createElement('input');
9
+ _clear = document.createElement('button');
10
+ _panel = document.createElement('div');
11
+ _tree = document.createElement('div');
12
+ _status = document.createElement('div');
13
+ _id = `hy-tree-select-${++treeSelectId}`;
14
+ _popup = new HYAnchoredPopup(this, this._control, this._panel, () => this._setOpen(false));
15
+ _options = [];
16
+ _entries = new Map();
17
+ _expanded = new Set();
18
+ _visible = [];
19
+ _active = '';
20
+ _query = '';
21
+ _composing = false;
22
+ _formDisabled = false;
23
+ _initialValues = null;
24
+ _listeners = null;
25
+ _customError = '';
26
+ static get observedAttributes() { return ['options', 'value', 'multiple', 'expanded-keys', 'open', 'disabled', 'readonly', 'searchable', 'clearable', 'required', 'loading', 'name', 'placeholder', 'aria-label', 'not-found-content']; }
27
+ constructor() {
28
+ super();
29
+ const root = this.attachShadow({ mode: 'open' });
30
+ const style = document.createElement('style');
31
+ style.textContent = HY_COMBOBOX_STYLES + `
32
+ .control { flex-wrap: wrap; cursor: text; } .tags { display: contents; } .tag { display: inline-flex; align-items: center; gap: 3px; max-width: 100%; margin-block: 3px; padding-inline: 5px; border-radius: var(--hy-radius-xs, 2px); background: var(--hy-hover-bg-color, #273241); }
33
+ .tag-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .tag button { border: 0; background: transparent; color: inherit; cursor: pointer; font: inherit; }
34
+ .option { display: flex; align-items: center; gap: 4px; padding-inline-start: calc(var(--_depth, 0) * 16px + 4px); }
35
+ .toggle { width: 22px; flex: 0 0 22px; height: 24px; padding: 0; border: 0; background: transparent; color: inherit; font: inherit; cursor: pointer; }
36
+ .check { flex: 0 0 14px; width: 14px; height: 14px; display: grid; place-items: center; border: 1px solid var(--hy-border-color, #303746); border-radius: 2px; }
37
+ [aria-checked="true"] .check { background: var(--hy-accent-strong-color, #554cc6); color: var(--hy-selected-text-color, #fff); } .label { min-width: 0; overflow-wrap: anywhere; }
38
+ `;
39
+ this._control.className = 'control';
40
+ this._control.part.add('control');
41
+ this._tags.className = 'tags';
42
+ this._tags.part.add('tags');
43
+ this._input.type = 'text';
44
+ this._input.autocomplete = 'off';
45
+ this._input.part.add('input');
46
+ this._input.setAttribute('role', 'combobox');
47
+ this._input.setAttribute('aria-haspopup', 'tree');
48
+ this._input.setAttribute('aria-controls', this._id);
49
+ this._input.setAttribute('aria-autocomplete', 'list');
50
+ this._clear.type = 'button';
51
+ this._clear.className = 'clear';
52
+ this._clear.textContent = '×';
53
+ this._clear.part.add('clear');
54
+ this._clear.setAttribute('aria-label', 'Clear selection');
55
+ this._panel.className = 'panel';
56
+ this._panel.part.add('panel');
57
+ this._tree.id = this._id;
58
+ this._tree.setAttribute('role', 'tree');
59
+ this._tree.part.add('tree');
60
+ this._status.className = 'status';
61
+ this._status.part.add('status');
62
+ this._status.setAttribute('role', 'status');
63
+ this._panel.append(this._tree, this._status);
64
+ this._control.append(this._tags, this._input, this._clear);
65
+ root.append(style, this._control, this._panel);
66
+ }
67
+ connectedCallback() {
68
+ this._initialValues ??= this.values;
69
+ this._listeners?.abort();
70
+ this._listeners = new AbortController();
71
+ const { signal } = this._listeners;
72
+ this._input.addEventListener('focus', () => this._setOpen(true), { signal });
73
+ this._input.addEventListener('click', () => this._setOpen(true), { signal });
74
+ this._input.addEventListener('blur', () => this._setOpen(false), { signal });
75
+ this._input.addEventListener('keydown', this._onKeydown, { signal });
76
+ this._input.addEventListener('input', () => { this._query = this._input.value; this._active = ''; this._setOpen(true); this._render(); if (!this._composing)
77
+ this._emitSearch(); }, { signal });
78
+ this._input.addEventListener('compositionstart', () => { this._composing = true; }, { signal });
79
+ this._input.addEventListener('compositionend', () => { this._composing = false; this._query = this._input.value; this._render(); this._emitSearch(); }, { signal });
80
+ this._panel.addEventListener('pointerdown', event => event.preventDefault(), { signal });
81
+ this._panel.addEventListener('click', this._onTreeClick, { signal });
82
+ this._clear.addEventListener('click', () => { if (this._disabled || this.readOnly)
83
+ return; this._query = ''; this._commit([]); this._input.focus(); this._setOpen(false); this.dispatchEvent(new Event('clear', { bubbles: true, composed: true })); }, { signal });
84
+ this._tags.addEventListener('click', event => { const button = event.target instanceof Element ? event.target.closest('button[data-remove]') : null; if (button && !this._disabled && !this.readOnly)
85
+ this._commit(this.values.filter(value => value !== button.dataset.remove)); }, { signal });
86
+ this._popup.connect();
87
+ this._sync();
88
+ }
89
+ disconnectedCallback() { this._listeners?.abort(); this._listeners = null; this._popup.dispose(); this._composing = false; }
90
+ attributeChangedCallback(name, previous, next) {
91
+ if (previous === next)
92
+ return;
93
+ if (name === 'options') {
94
+ let value = [];
95
+ try {
96
+ value = JSON.parse(next ?? '[]');
97
+ }
98
+ catch { /* Invalid options are empty. */ }
99
+ this._setOptions(value);
100
+ }
101
+ if (name === 'expanded-keys') {
102
+ try {
103
+ const value = JSON.parse(next ?? '[]');
104
+ this._expanded = new Set(Array.isArray(value) ? value.filter((key) => typeof key === 'string') : []);
105
+ }
106
+ catch {
107
+ this._expanded.clear();
108
+ }
109
+ }
110
+ this._sync();
111
+ if (this._disabled || this.readOnly)
112
+ this._setOpen(false);
113
+ }
114
+ get options() { return this._options.map(cloneOption); }
115
+ set options(value) { this._setOptions(value); this._sync(); }
116
+ get values() { const raw = this.getAttribute('value') ?? ''; if (!this.multiple)
117
+ return raw ? [raw] : []; try {
118
+ const value = JSON.parse(raw || '[]');
119
+ return Array.isArray(value) ? [...new Set(value.filter((item) => typeof item === 'string' && item.length > 0))] : raw ? [raw] : [];
120
+ }
121
+ catch {
122
+ return raw ? [raw] : [];
123
+ } }
124
+ set values(value) { this.value = this.multiple ? value : value[0] ?? ''; }
125
+ get value() { return this.multiple ? this.values : this.values[0] ?? ''; }
126
+ set value(value) { const values = Array.isArray(value) ? value : value ? [String(value)] : []; this.setAttribute('value', this.multiple ? JSON.stringify([...new Set(values)]) : values[0] ?? ''); }
127
+ get multiple() { return this.hasAttribute('multiple'); }
128
+ set multiple(value) { const values = this.values; this.toggleAttribute('multiple', value); this.values = values; }
129
+ get disabled() { return this.hasAttribute('disabled'); }
130
+ set disabled(value) { this.toggleAttribute('disabled', value); }
131
+ get readOnly() { return this.hasAttribute('readonly'); }
132
+ set readOnly(value) { this.toggleAttribute('readonly', value); }
133
+ get searchable() { return this.getAttribute('searchable') !== 'false'; }
134
+ set searchable(value) { this.setAttribute('searchable', String(value)); }
135
+ get clearable() { return this.getAttribute('clearable') !== 'false'; }
136
+ set clearable(value) { this.setAttribute('clearable', String(value)); }
137
+ get loading() { return this.hasAttribute('loading'); }
138
+ set loading(value) { this.toggleAttribute('loading', value); }
139
+ get open() { return this.hasAttribute('open'); }
140
+ set open(value) { this._setOpen(value); }
141
+ get expandedKeys() { return [...this._expanded]; }
142
+ set expandedKeys(value) { this.setAttribute('expanded-keys', JSON.stringify(value)); }
143
+ get placeholder() { return this.getAttribute('placeholder') ?? 'Select an item'; }
144
+ set placeholder(value) { this.setAttribute('placeholder', value); }
145
+ get form() { return this._internals.form; }
146
+ get validity() { return this._internals.validity; }
147
+ get validationMessage() { return this._internals.validationMessage; }
148
+ checkValidity() { return this._internals.checkValidity(); }
149
+ reportValidity() { return this._internals.reportValidity(); }
150
+ setCustomValidity(message) { this._customError = message; this._syncForm(); }
151
+ formDisabledCallback(disabled) { this._formDisabled = disabled; this._sync(); if (disabled)
152
+ this._setOpen(false); }
153
+ formResetCallback() { this.values = this._initialValues ?? []; this._query = ''; this._customError = ''; this._setOpen(false); this._sync(); }
154
+ formStateRestoreCallback(state) { if (typeof state === 'string') {
155
+ try {
156
+ const values = JSON.parse(state);
157
+ if (Array.isArray(values))
158
+ this.values = values.filter((value) => typeof value === 'string');
159
+ }
160
+ catch {
161
+ this.value = state;
162
+ }
163
+ } }
164
+ focus(options) { this._input.focus(options); }
165
+ blur() { this._input.blur(); }
166
+ get _disabled() { return this.disabled || this._formDisabled; }
167
+ _setOptions(value) {
168
+ this._options = normalizeTree(value);
169
+ this._entries.clear();
170
+ const build = (options, parent, depth, disabled) => options.flatMap((option, position) => {
171
+ const row = document.createElement('div');
172
+ row.className = 'option';
173
+ row.part.add('option');
174
+ row.id = `${this._id}-${this._entries.size}`;
175
+ row.dataset.value = option.value;
176
+ row.setAttribute('role', 'treeitem');
177
+ row.style.setProperty('--_depth', String(depth));
178
+ row.setAttribute('aria-level', String(depth + 1));
179
+ row.setAttribute('aria-posinset', String(position + 1));
180
+ row.setAttribute('aria-setsize', String(options.length));
181
+ const toggle = document.createElement('button');
182
+ toggle.type = 'button';
183
+ toggle.className = 'toggle';
184
+ toggle.part.add('toggle');
185
+ toggle.tabIndex = -1;
186
+ toggle.dataset.toggle = option.value;
187
+ toggle.textContent = option.children?.length ? '›' : '';
188
+ toggle.setAttribute('aria-label', `Expand ${option.label}`);
189
+ toggle.disabled = !option.children?.length || disabled || Boolean(option.disabled);
190
+ const check = document.createElement('span');
191
+ check.className = 'check';
192
+ check.setAttribute('aria-hidden', 'true');
193
+ const label = document.createElement('span');
194
+ label.className = 'label';
195
+ label.textContent = option.label;
196
+ row.append(toggle, check, label);
197
+ const entry = { option, parent, depth, disabled: disabled || Boolean(option.disabled), row, toggle };
198
+ this._entries.set(option.value, entry);
199
+ if (option.expanded)
200
+ this._expanded.add(option.value);
201
+ return [row, ...build(option.children ?? [], option.value, depth + 1, entry.disabled)];
202
+ });
203
+ this._tree.replaceChildren(...build(this._options, null, 0, false));
204
+ }
205
+ _sync() {
206
+ if (this._input.disabled !== this._disabled)
207
+ this._input.disabled = this._disabled;
208
+ this._input.readOnly = !this.searchable || this.readOnly;
209
+ this._control.classList.toggle('disabled', this._disabled);
210
+ const text = this.open ? this._query : this.multiple ? '' : this._entries.get(this.values[0] ?? '')?.option.label ?? this.values[0] ?? '';
211
+ if (this._input.value !== text)
212
+ this._input.value = text;
213
+ if (this._input.placeholder !== this.placeholder)
214
+ this._input.placeholder = this.placeholder;
215
+ this._input.setAttribute('aria-label', this.getAttribute('aria-label') || 'Tree selection');
216
+ this._clear.hidden = !this.clearable || !this.values.length || this.readOnly;
217
+ this._clear.disabled = this._disabled;
218
+ const tagKey = JSON.stringify(this.multiple ? this.values.map(value => [value, this._entries.get(value)?.option.label ?? value]) : []);
219
+ if (this._tags.dataset.values !== tagKey) {
220
+ this._tags.dataset.values = tagKey;
221
+ this._tags.replaceChildren(...(this.multiple ? this.values : []).map(value => { const tag = document.createElement('span'); tag.className = 'tag'; tag.part.add('tag'); const label = document.createElement('span'); label.className = 'tag-label'; label.textContent = this._entries.get(value)?.option.label ?? value; const remove = document.createElement('button'); remove.type = 'button'; remove.textContent = '×'; remove.dataset.remove = value; remove.setAttribute('aria-label', `Remove ${label.textContent}`); tag.append(label, remove); return tag; }));
222
+ }
223
+ for (const button of this._tags.querySelectorAll('button'))
224
+ button.disabled = this._disabled || this.readOnly;
225
+ this._syncForm();
226
+ this._render();
227
+ }
228
+ _syncForm() {
229
+ const name = this.getAttribute('name') ?? '';
230
+ const values = this.values;
231
+ if (this._disabled)
232
+ this._internals.setFormValue(null, JSON.stringify(values));
233
+ else if (this.multiple) {
234
+ const data = new FormData();
235
+ for (const value of values)
236
+ if (name)
237
+ data.append(name, value);
238
+ this._internals.setFormValue(data, JSON.stringify(values));
239
+ }
240
+ else
241
+ this._internals.setFormValue(values[0] ?? '', JSON.stringify(values));
242
+ const missing = !this._disabled && this.hasAttribute('required') && !values.length;
243
+ this._internals.setValidity(this._disabled ? {} : this._customError ? { customError: true } : missing ? { valueMissing: true } : {}, this._disabled ? '' : this._customError || (missing ? 'Select an item' : ''), this._input);
244
+ }
245
+ _render() {
246
+ const query = this._query.toLocaleLowerCase();
247
+ const matches = new Set();
248
+ if (query)
249
+ for (const entry of this._entries.values())
250
+ if (entry.option.label.toLocaleLowerCase().includes(query) || entry.option.value.toLocaleLowerCase().includes(query)) {
251
+ let key = entry.option.value;
252
+ while (key) {
253
+ matches.add(key);
254
+ key = this._entries.get(key)?.parent ?? null;
255
+ }
256
+ }
257
+ const visible = new Set();
258
+ this._visible = [];
259
+ for (const entry of this._entries.values()) {
260
+ const show = !this.loading && (query ? matches.has(entry.option.value) : !entry.parent || visible.has(entry.parent) && this._expanded.has(entry.parent));
261
+ entry.row.hidden = !show;
262
+ if (show) {
263
+ visible.add(entry.option.value);
264
+ this._visible.push(entry);
265
+ }
266
+ entry.row.setAttribute('aria-disabled', String(entry.disabled || entry.option.selectable === false && !entry.option.children?.length));
267
+ const selected = this.values.includes(entry.option.value);
268
+ entry.row.setAttribute(this.multiple ? 'aria-checked' : 'aria-selected', String(selected));
269
+ entry.row.removeAttribute(this.multiple ? 'aria-selected' : 'aria-checked');
270
+ const check = entry.row.querySelector('.check');
271
+ check.hidden = !this.multiple;
272
+ if (check.textContent !== (selected ? '✓' : ''))
273
+ check.textContent = selected ? '✓' : '';
274
+ const expanded = Boolean(query) || this._expanded.has(entry.option.value);
275
+ if (entry.option.children?.length)
276
+ entry.row.setAttribute('aria-expanded', String(expanded));
277
+ else
278
+ entry.row.removeAttribute('aria-expanded');
279
+ const symbol = entry.option.children?.length ? expanded ? '⌄' : '›' : '';
280
+ if (entry.toggle.textContent !== symbol)
281
+ entry.toggle.textContent = symbol;
282
+ entry.toggle.setAttribute('aria-label', `${expanded ? 'Collapse' : 'Expand'} ${entry.option.label}`);
283
+ }
284
+ if (!this._visible.some(entry => entry.option.value === this._active && !entry.disabled))
285
+ this._active = this._visible.find(entry => !entry.disabled)?.option.value ?? '';
286
+ const message = this.loading ? 'Loading…' : this.getAttribute('not-found-content') ?? 'No results';
287
+ if (this._status.textContent !== message)
288
+ this._status.textContent = message;
289
+ this._status.hidden = this._visible.length > 0;
290
+ this._tree.setAttribute('aria-label', this.getAttribute('aria-label') || 'Options');
291
+ this._tree.setAttribute('aria-multiselectable', String(this.multiple));
292
+ this._tree.setAttribute('aria-busy', String(this.loading));
293
+ const show = this.open && !this._disabled && !this.readOnly && this.isConnected;
294
+ this._input.setAttribute('aria-expanded', String(show));
295
+ if (show)
296
+ this._popup.show();
297
+ else
298
+ this._popup.hide();
299
+ this._syncActive();
300
+ }
301
+ _syncActive() { for (const entry of this._entries.values())
302
+ entry.row.classList.toggle('active', entry.option.value === this._active); const active = this._entries.get(this._active); if (this.open && active && !active.row.hidden)
303
+ this._input.setAttribute('aria-activedescendant', active.row.id);
304
+ else
305
+ this._input.removeAttribute('aria-activedescendant'); }
306
+ _setOpen(open) { if (open && (this._disabled || this.readOnly))
307
+ return; if (this.open === open)
308
+ return; if (!open)
309
+ this._query = ''; this.toggleAttribute('open', open); this.dispatchEvent(new CustomEvent('open-change', { detail: { open }, bubbles: true, composed: true })); }
310
+ _emitSearch() { this.dispatchEvent(new CustomEvent('search', { detail: { text: this._query }, bubbles: true, composed: true })); }
311
+ _commit(values) { if (this._disabled || this.readOnly)
312
+ return; this.values = values; this.dispatchEvent(new CustomEvent('value-change', { detail: { value: this.value, values: this.values, options: this.values.flatMap(value => { const option = this._entries.get(value)?.option; return option ? [cloneOption(option)] : []; }) }, bubbles: true, composed: true })); this.dispatchEvent(new Event('change', { bubbles: true, composed: true })); }
313
+ _select(key) { const entry = this._entries.get(key); if (!entry || entry.disabled || this.loading || entry.option.selectable === false)
314
+ return; this._commit(this.multiple ? this.values.includes(key) ? this.values.filter(value => value !== key) : [...this.values, key] : [key]); if (!this.multiple)
315
+ this._setOpen(false); }
316
+ _toggle(key, open) { const entry = this._entries.get(key); if (!entry?.option.children?.length || entry.disabled)
317
+ return; const keys = new Set(this._expanded); if (open ?? !keys.has(key))
318
+ keys.add(key);
319
+ else
320
+ keys.delete(key); this.expandedKeys = [...keys]; this.dispatchEvent(new CustomEvent('expand-change', { detail: { expandedKeys: this.expandedKeys }, bubbles: true, composed: true })); }
321
+ _onTreeClick = (event) => { const target = event.target instanceof Element ? event.target : null; const toggle = target?.closest('[data-toggle]'); if (toggle)
322
+ this._toggle(toggle.dataset.toggle);
323
+ else {
324
+ const row = target?.closest('[data-value]');
325
+ if (row) {
326
+ this._active = row.dataset.value;
327
+ this._select(this._active);
328
+ }
329
+ } };
330
+ _onKeydown = (event) => {
331
+ if (event.isComposing || this._composing || event.keyCode === 229 || this._disabled || this.readOnly)
332
+ return;
333
+ if (event.key === 'Escape' || event.key === 'Tab') {
334
+ if (this.open && event.key === 'Escape') {
335
+ event.preventDefault();
336
+ event.stopPropagation();
337
+ }
338
+ this._setOpen(false);
339
+ return;
340
+ }
341
+ const wasOpen = this.open;
342
+ if (['ArrowDown', 'ArrowUp'].includes(event.key))
343
+ this._setOpen(true);
344
+ if (!this.open)
345
+ return;
346
+ const entries = this._visible.filter(entry => !entry.disabled);
347
+ const index = entries.findIndex(entry => entry.option.value === this._active);
348
+ const current = this._entries.get(this._active);
349
+ let target;
350
+ if (event.key === 'ArrowDown')
351
+ target = entries[wasOpen ? (index + 1) % entries.length : 0];
352
+ else if (event.key === 'ArrowUp')
353
+ target = entries[wasOpen ? (index - 1 + entries.length) % entries.length : entries.length - 1];
354
+ else if (event.key === 'Home' && !this._query)
355
+ target = entries[0];
356
+ else if (event.key === 'End' && !this._query)
357
+ target = entries.at(-1);
358
+ else if (event.key === 'ArrowRight' && !this._query && current?.option.children?.length) {
359
+ event.preventDefault();
360
+ if (!this._expanded.has(current.option.value))
361
+ this._toggle(current.option.value, true);
362
+ else
363
+ target = entries.find(entry => entry.parent === current.option.value);
364
+ }
365
+ else if (event.key === 'ArrowLeft' && !this._query && current) {
366
+ event.preventDefault();
367
+ if (this._expanded.has(current.option.value))
368
+ this._toggle(current.option.value, false);
369
+ else if (current.parent)
370
+ target = this._entries.get(current.parent);
371
+ }
372
+ else if (event.key === 'Enter' && current && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
373
+ event.preventDefault();
374
+ event.stopPropagation();
375
+ if (current.option.selectable === false)
376
+ this._toggle(current.option.value);
377
+ else
378
+ this._select(current.option.value);
379
+ }
380
+ if (target) {
381
+ event.preventDefault();
382
+ this._active = target.option.value;
383
+ this._syncActive();
384
+ target.row.scrollIntoView({ block: 'nearest' });
385
+ }
386
+ };
387
+ }
388
+ function cloneOption(option) { return { ...option, ...(option.children ? { children: option.children.map(cloneOption) } : {}) }; }
389
+ function normalizeTree(value) {
390
+ const keys = new Set();
391
+ const visiting = new Set();
392
+ const visit = (items, depth) => !Array.isArray(items) || depth > 64 ? [] : items.flatMap(item => { if (!item || typeof item !== 'object' || typeof item.value !== 'string' || !item.value || keys.has(item.value) || visiting.has(item))
393
+ return []; keys.add(item.value); visiting.add(item); const children = visit(item.children, depth + 1); visiting.delete(item); return [{ value: item.value, label: typeof item.label === 'string' ? item.label : item.value, ...(item.disabled ? { disabled: true } : {}), ...(item.selectable === false ? { selectable: false } : {}), ...(item.expanded ? { expanded: true } : {}), ...(children.length ? { children } : {}) }]; });
394
+ return visit(value, 0);
395
+ }
396
+ export function defineTreeSelectComponents() { if (!customElements.get('hy-tree-select'))
397
+ customElements.define('hy-tree-select', HYTreeSelect); }
398
+ //# sourceMappingURL=tree-select.js.map
package/dist/tree.js CHANGED
@@ -68,7 +68,7 @@ export class HYTree extends HTMLElement {
68
68
  min-height: 0;
69
69
  color: var(--hy-tree-text-color, var(--hy-text-color, #c8d3e4));
70
70
  background: var(--hy-panel-bg-color, #171d28);
71
- font: 12px system-ui, sans-serif;
71
+ font: var(--hy-font-size-sm, 12px) var(--hy-font-family, system-ui, sans-serif);
72
72
  text-align: left;
73
73
  user-select: none;
74
74
  overflow: auto;
@@ -77,14 +77,15 @@ export class HYTree extends HTMLElement {
77
77
  box-sizing: border-box;
78
78
  width: 100%;
79
79
  flex: 0 0 auto;
80
- padding: 4px;
80
+ padding: var(--hy-space-2, 4px);
81
81
  }
82
82
  .row {
83
83
  height: 28px;
84
84
  display: flex;
85
85
  align-items: center;
86
- gap: 4px;
87
- border-radius: 4px;
86
+ gap: var(--hy-space-2, 4px);
87
+ padding-left: calc(var(--hy-space-2, 4px) + var(--_hy-tree-depth, 0) * var(--hy-space-8, 16px));
88
+ border-radius: var(--hy-radius-sm, 4px);
88
89
  color: var(--hy-tree-text-color, var(--hy-text-color, #c8d3e4));
89
90
  cursor: default;
90
91
  }
@@ -394,7 +395,7 @@ export class HYTree extends HTMLElement {
394
395
  const { row, toggle, content } = rendered;
395
396
  row.className = `row${selected ? ' selected' : ''}${active ? ' active' : ''}`;
396
397
  row.dataset.id = node.id;
397
- row.style.paddingLeft = `${depth * 16 + 4}px`;
398
+ row.style.setProperty('--_hy-tree-depth', String(depth));
398
399
  row.draggable = this.allowDrag;
399
400
  toggle.className = `${hasChildren ? 'toggle' : 'toggle empty'}${expanded ? ' expanded' : ''}`;
400
401
  toggle.disabled = !hasChildren;
@@ -0,0 +1,107 @@
1
+ export type HYUploadStatus = 'ready' | 'uploading' | 'done' | 'error';
2
+ export type HYUploadChangeReason = 'select' | 'remove' | 'start' | 'progress' | 'success' | 'error' | 'abort';
3
+ export type HYUploadRejectReason = 'type' | 'size' | 'count' | 'before-upload' | 'error';
4
+ export interface HYUploadFile {
5
+ uid: string;
6
+ name: string;
7
+ status: HYUploadStatus;
8
+ percent?: number;
9
+ size?: number;
10
+ type?: string;
11
+ file?: File;
12
+ response?: unknown;
13
+ error?: string;
14
+ }
15
+ export interface HYUploadChangeDetail {
16
+ file: HYUploadFile;
17
+ fileList: HYUploadFile[];
18
+ reason: HYUploadChangeReason;
19
+ }
20
+ export interface HYUploadRejectDetail {
21
+ file: File;
22
+ reason: HYUploadRejectReason;
23
+ message: string;
24
+ }
25
+ export interface HYUploadRemoveDetail {
26
+ file: HYUploadFile;
27
+ }
28
+ export interface HYUploadRequestOptions {
29
+ file: File;
30
+ signal: AbortSignal;
31
+ onProgress: (percent: number) => void;
32
+ }
33
+ export type HYUploadRequest = (options: HYUploadRequestOptions) => Promise<unknown>;
34
+ export type HYUploadBeforeUpload = (file: File, files: readonly File[]) => boolean | Promise<boolean>;
35
+ export declare class HYUpload extends HTMLElement {
36
+ static readonly formAssociated = true;
37
+ private readonly _internals;
38
+ private readonly _input;
39
+ private readonly _zone;
40
+ private readonly _button;
41
+ private readonly _slot;
42
+ private readonly _list;
43
+ private readonly _message;
44
+ private _files;
45
+ private _initialFiles;
46
+ private _request;
47
+ private _beforeUpload;
48
+ private _requests;
49
+ private _rows;
50
+ private _listeners;
51
+ private _formDisabled;
52
+ private _selection;
53
+ private _dragDepth;
54
+ static get observedAttributes(): string[];
55
+ constructor();
56
+ connectedCallback(): void;
57
+ disconnectedCallback(): void;
58
+ attributeChangedCallback(name: string, previous: string | null, next: string | null): void;
59
+ get fileList(): HYUploadFile[];
60
+ set fileList(value: HYUploadFile[]);
61
+ get files(): File[];
62
+ get request(): HYUploadRequest | null;
63
+ set request(value: HYUploadRequest | null);
64
+ get beforeUpload(): HYUploadBeforeUpload | null;
65
+ set beforeUpload(value: HYUploadBeforeUpload | null);
66
+ get name(): string;
67
+ set name(value: string);
68
+ get accept(): string;
69
+ set accept(value: string);
70
+ get multiple(): boolean;
71
+ set multiple(value: boolean);
72
+ get disabled(): boolean;
73
+ set disabled(value: boolean);
74
+ get drag(): boolean;
75
+ set drag(value: boolean);
76
+ get maxCount(): number;
77
+ set maxCount(value: number);
78
+ get maxSize(): number;
79
+ set maxSize(value: number);
80
+ get autoUpload(): boolean;
81
+ set autoUpload(value: boolean);
82
+ get showFileList(): boolean;
83
+ set showFileList(value: boolean);
84
+ get form(): HTMLFormElement | null;
85
+ get validity(): ValidityState;
86
+ get validationMessage(): string;
87
+ checkValidity(): boolean;
88
+ reportValidity(): boolean;
89
+ formDisabledCallback(disabled: boolean): void;
90
+ formResetCallback(): void;
91
+ focus(options?: FocusOptions): void;
92
+ openFileDialog(): void;
93
+ selectFiles(value: readonly File[] | FileList): Promise<void>;
94
+ upload(uid?: string): Promise<void>;
95
+ abort(uid?: string): void;
96
+ removeFile(uid: string): boolean;
97
+ private _upload;
98
+ private _emit;
99
+ private _reject;
100
+ private _sync;
101
+ private _onListClick;
102
+ private _onDragEnter;
103
+ private _onDragLeave;
104
+ private _onDragOver;
105
+ private _onDrop;
106
+ }
107
+ export declare function defineUploadComponents(): void;