@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,485 @@
1
+ import { HYFormControl } from './internal/form-control.js';
2
+ let mentionsId = 0;
3
+ /** A multiline text input with caret-anchored mention suggestions. Registration is explicit. */
4
+ export class HYMentions extends HTMLElement {
5
+ static formAssociated = true;
6
+ _textarea = document.createElement('textarea');
7
+ _formControl = new HYFormControl(this, this._textarea);
8
+ _panel = document.createElement('div');
9
+ _list = document.createElement('div');
10
+ _status = document.createElement('div');
11
+ _mirror = document.createElement('div');
12
+ _marker = document.createElement('span');
13
+ _mirrorBefore = document.createTextNode('');
14
+ _mirrorAfter = document.createTextNode('');
15
+ _markerText = document.createTextNode('');
16
+ _listId = `hy-mentions-${++mentionsId}`;
17
+ _options = [];
18
+ _filtered = [];
19
+ _items = [];
20
+ _visibleItems = [];
21
+ _filterOption = null;
22
+ _search = null;
23
+ _active = -1;
24
+ _dismissed = '';
25
+ _composing = false;
26
+ _committing = false;
27
+ _listeners = null;
28
+ _resizeObserver = null;
29
+ static get observedAttributes() {
30
+ return ['value', 'options', 'prefix', 'prefixes', 'split', 'placement', 'loading',
31
+ 'not-found-content', 'loading-text', 'placeholder', 'rows', 'disabled', 'readonly',
32
+ 'required', 'maxlength', 'name', 'aria-label', 'title'];
33
+ }
34
+ constructor() {
35
+ super();
36
+ const root = this.attachShadow({ mode: 'open' });
37
+ const style = document.createElement('style');
38
+ style.textContent = `
39
+ :host { display: block; width: 100%; min-width: 0; }
40
+ textarea {
41
+ display: block; box-sizing: border-box; width: 100%; min-width: 0; resize: vertical;
42
+ padding: var(--hy-space-3, 6px) var(--hy-space-4, 8px);
43
+ border: 1px solid var(--hy-border-color, #303746);
44
+ border-radius: var(--hy-input-radius, var(--hy-radius-sm, 4px)); outline: none;
45
+ color: var(--hy-text-color, #d8e2f2);
46
+ background: var(--hy-input-bg-color, var(--hy-surface-color, #121822));
47
+ font: var(--hy-input-font, var(--hy-font-size-sm, 12px) var(--hy-font-family, system-ui, sans-serif));
48
+ line-height: var(--hy-line-height-normal, 1.5); color-scheme: inherit;
49
+ transition: border-color 120ms ease, box-shadow 120ms ease;
50
+ }
51
+ textarea:hover:not(:disabled):not(:read-only) { border-color: var(--hy-hover-border-color, #435268); }
52
+ textarea:focus { border-color: var(--hy-focus-border-color, #3d6fa8); box-shadow: 0 0 0 2px var(--hy-focus-ring-color, rgba(61,111,168,.24)); }
53
+ textarea:disabled { opacity: .55; cursor: not-allowed; }
54
+ textarea:read-only { cursor: default; }
55
+ textarea::placeholder { color: var(--hy-muted-text-color, var(--hy-muted-color, #8290a3)); }
56
+ .panel {
57
+ position: fixed; inset: auto; margin: 0; box-sizing: border-box;
58
+ width: max-content; min-width: min(var(--hy-mentions-popup-min-width, 160px), calc(100vw - 16px));
59
+ max-width: min(var(--hy-mentions-popup-max-width, 320px), calc(100vw - 16px));
60
+ max-height: var(--hy-mentions-popup-max-height, 240px); overflow: auto;
61
+ padding: var(--hy-space-2, 4px); border: 1px solid var(--hy-border-color, #303746);
62
+ border-radius: var(--hy-radius-sm, 4px); color: var(--hy-text-color, #d8e2f2);
63
+ background: var(--hy-menu-bg-color, var(--hy-surface-color, #121822));
64
+ box-shadow: var(--hy-popup-shadow, 0 8px 24px rgba(0,0,0,.24));
65
+ font: var(--hy-font-size-sm, 12px)/1.5 var(--hy-font-family, system-ui, sans-serif);
66
+ z-index: var(--hy-mentions-z-index, 1050);
67
+ }
68
+ [hidden] { display: none !important; }
69
+ .option { padding: var(--hy-space-3, 6px) var(--hy-space-4, 8px); border-radius: var(--hy-radius-xs, 2px); cursor: pointer; overflow-wrap: anywhere; }
70
+ .option[aria-selected="true"] { color: var(--hy-selected-text-color, #fff); background: var(--hy-selected-bg-color, #255a91); }
71
+ .option[aria-disabled="true"] { color: var(--hy-disabled-text-color, #65738a); cursor: not-allowed; }
72
+ .status { padding: var(--hy-space-3, 6px) var(--hy-space-4, 8px); color: var(--hy-muted-text-color, var(--hy-muted-color, #8290a3)); }
73
+ .mirror { position: fixed; top: 0; left: 0; visibility: hidden; pointer-events: none; white-space: pre-wrap; overflow-wrap: break-word; overflow: hidden; }
74
+ @media (prefers-reduced-motion: reduce) { textarea { transition: none; } }
75
+ `;
76
+ this._textarea.part.add('textarea');
77
+ this._textarea.setAttribute('role', 'combobox');
78
+ this._textarea.setAttribute('aria-autocomplete', 'list');
79
+ this._textarea.setAttribute('aria-haspopup', 'listbox');
80
+ this._textarea.setAttribute('aria-multiline', 'true');
81
+ this._textarea.setAttribute('aria-expanded', 'false');
82
+ this._textarea.setAttribute('aria-controls', this._listId);
83
+ this._panel.className = 'panel';
84
+ this._panel.part.add('panel');
85
+ this._panel.hidden = true;
86
+ this._panel.setAttribute('popover', 'manual');
87
+ this._list.id = this._listId;
88
+ this._list.part.add('list');
89
+ this._list.setAttribute('role', 'listbox');
90
+ this._status.className = 'status';
91
+ this._status.part.add('status');
92
+ this._status.setAttribute('role', 'status');
93
+ this._mirror.className = 'mirror';
94
+ this._mirror.setAttribute('aria-hidden', 'true');
95
+ // Keep text nodes stable: replacing them during input splits Chromium's native undo group.
96
+ this._marker.append(this._markerText);
97
+ this._mirror.append(this._mirrorBefore, this._marker, this._mirrorAfter);
98
+ this._panel.append(this._list, this._status);
99
+ root.append(style, this._textarea, this._panel, this._mirror);
100
+ }
101
+ connectedCallback() {
102
+ this._listeners?.abort();
103
+ const controller = new AbortController();
104
+ this._listeners = controller;
105
+ const { signal } = controller;
106
+ this._textarea.addEventListener('input', this._onInput, { signal });
107
+ this._textarea.addEventListener('keydown', this._onKeydown, { signal });
108
+ this._textarea.addEventListener('click', this._onCaretChange, { signal });
109
+ this._textarea.addEventListener('select', this._onCaretChange, { signal });
110
+ this._textarea.addEventListener('keyup', this._onCaretKeyup, { signal });
111
+ this._textarea.addEventListener('scroll', this._positionPanel, { signal });
112
+ this._textarea.addEventListener('focus', this._onFocus, { signal });
113
+ this._textarea.addEventListener('blur', this._onBlur, { signal });
114
+ this._textarea.addEventListener('compositionstart', this._onCompositionStart, { signal });
115
+ this._textarea.addEventListener('compositionend', this._onCompositionEnd, { signal });
116
+ this._panel.addEventListener('pointerdown', this._onPanelPointerDown, { signal });
117
+ this._panel.addEventListener('pointermove', this._onPanelPointerMove, { signal });
118
+ this._panel.addEventListener('click', this._onPanelClick, { signal });
119
+ this.ownerDocument.addEventListener('pointerdown', this._onOutsidePointerDown, { capture: true, signal });
120
+ this.ownerDocument.addEventListener('scroll', this._onScroll, { capture: true, signal });
121
+ this.ownerDocument.defaultView?.addEventListener('resize', this._positionPanel, { signal });
122
+ this._resizeObserver = new ResizeObserver(this._positionPanel);
123
+ this._resizeObserver.observe(this._textarea);
124
+ this._sync();
125
+ this._formControl.connect();
126
+ }
127
+ disconnectedCallback() {
128
+ this._listeners?.abort();
129
+ this._listeners = null;
130
+ this._resizeObserver?.disconnect();
131
+ this._resizeObserver = null;
132
+ this._composing = false;
133
+ this._dismissed = '';
134
+ this._close();
135
+ this._mirrorBefore.data = this._markerText.data = this._mirrorAfter.data = '';
136
+ }
137
+ attributeChangedCallback(name, oldValue, newValue) {
138
+ if (oldValue === newValue)
139
+ return;
140
+ if (name === 'options') {
141
+ let parsed = [];
142
+ try {
143
+ parsed = JSON.parse(newValue ?? '[]');
144
+ }
145
+ catch { /* Invalid JSON means no options. */ }
146
+ this._setOptions(parsed);
147
+ }
148
+ this._sync();
149
+ if (!this._committing)
150
+ this._refreshSearch();
151
+ }
152
+ get value() { return this.getAttribute('value') ?? ''; }
153
+ set value(value) { this.setAttribute('value', String(value ?? '')); }
154
+ get options() { return this._options.map(option => ({ ...option })); }
155
+ set options(value) { this._setOptions(value); this._refreshSearch(); }
156
+ get prefix() { return this.getAttribute('prefix') || '@'; }
157
+ set prefix(value) { this.removeAttribute('prefixes'); this.setAttribute('prefix', String(value)); }
158
+ get prefixes() {
159
+ try {
160
+ const parsed = JSON.parse(this.getAttribute('prefixes') ?? 'null');
161
+ if (Array.isArray(parsed)) {
162
+ const values = parsed.filter((item) => typeof item === 'string' && item.length > 0 && !/\s/u.test(item));
163
+ if (values.length)
164
+ return [...new Set(values)];
165
+ }
166
+ }
167
+ catch { /* Fall back to the single prefix. */ }
168
+ return [this.prefix];
169
+ }
170
+ set prefixes(value) { this.setAttribute('prefixes', JSON.stringify(value)); }
171
+ get split() { return this.getAttribute('split') ?? ' '; }
172
+ set split(value) { this.setAttribute('split', String(value)); }
173
+ get placement() { return this.getAttribute('placement') === 'top' ? 'top' : 'bottom'; }
174
+ set placement(value) { this.setAttribute('placement', value); }
175
+ get loading() { return this.hasAttribute('loading'); }
176
+ set loading(value) { this.toggleAttribute('loading', Boolean(value)); }
177
+ get disabled() { return this.hasAttribute('disabled'); }
178
+ set disabled(value) { this.toggleAttribute('disabled', Boolean(value)); }
179
+ get readOnly() { return this.hasAttribute('readonly'); }
180
+ set readOnly(value) { this.toggleAttribute('readonly', Boolean(value)); }
181
+ get placeholder() { return this.getAttribute('placeholder') ?? ''; }
182
+ set placeholder(value) { this.setAttribute('placeholder', String(value)); }
183
+ get rows() { const value = Number(this.getAttribute('rows')); return Number.isInteger(value) && value > 0 ? Math.min(value, 1000) : 3; }
184
+ set rows(value) { this.setAttribute('rows', String(value)); }
185
+ get notFoundContent() { return this.getAttribute('not-found-content') ?? 'No results'; }
186
+ set notFoundContent(value) { this.setAttribute('not-found-content', String(value)); }
187
+ get loadingText() { return this.getAttribute('loading-text') ?? 'Loading…'; }
188
+ set loadingText(value) { this.setAttribute('loading-text', String(value)); }
189
+ get filterOption() { return this._filterOption; }
190
+ set filterOption(value) { this._filterOption = value; this._refreshSearch(); }
191
+ get open() { return !this._panel.hidden; }
192
+ focus(options) { this._textarea.focus(options); }
193
+ blur() { this._textarea.blur(); }
194
+ get form() { return this._formControl.internals.form; }
195
+ get validity() { return this._formControl.internals.validity; }
196
+ get validationMessage() { return this._formControl.internals.validationMessage; }
197
+ checkValidity() { return this._formControl.internals.checkValidity(); }
198
+ reportValidity() { return this._formControl.internals.reportValidity(); }
199
+ setCustomValidity(message) { this._formControl.setCustomValidity(message); }
200
+ formDisabledCallback(disabled) { this._formControl.disabled = disabled; this._sync(); this._refreshSearch(); }
201
+ formResetCallback() { this._formControl.reset(); this._close(); }
202
+ formStateRestoreCallback(state) { this._formControl.restore(state); }
203
+ _sync() {
204
+ if (this._textarea.value !== this.value)
205
+ this._textarea.value = this.value;
206
+ const disabled = this.disabled || this._formControl.disabled;
207
+ if (this._textarea.disabled !== disabled)
208
+ this._textarea.disabled = disabled;
209
+ if (this._textarea.readOnly !== this.readOnly)
210
+ this._textarea.readOnly = this.readOnly;
211
+ if (this._textarea.rows !== this.rows)
212
+ this._textarea.rows = this.rows;
213
+ if (this._textarea.required !== this.hasAttribute('required'))
214
+ this._textarea.required = this.hasAttribute('required');
215
+ for (const name of ['placeholder', 'maxlength', 'name', 'aria-label', 'title']) {
216
+ const value = this.getAttribute(name);
217
+ if (value === null) {
218
+ if (this._textarea.hasAttribute(name))
219
+ this._textarea.removeAttribute(name);
220
+ }
221
+ else if (this._textarea.getAttribute(name) !== value)
222
+ this._textarea.setAttribute(name, value);
223
+ }
224
+ this._list.setAttribute('aria-label', this.getAttribute('aria-label') || 'Mention suggestions');
225
+ this._formControl.sync();
226
+ }
227
+ _findSearch() {
228
+ if (!this.isConnected || this._textarea.disabled || this.readOnly || this._composing || this.shadowRoot?.activeElement !== this._textarea)
229
+ return null;
230
+ const end = this._textarea.selectionStart;
231
+ if (end !== this._textarea.selectionEnd)
232
+ return null;
233
+ const before = this._textarea.value.slice(0, end);
234
+ let found = null;
235
+ for (const prefix of this.prefixes) {
236
+ const start = before.lastIndexOf(prefix);
237
+ if (start < 0 || (start > 0 && !/\s/u.test(before[start - 1]) && !(this.split && before.slice(0, start).endsWith(this.split))))
238
+ continue;
239
+ const text = before.slice(start + prefix.length);
240
+ if (/\s/u.test(text) || (this.split && text.includes(this.split)))
241
+ continue;
242
+ if (!found || start > found.start || (start === found.start && prefix.length > found.prefix.length))
243
+ found = { start, end, text, prefix };
244
+ }
245
+ return found;
246
+ }
247
+ _refreshSearch = () => {
248
+ const next = this._findSearch();
249
+ if (!next || searchKey(next) === this._dismissed) {
250
+ this._close();
251
+ return;
252
+ }
253
+ const previous = this._search;
254
+ const changed = !previous || searchKey(next) !== searchKey(previous);
255
+ this._search = next;
256
+ if (changed) {
257
+ this._active = -1;
258
+ // Update identity before dispatch: consumers can synchronously replace options or set loading.
259
+ this.dispatchEvent(new CustomEvent('search', {
260
+ detail: { text: next.text, prefix: next.prefix }, bubbles: true, composed: true,
261
+ }));
262
+ if (this._search !== next || !this._findSearch())
263
+ return;
264
+ }
265
+ this._renderSuggestions();
266
+ };
267
+ _renderSuggestions() {
268
+ const search = this._search;
269
+ if (!search)
270
+ return;
271
+ const activeValue = this._filtered[this._active]?.value;
272
+ const filter = this._filterOption;
273
+ this._filtered = this.loading ? [] : this._options.filter(option => filter === false || (filter
274
+ ? filter(search.text, { ...option })
275
+ : `${option.value}\n${option.label ?? ''}`.toLocaleLowerCase().includes(search.text.toLocaleLowerCase())));
276
+ this._active = activeValue === undefined ? -1 : this._filtered.findIndex(option => option.value === activeValue && !option.disabled);
277
+ if (this._active < 0)
278
+ this._active = this._filtered.findIndex(option => !option.disabled);
279
+ const indices = new Map(this._filtered.map((option, index) => [option, index]));
280
+ this._visibleItems = [];
281
+ this._items.forEach((item, index) => {
282
+ const filteredIndex = indices.get(this._options[index]);
283
+ item.hidden = filteredIndex === undefined;
284
+ item.dataset.index = String(filteredIndex ?? -1);
285
+ if (filteredIndex !== undefined)
286
+ this._visibleItems.push(item);
287
+ });
288
+ const message = this.loading ? this.loadingText : this.notFoundContent;
289
+ if (this._status.textContent !== message)
290
+ this._status.textContent = message;
291
+ this._status.hidden = this._filtered.length > 0;
292
+ this._list.setAttribute('aria-busy', String(this.loading));
293
+ this._panel.hidden = false;
294
+ if (this._panel.showPopover && !this._panel.matches(':popover-open'))
295
+ this._panel.showPopover();
296
+ this._textarea.setAttribute('aria-expanded', 'true');
297
+ this._syncActive();
298
+ this._positionPanel();
299
+ }
300
+ _setOptions(value) {
301
+ const options = normalizeOptions(value);
302
+ if (options.length === this._options.length && options.every((option, index) => {
303
+ const previous = this._options[index];
304
+ return option.value === previous.value && option.label === previous.label && option.disabled === previous.disabled;
305
+ }))
306
+ return;
307
+ this._options = options;
308
+ this._items = this._options.map((option, index) => {
309
+ const item = document.createElement('div');
310
+ item.className = 'option';
311
+ item.part.add('option');
312
+ item.id = `${this._listId}-${index}`;
313
+ item.hidden = true;
314
+ item.setAttribute('role', 'option');
315
+ item.setAttribute('aria-disabled', String(Boolean(option.disabled)));
316
+ item.textContent = option.label ?? option.value;
317
+ return item;
318
+ });
319
+ this._list.replaceChildren(...this._items);
320
+ }
321
+ _syncActive() {
322
+ for (const item of this._items)
323
+ item.setAttribute('aria-selected', String(!item.hidden && Number(item.dataset.index) === this._active));
324
+ const active = this._visibleItems[this._active];
325
+ if (!active)
326
+ this._textarea.removeAttribute('aria-activedescendant');
327
+ else
328
+ this._textarea.setAttribute('aria-activedescendant', active.id);
329
+ }
330
+ _close() {
331
+ if (this._panel.hidePopover && this._panel.matches(':popover-open'))
332
+ this._panel.hidePopover();
333
+ this._panel.hidden = true;
334
+ this._search = null;
335
+ this._active = -1;
336
+ this._filtered = [];
337
+ this._textarea.setAttribute('aria-expanded', 'false');
338
+ this._textarea.removeAttribute('aria-activedescendant');
339
+ this._visibleItems = [];
340
+ }
341
+ _positionPanel = () => {
342
+ if (!this.open || !this._search)
343
+ return;
344
+ const view = this.ownerDocument.defaultView;
345
+ if (!view)
346
+ return;
347
+ const rect = this._textarea.getBoundingClientRect();
348
+ const computed = view.getComputedStyle(this._textarea);
349
+ // Mirror the native textarea so wrapped lines, proportional fonts and scrolling share the caret position.
350
+ for (const property of ['box-sizing', 'font-family', 'font-size', 'font-weight', 'font-style', 'line-height',
351
+ 'letter-spacing', 'text-transform', 'text-indent', 'text-align', 'direction', 'tab-size',
352
+ 'padding-top', 'padding-right', 'padding-bottom', 'padding-left', 'border-top-width',
353
+ 'border-right-width', 'border-bottom-width', 'border-left-width', 'border-style']) {
354
+ this._mirror.style.setProperty(property, computed.getPropertyValue(property));
355
+ }
356
+ // Exclude a vertical scrollbar from the mirror's text layout.
357
+ this._mirror.style.width = `${this._textarea.clientWidth + parseFloat(computed.borderLeftWidth) + parseFloat(computed.borderRightWidth)}px`;
358
+ this._mirrorBefore.data = this._textarea.value.slice(0, this._search.end);
359
+ this._markerText.data = this._textarea.value[this._search.end] || '\u200b';
360
+ this._mirrorAfter.data = this._textarea.value.slice(this._search.end + 1);
361
+ const marker = this._marker.getBoundingClientRect();
362
+ const mirror = this._mirror.getBoundingClientRect();
363
+ const x = rect.left + marker.left - mirror.left - this._textarea.scrollLeft;
364
+ const y = rect.top + marker.top - mirror.top - this._textarea.scrollTop;
365
+ const lineHeight = parseFloat(computed.lineHeight) || parseFloat(computed.fontSize) * 1.5;
366
+ if (y + lineHeight < rect.top || y > rect.bottom || rect.bottom < 0 || rect.top > view.innerHeight) {
367
+ this._close();
368
+ return;
369
+ }
370
+ this._panel.style.maxHeight = '';
371
+ const panelHeight = this._panel.getBoundingClientRect().height;
372
+ const below = view.innerHeight - (y + lineHeight) - 8;
373
+ const above = y - 8;
374
+ const top = this.placement === 'top' ? above >= panelHeight || above > below : below < panelHeight && above > below;
375
+ this._panel.style.maxHeight = `${Math.max(0, Math.min(parseFloat(view.getComputedStyle(this._panel).maxHeight) || 240, top ? above - 4 : below - 4))}px`;
376
+ const bounds = this._panel.getBoundingClientRect();
377
+ this._panel.style.left = `${Math.max(8, Math.min(x, view.innerWidth - bounds.width - 8))}px`;
378
+ this._panel.style.top = `${Math.max(8, top ? y - bounds.height - 4 : y + lineHeight + 4)}px`;
379
+ };
380
+ _commit(index) {
381
+ const search = this._search;
382
+ const option = this._filtered[index];
383
+ if (!search || !option || option.disabled || this.disabled || this.readOnly || this.loading)
384
+ return;
385
+ let end = search.end;
386
+ const text = this._textarea.value;
387
+ while (end < text.length && !/\s/u.test(text[end]) && !(this.split && text.startsWith(this.split, end)))
388
+ end++;
389
+ if (this.split && text.startsWith(this.split, end))
390
+ end += this.split.length;
391
+ const insertion = `${search.prefix}${option.value}${this.split}`;
392
+ const next = text.slice(0, search.start) + insertion + text.slice(end);
393
+ const maxLength = this._textarea.maxLength;
394
+ if (maxLength >= 0 && next.length > maxLength)
395
+ return;
396
+ this._committing = true;
397
+ try {
398
+ this._textarea.setRangeText(insertion, search.start, end, 'end');
399
+ this.value = this._textarea.value;
400
+ this._dismissed = searchKey(this._findSearch());
401
+ this._close();
402
+ this._textarea.focus();
403
+ this._textarea.dispatchEvent(new InputEvent('input', { bubbles: true, composed: true, inputType: 'insertReplacementText', data: insertion }));
404
+ this.dispatchEvent(new CustomEvent('mention-select', {
405
+ detail: { value: this.value, option: { ...option }, prefix: search.prefix }, bubbles: true, composed: true,
406
+ }));
407
+ }
408
+ finally {
409
+ this._committing = false;
410
+ }
411
+ }
412
+ _onInput = () => {
413
+ this.value = this._textarea.value;
414
+ this.dispatchEvent(new CustomEvent('value-change', { detail: { value: this.value }, bubbles: true, composed: true }));
415
+ if (!this._committing) {
416
+ this._dismissed = '';
417
+ this._refreshSearch();
418
+ }
419
+ };
420
+ _onFocus = () => { this._dismissed = ''; this._refreshSearch(); };
421
+ _onBlur = () => { this._close(); };
422
+ _onCaretChange = () => { if (!this._committing)
423
+ this._refreshSearch(); };
424
+ _onCaretKeyup = (event) => { if (['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home', 'End'].includes(event.key))
425
+ this._refreshSearch(); };
426
+ _onCompositionStart = () => { this._composing = true; this._close(); };
427
+ _onCompositionEnd = () => { this._composing = false; this._dismissed = ''; this._refreshSearch(); };
428
+ _onKeydown = (event) => {
429
+ if (event.isComposing || this._composing || event.keyCode === 229 || !this.open)
430
+ return;
431
+ if (event.key === 'Escape' || event.key === 'Tab') {
432
+ this._dismissed = searchKey(this._search);
433
+ this._close();
434
+ if (event.key === 'Escape') {
435
+ event.preventDefault();
436
+ event.stopPropagation();
437
+ }
438
+ }
439
+ else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
440
+ const enabled = this._filtered.map((option, index) => option.disabled ? -1 : index).filter(index => index >= 0);
441
+ if (!enabled.length)
442
+ return;
443
+ event.preventDefault();
444
+ event.stopPropagation();
445
+ const current = enabled.indexOf(this._active);
446
+ this._active = enabled[(current + (event.key === 'ArrowDown' ? 1 : -1) + enabled.length) % enabled.length];
447
+ this._syncActive();
448
+ this._visibleItems[this._active]?.scrollIntoView({ block: 'nearest' });
449
+ }
450
+ else if (event.key === 'Enter' && this._active >= 0 && !event.shiftKey && !event.ctrlKey && !event.metaKey && !event.altKey) {
451
+ event.preventDefault();
452
+ event.stopPropagation();
453
+ this._commit(this._active);
454
+ }
455
+ };
456
+ _optionIndex(event) {
457
+ const item = event.target instanceof Element ? event.target.closest('.option') : null;
458
+ return item ? Number(item.dataset.index) : -1;
459
+ }
460
+ _onPanelPointerDown = (event) => { event.preventDefault(); };
461
+ _onPanelPointerMove = (event) => {
462
+ const index = this._optionIndex(event);
463
+ if (index >= 0 && !this._filtered[index]?.disabled) {
464
+ this._active = index;
465
+ this._syncActive();
466
+ }
467
+ };
468
+ _onPanelClick = (event) => { this._commit(this._optionIndex(event)); };
469
+ _onOutsidePointerDown = (event) => { if (!event.composedPath().includes(this))
470
+ this._close(); };
471
+ _onScroll = (event) => { if (!event.composedPath().includes(this._panel))
472
+ this._positionPanel(); };
473
+ }
474
+ export function defineMentionsComponents() {
475
+ if (!customElements.get('hy-mentions'))
476
+ customElements.define('hy-mentions', HYMentions);
477
+ }
478
+ function normalizeOptions(value) {
479
+ if (!Array.isArray(value))
480
+ return [];
481
+ return value.filter((item) => Boolean(item) && typeof item === 'object' && typeof item.value === 'string' && item.value.length > 0)
482
+ .map(item => ({ value: item.value, ...(typeof item.label === 'string' ? { label: item.label } : {}), ...(item.disabled ? { disabled: true } : {}) }));
483
+ }
484
+ function searchKey(search) { return search ? JSON.stringify([search.start, search.end, search.prefix, search.text]) : ''; }
485
+ //# sourceMappingURL=mentions.js.map
@@ -9,6 +9,6 @@ export interface HYMenuButtonOptions {
9
9
  role?: string;
10
10
  tabIndex?: number;
11
11
  }
12
- export declare const GE_MENU_ITEM_STYLES = "\n button {\n box-sizing: border-box;\n width: 100%;\n height: 26px;\n display: flex;\n align-items: center;\n padding: 0 8px;\n border: 0;\n border-radius: 3px;\n color: var(--hy-text-color, #d8e2f2);\n background: transparent;\n font: inherit;\n text-align: left;\n cursor: pointer;\n }\n button:hover,\n button:focus,\n button.active {\n background: var(--hy-menu-item-hover-bg-color, var(--hy-accent-strong-color, #255a91));\n outline: none;\n }\n button:disabled {\n color: var(--hy-muted-color, #66758b);\n cursor: default;\n }\n button:disabled:hover {\n background: transparent;\n }\n .separator {\n height: 1px;\n margin: 4px 2px;\n background: var(--hy-border-color, #303746);\n }\n";
12
+ export declare const GE_MENU_ITEM_STYLES = "\n button {\n box-sizing: border-box;\n width: 100%;\n height: 26px;\n display: flex;\n align-items: center;\n padding: 0 var(--hy-space-4, 8px);\n border: 0;\n border-radius: calc(var(--hy-radius-xs, 2px) + 1px);\n color: var(--hy-text-color, #d8e2f2);\n background: transparent;\n font: inherit;\n text-align: left;\n cursor: pointer;\n }\n button:hover,\n button:focus,\n button.active {\n background: var(--hy-menu-item-hover-bg-color, var(--hy-accent-strong-color, #255a91));\n outline: none;\n }\n button:disabled {\n color: var(--hy-muted-color, #66758b);\n cursor: default;\n }\n button:disabled:hover {\n background: transparent;\n }\n .separator {\n height: 1px;\n margin: var(--hy-space-2, 4px) var(--hy-space-1, 2px);\n background: var(--hy-border-color, #303746);\n }\n";
13
13
  export declare function createMenuSeparator(): HTMLDivElement;
14
14
  export declare function createMenuItemButton(item: HYMenuItemLike, options?: HYMenuButtonOptions): HTMLButtonElement;
@@ -5,9 +5,9 @@ export const GE_MENU_ITEM_STYLES = `
5
5
  height: 26px;
6
6
  display: flex;
7
7
  align-items: center;
8
- padding: 0 8px;
8
+ padding: 0 var(--hy-space-4, 8px);
9
9
  border: 0;
10
- border-radius: 3px;
10
+ border-radius: calc(var(--hy-radius-xs, 2px) + 1px);
11
11
  color: var(--hy-text-color, #d8e2f2);
12
12
  background: transparent;
13
13
  font: inherit;
@@ -29,7 +29,7 @@ export const GE_MENU_ITEM_STYLES = `
29
29
  }
30
30
  .separator {
31
31
  height: 1px;
32
- margin: 4px 2px;
32
+ margin: var(--hy-space-2, 4px) var(--hy-space-1, 2px);
33
33
  background: var(--hy-border-color, #303746);
34
34
  }
35
35
  `;
package/dist/menu.d.ts ADDED
@@ -0,0 +1,63 @@
1
+ export type HYMenuMode = 'inline' | 'vertical' | 'horizontal';
2
+ export interface HYMenuItem {
3
+ key: string;
4
+ label?: string;
5
+ icon?: string;
6
+ href?: string;
7
+ disabled?: boolean;
8
+ type?: 'item' | 'group' | 'divider';
9
+ children?: HYMenuItem[];
10
+ }
11
+ export interface HYMenuSelectDetail {
12
+ key: string;
13
+ item: HYMenuItem;
14
+ selectedKeys: string[];
15
+ }
16
+ export interface HYMenuOpenChangeDetail {
17
+ openKeys: string[];
18
+ }
19
+ export declare class HYMenu extends HTMLElement {
20
+ private readonly _menu;
21
+ private readonly _id;
22
+ private _items;
23
+ private _entries;
24
+ private _popups;
25
+ private _pendingPopups;
26
+ private _listeners;
27
+ private _active;
28
+ private _typeahead;
29
+ private _typeTimer;
30
+ static get observedAttributes(): string[];
31
+ constructor();
32
+ connectedCallback(): void;
33
+ disconnectedCallback(): void;
34
+ attributeChangedCallback(name: string, previous: string | null, next: string | null): void;
35
+ get items(): HYMenuItem[];
36
+ set items(value: HYMenuItem[]);
37
+ get mode(): HYMenuMode;
38
+ set mode(value: HYMenuMode);
39
+ get collapsed(): boolean;
40
+ set collapsed(value: boolean);
41
+ get disabled(): boolean;
42
+ set disabled(value: boolean);
43
+ get multiple(): boolean;
44
+ set multiple(value: boolean);
45
+ get selectedKeys(): string[];
46
+ set selectedKeys(value: string[]);
47
+ get openKeys(): string[];
48
+ set openKeys(value: string[]);
49
+ focus(options?: FocusOptions): void;
50
+ private get _inline();
51
+ private _disposePopups;
52
+ private _render;
53
+ private _build;
54
+ private _ancestorsOpen;
55
+ private _allVisible;
56
+ private _syncTabStops;
57
+ private _setOpenKeys;
58
+ private _toggle;
59
+ private _button;
60
+ private _onClick;
61
+ private _onKeydown;
62
+ }
63
+ export declare function defineMenuComponents(): void;