@smilodon/core 1.0.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 (34) hide show
  1. package/dist/index.cjs +3685 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.js +3643 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.min.js +2 -0
  6. package/dist/index.min.js.map +1 -0
  7. package/dist/index.umd.js +3691 -0
  8. package/dist/index.umd.js.map +1 -0
  9. package/dist/index.umd.min.js +2 -0
  10. package/dist/index.umd.min.js.map +1 -0
  11. package/dist/types/src/components/enhanced-select.d.ts +136 -0
  12. package/dist/types/src/components/native-select.d.ts +41 -0
  13. package/dist/types/src/components/select-option.d.ts +77 -0
  14. package/dist/types/src/config/global-config.d.ts +194 -0
  15. package/dist/types/src/index.d.ts +14 -0
  16. package/dist/types/src/renderers/contracts.d.ts +5 -0
  17. package/dist/types/src/themes/importer.d.ts +112 -0
  18. package/dist/types/src/types.d.ts +85 -0
  19. package/dist/types/src/utils/csp-styles.d.ts +74 -0
  20. package/dist/types/src/utils/data-source.d.ts +37 -0
  21. package/dist/types/src/utils/dom-pool.d.ts +79 -0
  22. package/dist/types/src/utils/fenwick-tree.d.ts +71 -0
  23. package/dist/types/src/utils/security.d.ts +131 -0
  24. package/dist/types/src/utils/telemetry.d.ts +95 -0
  25. package/dist/types/src/utils/virtualizer.d.ts +53 -0
  26. package/dist/types/src/utils/worker-manager.d.ts +106 -0
  27. package/dist/types/tests/csp.spec.d.ts +5 -0
  28. package/dist/types/tests/interaction.spec.d.ts +1 -0
  29. package/dist/types/tests/setup.d.ts +6 -0
  30. package/dist/types/tests/shadow-dom.spec.d.ts +5 -0
  31. package/dist/types/tests/smoke.spec.d.ts +1 -0
  32. package/dist/types/tests/stress.spec.d.ts +5 -0
  33. package/dist/types/tests/virtualizer.spec.d.ts +1 -0
  34. package/package.json +52 -0
package/dist/index.js ADDED
@@ -0,0 +1,3643 @@
1
+ const createRendererHelpers = (onSelect) => ({
2
+ onSelect,
3
+ getIndex: (node) => {
4
+ const el = node?.closest?.('[data-selectable]');
5
+ if (!el)
6
+ return null;
7
+ const idx = Number(el.dataset.index);
8
+ return Number.isFinite(idx) ? idx : null;
9
+ },
10
+ keyboardFocus: (index) => {
11
+ const el = document.querySelector(`[data-selectable][data-index="${index}"]`);
12
+ el?.focus?.();
13
+ }
14
+ });
15
+ // Fast template render path: string -> DOM via DocumentFragment, with delegation markers
16
+ function renderTemplate(container, items, optionTemplate) {
17
+ const frag = document.createDocumentFragment();
18
+ for (let i = 0; i < items.length; i++) {
19
+ const item = items[i];
20
+ const wrapper = document.createElement('div');
21
+ wrapper.innerHTML = optionTemplate(item, i);
22
+ let el = wrapper.firstElementChild;
23
+ if (!el) {
24
+ // Edge case: template produced multiple or zero root nodes; wrap children
25
+ const wrap = document.createElement('div');
26
+ wrap.setAttribute('data-selectable', '');
27
+ wrap.setAttribute('data-index', String(i));
28
+ while (wrapper.firstChild)
29
+ wrap.appendChild(wrapper.firstChild);
30
+ frag.appendChild(wrap);
31
+ continue;
32
+ }
33
+ // Allow override if developer sets data-smilodon-handled="true"
34
+ if (!el.hasAttribute('data-smilodon-handled')) {
35
+ el.setAttribute('data-selectable', '');
36
+ el.setAttribute('data-index', String(i));
37
+ }
38
+ frag.appendChild(el);
39
+ }
40
+ container.replaceChildren(frag);
41
+ }
42
+
43
+ /**
44
+ * Fenwick Tree (Binary Indexed Tree) for O(log n) prefix sum queries and updates.
45
+ * Used for cumulative height calculations in variable-height virtualization.
46
+ *
47
+ * Space: O(n)
48
+ * Time: O(log n) for both query and update
49
+ *
50
+ * @see https://en.wikipedia.org/wiki/Fenwick_tree
51
+ */
52
+ class FenwickTree {
53
+ constructor(size) {
54
+ this.size = size;
55
+ // 1-indexed for cleaner bit manipulation
56
+ this.tree = new Float64Array(size + 1);
57
+ }
58
+ /**
59
+ * Add delta to index i (0-indexed input)
60
+ * Complexity: O(log n)
61
+ */
62
+ add(index, delta) {
63
+ // Convert to 1-indexed
64
+ let i = index + 1;
65
+ while (i <= this.size) {
66
+ this.tree[i] += delta;
67
+ // Move to next relevant index: i + LSB(i)
68
+ i += i & -i;
69
+ }
70
+ }
71
+ /**
72
+ * Get prefix sum from 0 to index (inclusive, 0-indexed input)
73
+ * Complexity: O(log n)
74
+ */
75
+ sum(index) {
76
+ if (index < 0)
77
+ return 0;
78
+ // Convert to 1-indexed
79
+ let i = index + 1;
80
+ let result = 0;
81
+ while (i > 0) {
82
+ result += this.tree[i];
83
+ // Move to parent: i - LSB(i)
84
+ i -= i & -i;
85
+ }
86
+ return result;
87
+ }
88
+ /**
89
+ * Get sum in range [left, right] (inclusive, 0-indexed)
90
+ * Complexity: O(log n)
91
+ */
92
+ rangeSum(left, right) {
93
+ if (left > right)
94
+ return 0;
95
+ return this.sum(right) - (left > 0 ? this.sum(left - 1) : 0);
96
+ }
97
+ /**
98
+ * Update value at index to newValue (handles delta internally)
99
+ * Requires tracking current values externally or use add() directly
100
+ * Complexity: O(log n)
101
+ */
102
+ update(index, oldValue, newValue) {
103
+ this.add(index, newValue - oldValue);
104
+ }
105
+ /**
106
+ * Binary search to find the smallest index where sum(index) >= target
107
+ * Useful for finding scroll position -> item index mapping
108
+ * Complexity: O(log² n) or O(log n) with optimization
109
+ *
110
+ * @returns index (0-indexed) or -1 if not found
111
+ */
112
+ lowerBound(target) {
113
+ if (target <= 0)
114
+ return 0;
115
+ // Optimized binary search on Fenwick tree structure
116
+ let pos = 0;
117
+ let sum = 0;
118
+ // Start from highest power of 2 <= size
119
+ let bit = 1 << Math.floor(Math.log2(this.size));
120
+ while (bit > 0) {
121
+ if (pos + bit <= this.size) {
122
+ const nextSum = sum + this.tree[pos + bit];
123
+ if (nextSum < target) {
124
+ pos += bit;
125
+ sum = nextSum;
126
+ }
127
+ }
128
+ bit >>= 1;
129
+ }
130
+ // Convert back to 0-indexed
131
+ return pos;
132
+ }
133
+ /**
134
+ * Reset all values to 0
135
+ * Complexity: O(n)
136
+ */
137
+ reset() {
138
+ this.tree.fill(0);
139
+ }
140
+ /**
141
+ * Resize the tree (useful for dynamic item lists)
142
+ * Complexity: O(n)
143
+ */
144
+ resize(newSize) {
145
+ if (newSize === this.size)
146
+ return;
147
+ const oldTree = this.tree;
148
+ const oldSize = this.size;
149
+ this.size = newSize;
150
+ this.tree = new Float64Array(newSize + 1);
151
+ // Copy existing values up to min(oldSize, newSize)
152
+ const copySize = Math.min(oldSize, newSize);
153
+ for (let i = 1; i <= copySize; i++) {
154
+ this.tree[i] = oldTree[i];
155
+ }
156
+ }
157
+ /**
158
+ * Get current size
159
+ */
160
+ getSize() {
161
+ return this.size;
162
+ }
163
+ /**
164
+ * Export state for serialization (e.g., to SharedArrayBuffer)
165
+ */
166
+ exportState() {
167
+ return {
168
+ size: this.size,
169
+ tree: Array.from(this.tree),
170
+ };
171
+ }
172
+ /**
173
+ * Import state from serialization
174
+ */
175
+ static fromState(state) {
176
+ const ft = new FenwickTree(state.size);
177
+ ft.tree = new Float64Array(state.tree);
178
+ return ft;
179
+ }
180
+ }
181
+
182
+ /**
183
+ * DOM Node Pool with LRU eviction and aggressive cleanup.
184
+ * Reuses nodes to minimize GC pressure and layout thrashing.
185
+ *
186
+ * Features:
187
+ * - LRU eviction when pool exceeds maxSize
188
+ * - Style reset to avoid style pollution
189
+ * - Event listener cleanup
190
+ * - Performance telemetry integration
191
+ */
192
+ class DOMPool {
193
+ constructor(options) {
194
+ this.pool = [];
195
+ // Telemetry
196
+ this.hits = 0;
197
+ this.misses = 0;
198
+ this.evictions = 0;
199
+ this.factory = options.factory;
200
+ this.reset = options.reset || this.defaultReset.bind(this);
201
+ this.maxSize = options.maxSize || 64;
202
+ this.telemetry = options.telemetry || false;
203
+ }
204
+ /**
205
+ * Acquire a node from pool or create new one
206
+ * Complexity: O(n) for LRU scan, amortized O(1) for small pools
207
+ */
208
+ acquire() {
209
+ const now = performance.now();
210
+ // Find first available node
211
+ for (let i = 0; i < this.pool.length; i++) {
212
+ const poolNode = this.pool[i];
213
+ if (!poolNode.inUse) {
214
+ poolNode.inUse = true;
215
+ poolNode.lastUsed = now;
216
+ this.reset(poolNode.node);
217
+ if (this.telemetry)
218
+ this.hits++;
219
+ return poolNode.node;
220
+ }
221
+ }
222
+ // No available nodes, create new one
223
+ const node = this.factory();
224
+ this.pool.push({
225
+ node,
226
+ lastUsed: now,
227
+ inUse: true,
228
+ });
229
+ if (this.telemetry)
230
+ this.misses++;
231
+ // Evict if pool too large
232
+ if (this.pool.length > this.maxSize) {
233
+ this.evictLRU();
234
+ }
235
+ return node;
236
+ }
237
+ /**
238
+ * Release node back to pool
239
+ * Complexity: O(n) to find node
240
+ */
241
+ release(node) {
242
+ const poolNode = this.pool.find(p => p.node === node);
243
+ if (poolNode) {
244
+ poolNode.inUse = false;
245
+ poolNode.lastUsed = performance.now();
246
+ // Aggressive cleanup
247
+ this.reset(poolNode.node);
248
+ }
249
+ }
250
+ /**
251
+ * Evict least recently used node
252
+ * Complexity: O(n)
253
+ */
254
+ evictLRU() {
255
+ // Find LRU node that's not in use
256
+ let lruIndex = -1;
257
+ let oldestTime = Infinity;
258
+ for (let i = 0; i < this.pool.length; i++) {
259
+ const poolNode = this.pool[i];
260
+ if (!poolNode.inUse && poolNode.lastUsed < oldestTime) {
261
+ lruIndex = i;
262
+ oldestTime = poolNode.lastUsed;
263
+ }
264
+ }
265
+ if (lruIndex >= 0) {
266
+ const evicted = this.pool.splice(lruIndex, 1)[0];
267
+ // Full cleanup before GC
268
+ this.deepCleanup(evicted.node);
269
+ if (this.telemetry)
270
+ this.evictions++;
271
+ }
272
+ }
273
+ /**
274
+ * Default reset: clear content, remove inline styles, remove attributes
275
+ */
276
+ defaultReset(node) {
277
+ // Clear content
278
+ node.textContent = '';
279
+ // Remove inline styles
280
+ node.removeAttribute('style');
281
+ // Remove data attributes (preserve data-index, data-selectable for delegation)
282
+ const attrs = Array.from(node.attributes);
283
+ for (const attr of attrs) {
284
+ if (attr.name.startsWith('data-') &&
285
+ attr.name !== 'data-index' &&
286
+ attr.name !== 'data-selectable') {
287
+ node.removeAttribute(attr.name);
288
+ }
289
+ }
290
+ // Remove ARIA attributes that might pollute reuse
291
+ const ariaAttrs = ['aria-selected', 'aria-checked', 'aria-disabled'];
292
+ ariaAttrs.forEach(attr => node.removeAttribute(attr));
293
+ }
294
+ /**
295
+ * Deep cleanup before eviction/destruction
296
+ */
297
+ deepCleanup(node) {
298
+ // Remove all event listeners by cloning (nuclear option)
299
+ const clone = node.cloneNode(true);
300
+ node.parentNode?.replaceChild(clone, node);
301
+ // Clear references
302
+ node.textContent = '';
303
+ }
304
+ /**
305
+ * Clear entire pool
306
+ */
307
+ clear() {
308
+ for (const poolNode of this.pool) {
309
+ this.deepCleanup(poolNode.node);
310
+ }
311
+ this.pool = [];
312
+ this.resetTelemetry();
313
+ }
314
+ /**
315
+ * Adjust max pool size dynamically
316
+ */
317
+ setMaxSize(size) {
318
+ this.maxSize = size;
319
+ // Evict excess nodes
320
+ while (this.pool.length > this.maxSize) {
321
+ this.evictLRU();
322
+ }
323
+ }
324
+ /**
325
+ * Get pool statistics
326
+ */
327
+ getStats() {
328
+ const available = this.pool.filter(p => !p.inUse).length;
329
+ const hitRate = this.hits + this.misses > 0
330
+ ? this.hits / (this.hits + this.misses)
331
+ : 0;
332
+ return {
333
+ total: this.pool.length,
334
+ available,
335
+ inUse: this.pool.length - available,
336
+ maxSize: this.maxSize,
337
+ hits: this.hits,
338
+ misses: this.misses,
339
+ evictions: this.evictions,
340
+ hitRate,
341
+ };
342
+ }
343
+ /**
344
+ * Reset telemetry counters
345
+ */
346
+ resetTelemetry() {
347
+ this.hits = 0;
348
+ this.misses = 0;
349
+ this.evictions = 0;
350
+ }
351
+ }
352
+
353
+ class Virtualizer {
354
+ constructor(container, itemsLength, itemGetter, options) {
355
+ this.measuredHeights = new Map();
356
+ this.activeNodes = new Map(); // index -> node mapping
357
+ this.container = container;
358
+ this.itemsLength = itemsLength;
359
+ this.itemGetter = itemGetter;
360
+ this.options = options;
361
+ this.averageHeight = options.estimatedItemHeight;
362
+ // Initialize DOM pool with LRU eviction
363
+ const maxPoolSize = Math.ceil((options.buffer * 2 + 10) * 1.5) + (options.maxPoolExtra ?? 32);
364
+ this.pool = new DOMPool({
365
+ maxSize: maxPoolSize,
366
+ factory: () => {
367
+ const div = document.createElement('div');
368
+ div.setAttribute('data-selectable', '');
369
+ return div;
370
+ },
371
+ reset: (node) => {
372
+ // Clear content but preserve data-selectable
373
+ node.textContent = '';
374
+ node.removeAttribute('style');
375
+ // Remove aria attributes
376
+ node.removeAttribute('aria-selected');
377
+ node.removeAttribute('aria-checked');
378
+ },
379
+ telemetry: options.enableTelemetry || false,
380
+ });
381
+ // Use Fenwick tree for large lists (>5000 items)
382
+ if (itemsLength > 5000) {
383
+ this.fenwick = new FenwickTree(itemsLength);
384
+ // Initialize with estimated heights
385
+ for (let i = 0; i < itemsLength; i++) {
386
+ this.fenwick.add(i, options.estimatedItemHeight);
387
+ }
388
+ }
389
+ this.container.style.willChange = 'transform';
390
+ }
391
+ computeWindow(scrollTop, viewportHeight) {
392
+ const { buffer } = this.options;
393
+ const startIndex = Math.max(Math.floor(scrollTop / this.averageHeight) - buffer, 0);
394
+ const endIndex = Math.min(Math.ceil((scrollTop + viewportHeight) / this.averageHeight) + buffer, this.itemsLength - 1);
395
+ const windowSize = Math.max(0, endIndex - startIndex + 1);
396
+ // Adjust pool size dynamically based on window
397
+ const newPoolSize = windowSize + (this.options.maxPoolExtra ?? 32);
398
+ this.pool.setMaxSize(newPoolSize);
399
+ return { startIndex, endIndex, windowSize };
400
+ }
401
+ cumulativeOffset(index) {
402
+ if (index <= 0)
403
+ return 0;
404
+ // Use Fenwick tree for O(log n) prefix sum
405
+ if (this.fenwick) {
406
+ return this.fenwick.sum(index - 1);
407
+ }
408
+ // Fallback: approximate using average height with sparse adjustments
409
+ let total = 0;
410
+ for (let i = 0; i < index; i++) {
411
+ total += this.measuredHeights.get(i) ?? this.averageHeight;
412
+ }
413
+ return total;
414
+ }
415
+ acquireNode(index) {
416
+ // Check if we already have this node active (reuse)
417
+ const existing = this.activeNodes.get(index);
418
+ if (existing)
419
+ return existing;
420
+ const node = this.pool.acquire();
421
+ node.setAttribute('data-index', String(index));
422
+ this.activeNodes.set(index, node);
423
+ return node;
424
+ }
425
+ releaseNode(index) {
426
+ const node = this.activeNodes.get(index);
427
+ if (node) {
428
+ this.pool.release(node);
429
+ this.activeNodes.delete(index);
430
+ }
431
+ }
432
+ releaseExcess(activeIndices) {
433
+ // Release nodes not in active set
434
+ for (const [index, node] of this.activeNodes) {
435
+ if (!activeIndices.has(index)) {
436
+ this.pool.release(node);
437
+ this.activeNodes.delete(index);
438
+ }
439
+ }
440
+ }
441
+ render(startIndex, endIndex, updateNode) {
442
+ const frag = document.createDocumentFragment();
443
+ const activeIndices = new Set();
444
+ for (let i = startIndex; i <= endIndex; i++) {
445
+ const node = this.acquireNode(i);
446
+ updateNode(node, this.itemGetter(i), i);
447
+ frag.appendChild(node);
448
+ activeIndices.add(i);
449
+ // Measure on appear (deferred)
450
+ queueMicrotask(() => this.measureOnAppear(node, i));
451
+ }
452
+ // Translate container by cumulative offset of startIndex
453
+ const topOffset = this.cumulativeOffset(startIndex);
454
+ this.container.style.transform = `translate3d(0, ${Math.round(topOffset * 100) / 100}px, 0)`;
455
+ this.container.replaceChildren(frag);
456
+ // Recycle nodes not in use
457
+ requestAnimationFrame(() => this.releaseExcess(activeIndices));
458
+ }
459
+ measureOnAppear(node, index) {
460
+ const h = node.offsetHeight;
461
+ const prev = this.measuredHeights.get(index);
462
+ const threshold = this.options.measurementThreshold ?? 5;
463
+ if (prev === undefined || Math.abs(prev - h) > threshold) {
464
+ const oldValue = prev ?? this.averageHeight;
465
+ this.measuredHeights.set(index, h);
466
+ // Update Fenwick tree with new measurement
467
+ if (this.fenwick) {
468
+ this.fenwick.update(index, oldValue, h);
469
+ }
470
+ // Update running average (weighted by measured count)
471
+ const count = this.measuredHeights.size;
472
+ const total = Array.from(this.measuredHeights.values()).reduce((a, b) => a + b, 0);
473
+ this.averageHeight = Math.round((total / count) * 100) / 100;
474
+ }
475
+ }
476
+ /**
477
+ * Get pool statistics (if telemetry enabled)
478
+ */
479
+ getPoolStats() {
480
+ return this.pool.getStats();
481
+ }
482
+ /**
483
+ * Update items length and resize Fenwick tree if needed
484
+ */
485
+ setItemsLength(newLength) {
486
+ this.itemsLength = newLength;
487
+ if (this.fenwick) {
488
+ this.fenwick.resize(newLength);
489
+ }
490
+ else if (newLength > 5000 && !this.fenwick) {
491
+ // Activate Fenwick tree when crossing threshold
492
+ this.fenwick = new FenwickTree(newLength);
493
+ // Populate with existing measurements
494
+ for (const [index, height] of this.measuredHeights) {
495
+ this.fenwick.update(index, this.averageHeight, height);
496
+ }
497
+ }
498
+ }
499
+ /**
500
+ * Cleanup resources
501
+ */
502
+ destroy() {
503
+ this.pool.clear();
504
+ this.activeNodes.clear();
505
+ this.measuredHeights.clear();
506
+ if (this.fenwick) {
507
+ this.fenwick.reset();
508
+ }
509
+ }
510
+ }
511
+
512
+ class NativeSelectElement extends HTMLElement {
513
+ static get observedAttributes() {
514
+ return ['placement', 'strategy', 'portal'];
515
+ }
516
+ constructor() {
517
+ super();
518
+ this._options = {};
519
+ this._items = [];
520
+ // Multi-select & interaction state
521
+ this._selectedSet = new Set(); // indices
522
+ this._selectedItems = new Map(); // index -> item
523
+ this._activeIndex = -1;
524
+ this._multi = false;
525
+ this._typeBuffer = '';
526
+ this._shadow = this.attachShadow({ mode: 'open' });
527
+ this._listRoot = document.createElement('div');
528
+ this._listRoot.setAttribute('role', 'listbox');
529
+ this._listRoot.setAttribute('tabindex', '0');
530
+ this._shadow.appendChild(this._listRoot);
531
+ // Live region for screen reader announcements
532
+ this._liveRegion = document.createElement('div');
533
+ this._liveRegion.setAttribute('role', 'status');
534
+ this._liveRegion.setAttribute('aria-live', 'polite');
535
+ this._liveRegion.style.cssText = 'position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden;';
536
+ this._shadow.appendChild(this._liveRegion);
537
+ this._helpers = createRendererHelpers((item, index) => this._onSelect(item, index));
538
+ // Delegated click
539
+ this._listRoot.addEventListener('click', (e) => {
540
+ const el = e.target.closest('[data-selectable]');
541
+ if (!el)
542
+ return;
543
+ const idx = Number(el.dataset.index);
544
+ const item = this._items[idx];
545
+ this._onSelect(item, idx);
546
+ });
547
+ // Keyboard navigation
548
+ this._listRoot.addEventListener('keydown', (e) => this._onKeydown(e));
549
+ }
550
+ connectedCallback() {
551
+ // Initialize ARIA roles and open event
552
+ this._listRoot.setAttribute('role', 'listbox');
553
+ this._listRoot.setAttribute('aria-label', 'Options list');
554
+ if (this._multi)
555
+ this._listRoot.setAttribute('aria-multiselectable', 'true');
556
+ this._emit('open', {});
557
+ }
558
+ disconnectedCallback() {
559
+ this._emit('close', {});
560
+ // Cleanup: remove listeners if any were added outside constructor
561
+ if (this._typeTimeout)
562
+ window.clearTimeout(this._typeTimeout);
563
+ }
564
+ attributeChangedCallback(name, _oldValue, newValue) {
565
+ switch (name) {
566
+ case 'placement':
567
+ this._options.placement = (newValue ?? undefined);
568
+ break;
569
+ case 'strategy':
570
+ this._options.strategy = (newValue ?? undefined);
571
+ break;
572
+ case 'portal':
573
+ this._options.portal = newValue === 'true' ? true : newValue === 'false' ? false : undefined;
574
+ break;
575
+ }
576
+ }
577
+ set items(items) {
578
+ this._items = items ?? [];
579
+ // initialize virtualizer with estimated height (default 48) and buffer
580
+ this._virtualizer = new Virtualizer(this._listRoot, this._items.length, (i) => this._items[i], { estimatedItemHeight: 48, buffer: 5 });
581
+ this.render();
582
+ }
583
+ get items() {
584
+ return this._items;
585
+ }
586
+ set multi(value) {
587
+ this._multi = value;
588
+ if (value) {
589
+ this._listRoot.setAttribute('aria-multiselectable', 'true');
590
+ }
591
+ else {
592
+ this._listRoot.removeAttribute('aria-multiselectable');
593
+ }
594
+ }
595
+ get multi() {
596
+ return this._multi;
597
+ }
598
+ get selectedIndices() {
599
+ return Array.from(this._selectedSet);
600
+ }
601
+ get selectedItems() {
602
+ return Array.from(this._selectedItems.values());
603
+ }
604
+ set optionTemplate(template) {
605
+ this._options.optionTemplate = template;
606
+ this.render();
607
+ }
608
+ set optionRenderer(renderer) {
609
+ this._options.optionRenderer = renderer;
610
+ this.render();
611
+ }
612
+ render() {
613
+ const { optionTemplate, optionRenderer } = this._options;
614
+ const viewportHeight = this.getBoundingClientRect().height || 300;
615
+ const scrollTop = this.scrollTop || 0;
616
+ // Update aria-activedescendant
617
+ if (this._activeIndex >= 0) {
618
+ this._listRoot.setAttribute('aria-activedescendant', `option-${this._activeIndex}`);
619
+ }
620
+ else {
621
+ this._listRoot.removeAttribute('aria-activedescendant');
622
+ }
623
+ if (this._virtualizer) {
624
+ const { startIndex, endIndex } = this._virtualizer.computeWindow(scrollTop, viewportHeight);
625
+ this._virtualizer.render(startIndex, endIndex, (node, item, i) => {
626
+ this._applyOptionAttrs(node, i);
627
+ if (optionRenderer) {
628
+ const el = optionRenderer(item, i, this._helpers);
629
+ // replace node contents
630
+ node.replaceChildren(el);
631
+ }
632
+ else if (optionTemplate) {
633
+ const wrapper = document.createElement('div');
634
+ wrapper.innerHTML = optionTemplate(item, i);
635
+ const el = wrapper.firstElementChild;
636
+ node.replaceChildren(el ?? document.createTextNode(String(item)));
637
+ }
638
+ else {
639
+ node.textContent = String(item);
640
+ }
641
+ });
642
+ return;
643
+ }
644
+ const frag = document.createDocumentFragment();
645
+ for (let i = 0; i < this._items.length; i++) {
646
+ const item = this._items[i];
647
+ if (optionRenderer) {
648
+ const el = optionRenderer(item, i, this._helpers);
649
+ if (!el.hasAttribute('data-selectable')) {
650
+ el.setAttribute('data-selectable', '');
651
+ el.setAttribute('data-index', String(i));
652
+ }
653
+ this._applyOptionAttrs(el, i);
654
+ frag.appendChild(el);
655
+ }
656
+ else if (optionTemplate) {
657
+ // Fast path: render all via DocumentFragment in one call
658
+ renderTemplate(this._listRoot, this._items, optionTemplate);
659
+ // Apply ARIA attrs after template render
660
+ this._applyAriaToAll();
661
+ return; // rendering complete
662
+ }
663
+ else {
664
+ const el = document.createElement('div');
665
+ el.textContent = String(item);
666
+ el.setAttribute('data-selectable', '');
667
+ el.setAttribute('data-index', String(i));
668
+ this._applyOptionAttrs(el, i);
669
+ frag.appendChild(el);
670
+ }
671
+ }
672
+ this._listRoot.replaceChildren(frag);
673
+ }
674
+ _applyOptionAttrs(node, index) {
675
+ node.setAttribute('role', 'option');
676
+ node.id = `option-${index}`;
677
+ if (this._selectedSet.has(index)) {
678
+ node.setAttribute('aria-selected', 'true');
679
+ }
680
+ else {
681
+ node.setAttribute('aria-selected', 'false');
682
+ }
683
+ }
684
+ _applyAriaToAll() {
685
+ const children = Array.from(this._listRoot.children);
686
+ for (const child of children) {
687
+ const idx = Number(child.dataset.index);
688
+ if (Number.isFinite(idx))
689
+ this._applyOptionAttrs(child, idx);
690
+ }
691
+ }
692
+ _emit(name, detail) {
693
+ this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true }));
694
+ }
695
+ // Multi-select and interaction methods
696
+ _onSelect(item, index) {
697
+ if (this._multi) {
698
+ if (this._selectedSet.has(index)) {
699
+ this._selectedSet.delete(index);
700
+ this._selectedItems.delete(index);
701
+ }
702
+ else {
703
+ this._selectedSet.add(index);
704
+ this._selectedItems.set(index, item);
705
+ }
706
+ }
707
+ else {
708
+ this._selectedSet.clear();
709
+ this._selectedItems.clear();
710
+ this._selectedSet.add(index);
711
+ this._selectedItems.set(index, item);
712
+ }
713
+ this._activeIndex = index;
714
+ this.render();
715
+ // Emit with all required fields
716
+ const selected = this._selectedSet.has(index);
717
+ this._emit('select', {
718
+ item,
719
+ index,
720
+ value: item?.value ?? item,
721
+ label: item?.label ?? String(item),
722
+ selected,
723
+ multi: this._multi
724
+ });
725
+ this._announce(`Selected ${String(item)}`);
726
+ }
727
+ _onKeydown(e) {
728
+ switch (e.key) {
729
+ case 'ArrowDown':
730
+ e.preventDefault();
731
+ this._moveActive(1);
732
+ break;
733
+ case 'ArrowUp':
734
+ e.preventDefault();
735
+ this._moveActive(-1);
736
+ break;
737
+ case 'Home':
738
+ e.preventDefault();
739
+ this._setActive(0);
740
+ break;
741
+ case 'End':
742
+ e.preventDefault();
743
+ this._setActive(this._items.length - 1);
744
+ break;
745
+ case 'PageDown':
746
+ e.preventDefault();
747
+ this._moveActive(10);
748
+ break;
749
+ case 'PageUp':
750
+ e.preventDefault();
751
+ this._moveActive(-10);
752
+ break;
753
+ case 'Enter':
754
+ case ' ':
755
+ e.preventDefault();
756
+ if (this._activeIndex >= 0) {
757
+ const item = this._items[this._activeIndex];
758
+ this._onSelect(item, this._activeIndex);
759
+ }
760
+ break;
761
+ case 'Escape':
762
+ e.preventDefault();
763
+ this._emit('close', {});
764
+ break;
765
+ default:
766
+ // Type-ahead buffer
767
+ if (e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey) {
768
+ this._onType(e.key);
769
+ }
770
+ break;
771
+ }
772
+ }
773
+ _moveActive(delta) {
774
+ const next = Math.max(0, Math.min(this._items.length - 1, this._activeIndex + delta));
775
+ this._setActive(next);
776
+ }
777
+ _setActive(index) {
778
+ this._activeIndex = index;
779
+ this.render();
780
+ this._scrollToActive();
781
+ this._announce(`Navigated to ${String(this._items[index])}`);
782
+ }
783
+ _scrollToActive() {
784
+ const el = this._shadow.getElementById(`option-${this._activeIndex}`);
785
+ el?.scrollIntoView({ block: 'nearest', inline: 'nearest' });
786
+ }
787
+ _onType(char) {
788
+ if (this._typeTimeout)
789
+ window.clearTimeout(this._typeTimeout);
790
+ this._typeBuffer += char.toLowerCase();
791
+ this._typeTimeout = window.setTimeout(() => {
792
+ this._typeBuffer = '';
793
+ }, 400);
794
+ // Find first matching item
795
+ const match = this._items.findIndex((item) => String(item).toLowerCase().startsWith(this._typeBuffer));
796
+ if (match >= 0) {
797
+ this._setActive(match);
798
+ }
799
+ }
800
+ _announce(msg) {
801
+ if (this._liveRegion) {
802
+ this._liveRegion.textContent = msg;
803
+ setTimeout(() => {
804
+ if (this._liveRegion)
805
+ this._liveRegion.textContent = '';
806
+ }, 1000);
807
+ }
808
+ }
809
+ // Focus management
810
+ focus() {
811
+ this._listRoot.focus();
812
+ }
813
+ }
814
+ customElements.define('smilodon-select', NativeSelectElement);
815
+
816
+ /**
817
+ * Global Configuration System for Select Components
818
+ * Allows users to define default behaviors that can be overridden at component level
819
+ */
820
+ /**
821
+ * Default global configuration
822
+ */
823
+ const defaultConfig = {
824
+ selection: {
825
+ mode: 'single',
826
+ allowDeselect: false,
827
+ maxSelections: 0,
828
+ showRemoveButton: true,
829
+ closeOnSelect: true,
830
+ },
831
+ scrollToSelected: {
832
+ enabled: true,
833
+ multiSelectTarget: 'first',
834
+ behavior: 'smooth',
835
+ block: 'nearest',
836
+ },
837
+ loadMore: {
838
+ enabled: false,
839
+ itemsPerLoad: 3,
840
+ threshold: 100,
841
+ showLoader: true,
842
+ },
843
+ busyBucket: {
844
+ enabled: true,
845
+ showSpinner: true,
846
+ message: 'Loading...',
847
+ minDisplayTime: 200,
848
+ },
849
+ styles: {
850
+ classNames: {},
851
+ },
852
+ serverSide: {
853
+ enabled: false,
854
+ getValueFromItem: (item) => item?.value ?? item,
855
+ getLabelFromItem: (item) => item?.label ?? String(item),
856
+ },
857
+ infiniteScroll: {
858
+ enabled: false,
859
+ pageSize: 20,
860
+ initialPage: 1,
861
+ cachePages: true,
862
+ maxCachedPages: 10,
863
+ preloadAdjacent: true,
864
+ scrollRestoration: 'auto',
865
+ },
866
+ callbacks: {},
867
+ enabled: true,
868
+ searchable: false,
869
+ placeholder: 'Select an option...',
870
+ virtualize: true,
871
+ estimatedItemHeight: 48,
872
+ };
873
+ /**
874
+ * Global configuration instance
875
+ */
876
+ class SelectConfigManager {
877
+ constructor() {
878
+ this.config = this.deepClone(defaultConfig);
879
+ }
880
+ /**
881
+ * Get current global configuration
882
+ */
883
+ getConfig() {
884
+ return this.config;
885
+ }
886
+ /**
887
+ * Update global configuration (deep merge)
888
+ */
889
+ updateConfig(updates) {
890
+ this.config = this.deepMerge(this.config, updates);
891
+ }
892
+ /**
893
+ * Reset to default configuration
894
+ */
895
+ resetConfig() {
896
+ this.config = this.deepClone(defaultConfig);
897
+ }
898
+ /**
899
+ * Merge component-level config with global config
900
+ * Component-level config takes precedence
901
+ */
902
+ mergeWithComponentConfig(componentConfig) {
903
+ return this.deepMerge(this.deepClone(this.config), componentConfig);
904
+ }
905
+ deepClone(obj) {
906
+ return JSON.parse(JSON.stringify(obj));
907
+ }
908
+ deepMerge(target, source) {
909
+ const result = { ...target };
910
+ for (const key in source) {
911
+ if (source.hasOwnProperty(key)) {
912
+ const sourceValue = source[key];
913
+ const targetValue = result[key];
914
+ if (sourceValue && typeof sourceValue === 'object' && !Array.isArray(sourceValue)) {
915
+ result[key] = this.deepMerge(targetValue && typeof targetValue === 'object' ? targetValue : {}, sourceValue);
916
+ }
917
+ else {
918
+ result[key] = sourceValue;
919
+ }
920
+ }
921
+ }
922
+ return result;
923
+ }
924
+ }
925
+ /**
926
+ * Singleton instance
927
+ */
928
+ const selectConfig = new SelectConfigManager();
929
+ /**
930
+ * Helper function to configure select globally
931
+ */
932
+ function configureSelect(config) {
933
+ selectConfig.updateConfig(config);
934
+ }
935
+ /**
936
+ * Helper function to reset select configuration
937
+ */
938
+ function resetSelectConfig() {
939
+ selectConfig.resetConfig();
940
+ }
941
+
942
+ /**
943
+ * Enhanced Select Component
944
+ * Implements all advanced features: infinite scroll, load more, busy state,
945
+ * server-side selection, and full customization
946
+ */
947
+ class EnhancedSelect extends HTMLElement {
948
+ constructor() {
949
+ super();
950
+ this._pageCache = {};
951
+ this._typeBuffer = '';
952
+ this._hasError = false;
953
+ this._errorMessage = '';
954
+ this._boundArrowClick = null;
955
+ this._shadow = this.attachShadow({ mode: 'open' });
956
+ this._uniqueId = `enhanced-select-${Math.random().toString(36).substr(2, 9)}`;
957
+ // Merge global config with component-level config
958
+ this._config = selectConfig.getConfig();
959
+ // Initialize state
960
+ this._state = {
961
+ isOpen: false,
962
+ isBusy: false,
963
+ isSearching: false,
964
+ currentPage: this._config.infiniteScroll.initialPage || 1,
965
+ totalPages: 1,
966
+ selectedIndices: new Set(),
967
+ selectedItems: new Map(),
968
+ activeIndex: -1,
969
+ searchQuery: '',
970
+ loadedItems: [],
971
+ groupedItems: [],
972
+ preserveScrollPosition: false,
973
+ lastScrollPosition: 0,
974
+ lastNotifiedQuery: null,
975
+ lastNotifiedResultCount: 0,
976
+ };
977
+ // Create DOM structure
978
+ this._container = this._createContainer();
979
+ this._inputContainer = this._createInputContainer();
980
+ this._input = this._createInput();
981
+ this._arrowContainer = this._createArrowContainer();
982
+ this._dropdown = this._createDropdown();
983
+ this._optionsContainer = this._createOptionsContainer();
984
+ this._liveRegion = this._createLiveRegion();
985
+ this._assembleDOM();
986
+ this._initializeStyles();
987
+ this._attachEventListeners();
988
+ this._initializeObservers();
989
+ }
990
+ connectedCallback() {
991
+ // Load initial data if server-side is enabled
992
+ if (this._config.serverSide.enabled && this._config.serverSide.initialSelectedValues) {
993
+ this._loadInitialSelectedItems();
994
+ }
995
+ // Emit open event if configured to start open
996
+ if (this._config.callbacks.onOpen) {
997
+ this._config.callbacks.onOpen();
998
+ }
999
+ }
1000
+ disconnectedCallback() {
1001
+ // Cleanup observers
1002
+ this._resizeObserver?.disconnect();
1003
+ this._intersectionObserver?.disconnect();
1004
+ if (this._busyTimeout)
1005
+ clearTimeout(this._busyTimeout);
1006
+ if (this._typeTimeout)
1007
+ clearTimeout(this._typeTimeout);
1008
+ if (this._searchTimeout)
1009
+ clearTimeout(this._searchTimeout);
1010
+ // Cleanup arrow click listener
1011
+ if (this._boundArrowClick && this._arrowContainer) {
1012
+ this._arrowContainer.removeEventListener('click', this._boundArrowClick);
1013
+ }
1014
+ }
1015
+ _createContainer() {
1016
+ const container = document.createElement('div');
1017
+ container.className = 'select-container';
1018
+ if (this._config.styles.classNames?.container) {
1019
+ container.className += ' ' + this._config.styles.classNames.container;
1020
+ }
1021
+ if (this._config.styles.container) {
1022
+ Object.assign(container.style, this._config.styles.container);
1023
+ }
1024
+ return container;
1025
+ }
1026
+ _createInputContainer() {
1027
+ const container = document.createElement('div');
1028
+ container.className = 'input-container';
1029
+ return container;
1030
+ }
1031
+ _createInput() {
1032
+ const input = document.createElement('input');
1033
+ input.type = 'text';
1034
+ input.className = 'select-input';
1035
+ input.placeholder = this._config.placeholder || 'Select an option...';
1036
+ input.disabled = !this._config.enabled;
1037
+ input.readOnly = !this._config.searchable;
1038
+ // Update readonly when input is focused if searchable
1039
+ input.addEventListener('focus', () => {
1040
+ if (this._config.searchable) {
1041
+ input.readOnly = false;
1042
+ }
1043
+ });
1044
+ if (this._config.styles.classNames?.input) {
1045
+ input.className += ' ' + this._config.styles.classNames.input;
1046
+ }
1047
+ if (this._config.styles.input) {
1048
+ Object.assign(input.style, this._config.styles.input);
1049
+ }
1050
+ input.setAttribute('role', 'combobox');
1051
+ input.setAttribute('aria-expanded', 'false');
1052
+ input.setAttribute('aria-haspopup', 'listbox');
1053
+ input.setAttribute('aria-autocomplete', this._config.searchable ? 'list' : 'none');
1054
+ return input;
1055
+ }
1056
+ _createDropdown() {
1057
+ const dropdown = document.createElement('div');
1058
+ dropdown.className = 'select-dropdown';
1059
+ dropdown.style.display = 'none';
1060
+ if (this._config.styles.classNames?.dropdown) {
1061
+ dropdown.className += ' ' + this._config.styles.classNames.dropdown;
1062
+ }
1063
+ if (this._config.styles.dropdown) {
1064
+ Object.assign(dropdown.style, this._config.styles.dropdown);
1065
+ }
1066
+ dropdown.setAttribute('role', 'listbox');
1067
+ if (this._config.selection.mode === 'multi') {
1068
+ dropdown.setAttribute('aria-multiselectable', 'true');
1069
+ }
1070
+ return dropdown;
1071
+ }
1072
+ _createOptionsContainer() {
1073
+ const container = document.createElement('div');
1074
+ container.className = 'options-container';
1075
+ return container;
1076
+ }
1077
+ _createLiveRegion() {
1078
+ const liveRegion = document.createElement('div');
1079
+ liveRegion.setAttribute('role', 'status');
1080
+ liveRegion.setAttribute('aria-live', 'polite');
1081
+ liveRegion.setAttribute('aria-atomic', 'true');
1082
+ liveRegion.style.cssText = 'position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0;';
1083
+ return liveRegion;
1084
+ }
1085
+ _createArrowContainer() {
1086
+ const container = document.createElement('div');
1087
+ container.className = 'dropdown-arrow-container';
1088
+ container.innerHTML = `
1089
+ <svg class="dropdown-arrow" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
1090
+ <path d="M4 6L8 10L12 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
1091
+ </svg>
1092
+ `;
1093
+ return container;
1094
+ }
1095
+ _assembleDOM() {
1096
+ this._inputContainer.appendChild(this._input);
1097
+ if (this._arrowContainer) {
1098
+ this._inputContainer.appendChild(this._arrowContainer);
1099
+ }
1100
+ this._container.appendChild(this._inputContainer);
1101
+ this._dropdown.appendChild(this._optionsContainer);
1102
+ this._container.appendChild(this._dropdown);
1103
+ this._shadow.appendChild(this._container);
1104
+ if (this._liveRegion) {
1105
+ this._shadow.appendChild(this._liveRegion);
1106
+ }
1107
+ // Set ARIA relationships
1108
+ const listboxId = `${this._uniqueId}-listbox`;
1109
+ this._dropdown.id = listboxId;
1110
+ this._input.setAttribute('aria-controls', listboxId);
1111
+ this._input.setAttribute('aria-owns', listboxId);
1112
+ }
1113
+ _initializeStyles() {
1114
+ const style = document.createElement('style');
1115
+ style.textContent = `
1116
+ :host {
1117
+ display: block;
1118
+ position: relative;
1119
+ width: 100%;
1120
+ }
1121
+
1122
+ .select-container {
1123
+ position: relative;
1124
+ width: 100%;
1125
+ }
1126
+
1127
+ .input-container {
1128
+ position: relative;
1129
+ width: 100%;
1130
+ display: flex;
1131
+ align-items: center;
1132
+ flex-wrap: wrap;
1133
+ gap: 6px;
1134
+ padding: 6px 52px 6px 8px;
1135
+ min-height: 44px;
1136
+ background: white;
1137
+ border: 1px solid #d1d5db;
1138
+ border-radius: 6px;
1139
+ box-sizing: border-box;
1140
+ transition: all 0.2s ease;
1141
+ }
1142
+
1143
+ .input-container:focus-within {
1144
+ border-color: #667eea;
1145
+ box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
1146
+ }
1147
+
1148
+ /* Gradient separator before arrow */
1149
+ .input-container::after {
1150
+ content: '';
1151
+ position: absolute;
1152
+ top: 50%;
1153
+ right: 40px;
1154
+ transform: translateY(-50%);
1155
+ width: 1px;
1156
+ height: 60%;
1157
+ background: linear-gradient(
1158
+ to bottom,
1159
+ transparent 0%,
1160
+ rgba(0, 0, 0, 0.1) 20%,
1161
+ rgba(0, 0, 0, 0.1) 80%,
1162
+ transparent 100%
1163
+ );
1164
+ pointer-events: none;
1165
+ z-index: 1;
1166
+ }
1167
+
1168
+ .dropdown-arrow-container {
1169
+ position: absolute;
1170
+ top: 0;
1171
+ right: 0;
1172
+ bottom: 0;
1173
+ width: 40px;
1174
+ display: flex;
1175
+ align-items: center;
1176
+ justify-content: center;
1177
+ cursor: pointer;
1178
+ transition: background-color 0.2s ease;
1179
+ border-radius: 0 4px 4px 0;
1180
+ z-index: 2;
1181
+ }
1182
+
1183
+ .dropdown-arrow-container:hover {
1184
+ background-color: rgba(102, 126, 234, 0.08);
1185
+ }
1186
+
1187
+ .dropdown-arrow {
1188
+ width: 16px;
1189
+ height: 16px;
1190
+ color: #667eea;
1191
+ transition: transform 0.2s ease, color 0.2s ease;
1192
+ transform: translateY(0);
1193
+ }
1194
+
1195
+ .dropdown-arrow-container:hover .dropdown-arrow {
1196
+ color: #667eea;
1197
+ }
1198
+
1199
+ .dropdown-arrow.open {
1200
+ transform: rotate(180deg);
1201
+ }
1202
+
1203
+ .select-input {
1204
+ flex: 1;
1205
+ min-width: 120px;
1206
+ padding: 4px;
1207
+ border: none;
1208
+ font-size: 14px;
1209
+ line-height: 1.5;
1210
+ color: #1f2937;
1211
+ background: transparent;
1212
+ box-sizing: border-box;
1213
+ outline: none;
1214
+ }
1215
+
1216
+ .select-input::placeholder {
1217
+ color: #9ca3af;
1218
+ }
1219
+
1220
+ .selection-badge {
1221
+ display: inline-flex;
1222
+ align-items: center;
1223
+ gap: 4px;
1224
+ padding: 4px 8px;
1225
+ margin: 2px;
1226
+ background: #667eea;
1227
+ color: white;
1228
+ border-radius: 4px;
1229
+ font-size: 13px;
1230
+ line-height: 1;
1231
+ }
1232
+
1233
+ .badge-remove {
1234
+ display: inline-flex;
1235
+ align-items: center;
1236
+ justify-content: center;
1237
+ width: 16px;
1238
+ height: 16px;
1239
+ padding: 0;
1240
+ margin-left: 4px;
1241
+ background: rgba(255, 255, 255, 0.3);
1242
+ border: none;
1243
+ border-radius: 50%;
1244
+ color: white;
1245
+ font-size: 16px;
1246
+ line-height: 1;
1247
+ cursor: pointer;
1248
+ transition: background 0.2s;
1249
+ }
1250
+
1251
+ .badge-remove:hover {
1252
+ background: rgba(255, 255, 255, 0.5);
1253
+ }
1254
+
1255
+ .select-input:disabled {
1256
+ background-color: var(--select-disabled-bg, #f5f5f5);
1257
+ cursor: not-allowed;
1258
+ }
1259
+
1260
+ .select-dropdown {
1261
+ position: absolute;
1262
+ scroll-behavior: smooth;
1263
+ top: 100%;
1264
+ left: 0;
1265
+ right: 0;
1266
+ margin-top: 4px;
1267
+ max-height: 300px;
1268
+ overflow-y: auto;
1269
+ background: var(--select-dropdown-bg, white);
1270
+ border: 1px solid var(--select-dropdown-border, #ccc);
1271
+ border-radius: var(--select-border-radius, 4px);
1272
+ box-shadow: var(--select-dropdown-shadow, 0 4px 6px rgba(0,0,0,0.1));
1273
+ z-index: var(--select-dropdown-z-index, 1000);
1274
+ }
1275
+
1276
+ .options-container {
1277
+ position: relative;
1278
+ transition: opacity 0.2s ease-in-out;
1279
+ }
1280
+
1281
+ .option {
1282
+ padding: 8px 12px;
1283
+ cursor: pointer;
1284
+ color: inherit;
1285
+ transition: background-color 0.15s ease;
1286
+ user-select: none;
1287
+ }
1288
+
1289
+ .option:hover {
1290
+ background-color: #f3f4f6;
1291
+ }
1292
+
1293
+ .option.selected {
1294
+ background-color: #e0e7ff;
1295
+ color: #4338ca;
1296
+ font-weight: 500;
1297
+ }
1298
+
1299
+ .option.active {
1300
+ background-color: #f3f4f6;
1301
+ }
1302
+
1303
+ .load-more-container {
1304
+ padding: 12px;
1305
+ text-align: center;
1306
+ border-top: 1px solid var(--select-divider-color, #e0e0e0);
1307
+ }
1308
+
1309
+ .load-more-button {
1310
+ padding: 8px 16px;
1311
+ border: 1px solid var(--select-button-border, #1976d2);
1312
+ background: var(--select-button-bg, white);
1313
+ color: var(--select-button-color, #1976d2);
1314
+ border-radius: 4px;
1315
+ cursor: pointer;
1316
+ font-size: 14px;
1317
+ transition: all 0.2s ease;
1318
+ }
1319
+
1320
+ .load-more-button:hover {
1321
+ background: var(--select-button-hover-bg, #1976d2);
1322
+ color: var(--select-button-hover-color, white);
1323
+ }
1324
+
1325
+ .load-more-button:disabled {
1326
+ opacity: 0.5;
1327
+ cursor: not-allowed;
1328
+ }
1329
+
1330
+ .busy-bucket {
1331
+ padding: 16px;
1332
+ text-align: center;
1333
+ color: var(--select-busy-color, #666);
1334
+ }
1335
+
1336
+ .spinner {
1337
+ display: inline-block;
1338
+ width: 20px;
1339
+ height: 20px;
1340
+ border: 2px solid var(--select-spinner-color, #ccc);
1341
+ border-top-color: var(--select-spinner-active-color, #1976d2);
1342
+ border-radius: 50%;
1343
+ animation: spin 0.6s linear infinite;
1344
+ }
1345
+
1346
+ @keyframes spin {
1347
+ to { transform: rotate(360deg); }
1348
+ }
1349
+
1350
+ .empty-state {
1351
+ padding: 24px;
1352
+ text-align: center;
1353
+ color: var(--select-empty-color, #999);
1354
+ }
1355
+
1356
+ .searching-state {
1357
+ padding: 24px;
1358
+ text-align: center;
1359
+ color: #667eea;
1360
+ font-style: italic;
1361
+ animation: pulse 1.5s ease-in-out infinite;
1362
+ }
1363
+
1364
+ @keyframes pulse {
1365
+ 0%, 100% { opacity: 1; }
1366
+ 50% { opacity: 0.5; }
1367
+ }
1368
+
1369
+ /* Error states */
1370
+ .select-input[aria-invalid="true"] {
1371
+ border-color: var(--select-error-border, #dc2626);
1372
+ }
1373
+
1374
+ .select-input[aria-invalid="true"]:focus {
1375
+ border-color: var(--select-error-border, #dc2626);
1376
+ box-shadow: 0 0 0 2px var(--select-error-shadow, rgba(220, 38, 38, 0.1));
1377
+ outline-color: var(--select-error-border, #dc2626);
1378
+ }
1379
+
1380
+ /* Accessibility: Reduced motion */
1381
+ @media (prefers-reduced-motion: reduce) {
1382
+ * {
1383
+ animation-duration: 0.01ms !important;
1384
+ animation-iteration-count: 1 !important;
1385
+ transition-duration: 0.01ms !important;
1386
+ }
1387
+ }
1388
+
1389
+ /* Accessibility: Dark mode */
1390
+ @media (prefers-color-scheme: dark) {
1391
+ .select-input {
1392
+ background: var(--select-dark-bg, #1f2937);
1393
+ color: var(--select-dark-text, #f9fafb);
1394
+ border-color: var(--select-dark-border, #4b5563);
1395
+ }
1396
+
1397
+ .select-dropdown {
1398
+ background: var(--select-dark-dropdown-bg, #1f2937);
1399
+ border-color: var(--select-dark-dropdown-border, #4b5563);
1400
+ color: var(--select-dark-text, #f9fafb);
1401
+ }
1402
+
1403
+ .option:hover {
1404
+ background-color: var(--select-dark-option-hover-bg, #374151);
1405
+ }
1406
+
1407
+ .option.selected {
1408
+ background-color: var(--select-dark-option-selected-bg, #3730a3);
1409
+ color: var(--select-dark-option-selected-text, #e0e7ff);
1410
+ }
1411
+
1412
+ .option.active {
1413
+ background-color: var(--select-dark-option-active-bg, #374151);
1414
+ }
1415
+
1416
+ .busy-bucket {
1417
+ color: var(--select-dark-busy-color, #9ca3af);
1418
+ }
1419
+ }
1420
+
1421
+ /* Accessibility: High contrast mode */
1422
+ @media (prefers-contrast: high) {
1423
+ .select-input:focus {
1424
+ outline-width: 3px;
1425
+ outline-color: Highlight;
1426
+ }
1427
+
1428
+ .select-input {
1429
+ border-width: 2px;
1430
+ }
1431
+ }
1432
+
1433
+ /* Touch targets (WCAG 2.5.5) */
1434
+ .load-more-button,
1435
+ select-option {
1436
+ min-height: 44px;
1437
+ }
1438
+ `;
1439
+ this._shadow.appendChild(style);
1440
+ }
1441
+ _attachEventListeners() {
1442
+ // Arrow click handler
1443
+ if (this._arrowContainer) {
1444
+ this._boundArrowClick = (e) => {
1445
+ e.stopPropagation();
1446
+ e.preventDefault();
1447
+ const wasOpen = this._state.isOpen;
1448
+ this._state.isOpen = !this._state.isOpen;
1449
+ this._updateDropdownVisibility();
1450
+ this._updateArrowRotation();
1451
+ if (this._state.isOpen && this._config.callbacks.onOpen) {
1452
+ this._config.callbacks.onOpen();
1453
+ }
1454
+ else if (!this._state.isOpen && this._config.callbacks.onClose) {
1455
+ this._config.callbacks.onClose();
1456
+ }
1457
+ // Scroll to selected when opening
1458
+ if (!wasOpen && this._state.isOpen && this._state.selectedIndices.size > 0) {
1459
+ setTimeout(() => this._scrollToSelected(), 50);
1460
+ }
1461
+ };
1462
+ this._arrowContainer.addEventListener('click', this._boundArrowClick);
1463
+ }
1464
+ // Input container click - prevent event from reaching document listener
1465
+ this._container.addEventListener('click', (e) => {
1466
+ e.stopPropagation();
1467
+ });
1468
+ // Input focus/blur
1469
+ this._input.addEventListener('focus', () => this._handleOpen());
1470
+ this._input.addEventListener('blur', (e) => {
1471
+ // Delay to allow option click
1472
+ setTimeout(() => {
1473
+ if (!this._dropdown.contains(document.activeElement)) {
1474
+ this._handleClose();
1475
+ }
1476
+ }, 200);
1477
+ });
1478
+ // Input search
1479
+ this._input.addEventListener('input', (e) => {
1480
+ if (!this._config.searchable)
1481
+ return;
1482
+ console.log('[EnhancedSelect] Input event fired', e.target.value);
1483
+ const query = e.target.value;
1484
+ this._handleSearch(query);
1485
+ });
1486
+ // Keyboard navigation
1487
+ this._input.addEventListener('keydown', (e) => this._handleKeydown(e));
1488
+ // Click outside to close
1489
+ document.addEventListener('click', (e) => {
1490
+ const target = e.target;
1491
+ // Check if click is outside shadow root
1492
+ if (!this._shadow.contains(target) && !this._container.contains(target)) {
1493
+ this._handleClose();
1494
+ }
1495
+ });
1496
+ }
1497
+ _initializeObservers() {
1498
+ // Disconnect existing observer if any
1499
+ if (this._intersectionObserver) {
1500
+ this._intersectionObserver.disconnect();
1501
+ this._intersectionObserver = undefined;
1502
+ }
1503
+ // Intersection observer for infinite scroll
1504
+ if (this._config.infiniteScroll.enabled) {
1505
+ this._intersectionObserver = new IntersectionObserver((entries) => {
1506
+ entries.forEach((entry) => {
1507
+ if (entry.isIntersecting) {
1508
+ console.log('[InfiniteScroll] Sentinel intersected. isBusy:', this._state.isBusy);
1509
+ if (!this._state.isBusy) {
1510
+ this._loadMoreItems();
1511
+ }
1512
+ }
1513
+ });
1514
+ }, { threshold: 0.1 });
1515
+ }
1516
+ }
1517
+ async _loadInitialSelectedItems() {
1518
+ if (!this._config.serverSide.fetchSelectedItems || !this._config.serverSide.initialSelectedValues) {
1519
+ return;
1520
+ }
1521
+ this._setBusy(true);
1522
+ try {
1523
+ const items = await this._config.serverSide.fetchSelectedItems(this._config.serverSide.initialSelectedValues);
1524
+ // Add to state
1525
+ items.forEach((item, index) => {
1526
+ this._state.selectedItems.set(index, item);
1527
+ this._state.selectedIndices.add(index);
1528
+ });
1529
+ this._updateInputDisplay();
1530
+ }
1531
+ catch (error) {
1532
+ this._handleError(error);
1533
+ }
1534
+ finally {
1535
+ this._setBusy(false);
1536
+ }
1537
+ }
1538
+ _handleOpen() {
1539
+ if (!this._config.enabled || this._state.isOpen)
1540
+ return;
1541
+ this._state.isOpen = true;
1542
+ this._dropdown.style.display = 'block';
1543
+ this._input.setAttribute('aria-expanded', 'true');
1544
+ this._updateArrowRotation();
1545
+ // Clear search query when opening to show all options
1546
+ // This ensures we can scroll to selected item
1547
+ if (this._config.searchable) {
1548
+ this._state.searchQuery = '';
1549
+ // Don't clear input value if it represents selection
1550
+ // But if we want to search, we might want to clear it?
1551
+ // Standard behavior: input keeps value (label), but dropdown shows all options
1552
+ // until user types.
1553
+ // However, our filtering logic uses _state.searchQuery.
1554
+ // So clearing it here resets the filter.
1555
+ }
1556
+ // Render options when opening
1557
+ this._renderOptions();
1558
+ this._emit('open', {});
1559
+ this._config.callbacks.onOpen?.();
1560
+ // Scroll to selected if configured
1561
+ if (this._config.scrollToSelected.enabled) {
1562
+ // Use setTimeout to allow render to complete
1563
+ setTimeout(() => this._scrollToSelected(), 0);
1564
+ }
1565
+ }
1566
+ _handleClose() {
1567
+ if (!this._state.isOpen)
1568
+ return;
1569
+ this._state.isOpen = false;
1570
+ this._dropdown.style.display = 'none';
1571
+ this._input.setAttribute('aria-expanded', 'false');
1572
+ this._updateArrowRotation();
1573
+ this._emit('close', {});
1574
+ this._config.callbacks.onClose?.();
1575
+ }
1576
+ _updateDropdownVisibility() {
1577
+ if (this._state.isOpen) {
1578
+ this._dropdown.style.display = 'block';
1579
+ this._input.setAttribute('aria-expanded', 'true');
1580
+ }
1581
+ else {
1582
+ this._dropdown.style.display = 'none';
1583
+ this._input.setAttribute('aria-expanded', 'false');
1584
+ }
1585
+ }
1586
+ _updateArrowRotation() {
1587
+ if (this._arrowContainer) {
1588
+ const arrow = this._arrowContainer.querySelector('.dropdown-arrow');
1589
+ if (arrow) {
1590
+ if (this._state.isOpen) {
1591
+ arrow.classList.add('open');
1592
+ }
1593
+ else {
1594
+ arrow.classList.remove('open');
1595
+ }
1596
+ }
1597
+ }
1598
+ }
1599
+ _handleSearch(query) {
1600
+ console.log('[EnhancedSelect] _handleSearch called with:', JSON.stringify(query));
1601
+ this._state.searchQuery = query;
1602
+ // Clear previous search timeout
1603
+ if (this._searchTimeout) {
1604
+ clearTimeout(this._searchTimeout);
1605
+ }
1606
+ // Search immediately - no debouncing for better responsiveness
1607
+ // Users expect instant feedback as they type
1608
+ this._state.isSearching = false;
1609
+ // Ensure dropdown is open when searching
1610
+ if (!this._state.isOpen) {
1611
+ console.log('[EnhancedSelect] Opening dropdown for search');
1612
+ this._handleOpen();
1613
+ }
1614
+ else {
1615
+ // Filter and render options immediately
1616
+ console.log('[EnhancedSelect] Dropdown already open, re-rendering options');
1617
+ this._renderOptions();
1618
+ }
1619
+ // Get filtered items based on search query - searches ENTIRE phrase
1620
+ const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
1621
+ // FIX: Do not trim query to allow searching for phrases with spaces
1622
+ const searchQuery = query.toLowerCase();
1623
+ const filteredItems = searchQuery
1624
+ ? this._state.loadedItems.filter((item) => {
1625
+ try {
1626
+ const label = String(getLabel(item)).toLowerCase();
1627
+ // Match the entire search phrase
1628
+ return label.includes(searchQuery);
1629
+ }
1630
+ catch (e) {
1631
+ return false;
1632
+ }
1633
+ })
1634
+ : this._state.loadedItems;
1635
+ const count = filteredItems.length;
1636
+ console.log(`[EnhancedSelect] Search results: ${count} items found for query "${searchQuery}"`);
1637
+ // Announce search results for accessibility
1638
+ if (searchQuery) {
1639
+ this._announce(`${count} result${count !== 1 ? 's' : ''} found for "${query}"`);
1640
+ }
1641
+ // Only notify if query or result count changed to prevent infinite loops
1642
+ if (query !== this._state.lastNotifiedQuery || count !== this._state.lastNotifiedResultCount) {
1643
+ this._state.lastNotifiedQuery = query;
1644
+ this._state.lastNotifiedResultCount = count;
1645
+ // Use setTimeout to avoid synchronous state updates during render
1646
+ setTimeout(() => {
1647
+ this._emit('search', { query, results: filteredItems, count });
1648
+ this._config.callbacks.onSearch?.(query);
1649
+ }, 0);
1650
+ }
1651
+ }
1652
+ _handleKeydown(e) {
1653
+ switch (e.key) {
1654
+ case 'ArrowDown':
1655
+ e.preventDefault();
1656
+ if (!this._state.isOpen) {
1657
+ this._handleOpen();
1658
+ }
1659
+ else {
1660
+ this._moveActive(1);
1661
+ }
1662
+ break;
1663
+ case 'ArrowUp':
1664
+ e.preventDefault();
1665
+ if (!this._state.isOpen) {
1666
+ this._handleOpen();
1667
+ }
1668
+ else {
1669
+ this._moveActive(-1);
1670
+ }
1671
+ break;
1672
+ case 'Home':
1673
+ e.preventDefault();
1674
+ if (this._state.isOpen) {
1675
+ this._setActive(0);
1676
+ }
1677
+ break;
1678
+ case 'End':
1679
+ e.preventDefault();
1680
+ if (this._state.isOpen) {
1681
+ const options = Array.from(this._optionsContainer.children);
1682
+ this._setActive(options.length - 1);
1683
+ }
1684
+ break;
1685
+ case 'PageDown':
1686
+ e.preventDefault();
1687
+ if (this._state.isOpen) {
1688
+ this._moveActive(10);
1689
+ }
1690
+ break;
1691
+ case 'PageUp':
1692
+ e.preventDefault();
1693
+ if (this._state.isOpen) {
1694
+ this._moveActive(-10);
1695
+ }
1696
+ break;
1697
+ case 'Enter':
1698
+ e.preventDefault();
1699
+ if (this._state.activeIndex >= 0) {
1700
+ this._selectOption(this._state.activeIndex);
1701
+ }
1702
+ break;
1703
+ case 'Escape':
1704
+ e.preventDefault();
1705
+ this._handleClose();
1706
+ break;
1707
+ case 'a':
1708
+ case 'A':
1709
+ if ((e.ctrlKey || e.metaKey) && this._config.selection.mode === 'multi') {
1710
+ e.preventDefault();
1711
+ this._selectAll();
1712
+ }
1713
+ break;
1714
+ default:
1715
+ // Type-ahead search
1716
+ if (e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey) {
1717
+ this._handleTypeAhead(e.key);
1718
+ }
1719
+ break;
1720
+ }
1721
+ }
1722
+ _moveActive(delta) {
1723
+ const options = Array.from(this._optionsContainer.children);
1724
+ const next = Math.max(0, Math.min(options.length - 1, this._state.activeIndex + delta));
1725
+ this._setActive(next);
1726
+ }
1727
+ _setActive(index) {
1728
+ const options = Array.from(this._optionsContainer.children);
1729
+ // Clear previous active state
1730
+ if (this._state.activeIndex >= 0 && options[this._state.activeIndex]) {
1731
+ options[this._state.activeIndex].setActive(false);
1732
+ }
1733
+ this._state.activeIndex = index;
1734
+ // Set new active state
1735
+ if (options[index]) {
1736
+ options[index].setActive(true);
1737
+ options[index].scrollIntoView({ block: 'nearest', behavior: 'smooth' });
1738
+ // Announce position for screen readers
1739
+ const total = options.length;
1740
+ this._announce(`Item ${index + 1} of ${total}`);
1741
+ // Update aria-activedescendant
1742
+ const optionId = `${this._uniqueId}-option-${index}`;
1743
+ this._input.setAttribute('aria-activedescendant', optionId);
1744
+ }
1745
+ }
1746
+ _handleTypeAhead(char) {
1747
+ if (this._typeTimeout)
1748
+ clearTimeout(this._typeTimeout);
1749
+ this._typeBuffer += char.toLowerCase();
1750
+ this._typeTimeout = window.setTimeout(() => {
1751
+ this._typeBuffer = '';
1752
+ }, 500);
1753
+ // Find first matching option
1754
+ const getValue = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
1755
+ const matchIndex = this._state.loadedItems.findIndex((item) => getValue(item).toLowerCase().startsWith(this._typeBuffer));
1756
+ if (matchIndex >= 0) {
1757
+ this._setActive(matchIndex);
1758
+ }
1759
+ }
1760
+ _selectAll() {
1761
+ if (this._config.selection.mode !== 'multi')
1762
+ return;
1763
+ const options = Array.from(this._optionsContainer.children);
1764
+ const maxSelections = this._config.selection.maxSelections || 0;
1765
+ options.forEach((option, index) => {
1766
+ if (maxSelections > 0 && this._state.selectedIndices.size >= maxSelections) {
1767
+ return;
1768
+ }
1769
+ if (!this._state.selectedIndices.has(index)) {
1770
+ const config = option.getConfig();
1771
+ this._state.selectedIndices.add(index);
1772
+ this._state.selectedItems.set(index, config.item);
1773
+ option.setSelected(true);
1774
+ }
1775
+ });
1776
+ this._updateInputDisplay();
1777
+ this._emitChange();
1778
+ this._announce(`Selected all ${options.length} items`);
1779
+ }
1780
+ _announce(message) {
1781
+ if (this._liveRegion) {
1782
+ this._liveRegion.textContent = message;
1783
+ setTimeout(() => {
1784
+ if (this._liveRegion)
1785
+ this._liveRegion.textContent = '';
1786
+ }, 1000);
1787
+ }
1788
+ }
1789
+ _selectOption(index) {
1790
+ // FIX: Do not rely on this._optionsContainer.children[index] because filtering changes the children
1791
+ // Instead, use the index to update state directly
1792
+ const item = this._state.loadedItems[index];
1793
+ if (!item)
1794
+ return;
1795
+ const isCurrentlySelected = this._state.selectedIndices.has(index);
1796
+ if (this._config.selection.mode === 'single') {
1797
+ // Single select: clear previous and select new
1798
+ const wasSelected = this._state.selectedIndices.has(index);
1799
+ this._state.selectedIndices.clear();
1800
+ this._state.selectedItems.clear();
1801
+ if (!wasSelected) {
1802
+ // Select this option
1803
+ this._state.selectedIndices.add(index);
1804
+ this._state.selectedItems.set(index, item);
1805
+ }
1806
+ // Re-render to update all option styles
1807
+ this._renderOptions();
1808
+ if (this._config.selection.closeOnSelect) {
1809
+ this._handleClose();
1810
+ }
1811
+ }
1812
+ else {
1813
+ // Multi select with toggle
1814
+ const maxSelections = this._config.selection.maxSelections || 0;
1815
+ if (isCurrentlySelected) {
1816
+ // Deselect (toggle off)
1817
+ this._state.selectedIndices.delete(index);
1818
+ this._state.selectedItems.delete(index);
1819
+ }
1820
+ else {
1821
+ // Select (toggle on)
1822
+ if (maxSelections > 0 && this._state.selectedIndices.size >= maxSelections) {
1823
+ this._announce(`Maximum ${maxSelections} selections allowed`);
1824
+ return; // Max selections reached
1825
+ }
1826
+ this._state.selectedIndices.add(index);
1827
+ this._state.selectedItems.set(index, item);
1828
+ }
1829
+ // Re-render to update styles (safer than trying to find the element in filtered list)
1830
+ this._renderOptions();
1831
+ }
1832
+ this._updateInputDisplay();
1833
+ this._emitChange();
1834
+ // Call user callback
1835
+ const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
1836
+ const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
1837
+ this._config.callbacks.onSelect?.({
1838
+ item: item,
1839
+ index,
1840
+ value: getValue(item),
1841
+ label: getLabel(item),
1842
+ selected: this._state.selectedIndices.has(index),
1843
+ });
1844
+ }
1845
+ _handleOptionRemove(index) {
1846
+ const option = this._optionsContainer.children[index];
1847
+ if (!option)
1848
+ return;
1849
+ this._state.selectedIndices.delete(index);
1850
+ this._state.selectedItems.delete(index);
1851
+ option.setSelected(false);
1852
+ this._updateInputDisplay();
1853
+ this._emitChange();
1854
+ const config = option.getConfig();
1855
+ this._emit('remove', { item: config.item, index });
1856
+ }
1857
+ _updateInputDisplay() {
1858
+ const selectedItems = Array.from(this._state.selectedItems.values());
1859
+ const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
1860
+ if (selectedItems.length === 0) {
1861
+ this._input.value = '';
1862
+ this._input.placeholder = this._config.placeholder || 'Select an option...';
1863
+ // Clear any badges
1864
+ const existingBadges = this._inputContainer.querySelectorAll('.selection-badge');
1865
+ existingBadges.forEach(badge => badge.remove());
1866
+ }
1867
+ else if (this._config.selection.mode === 'single') {
1868
+ this._input.value = getLabel(selectedItems[0]);
1869
+ }
1870
+ else {
1871
+ // Multi-select: show badges instead of text in input
1872
+ this._input.value = '';
1873
+ this._input.placeholder = '';
1874
+ // Clear existing badges
1875
+ const existingBadges = this._inputContainer.querySelectorAll('.selection-badge');
1876
+ existingBadges.forEach(badge => badge.remove());
1877
+ // Create badges for each selected item
1878
+ const selectedEntries = Array.from(this._state.selectedItems.entries());
1879
+ selectedEntries.forEach(([index, item]) => {
1880
+ const badge = document.createElement('span');
1881
+ badge.className = 'selection-badge';
1882
+ badge.textContent = getLabel(item);
1883
+ // Add remove button to badge
1884
+ const removeBtn = document.createElement('button');
1885
+ removeBtn.className = 'badge-remove';
1886
+ removeBtn.innerHTML = '×';
1887
+ removeBtn.setAttribute('aria-label', `Remove ${getLabel(item)}`);
1888
+ removeBtn.addEventListener('click', (e) => {
1889
+ e.stopPropagation();
1890
+ this._state.selectedIndices.delete(index);
1891
+ this._state.selectedItems.delete(index);
1892
+ this._updateInputDisplay();
1893
+ this._renderOptions();
1894
+ this._emitChange();
1895
+ });
1896
+ badge.appendChild(removeBtn);
1897
+ this._inputContainer.insertBefore(badge, this._input);
1898
+ });
1899
+ }
1900
+ }
1901
+ _renderOptionsWithAnimation() {
1902
+ // Add fade-out animation
1903
+ this._optionsContainer.style.opacity = '0';
1904
+ this._optionsContainer.style.transition = 'opacity 0.15s ease-out';
1905
+ setTimeout(() => {
1906
+ this._renderOptions();
1907
+ // Fade back in
1908
+ this._optionsContainer.style.opacity = '1';
1909
+ this._optionsContainer.style.transition = 'opacity 0.2s ease-in';
1910
+ }, 150);
1911
+ }
1912
+ _scrollToSelected() {
1913
+ if (this._state.selectedIndices.size === 0)
1914
+ return;
1915
+ const target = this._config.scrollToSelected.multiSelectTarget;
1916
+ const indices = Array.from(this._state.selectedIndices).sort((a, b) => a - b);
1917
+ const targetIndex = target === 'first' ? indices[0] : indices[indices.length - 1];
1918
+ // FIX: Find the option element by ID instead of index in children
1919
+ // because children list might be filtered or reordered
1920
+ const optionId = `${this._uniqueId}-option-${targetIndex}`;
1921
+ // We need to search in shadow root or options container
1922
+ // Since options are custom elements, we can find them by ID if we set it (we do)
1923
+ // But wait, we set ID on the element instance, but is it in the DOM?
1924
+ // If filtered out, it won't be in the DOM.
1925
+ // If we are searching, we might not want to scroll to selected if it's not visible
1926
+ // But if we just opened the dropdown, we usually want to see the selected item.
1927
+ // If the selected item is filtered out, we can't scroll to it.
1928
+ // Try to find the element in the options container
1929
+ // Note: querySelector on shadowRoot works if we set the ID attribute
1930
+ // In _renderOptions we set: option.id = ...
1931
+ const option = this._optionsContainer.querySelector(`[id="${optionId}"]`);
1932
+ if (option) {
1933
+ option.scrollIntoView({
1934
+ block: this._config.scrollToSelected.block || 'center',
1935
+ behavior: 'smooth',
1936
+ });
1937
+ }
1938
+ }
1939
+ async _loadMoreItems() {
1940
+ if (this._state.isBusy)
1941
+ return;
1942
+ console.log('[InfiniteScroll] _loadMoreItems triggered');
1943
+ this._setBusy(true);
1944
+ // Save scroll position before loading
1945
+ if (this._dropdown) {
1946
+ this._state.lastScrollPosition = this._dropdown.scrollTop;
1947
+ this._state.preserveScrollPosition = true;
1948
+ // Update dropdown to show loading indicator but keep the
1949
+ // same scrollTop so the visible items don't move.
1950
+ this._renderOptions();
1951
+ this._dropdown.scrollTop = this._state.lastScrollPosition;
1952
+ }
1953
+ try {
1954
+ // Emit event for parent to handle
1955
+ this._state.currentPage++;
1956
+ console.log(`[InfiniteScroll] Emitting loadMore event for page ${this._state.currentPage}`);
1957
+ this._emit('loadMore', { page: this._state.currentPage, items: [] });
1958
+ this._config.callbacks.onLoadMore?.(this._state.currentPage);
1959
+ // NOTE: We do NOT set isBusy = false here.
1960
+ // The parent component MUST call setItems() or similar to clear the busy state.
1961
+ // This prevents the sentinel from reappearing before new items are loaded.
1962
+ }
1963
+ catch (error) {
1964
+ this._handleError(error);
1965
+ this._setBusy(false); // Only clear on error
1966
+ }
1967
+ }
1968
+ _setBusy(busy) {
1969
+ this._state.isBusy = busy;
1970
+ // Trigger re-render to show/hide busy indicator
1971
+ // We use _renderOptions to handle the UI update
1972
+ this._renderOptions();
1973
+ }
1974
+ _showBusyBucket() {
1975
+ // Deprecated: Logic moved to _renderOptions
1976
+ }
1977
+ _hideBusyBucket() {
1978
+ // Deprecated: Logic moved to _renderOptions
1979
+ }
1980
+ _handleError(error) {
1981
+ this._emit('error', { message: error.message, cause: error });
1982
+ this._config.callbacks.onError?.(error);
1983
+ }
1984
+ _emit(name, detail) {
1985
+ this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true }));
1986
+ }
1987
+ _emitChange() {
1988
+ const selectedItems = Array.from(this._state.selectedItems.values());
1989
+ const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
1990
+ const selectedValues = selectedItems.map(getValue);
1991
+ const selectedIndices = Array.from(this._state.selectedIndices);
1992
+ this._emit('change', { selectedItems, selectedValues, selectedIndices });
1993
+ this._config.callbacks.onChange?.(selectedItems, selectedValues);
1994
+ }
1995
+ // Public API
1996
+ /**
1997
+ * Set items to display in the select
1998
+ */
1999
+ setItems(items) {
2000
+ const previousLength = this._state.loadedItems.length;
2001
+ this._state.loadedItems = items;
2002
+ // If grouped items exist, flatten them to items
2003
+ if (this._state.groupedItems.length > 0) {
2004
+ this._state.loadedItems = this._state.groupedItems.flatMap(group => group.options);
2005
+ }
2006
+ const newLength = this._state.loadedItems.length;
2007
+ // When infinite scroll is active (preserveScrollPosition = true),
2008
+ // we need to maintain scroll position during the update
2009
+ if (this._state.preserveScrollPosition && this._dropdown) {
2010
+ const targetScrollTop = this._state.lastScrollPosition;
2011
+ console.log('[InfiniteScroll] setItems: before render', {
2012
+ previousLength,
2013
+ newLength,
2014
+ lastScrollPosition: this._state.lastScrollPosition,
2015
+ scrollTop: this._dropdown.scrollTop,
2016
+ scrollHeight: this._dropdown.scrollHeight,
2017
+ clientHeight: this._dropdown.clientHeight
2018
+ });
2019
+ // Only clear loading if we actually got more items
2020
+ if (newLength > previousLength) {
2021
+ this._state.isBusy = false;
2022
+ }
2023
+ this._renderOptions();
2024
+ // Restore the exact scrollTop we had before loading
2025
+ // so the previously visible items stay in place and
2026
+ // new ones simply appear below.
2027
+ this._dropdown.scrollTop = targetScrollTop;
2028
+ // Ensure it sticks after layout
2029
+ requestAnimationFrame(() => {
2030
+ if (this._dropdown) {
2031
+ this._dropdown.scrollTop = targetScrollTop;
2032
+ console.log('[InfiniteScroll] setItems: after render', {
2033
+ newLength,
2034
+ lastScrollPosition: this._state.lastScrollPosition,
2035
+ scrollTop: this._dropdown.scrollTop,
2036
+ scrollHeight: this._dropdown.scrollHeight,
2037
+ clientHeight: this._dropdown.clientHeight
2038
+ });
2039
+ }
2040
+ });
2041
+ // Only clear preserveScrollPosition if we got new items
2042
+ if (newLength > previousLength) {
2043
+ this._state.preserveScrollPosition = false;
2044
+ }
2045
+ }
2046
+ else {
2047
+ // Normal update - just render normally
2048
+ this._state.isBusy = false;
2049
+ this._renderOptions();
2050
+ }
2051
+ }
2052
+ /**
2053
+ * Set grouped items
2054
+ */
2055
+ setGroupedItems(groupedItems) {
2056
+ this._state.groupedItems = groupedItems;
2057
+ this._state.loadedItems = groupedItems.flatMap(group => group.options);
2058
+ this._renderOptions();
2059
+ }
2060
+ /**
2061
+ * Get currently selected items
2062
+ */
2063
+ getSelectedItems() {
2064
+ return Array.from(this._state.selectedItems.values());
2065
+ }
2066
+ /**
2067
+ * Get all loaded items
2068
+ */
2069
+ get loadedItems() {
2070
+ return this._state.loadedItems;
2071
+ }
2072
+ /**
2073
+ * Get currently selected values
2074
+ */
2075
+ getSelectedValues() {
2076
+ const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
2077
+ return this.getSelectedItems().map(getValue);
2078
+ }
2079
+ /**
2080
+ * Set selected items by value
2081
+ */
2082
+ async setSelectedValues(values) {
2083
+ if (this._config.serverSide.enabled && this._config.serverSide.fetchSelectedItems) {
2084
+ await this._loadSelectedItemsByValues(values);
2085
+ }
2086
+ else {
2087
+ // Select from loaded items
2088
+ const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
2089
+ this._state.selectedIndices.clear();
2090
+ this._state.selectedItems.clear();
2091
+ this._state.loadedItems.forEach((item, index) => {
2092
+ if (values.includes(getValue(item))) {
2093
+ this._state.selectedIndices.add(index);
2094
+ this._state.selectedItems.set(index, item);
2095
+ }
2096
+ });
2097
+ this._renderOptions();
2098
+ this._updateInputDisplay();
2099
+ this._emitChange();
2100
+ }
2101
+ }
2102
+ /**
2103
+ * Load and select items by their values (for infinite scroll scenario)
2104
+ */
2105
+ async _loadSelectedItemsByValues(values) {
2106
+ if (!this._config.serverSide.fetchSelectedItems)
2107
+ return;
2108
+ this._setBusy(true);
2109
+ try {
2110
+ const items = await this._config.serverSide.fetchSelectedItems(values);
2111
+ this._state.selectedIndices.clear();
2112
+ this._state.selectedItems.clear();
2113
+ items.forEach((item, index) => {
2114
+ this._state.selectedIndices.add(index);
2115
+ this._state.selectedItems.set(index, item);
2116
+ });
2117
+ this._renderOptions();
2118
+ this._updateInputDisplay();
2119
+ this._emitChange();
2120
+ // Scroll to selected if configured
2121
+ if (this._config.scrollToSelected.enabled) {
2122
+ this._scrollToSelected();
2123
+ }
2124
+ }
2125
+ catch (error) {
2126
+ this._handleError(error);
2127
+ }
2128
+ finally {
2129
+ this._setBusy(false);
2130
+ }
2131
+ }
2132
+ /**
2133
+ * Clear all selections
2134
+ */
2135
+ clear() {
2136
+ this._state.selectedIndices.clear();
2137
+ this._state.selectedItems.clear();
2138
+ this._renderOptions();
2139
+ this._updateInputDisplay();
2140
+ this._emitChange();
2141
+ }
2142
+ /**
2143
+ * Open dropdown
2144
+ */
2145
+ open() {
2146
+ this._handleOpen();
2147
+ }
2148
+ /**
2149
+ * Close dropdown
2150
+ */
2151
+ close() {
2152
+ this._handleClose();
2153
+ }
2154
+ /**
2155
+ * Update component configuration
2156
+ */
2157
+ updateConfig(config) {
2158
+ this._config = selectConfig.mergeWithComponentConfig(config);
2159
+ // Update input state based on new config
2160
+ if (this._input) {
2161
+ this._input.readOnly = !this._config.searchable;
2162
+ this._input.setAttribute('aria-autocomplete', this._config.searchable ? 'list' : 'none');
2163
+ }
2164
+ // Re-initialize observers in case infinite scroll was enabled/disabled
2165
+ this._initializeObservers();
2166
+ this._renderOptions();
2167
+ }
2168
+ /**
2169
+ * Set error state
2170
+ */
2171
+ setError(message) {
2172
+ this._hasError = true;
2173
+ this._errorMessage = message;
2174
+ this._input.setAttribute('aria-invalid', 'true');
2175
+ this._announce(`Error: ${message}`);
2176
+ }
2177
+ /**
2178
+ * Clear error state
2179
+ */
2180
+ clearError() {
2181
+ this._hasError = false;
2182
+ this._errorMessage = '';
2183
+ this._input.removeAttribute('aria-invalid');
2184
+ }
2185
+ /**
2186
+ * Set required state
2187
+ */
2188
+ setRequired(required) {
2189
+ if (required) {
2190
+ this._input.setAttribute('aria-required', 'true');
2191
+ this._input.setAttribute('required', '');
2192
+ }
2193
+ else {
2194
+ this._input.removeAttribute('aria-required');
2195
+ this._input.removeAttribute('required');
2196
+ }
2197
+ }
2198
+ /**
2199
+ * Validate selection (for required fields)
2200
+ */
2201
+ validate() {
2202
+ const isRequired = this._input.hasAttribute('required');
2203
+ if (isRequired && this._state.selectedIndices.size === 0) {
2204
+ this.setError('Selection is required');
2205
+ return false;
2206
+ }
2207
+ this.clearError();
2208
+ return true;
2209
+ }
2210
+ /**
2211
+ * Render options based on current state
2212
+ */
2213
+ _renderOptions() {
2214
+ console.log('[EnhancedSelect] _renderOptions called');
2215
+ // Cleanup observer
2216
+ if (this._loadMoreTrigger && this._intersectionObserver) {
2217
+ this._intersectionObserver.unobserve(this._loadMoreTrigger);
2218
+ }
2219
+ // Clear options container
2220
+ this._optionsContainer.innerHTML = '';
2221
+ // Ensure dropdown only contains options container (cleanup legacy direct children)
2222
+ // We need to preserve optionsContainer, so we can't just clear dropdown.innerHTML
2223
+ // But we can check if there are other children and remove them
2224
+ Array.from(this._dropdown.children).forEach(child => {
2225
+ if (child !== this._optionsContainer) {
2226
+ this._dropdown.removeChild(child);
2227
+ }
2228
+ });
2229
+ // Ensure dropdown is visible if we are rendering options
2230
+ if (this._state.isOpen && this._dropdown.style.display === 'none') {
2231
+ this._dropdown.style.display = 'block';
2232
+ }
2233
+ // Show searching state (exclusive state)
2234
+ if (this._state.isSearching) {
2235
+ const searching = document.createElement('div');
2236
+ searching.className = 'searching-state';
2237
+ searching.textContent = 'Searching...';
2238
+ this._optionsContainer.appendChild(searching);
2239
+ return;
2240
+ }
2241
+ const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
2242
+ const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
2243
+ // Filter items by search query
2244
+ const query = this._state.searchQuery.toLowerCase();
2245
+ // Handle Grouped Items Rendering (when no search query)
2246
+ if (this._state.groupedItems.length > 0 && !query) {
2247
+ this._state.groupedItems.forEach(group => {
2248
+ const header = document.createElement('div');
2249
+ header.className = 'group-header';
2250
+ header.textContent = group.label;
2251
+ Object.assign(header.style, {
2252
+ padding: '8px 12px',
2253
+ fontWeight: '600',
2254
+ color: '#6b7280',
2255
+ backgroundColor: '#f3f4f6',
2256
+ fontSize: '12px',
2257
+ textTransform: 'uppercase',
2258
+ letterSpacing: '0.05em',
2259
+ position: 'sticky',
2260
+ top: '0',
2261
+ zIndex: '1',
2262
+ borderBottom: '1px solid #e5e7eb'
2263
+ });
2264
+ this._optionsContainer.appendChild(header);
2265
+ group.options.forEach(item => {
2266
+ this._renderSingleOption(item, getValue, getLabel);
2267
+ });
2268
+ });
2269
+ }
2270
+ else {
2271
+ // Normal rendering (flat list or filtered)
2272
+ const itemsToRender = query
2273
+ ? this._state.loadedItems.filter((item) => {
2274
+ try {
2275
+ const label = String(getLabel(item)).toLowerCase();
2276
+ return label.includes(query);
2277
+ }
2278
+ catch (e) {
2279
+ return false;
2280
+ }
2281
+ })
2282
+ : this._state.loadedItems;
2283
+ if (itemsToRender.length === 0 && !this._state.isBusy) {
2284
+ const empty = document.createElement('div');
2285
+ empty.className = 'empty-state';
2286
+ if (query) {
2287
+ empty.textContent = `No results found for "${this._state.searchQuery}"`;
2288
+ }
2289
+ else {
2290
+ empty.textContent = 'No options available';
2291
+ }
2292
+ this._optionsContainer.appendChild(empty);
2293
+ }
2294
+ else {
2295
+ itemsToRender.forEach((item) => {
2296
+ this._renderSingleOption(item, getValue, getLabel);
2297
+ });
2298
+ }
2299
+ }
2300
+ // Append Busy Indicator if busy
2301
+ if (this._state.isBusy && this._config.busyBucket.enabled) {
2302
+ const busyBucket = document.createElement('div');
2303
+ busyBucket.className = 'busy-bucket';
2304
+ if (this._config.busyBucket.showSpinner) {
2305
+ const spinner = document.createElement('div');
2306
+ spinner.className = 'spinner';
2307
+ busyBucket.appendChild(spinner);
2308
+ }
2309
+ if (this._config.busyBucket.message) {
2310
+ const message = document.createElement('div');
2311
+ message.textContent = this._config.busyBucket.message;
2312
+ busyBucket.appendChild(message);
2313
+ }
2314
+ this._optionsContainer.appendChild(busyBucket);
2315
+ }
2316
+ // Append Load More Trigger (Button or Sentinel) if enabled and not busy
2317
+ else if ((this._config.loadMore.enabled || this._config.infiniteScroll.enabled) && this._state.loadedItems.length > 0) {
2318
+ this._addLoadMoreTrigger();
2319
+ }
2320
+ }
2321
+ _renderSingleOption(item, getValue, getLabel) {
2322
+ const option = document.createElement('div');
2323
+ option.className = 'option';
2324
+ const value = getValue(item);
2325
+ const label = getLabel(item);
2326
+ option.textContent = label;
2327
+ option.dataset.value = String(value);
2328
+ // Check if selected using selectedItems map
2329
+ const isSelected = Array.from(this._state.selectedItems.values()).some(selectedItem => {
2330
+ const selectedValue = getValue(selectedItem);
2331
+ return selectedValue === value;
2332
+ });
2333
+ if (isSelected) {
2334
+ option.classList.add('selected');
2335
+ }
2336
+ option.addEventListener('click', () => {
2337
+ const index = this._state.loadedItems.indexOf(item);
2338
+ if (index !== -1) {
2339
+ this._selectOption(index);
2340
+ }
2341
+ });
2342
+ this._optionsContainer.appendChild(option);
2343
+ }
2344
+ _addLoadMoreTrigger() {
2345
+ const container = document.createElement('div');
2346
+ container.className = 'load-more-container';
2347
+ if (this._config.infiniteScroll.enabled) {
2348
+ // Infinite Scroll: Render an invisible sentinel
2349
+ // It must have some height to be intersected
2350
+ const sentinel = document.createElement('div');
2351
+ sentinel.className = 'infinite-scroll-sentinel';
2352
+ sentinel.style.height = '10px';
2353
+ sentinel.style.width = '100%';
2354
+ sentinel.style.opacity = '0'; // Invisible
2355
+ this._loadMoreTrigger = sentinel;
2356
+ container.appendChild(sentinel);
2357
+ }
2358
+ else {
2359
+ // Manual Load More: Render a button
2360
+ const button = document.createElement('button');
2361
+ button.className = 'load-more-button';
2362
+ button.textContent = `Load ${this._config.loadMore.itemsPerLoad} more`;
2363
+ button.addEventListener('click', () => this._loadMoreItems());
2364
+ this._loadMoreTrigger = button;
2365
+ container.appendChild(button);
2366
+ }
2367
+ this._optionsContainer.appendChild(container);
2368
+ // Setup intersection observer for auto-load
2369
+ if (this._intersectionObserver && this._loadMoreTrigger) {
2370
+ console.log('[InfiniteScroll] Observing sentinel');
2371
+ this._intersectionObserver.observe(this._loadMoreTrigger);
2372
+ }
2373
+ }
2374
+ }
2375
+ // Register custom element
2376
+ if (!customElements.get('enhanced-select')) {
2377
+ customElements.define('enhanced-select', EnhancedSelect);
2378
+ }
2379
+
2380
+ /**
2381
+ * Independent Option Component
2382
+ * High cohesion, low coupling - handles its own selection state and events
2383
+ */
2384
+ class SelectOption extends HTMLElement {
2385
+ constructor(config) {
2386
+ super();
2387
+ this._config = config;
2388
+ this._shadow = this.attachShadow({ mode: 'open' });
2389
+ this._container = document.createElement('div');
2390
+ this._container.className = 'option-container';
2391
+ this._initializeStyles();
2392
+ this._render();
2393
+ this._attachEventListeners();
2394
+ this._shadow.appendChild(this._container);
2395
+ }
2396
+ _initializeStyles() {
2397
+ const style = document.createElement('style');
2398
+ style.textContent = `
2399
+ :host {
2400
+ display: block;
2401
+ position: relative;
2402
+ }
2403
+
2404
+ .option-container {
2405
+ display: flex;
2406
+ align-items: center;
2407
+ justify-content: space-between;
2408
+ padding: 8px 12px;
2409
+ cursor: pointer;
2410
+ user-select: none;
2411
+ transition: background-color 0.2s ease;
2412
+ }
2413
+
2414
+ .option-container:hover {
2415
+ background-color: var(--select-option-hover-bg, #f0f0f0);
2416
+ }
2417
+
2418
+ .option-container.selected {
2419
+ background-color: var(--select-option-selected-bg, #e3f2fd);
2420
+ color: var(--select-option-selected-color, #1976d2);
2421
+ }
2422
+
2423
+ .option-container.active {
2424
+ outline: 2px solid var(--select-option-active-outline, #1976d2);
2425
+ outline-offset: -2px;
2426
+ }
2427
+
2428
+ .option-container.disabled {
2429
+ opacity: 0.5;
2430
+ cursor: not-allowed;
2431
+ pointer-events: none;
2432
+ }
2433
+
2434
+ .option-content {
2435
+ flex: 1;
2436
+ overflow: hidden;
2437
+ text-overflow: ellipsis;
2438
+ white-space: nowrap;
2439
+ }
2440
+
2441
+ .remove-button {
2442
+ margin-left: 8px;
2443
+ padding: 2px 6px;
2444
+ border: none;
2445
+ background-color: var(--select-remove-btn-bg, transparent);
2446
+ color: var(--select-remove-btn-color, #666);
2447
+ cursor: pointer;
2448
+ border-radius: 3px;
2449
+ font-size: 16px;
2450
+ line-height: 1;
2451
+ transition: all 0.2s ease;
2452
+ }
2453
+
2454
+ .remove-button:hover {
2455
+ background-color: var(--select-remove-btn-hover-bg, #ffebee);
2456
+ color: var(--select-remove-btn-hover-color, #c62828);
2457
+ }
2458
+
2459
+ .remove-button:focus {
2460
+ outline: 2px solid var(--select-remove-btn-focus-outline, #1976d2);
2461
+ outline-offset: 2px;
2462
+ }
2463
+ `;
2464
+ this._shadow.appendChild(style);
2465
+ }
2466
+ _render() {
2467
+ const { item, index, selected, disabled, active, render, showRemoveButton } = this._config;
2468
+ // Clear container
2469
+ this._container.innerHTML = '';
2470
+ // Apply state classes
2471
+ this._container.classList.toggle('selected', selected);
2472
+ this._container.classList.toggle('disabled', disabled || false);
2473
+ this._container.classList.toggle('active', active || false);
2474
+ // Custom class name
2475
+ if (this._config.className) {
2476
+ this._container.className += ' ' + this._config.className;
2477
+ }
2478
+ // Apply custom styles
2479
+ if (this._config.style) {
2480
+ Object.assign(this._container.style, this._config.style);
2481
+ }
2482
+ // Render content
2483
+ const contentDiv = document.createElement('div');
2484
+ contentDiv.className = 'option-content';
2485
+ if (render) {
2486
+ const rendered = render(item, index);
2487
+ if (typeof rendered === 'string') {
2488
+ contentDiv.innerHTML = rendered;
2489
+ }
2490
+ else {
2491
+ contentDiv.appendChild(rendered);
2492
+ }
2493
+ }
2494
+ else {
2495
+ const label = this._getLabel();
2496
+ contentDiv.textContent = label;
2497
+ }
2498
+ this._container.appendChild(contentDiv);
2499
+ // Add remove button if needed
2500
+ if (showRemoveButton && selected) {
2501
+ this._removeButton = document.createElement('button');
2502
+ this._removeButton.className = 'remove-button';
2503
+ this._removeButton.innerHTML = '×';
2504
+ this._removeButton.setAttribute('aria-label', 'Remove option');
2505
+ this._removeButton.setAttribute('type', 'button');
2506
+ this._container.appendChild(this._removeButton);
2507
+ }
2508
+ // Set ARIA attributes
2509
+ this.setAttribute('role', 'option');
2510
+ this.setAttribute('aria-selected', String(selected));
2511
+ if (disabled)
2512
+ this.setAttribute('aria-disabled', 'true');
2513
+ this.id = `select-option-${index}`;
2514
+ }
2515
+ _attachEventListeners() {
2516
+ // Click handler for selection
2517
+ this._container.addEventListener('click', (e) => {
2518
+ // Don't trigger selection if clicking remove button
2519
+ if (e.target === this._removeButton) {
2520
+ return;
2521
+ }
2522
+ if (!this._config.disabled) {
2523
+ this._handleSelect();
2524
+ }
2525
+ });
2526
+ // Remove button handler
2527
+ if (this._removeButton) {
2528
+ this._removeButton.addEventListener('click', (e) => {
2529
+ e.stopPropagation();
2530
+ this._handleRemove();
2531
+ });
2532
+ }
2533
+ // Keyboard handler
2534
+ this.addEventListener('keydown', (e) => {
2535
+ if (this._config.disabled)
2536
+ return;
2537
+ if (e.key === 'Enter' || e.key === ' ') {
2538
+ e.preventDefault();
2539
+ this._handleSelect();
2540
+ }
2541
+ else if (e.key === 'Delete' || e.key === 'Backspace') {
2542
+ if (this._config.selected && this._config.showRemoveButton) {
2543
+ e.preventDefault();
2544
+ this._handleRemove();
2545
+ }
2546
+ }
2547
+ });
2548
+ }
2549
+ _handleSelect() {
2550
+ const detail = {
2551
+ item: this._config.item,
2552
+ index: this._config.index,
2553
+ value: this._getValue(),
2554
+ label: this._getLabel(),
2555
+ selected: !this._config.selected,
2556
+ };
2557
+ this.dispatchEvent(new CustomEvent('optionSelect', {
2558
+ detail,
2559
+ bubbles: true,
2560
+ composed: true,
2561
+ }));
2562
+ }
2563
+ _handleRemove() {
2564
+ const detail = {
2565
+ item: this._config.item,
2566
+ index: this._config.index,
2567
+ value: this._getValue(),
2568
+ label: this._getLabel(),
2569
+ selected: false,
2570
+ };
2571
+ this.dispatchEvent(new CustomEvent('optionRemove', {
2572
+ detail,
2573
+ bubbles: true,
2574
+ composed: true,
2575
+ }));
2576
+ }
2577
+ _getValue() {
2578
+ if (this._config.getValue) {
2579
+ return this._config.getValue(this._config.item);
2580
+ }
2581
+ return this._config.item?.value ?? this._config.item;
2582
+ }
2583
+ _getLabel() {
2584
+ if (this._config.getLabel) {
2585
+ return this._config.getLabel(this._config.item);
2586
+ }
2587
+ return this._config.item?.label ?? String(this._config.item);
2588
+ }
2589
+ /**
2590
+ * Update option configuration and re-render
2591
+ */
2592
+ updateConfig(updates) {
2593
+ this._config = { ...this._config, ...updates };
2594
+ this._render();
2595
+ this._attachEventListeners();
2596
+ }
2597
+ /**
2598
+ * Get current configuration
2599
+ */
2600
+ getConfig() {
2601
+ return this._config;
2602
+ }
2603
+ /**
2604
+ * Get option value
2605
+ */
2606
+ getValue() {
2607
+ return this._getValue();
2608
+ }
2609
+ /**
2610
+ * Get option label
2611
+ */
2612
+ getLabel() {
2613
+ return this._getLabel();
2614
+ }
2615
+ /**
2616
+ * Set selected state
2617
+ */
2618
+ setSelected(selected) {
2619
+ this._config.selected = selected;
2620
+ this._render();
2621
+ }
2622
+ /**
2623
+ * Set active state
2624
+ */
2625
+ setActive(active) {
2626
+ this._config.active = active;
2627
+ this._render();
2628
+ }
2629
+ /**
2630
+ * Set disabled state
2631
+ */
2632
+ setDisabled(disabled) {
2633
+ this._config.disabled = disabled;
2634
+ this._render();
2635
+ }
2636
+ }
2637
+ // Register custom element
2638
+ if (!customElements.get('select-option')) {
2639
+ customElements.define('select-option', SelectOption);
2640
+ }
2641
+
2642
+ /**
2643
+ * Web Worker protocol types for off-main-thread processing.
2644
+ * Supports heavy transforms, fuzzy search, and data processing.
2645
+ */
2646
+ /**
2647
+ * Worker manager for handling off-main-thread processing.
2648
+ * Automatically falls back if workers not available or blocked by COOP/COEP.
2649
+ */
2650
+ class WorkerManager {
2651
+ constructor() {
2652
+ this.worker = null;
2653
+ this.pending = new Map();
2654
+ this.supportsWorkers = false;
2655
+ this.supportsSharedArrayBuffer = false;
2656
+ this.nextId = 0;
2657
+ this.detectFeatures();
2658
+ this.initWorker();
2659
+ }
2660
+ /**
2661
+ * Detect browser capabilities
2662
+ */
2663
+ detectFeatures() {
2664
+ this.supportsWorkers = typeof Worker !== 'undefined';
2665
+ this.supportsSharedArrayBuffer = typeof SharedArrayBuffer !== 'undefined';
2666
+ }
2667
+ /**
2668
+ * Initialize worker (inline blob URL for simplicity)
2669
+ */
2670
+ initWorker() {
2671
+ if (!this.supportsWorkers)
2672
+ return;
2673
+ try {
2674
+ const workerCode = this.generateWorkerCode();
2675
+ const blob = new Blob([workerCode], { type: 'application/javascript' });
2676
+ const url = URL.createObjectURL(blob);
2677
+ this.worker = new Worker(url);
2678
+ this.worker.onmessage = this.handleMessage.bind(this);
2679
+ this.worker.onerror = this.handleError.bind(this);
2680
+ // Cleanup blob URL after worker loads
2681
+ URL.revokeObjectURL(url);
2682
+ }
2683
+ catch (err) {
2684
+ console.warn('Worker initialization failed, falling back to main thread', err);
2685
+ this.worker = null;
2686
+ }
2687
+ }
2688
+ /**
2689
+ * Generate worker code as string
2690
+ */
2691
+ generateWorkerCode() {
2692
+ return `
2693
+ // Worker code
2694
+ self.onmessage = function(e) {
2695
+ const start = performance.now();
2696
+ const { id, type, payload } = e.data;
2697
+
2698
+ try {
2699
+ let result;
2700
+
2701
+ switch (type) {
2702
+ case 'transform':
2703
+ result = handleTransform(payload);
2704
+ break;
2705
+ case 'search':
2706
+ result = handleSearch(payload);
2707
+ break;
2708
+ case 'filter':
2709
+ result = handleFilter(payload);
2710
+ break;
2711
+ case 'sort':
2712
+ result = handleSort(payload);
2713
+ break;
2714
+ default:
2715
+ throw new Error('Unknown operation: ' + type);
2716
+ }
2717
+
2718
+ const duration = performance.now() - start;
2719
+ self.postMessage({
2720
+ id,
2721
+ success: true,
2722
+ data: result,
2723
+ duration,
2724
+ });
2725
+ } catch (error) {
2726
+ self.postMessage({
2727
+ id,
2728
+ success: false,
2729
+ error: error.message,
2730
+ });
2731
+ }
2732
+ };
2733
+
2734
+ function handleTransform({ items, transformer }) {
2735
+ const fn = new Function('item', 'index', 'return (' + transformer + ')(item, index)');
2736
+ return items.map((item, i) => fn(item, i));
2737
+ }
2738
+
2739
+ function handleSearch({ items, query, fuzzy, maxResults }) {
2740
+ const lowerQuery = query.toLowerCase();
2741
+ const results = [];
2742
+
2743
+ for (let i = 0; i < items.length; i++) {
2744
+ const item = items[i];
2745
+ const text = String(item).toLowerCase();
2746
+
2747
+ if (fuzzy) {
2748
+ if (fuzzyMatch(text, lowerQuery)) {
2749
+ results.push({ item, index: i, score: fuzzyScore(text, lowerQuery) });
2750
+ }
2751
+ } else {
2752
+ if (text.includes(lowerQuery)) {
2753
+ results.push({ item, index: i });
2754
+ }
2755
+ }
2756
+
2757
+ if (maxResults && results.length >= maxResults) break;
2758
+ }
2759
+
2760
+ if (fuzzy) {
2761
+ results.sort((a, b) => b.score - a.score);
2762
+ }
2763
+
2764
+ return results;
2765
+ }
2766
+
2767
+ function handleFilter({ items, predicate }) {
2768
+ const fn = new Function('item', 'index', 'return (' + predicate + ')(item, index)');
2769
+ return items.filter((item, i) => fn(item, i));
2770
+ }
2771
+
2772
+ function handleSort({ items, comparator }) {
2773
+ const fn = new Function('a', 'b', 'return (' + comparator + ')(a, b)');
2774
+ return [...items].sort(fn);
2775
+ }
2776
+
2777
+ // Simple fuzzy matching (Levenshtein-inspired)
2778
+ function fuzzyMatch(text, query) {
2779
+ let qi = 0;
2780
+ for (let ti = 0; ti < text.length && qi < query.length; ti++) {
2781
+ if (text[ti] === query[qi]) qi++;
2782
+ }
2783
+ return qi === query.length;
2784
+ }
2785
+
2786
+ function fuzzyScore(text, query) {
2787
+ let score = 0;
2788
+ let qi = 0;
2789
+ for (let ti = 0; ti < text.length && qi < query.length; ti++) {
2790
+ if (text[ti] === query[qi]) {
2791
+ score += 100 - ti; // Earlier matches score higher
2792
+ qi++;
2793
+ }
2794
+ }
2795
+ return score;
2796
+ }
2797
+ `;
2798
+ }
2799
+ /**
2800
+ * Handle worker message
2801
+ */
2802
+ handleMessage(e) {
2803
+ const { id, success, data, error } = e.data;
2804
+ const pending = this.pending.get(id);
2805
+ if (!pending)
2806
+ return;
2807
+ clearTimeout(pending.timeout);
2808
+ this.pending.delete(id);
2809
+ if (success) {
2810
+ pending.resolve(data);
2811
+ }
2812
+ else {
2813
+ pending.reject(new Error(error || 'Worker error'));
2814
+ }
2815
+ }
2816
+ /**
2817
+ * Handle worker error
2818
+ */
2819
+ handleError(err) {
2820
+ console.error('Worker error:', err);
2821
+ // Reject all pending requests
2822
+ for (const [id, pending] of this.pending) {
2823
+ clearTimeout(pending.timeout);
2824
+ pending.reject(new Error('Worker crashed'));
2825
+ }
2826
+ this.pending.clear();
2827
+ }
2828
+ /**
2829
+ * Execute operation (worker or fallback)
2830
+ */
2831
+ async execute(type, payload, timeout = 5000) {
2832
+ // Fallback to main thread if no worker
2833
+ if (!this.worker) {
2834
+ return this.executeFallback(type, payload);
2835
+ }
2836
+ const id = `req_${this.nextId++}`;
2837
+ return new Promise((resolve, reject) => {
2838
+ const timeoutId = setTimeout(() => {
2839
+ this.pending.delete(id);
2840
+ reject(new Error('Worker timeout'));
2841
+ }, timeout);
2842
+ this.pending.set(id, { resolve, reject, timeout: timeoutId });
2843
+ this.worker.postMessage({ id, type, payload });
2844
+ });
2845
+ }
2846
+ /**
2847
+ * Fallback to main thread execution
2848
+ */
2849
+ async executeFallback(type, payload) {
2850
+ // Simple synchronous fallback implementations
2851
+ switch (type) {
2852
+ case 'transform': {
2853
+ const { items, transformer } = payload;
2854
+ const fn = new Function('item', 'index', `return (${transformer})(item, index)`);
2855
+ return items.map((item, i) => fn(item, i));
2856
+ }
2857
+ case 'search': {
2858
+ const { items, query, fuzzy } = payload;
2859
+ const lowerQuery = query.toLowerCase();
2860
+ const results = [];
2861
+ items.forEach((item, index) => {
2862
+ const itemStr = String(item).toLowerCase();
2863
+ if (fuzzy) {
2864
+ // Simple fuzzy match - check if all query chars appear in order
2865
+ let queryIndex = 0;
2866
+ for (let i = 0; i < itemStr.length && queryIndex < lowerQuery.length; i++) {
2867
+ if (itemStr[i] === lowerQuery[queryIndex]) {
2868
+ queryIndex++;
2869
+ }
2870
+ }
2871
+ if (queryIndex === lowerQuery.length) {
2872
+ results.push({ item, index });
2873
+ }
2874
+ }
2875
+ else {
2876
+ // Exact substring match
2877
+ if (itemStr.includes(lowerQuery)) {
2878
+ results.push({ item, index });
2879
+ }
2880
+ }
2881
+ });
2882
+ return results;
2883
+ }
2884
+ case 'filter': {
2885
+ const { items, predicate } = payload;
2886
+ const fn = new Function('item', 'index', `return (${predicate})(item, index)`);
2887
+ return items.filter((item, i) => fn(item, i));
2888
+ }
2889
+ case 'sort': {
2890
+ const { items, comparator } = payload;
2891
+ const fn = new Function('a', 'b', `return (${comparator})(a, b)`);
2892
+ return [...items].sort(fn);
2893
+ }
2894
+ default:
2895
+ throw new Error(`Unknown operation: ${type}`);
2896
+ }
2897
+ }
2898
+ /**
2899
+ * Transform items with custom function
2900
+ */
2901
+ async transform(items, transformer) {
2902
+ return this.execute('transform', {
2903
+ items,
2904
+ transformer: transformer.toString(),
2905
+ });
2906
+ }
2907
+ /**
2908
+ * Search items with optional fuzzy matching
2909
+ */
2910
+ async search(items, query, fuzzy = false) {
2911
+ return this.execute('search', { items, query, fuzzy });
2912
+ }
2913
+ /**
2914
+ * Filter items with predicate
2915
+ */
2916
+ async filter(items, predicate) {
2917
+ return this.execute('filter', {
2918
+ items,
2919
+ predicate: predicate.toString(),
2920
+ });
2921
+ }
2922
+ /**
2923
+ * Sort items with comparator
2924
+ */
2925
+ async sort(items, comparator) {
2926
+ return this.execute('sort', {
2927
+ items,
2928
+ comparator: comparator.toString(),
2929
+ });
2930
+ }
2931
+ /**
2932
+ * Check if workers are supported
2933
+ */
2934
+ get hasWorkerSupport() {
2935
+ return this.supportsWorkers && this.worker !== null;
2936
+ }
2937
+ /**
2938
+ * Check if SharedArrayBuffer is supported
2939
+ */
2940
+ get hasSharedArrayBuffer() {
2941
+ return this.supportsSharedArrayBuffer;
2942
+ }
2943
+ /**
2944
+ * Terminate worker
2945
+ */
2946
+ destroy() {
2947
+ if (this.worker) {
2948
+ // Reject all pending
2949
+ for (const [id, pending] of this.pending) {
2950
+ clearTimeout(pending.timeout);
2951
+ pending.reject(new Error('Worker terminated'));
2952
+ }
2953
+ this.pending.clear();
2954
+ this.worker.terminate();
2955
+ this.worker = null;
2956
+ }
2957
+ }
2958
+ }
2959
+ /**
2960
+ * Singleton instance
2961
+ */
2962
+ let workerManager = null;
2963
+ function getWorkerManager() {
2964
+ if (!workerManager) {
2965
+ workerManager = new WorkerManager();
2966
+ }
2967
+ return workerManager;
2968
+ }
2969
+
2970
+ /**
2971
+ * Performance telemetry module for tracking frame timing and performance metrics.
2972
+ * Uses PerformanceObserver for precise measurements.
2973
+ *
2974
+ * Tracks:
2975
+ * - Frame time (should be < 16.67ms for 60fps)
2976
+ * - Main thread work per frame (target < 8ms, ideal 4-6ms)
2977
+ * - Long tasks (> 50ms blocks)
2978
+ * - Memory usage (if available)
2979
+ */
2980
+ class PerformanceTelemetry {
2981
+ constructor() {
2982
+ this.frameTimes = [];
2983
+ this.longTasks = 0;
2984
+ this.lastFrameTime = 0;
2985
+ this.rafId = 0;
2986
+ this.measuring = false;
2987
+ this.observer = null;
2988
+ this.workTimes = [];
2989
+ /**
2990
+ * Measure frame timing via requestAnimationFrame
2991
+ */
2992
+ this.measureFrame = () => {
2993
+ if (!this.measuring)
2994
+ return;
2995
+ const now = performance.now();
2996
+ const frameTime = now - this.lastFrameTime;
2997
+ this.frameTimes.push(frameTime);
2998
+ // Keep last 60 frames (1 second at 60fps)
2999
+ if (this.frameTimes.length > 60) {
3000
+ this.frameTimes.shift();
3001
+ }
3002
+ this.lastFrameTime = now;
3003
+ this.rafId = requestAnimationFrame(this.measureFrame);
3004
+ };
3005
+ this.setupObserver();
3006
+ }
3007
+ /**
3008
+ * Setup PerformanceObserver for long tasks
3009
+ */
3010
+ setupObserver() {
3011
+ if (typeof PerformanceObserver === 'undefined')
3012
+ return;
3013
+ try {
3014
+ this.observer = new PerformanceObserver((list) => {
3015
+ for (const entry of list.getEntries()) {
3016
+ if (entry.entryType === 'longtask' && entry.duration > 50) {
3017
+ this.longTasks++;
3018
+ }
3019
+ if (entry.entryType === 'measure') {
3020
+ this.workTimes.push(entry.duration);
3021
+ }
3022
+ }
3023
+ });
3024
+ // Observe long tasks if supported
3025
+ try {
3026
+ this.observer.observe({ entryTypes: ['longtask', 'measure'] });
3027
+ }
3028
+ catch {
3029
+ // Fallback to just measure if longtask not supported
3030
+ this.observer.observe({ entryTypes: ['measure'] });
3031
+ }
3032
+ }
3033
+ catch (err) {
3034
+ console.warn('PerformanceObserver not available', err);
3035
+ }
3036
+ }
3037
+ /**
3038
+ * Start measuring frame performance
3039
+ */
3040
+ start() {
3041
+ if (this.measuring)
3042
+ return;
3043
+ this.measuring = true;
3044
+ this.frameTimes = [];
3045
+ this.workTimes = [];
3046
+ this.longTasks = 0;
3047
+ this.lastFrameTime = performance.now();
3048
+ this.measureFrame();
3049
+ }
3050
+ /**
3051
+ * Stop measuring
3052
+ */
3053
+ stop() {
3054
+ this.measuring = false;
3055
+ if (this.rafId) {
3056
+ cancelAnimationFrame(this.rafId);
3057
+ this.rafId = 0;
3058
+ }
3059
+ }
3060
+ /**
3061
+ * Mark start of work for custom measurement
3062
+ */
3063
+ markStart(label) {
3064
+ performance.mark(`${label}-start`);
3065
+ }
3066
+ /**
3067
+ * Mark end of work and measure duration
3068
+ */
3069
+ markEnd(label) {
3070
+ performance.mark(`${label}-end`);
3071
+ performance.measure(label, `${label}-start`, `${label}-end`);
3072
+ const measure = performance.getEntriesByName(label, 'measure')[0];
3073
+ return measure ? measure.duration : 0;
3074
+ }
3075
+ /**
3076
+ * Get current metrics snapshot
3077
+ */
3078
+ getMetrics() {
3079
+ const avgFrameTime = this.frameTimes.length > 0
3080
+ ? this.frameTimes.reduce((a, b) => a + b, 0) / this.frameTimes.length
3081
+ : 0;
3082
+ const fps = avgFrameTime > 0 ? 1000 / avgFrameTime : 0;
3083
+ const longFrames = this.frameTimes.filter(t => t > 16.67).length;
3084
+ const droppedFrames = this.frameTimes.filter(t => t > 33.33).length; // > 2 frames
3085
+ const avgMainThreadWork = this.workTimes.length > 0
3086
+ ? this.workTimes.reduce((a, b) => a + b, 0) / this.workTimes.length
3087
+ : 0;
3088
+ const metrics = {
3089
+ frames: {
3090
+ frameTime: avgFrameTime,
3091
+ fps,
3092
+ longTasks: longFrames,
3093
+ droppedFrames,
3094
+ },
3095
+ mainThreadWork: avgMainThreadWork,
3096
+ longTaskCount: this.longTasks,
3097
+ };
3098
+ // Add memory info if available
3099
+ if ('memory' in performance && performance.memory) {
3100
+ const mem = performance.memory;
3101
+ metrics.memory = {
3102
+ usedJSHeapSize: mem.usedJSHeapSize,
3103
+ totalJSHeapSize: mem.totalJSHeapSize,
3104
+ jsHeapSizeLimit: mem.jsHeapSizeLimit,
3105
+ };
3106
+ }
3107
+ return metrics;
3108
+ }
3109
+ /**
3110
+ * Check if performance is acceptable
3111
+ */
3112
+ isPerformanceGood() {
3113
+ const metrics = this.getMetrics();
3114
+ // Target: 60fps (16.67ms), < 8ms main thread work, minimal long tasks
3115
+ return (metrics.frames.fps >= 55 && // Allow some variance
3116
+ metrics.mainThreadWork < 8 &&
3117
+ metrics.frames.longTasks < 3 // Max 5% long frames
3118
+ );
3119
+ }
3120
+ /**
3121
+ * Get detailed report as string
3122
+ */
3123
+ getReport() {
3124
+ const metrics = this.getMetrics();
3125
+ let report = '=== Performance Report ===\n';
3126
+ report += `FPS: ${metrics.frames.fps.toFixed(2)}\n`;
3127
+ report += `Avg Frame Time: ${metrics.frames.frameTime.toFixed(2)}ms\n`;
3128
+ report += `Long Frames (>16.67ms): ${metrics.frames.longTasks}\n`;
3129
+ report += `Dropped Frames (>33ms): ${metrics.frames.droppedFrames}\n`;
3130
+ report += `Avg Main Thread Work: ${metrics.mainThreadWork.toFixed(2)}ms\n`;
3131
+ report += `Long Tasks (>50ms): ${metrics.longTaskCount}\n`;
3132
+ if (metrics.memory) {
3133
+ const usedMB = (metrics.memory.usedJSHeapSize / 1024 / 1024).toFixed(2);
3134
+ const limitMB = (metrics.memory.jsHeapSizeLimit / 1024 / 1024).toFixed(2);
3135
+ report += `Memory: ${usedMB}MB / ${limitMB}MB\n`;
3136
+ }
3137
+ report += `Status: ${this.isPerformanceGood() ? '✓ GOOD' : '✗ POOR'}\n`;
3138
+ return report;
3139
+ }
3140
+ /**
3141
+ * Reset all metrics
3142
+ */
3143
+ reset() {
3144
+ this.frameTimes = [];
3145
+ this.workTimes = [];
3146
+ this.longTasks = 0;
3147
+ }
3148
+ /**
3149
+ * Cleanup
3150
+ */
3151
+ destroy() {
3152
+ this.stop();
3153
+ if (this.observer) {
3154
+ this.observer.disconnect();
3155
+ this.observer = null;
3156
+ }
3157
+ }
3158
+ }
3159
+ /**
3160
+ * Singleton instance
3161
+ */
3162
+ let telemetry = null;
3163
+ function getTelemetry() {
3164
+ if (!telemetry) {
3165
+ telemetry = new PerformanceTelemetry();
3166
+ }
3167
+ return telemetry;
3168
+ }
3169
+ /**
3170
+ * Helper to measure async function execution
3171
+ */
3172
+ async function measureAsync(label, fn) {
3173
+ const start = performance.now();
3174
+ const result = await fn();
3175
+ const duration = performance.now() - start;
3176
+ console.log(`[Perf] ${label}: ${duration.toFixed(2)}ms`);
3177
+ return { result, duration };
3178
+ }
3179
+ /**
3180
+ * Helper to measure sync function execution
3181
+ */
3182
+ function measureSync(label, fn) {
3183
+ const start = performance.now();
3184
+ const result = fn();
3185
+ const duration = performance.now() - start;
3186
+ console.log(`[Perf] ${label}: ${duration.toFixed(2)}ms`);
3187
+ return { result, duration };
3188
+ }
3189
+
3190
+ /**
3191
+ * Security utilities for CSP-compliant environments.
3192
+ * All utilities are opt-in and do not run by default.
3193
+ */
3194
+ /**
3195
+ * No-op sanitizer - DOES NOT sanitize HTML.
3196
+ * Used by default to avoid forcing a dependency on sanitizer libraries.
3197
+ * Developers using untrusted data MUST provide a real sanitizer.
3198
+ */
3199
+ class NoOpSanitizer {
3200
+ sanitize(html) {
3201
+ return html;
3202
+ }
3203
+ }
3204
+ /**
3205
+ * Registry for custom sanitizer implementation.
3206
+ * Defaults to no-op - developers must explicitly set a sanitizer.
3207
+ */
3208
+ let globalSanitizer = new NoOpSanitizer();
3209
+ /**
3210
+ * Set a custom HTML sanitizer (e.g., DOMPurify).
3211
+ *
3212
+ * @example
3213
+ * import DOMPurify from 'dompurify';
3214
+ * setHTMLSanitizer({
3215
+ * sanitize: (html) => DOMPurify.sanitize(html)
3216
+ * });
3217
+ */
3218
+ function setHTMLSanitizer(sanitizer) {
3219
+ globalSanitizer = sanitizer;
3220
+ }
3221
+ /**
3222
+ * Get the current HTML sanitizer.
3223
+ */
3224
+ function getHTMLSanitizer() {
3225
+ return globalSanitizer;
3226
+ }
3227
+ /**
3228
+ * Sanitize HTML using the registered sanitizer.
3229
+ * WARNING: Default is no-op! Set a real sanitizer for untrusted content.
3230
+ *
3231
+ * @param html - HTML string to sanitize
3232
+ * @returns Sanitized HTML string
3233
+ */
3234
+ function sanitizeHTML(html) {
3235
+ return globalSanitizer.sanitize(html);
3236
+ }
3237
+ /**
3238
+ * Create a text node (CSP-safe alternative to innerHTML for text content).
3239
+ *
3240
+ * @param text - Text content
3241
+ * @returns Text node
3242
+ */
3243
+ function createTextNode(text) {
3244
+ return document.createTextNode(text);
3245
+ }
3246
+ /**
3247
+ * Set text content safely (CSP-compliant).
3248
+ *
3249
+ * @param element - Target element
3250
+ * @param text - Text content
3251
+ */
3252
+ function setTextContent(element, text) {
3253
+ element.textContent = text;
3254
+ }
3255
+ /**
3256
+ * Create an element with attributes (CSP-safe).
3257
+ * No inline styles or event handlers.
3258
+ *
3259
+ * @param tagName - HTML tag name
3260
+ * @param attributes - Element attributes (excluding style and on* handlers)
3261
+ * @param textContent - Optional text content
3262
+ * @returns Created element
3263
+ */
3264
+ function createElement(tagName, attributes, textContent) {
3265
+ const el = document.createElement(tagName);
3266
+ if (attributes) {
3267
+ for (const [key, value] of Object.entries(attributes)) {
3268
+ // Block dangerous attributes
3269
+ if (key.startsWith('on') || key === 'style') {
3270
+ console.warn(`[NativeSelect Security] Blocked attribute: ${key}`);
3271
+ continue;
3272
+ }
3273
+ el.setAttribute(key, value);
3274
+ }
3275
+ }
3276
+ if (textContent !== undefined) {
3277
+ el.textContent = textContent;
3278
+ }
3279
+ return el;
3280
+ }
3281
+ /**
3282
+ * Set CSS custom properties (CSP-safe way to style elements).
3283
+ *
3284
+ * @param element - Target element
3285
+ * @param properties - CSS custom properties (e.g., { '--color': 'red' })
3286
+ */
3287
+ function setCSSProperties(element, properties) {
3288
+ for (const [key, value] of Object.entries(properties)) {
3289
+ if (!key.startsWith('--')) {
3290
+ console.warn(`[NativeSelect Security] CSS property must start with --: ${key}`);
3291
+ continue;
3292
+ }
3293
+ element.style.setProperty(key, value);
3294
+ }
3295
+ }
3296
+ /**
3297
+ * Feature detection utilities for CSP-restricted environments.
3298
+ */
3299
+ const CSPFeatures = {
3300
+ /**
3301
+ * Check if inline scripts are allowed (CSP: script-src 'unsafe-inline').
3302
+ */
3303
+ hasInlineScripts() {
3304
+ try {
3305
+ // Try to create a function (blocked by CSP: script-src without 'unsafe-eval')
3306
+ new Function('return true');
3307
+ return true;
3308
+ }
3309
+ catch {
3310
+ return false;
3311
+ }
3312
+ },
3313
+ /**
3314
+ * Check if eval is allowed (CSP: script-src 'unsafe-eval').
3315
+ */
3316
+ hasEval() {
3317
+ try {
3318
+ eval('true');
3319
+ return true;
3320
+ }
3321
+ catch {
3322
+ return false;
3323
+ }
3324
+ },
3325
+ /**
3326
+ * Check if SharedArrayBuffer is available.
3327
+ * Required for some Worker features, blocked without COOP/COEP headers.
3328
+ */
3329
+ hasSharedArrayBuffer() {
3330
+ return typeof SharedArrayBuffer !== 'undefined';
3331
+ },
3332
+ /**
3333
+ * Check if Web Workers are available and functional.
3334
+ */
3335
+ hasWorkers() {
3336
+ try {
3337
+ return typeof Worker !== 'undefined';
3338
+ }
3339
+ catch {
3340
+ return false;
3341
+ }
3342
+ },
3343
+ /**
3344
+ * Check if we're in a sandboxed iframe.
3345
+ */
3346
+ isSandboxed() {
3347
+ try {
3348
+ // Sandboxed iframes may not have access to parent
3349
+ return window !== window.parent && !window.parent;
3350
+ }
3351
+ catch {
3352
+ return true;
3353
+ }
3354
+ },
3355
+ /**
3356
+ * Check CSP policy via meta tag or SecurityPolicyViolationEvent.
3357
+ */
3358
+ getCSPDirectives() {
3359
+ const metaTags = document.querySelectorAll('meta[http-equiv="Content-Security-Policy"]');
3360
+ const directives = [];
3361
+ metaTags.forEach((meta) => {
3362
+ const content = meta.getAttribute('content');
3363
+ if (content) {
3364
+ directives.push(content);
3365
+ }
3366
+ });
3367
+ return directives;
3368
+ }
3369
+ };
3370
+ /**
3371
+ * Detect environment capabilities once at initialization.
3372
+ */
3373
+ function detectEnvironment() {
3374
+ return {
3375
+ canUseWorkers: CSPFeatures.hasWorkers(),
3376
+ canUseSharedArrayBuffer: CSPFeatures.hasSharedArrayBuffer(),
3377
+ canUseInlineScripts: CSPFeatures.hasInlineScripts(),
3378
+ canUseEval: CSPFeatures.hasEval(),
3379
+ isSandboxed: CSPFeatures.isSandboxed(),
3380
+ cspDirectives: CSPFeatures.getCSPDirectives()
3381
+ };
3382
+ }
3383
+ /**
3384
+ * Validation for untrusted HTML in templates.
3385
+ * Logs warning if no sanitizer is configured.
3386
+ */
3387
+ function validateTemplate(html, source) {
3388
+ if (globalSanitizer instanceof NoOpSanitizer) {
3389
+ console.warn(`[NativeSelect Security] Template from "${source}" contains HTML but no sanitizer is configured. ` +
3390
+ `This may be unsafe if the content is untrusted. ` +
3391
+ `Call setHTMLSanitizer() with a sanitizer like DOMPurify.`);
3392
+ }
3393
+ return sanitizeHTML(html);
3394
+ }
3395
+ /**
3396
+ * Check if a string looks like it might contain executable code.
3397
+ */
3398
+ function containsSuspiciousPatterns(str) {
3399
+ const patterns = [
3400
+ /<script/i,
3401
+ /javascript:/i,
3402
+ /on\w+\s*=/i, // event handlers like onclick=
3403
+ /eval\s*\(/i,
3404
+ /Function\s*\(/i,
3405
+ /setTimeout\s*\(/i,
3406
+ /setInterval\s*\(/i
3407
+ ];
3408
+ return patterns.some(pattern => pattern.test(str));
3409
+ }
3410
+ /**
3411
+ * Escape HTML entities for text content.
3412
+ */
3413
+ function escapeHTML(text) {
3414
+ const div = document.createElement('div');
3415
+ div.textContent = text;
3416
+ return div.innerHTML;
3417
+ }
3418
+ /**
3419
+ * Escape attribute values.
3420
+ */
3421
+ function escapeAttribute(attr) {
3422
+ return attr
3423
+ .replace(/&/g, '&amp;')
3424
+ .replace(/"/g, '&quot;')
3425
+ .replace(/'/g, '&#39;')
3426
+ .replace(/</g, '&lt;')
3427
+ .replace(/>/g, '&gt;');
3428
+ }
3429
+
3430
+ /**
3431
+ * CSP-compliant styling utilities.
3432
+ * No inline styles or runtime style tag injection.
3433
+ * Uses CSS custom properties and class names only.
3434
+ */
3435
+ /**
3436
+ * Apply CSS classes to an element (CSP-safe).
3437
+ */
3438
+ function applyClasses(element, classes) {
3439
+ element.className = classes.filter(Boolean).join(' ');
3440
+ }
3441
+ /**
3442
+ * Toggle a CSS class (CSP-safe).
3443
+ */
3444
+ function toggleClass(element, className, force) {
3445
+ element.classList.toggle(className, force);
3446
+ }
3447
+ /**
3448
+ * Set CSS custom properties (CSP-safe dynamic styling).
3449
+ * All properties must start with -- (CSS variables).
3450
+ *
3451
+ * @example
3452
+ * setCustomProperties(element, {
3453
+ * '--item-height': '48px',
3454
+ * '--selected-bg': '#0066cc'
3455
+ * });
3456
+ */
3457
+ function setCustomProperties(element, properties) {
3458
+ for (const [key, value] of Object.entries(properties)) {
3459
+ if (!key.startsWith('--')) {
3460
+ if (process.env.NODE_ENV !== 'production') {
3461
+ console.warn(`[NativeSelect CSP] Custom property must start with --: "${key}". Skipping.`);
3462
+ }
3463
+ continue;
3464
+ }
3465
+ element.style.setProperty(key, value);
3466
+ }
3467
+ }
3468
+ /**
3469
+ * Get a CSS custom property value.
3470
+ */
3471
+ function getCustomProperty(element, propertyName) {
3472
+ return getComputedStyle(element).getPropertyValue(propertyName).trim();
3473
+ }
3474
+ /**
3475
+ * Remove a CSS custom property.
3476
+ */
3477
+ function removeCustomProperty(element, propertyName) {
3478
+ element.style.removeProperty(propertyName);
3479
+ }
3480
+ /**
3481
+ * Predefined theme as CSS custom properties.
3482
+ * Can be overridden by consumers via CSS.
3483
+ */
3484
+ const defaultTheme = {
3485
+ '--ns-item-height': '40px',
3486
+ '--ns-item-padding': '8px 12px',
3487
+ '--ns-item-bg': 'transparent',
3488
+ '--ns-item-hover-bg': 'rgba(0, 0, 0, 0.05)',
3489
+ '--ns-item-selected-bg': 'rgba(0, 102, 204, 0.1)',
3490
+ '--ns-item-active-bg': 'rgba(0, 102, 204, 0.2)',
3491
+ '--ns-item-color': 'inherit',
3492
+ '--ns-item-selected-color': '#0066cc',
3493
+ '--ns-border-color': '#ccc',
3494
+ '--ns-border-radius': '4px',
3495
+ '--ns-focus-outline': '2px solid #0066cc',
3496
+ '--ns-font-size': '14px',
3497
+ '--ns-font-family': 'system-ui, -apple-system, sans-serif'
3498
+ };
3499
+ /**
3500
+ * Apply the default theme to an element.
3501
+ */
3502
+ function applyDefaultTheme(element) {
3503
+ setCustomProperties(element, defaultTheme);
3504
+ }
3505
+ /**
3506
+ * Static CSS for shadow DOM (no runtime injection).
3507
+ * This CSS is CSP-safe because it's defined at build time as a string constant,
3508
+ * not generated at runtime.
3509
+ */
3510
+ const shadowDOMStyles = `
3511
+ :host {
3512
+ display: block;
3513
+ box-sizing: border-box;
3514
+ font-family: var(--ns-font-family, system-ui, -apple-system, sans-serif);
3515
+ font-size: var(--ns-font-size, 14px);
3516
+ }
3517
+
3518
+ * {
3519
+ box-sizing: border-box;
3520
+ }
3521
+
3522
+ .ns-list {
3523
+ position: relative;
3524
+ overflow: auto;
3525
+ max-height: var(--ns-max-height, 300px);
3526
+ border: 1px solid var(--ns-border-color, #ccc);
3527
+ border-radius: var(--ns-border-radius, 4px);
3528
+ background: var(--ns-bg, white);
3529
+ outline: none;
3530
+ }
3531
+
3532
+ .ns-list:focus {
3533
+ outline: var(--ns-focus-outline, 2px solid #0066cc);
3534
+ outline-offset: -2px;
3535
+ }
3536
+
3537
+ .ns-item {
3538
+ padding: var(--ns-item-padding, 8px 12px);
3539
+ min-height: var(--ns-item-height, 40px);
3540
+ color: var(--ns-item-color, inherit);
3541
+ background: var(--ns-item-bg, transparent);
3542
+ cursor: pointer;
3543
+ user-select: none;
3544
+ display: flex;
3545
+ align-items: center;
3546
+ transition: background-color 0.15s ease;
3547
+ }
3548
+
3549
+ .ns-item:hover {
3550
+ background: var(--ns-item-hover-bg, rgba(0, 0, 0, 0.05));
3551
+ }
3552
+
3553
+ .ns-item[aria-selected="true"] {
3554
+ background: var(--ns-item-selected-bg, rgba(0, 102, 204, 0.1));
3555
+ color: var(--ns-item-selected-color, #0066cc);
3556
+ }
3557
+
3558
+ .ns-item[data-active="true"] {
3559
+ background: var(--ns-item-active-bg, rgba(0, 102, 204, 0.2));
3560
+ }
3561
+
3562
+ .ns-item[aria-disabled="true"] {
3563
+ opacity: 0.5;
3564
+ cursor: not-allowed;
3565
+ }
3566
+
3567
+ .ns-viewport {
3568
+ position: relative;
3569
+ overflow: auto;
3570
+ -webkit-overflow-scrolling: touch;
3571
+ }
3572
+
3573
+ .ns-spacer {
3574
+ position: absolute;
3575
+ top: 0;
3576
+ left: 0;
3577
+ width: 1px;
3578
+ pointer-events: none;
3579
+ }
3580
+
3581
+ /* Screen reader only */
3582
+ .ns-sr-only {
3583
+ position: absolute;
3584
+ left: -9999px;
3585
+ width: 1px;
3586
+ height: 1px;
3587
+ overflow: hidden;
3588
+ clip: rect(0, 0, 0, 0);
3589
+ white-space: nowrap;
3590
+ }
3591
+
3592
+ /* Portal mode (teleported outside shadow DOM) */
3593
+ .ns-portal {
3594
+ position: fixed;
3595
+ z-index: var(--ns-portal-z-index, 9999);
3596
+ }
3597
+
3598
+ /* CSP warning banner (only shown in development) */
3599
+ .ns-csp-warning {
3600
+ padding: 8px;
3601
+ background: #fff3cd;
3602
+ border: 1px solid #ffc107;
3603
+ border-radius: 4px;
3604
+ color: #856404;
3605
+ font-size: 12px;
3606
+ margin-bottom: 8px;
3607
+ }
3608
+ `;
3609
+ /**
3610
+ * Inject static styles into shadow root (CSP-safe).
3611
+ * Only called once during component initialization.
3612
+ */
3613
+ function injectShadowStyles(shadowRoot) {
3614
+ const style = document.createElement('style');
3615
+ style.textContent = shadowDOMStyles;
3616
+ shadowRoot.appendChild(style);
3617
+ }
3618
+ /**
3619
+ * Check if element has overflow:hidden ancestors (portal fallback detection).
3620
+ */
3621
+ function hasOverflowHiddenAncestor(element) {
3622
+ let current = element.parentElement;
3623
+ while (current && current !== document.body) {
3624
+ const overflow = getComputedStyle(current).overflow;
3625
+ if (overflow === 'hidden' || overflow === 'clip') {
3626
+ return true;
3627
+ }
3628
+ current = current.parentElement;
3629
+ }
3630
+ return false;
3631
+ }
3632
+ /**
3633
+ * Runtime warning for CSP violations (development only).
3634
+ */
3635
+ function warnCSPViolation(feature, fallback) {
3636
+ if (process.env.NODE_ENV !== 'production') {
3637
+ console.warn(`[NativeSelect CSP] Feature "${feature}" blocked by Content Security Policy. ` +
3638
+ `Falling back to "${fallback}".`);
3639
+ }
3640
+ }
3641
+
3642
+ export { CSPFeatures, DOMPool, EnhancedSelect, FenwickTree, NativeSelectElement, PerformanceTelemetry, SelectOption, Virtualizer, WorkerManager, applyClasses, applyDefaultTheme, configureSelect, containsSuspiciousPatterns, createElement, createRendererHelpers, createTextNode, defaultTheme, detectEnvironment, escapeAttribute, escapeHTML, getCustomProperty, getHTMLSanitizer, getTelemetry, getWorkerManager, hasOverflowHiddenAncestor, injectShadowStyles, measureAsync, measureSync, removeCustomProperty, renderTemplate, resetSelectConfig, sanitizeHTML, selectConfig, setCSSProperties, setCustomProperties, setHTMLSanitizer, setTextContent, shadowDOMStyles, toggleClass, validateTemplate, warnCSPViolation };
3643
+ //# sourceMappingURL=index.js.map