@smilodon/core 1.13.0 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2105,6 +2105,8 @@ const defaultConfig = {
2105
2105
  overflowX: 'hidden',
2106
2106
  overflowY: 'auto',
2107
2107
  dragScroll: true,
2108
+ scrollToLatestChip: false,
2109
+ scrollToLatestChipBehavior: 'smooth',
2108
2110
  },
2109
2111
  scrollToSelected: {
2110
2112
  enabled: true,
@@ -2970,23 +2972,23 @@ class SelectOption extends HTMLElement {
2970
2972
  }
2971
2973
  }
2972
2974
  _attachEventListeners() {
2973
- // Click handler for selection
2975
+ // One delegated handler survives all SelectOption re-renders. In
2976
+ // particular, SVG descendants of the remove icon must remove the option,
2977
+ // not accidentally activate the surrounding option.
2974
2978
  this._container.addEventListener('click', (e) => {
2975
- // Don't trigger selection if clicking remove button
2976
- if (e.target === this._removeButton) {
2979
+ const target = e.target;
2980
+ if (this._removeButton
2981
+ && target instanceof Node
2982
+ && this._removeButton.contains(target)) {
2983
+ e.preventDefault();
2984
+ e.stopPropagation();
2985
+ this._handleRemove();
2977
2986
  return;
2978
2987
  }
2979
2988
  if (!this._isDisabledSelectionBlocked()) {
2980
2989
  this._handleSelect();
2981
2990
  }
2982
2991
  });
2983
- // Remove button handler
2984
- if (this._removeButton) {
2985
- this._removeButton.addEventListener('click', (e) => {
2986
- e.stopPropagation();
2987
- this._handleRemove();
2988
- });
2989
- }
2990
2992
  // Keyboard handler
2991
2993
  this.addEventListener('keydown', (e) => {
2992
2994
  if (this._isDisabledSelectionBlocked())
@@ -3049,7 +3051,6 @@ class SelectOption extends HTMLElement {
3049
3051
  updateConfig(updates) {
3050
3052
  this._config = { ...this._config, ...updates };
3051
3053
  this._render();
3052
- this._attachEventListeners();
3053
3054
  }
3054
3055
  /**
3055
3056
  * Get current configuration
@@ -4064,6 +4065,7 @@ class EnhancedSelect extends HTMLElement {
4064
4065
  intendedRenderKeys: [],
4065
4066
  filteredIndexByKey: new Map(),
4066
4067
  };
4068
+ this._emptyStateContext = null;
4067
4069
  // Null keeps the public/default renderer on its legacy full path. This
4068
4070
  // private state is only used by guarded virtualization integration.
4069
4071
  this._guardedVirtualWindow = null;
@@ -4120,6 +4122,10 @@ class EnhancedSelect extends HTMLElement {
4120
4122
  this._pendingFirstRenderMark = false;
4121
4123
  this._pendingSearchRenderMark = false;
4122
4124
  this._rangeAnchorIndex = null;
4125
+ /** Chip identity is tracked independently of dropdown option positions. */
4126
+ this._lastRenderedChipKeys = null;
4127
+ this._latestChipScrollGeneration = 0;
4128
+ this._latestChipScrollFrame = null;
4123
4129
  this._customOptionBoundElements = new WeakSet();
4124
4130
  this._customOptionManagedActiveClasses = new WeakMap();
4125
4131
  this._customOptionNormalizedIds = new WeakMap();
@@ -4310,6 +4316,7 @@ class EnhancedSelect extends HTMLElement {
4310
4316
  this._syncListboxAccessibility();
4311
4317
  }
4312
4318
  disconnectedCallback() {
4319
+ this._cancelLatestChipScroll();
4313
4320
  this._cancelBusyPresentation();
4314
4321
  if (this._dataRequestCoordinator) {
4315
4322
  const actions = this._dataRequestCoordinator.disconnect();
@@ -7902,6 +7909,12 @@ class EnhancedSelect extends HTMLElement {
7902
7909
  : delta >= 0
7903
7910
  ? -1
7904
7911
  : selectableIndices.length;
7912
+ // The final selectable option is the keyboard pagination boundary.
7913
+ // Keep the active option stable while the coordinator requests more data.
7914
+ if (delta === 1
7915
+ && currentPosition === selectableIndices.length - 1
7916
+ && this._requestKeyboardLoadMore())
7917
+ return;
7905
7918
  const nextPosition = Math.max(0, Math.min(selectableIndices.length - 1, basePosition + delta));
7906
7919
  const next = selectableIndices[nextPosition];
7907
7920
  this._setActive(next);
@@ -7914,6 +7927,33 @@ class EnhancedSelect extends HTMLElement {
7914
7927
  this._selectRange(anchorIndex, next, { clear: !opts?.toggleKey });
7915
7928
  }
7916
7929
  }
7930
+ /** Return true only when boundary navigation is held for an in-flight/new page. */
7931
+ _requestKeyboardLoadMore() {
7932
+ if (!this._state.isOpen
7933
+ || !this._config.infiniteScroll.enabled
7934
+ || this._dataLoadingConfig.protocol !== 'acknowledged'
7935
+ || !this._dataRequestCoordinator
7936
+ || !this._acknowledgedReady)
7937
+ return false;
7938
+ if (this._acknowledgedProjectionPending || this._acknowledgedProjectionSettling)
7939
+ return true;
7940
+ const snapshot = this._dataRequestCoordinator.getSnapshot();
7941
+ if (!snapshot.connected)
7942
+ return false;
7943
+ if (snapshot.busy)
7944
+ return true;
7945
+ if (!snapshot.hasMore
7946
+ || snapshot.error !== null
7947
+ || snapshot.resourceLimit !== null
7948
+ || (snapshot.status !== 'succeeded' && snapshot.status !== 'empty'))
7949
+ return false;
7950
+ // Delegate identity, pagination, and in-flight protection to the coordinator.
7951
+ const actions = this._dataRequestCoordinator.requestLoadMore('manual');
7952
+ if (actions.length === 0)
7953
+ return false;
7954
+ this._dispatchAcknowledgedActions(actions);
7955
+ return true;
7956
+ }
7917
7957
  _setActive(index, reason = 'keyboard') {
7918
7958
  this._ensureRegistryCurrent();
7919
7959
  const record = this._getRecordByIndex(index);
@@ -8233,6 +8273,19 @@ class EnhancedSelect extends HTMLElement {
8233
8273
  _updateInputDisplay() {
8234
8274
  const selectedEntries = this._getSelectedEntries();
8235
8275
  const selectedItems = selectedEntries.map(({ item }) => item);
8276
+ const nextKeys = new Set(selectedEntries.map(({ key }) => key));
8277
+ const previousKeys = this._lastRenderedChipKeys;
8278
+ let newlyAddedKey;
8279
+ if (previousKeys) {
8280
+ for (const key of nextKeys) {
8281
+ if (!previousKeys.has(key))
8282
+ newlyAddedKey = key;
8283
+ }
8284
+ }
8285
+ this._lastRenderedChipKeys = nextKeys;
8286
+ const previousChipScrollLeft = this._inputContent.scrollLeft;
8287
+ // Only a newly added selected key can initiate auto-scroll.
8288
+ this._cancelLatestChipScroll();
8236
8289
  const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
8237
8290
  if (selectedItems.length === 0) {
8238
8291
  this._input.value = '';
@@ -8308,8 +8361,87 @@ class EnhancedSelect extends HTMLElement {
8308
8361
  if (this._acknowledgedPendingSearchQuery !== null) {
8309
8362
  this._input.value = this._acknowledgedPendingSearchQuery;
8310
8363
  }
8364
+ // Recreating the chip DOM must not reset manual scrolling. In particular,
8365
+ // remove/clear, config rerenders, and item refreshes are not scroll intents.
8366
+ if (this._config.selection.mode === 'multi') {
8367
+ this._inputContent.scrollLeft = previousChipScrollLeft;
8368
+ if (newlyAddedKey !== undefined)
8369
+ this._scheduleLatestChipScroll(newlyAddedKey);
8370
+ }
8311
8371
  this._syncClearControlState();
8312
8372
  }
8373
+ _cancelLatestChipScroll() {
8374
+ this._latestChipScrollGeneration += 1;
8375
+ if (this._latestChipScrollFrame !== null) {
8376
+ cancelAnimationFrame(this._latestChipScrollFrame);
8377
+ this._latestChipScrollFrame = null;
8378
+ }
8379
+ }
8380
+ _scheduleLatestChipScroll(key) {
8381
+ if (!this.isConnected
8382
+ || this._config.selection.mode !== 'multi'
8383
+ || this._config.multiSelectDisplay.mode !== 'horizontal'
8384
+ || this._config.multiSelectDisplay.scrollToLatestChip !== true
8385
+ || this._config.multiSelectDisplay.overflowX === 'hidden')
8386
+ return;
8387
+ const generation = this._latestChipScrollGeneration;
8388
+ this._latestChipScrollFrame = requestAnimationFrame(() => {
8389
+ this._latestChipScrollFrame = null;
8390
+ if (generation !== this._latestChipScrollGeneration
8391
+ || !this.isConnected
8392
+ || this._multiScrollDrag.active
8393
+ || this._config.selection.mode !== 'multi'
8394
+ || this._config.multiSelectDisplay.mode !== 'horizontal'
8395
+ || this._config.multiSelectDisplay.scrollToLatestChip !== true)
8396
+ return;
8397
+ // Match the canonical selected-key sequence, not option indices:
8398
+ // selected items may be reordered or detached from the loaded page.
8399
+ const entries = this._getSelectedEntries();
8400
+ const index = entries.findIndex((entry) => entry.key === key);
8401
+ if (index < 0)
8402
+ return;
8403
+ const chip = this._inputContent.querySelectorAll('.selection-badge')[index];
8404
+ if (!chip)
8405
+ return;
8406
+ const hostBounds = this._inputContent.getBoundingClientRect();
8407
+ const chipBounds = chip.getBoundingClientRect();
8408
+ const leftEdge = hostBounds.left + this._inputContent.clientLeft;
8409
+ const rightEdge = leftEdge + this._inputContent.clientWidth;
8410
+ let delta;
8411
+ if (chip.parentElement === this._inputContent && chip.offsetWidth > 0) {
8412
+ // getBoundingClientRect() includes the chip's entry animation transform.
8413
+ // Use layout dimensions so the scroll target remains correct while the
8414
+ // chip scales into its final size. In RTL layouts, offsetLeft and
8415
+ // scrollLeft are both negative relative to the inline-start origin, so
8416
+ // the same subtraction maps the chip into the viewport's coordinates.
8417
+ const chipLeft = chip.offsetLeft - this._inputContent.scrollLeft;
8418
+ const chipRight = chipLeft + chip.offsetWidth;
8419
+ delta = chipRight > this._inputContent.clientWidth
8420
+ ? chipRight - this._inputContent.clientWidth
8421
+ : chipLeft < 0
8422
+ ? chipLeft
8423
+ : 0;
8424
+ }
8425
+ else {
8426
+ // DOM test environments without layout use the measurable rectangle.
8427
+ delta = chipBounds.right > rightEdge
8428
+ ? chipBounds.right - rightEdge
8429
+ : chipBounds.left < leftEdge
8430
+ ? chipBounds.left - leftEdge
8431
+ : 0;
8432
+ }
8433
+ if (Math.abs(delta) < 0.5)
8434
+ return;
8435
+ const reducedMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
8436
+ const behavior = reducedMotion ? 'auto' : (this._config.multiSelectDisplay.scrollToLatestChipBehavior ?? 'smooth');
8437
+ if (typeof this._inputContent.scrollBy === 'function') {
8438
+ this._inputContent.scrollBy({ left: delta, behavior });
8439
+ }
8440
+ else {
8441
+ this._inputContent.scrollLeft += delta;
8442
+ }
8443
+ });
8444
+ }
8313
8445
  _isActivelyEditingSearchInput() {
8314
8446
  return this._config.searchable === true
8315
8447
  && this._state.isOpen
@@ -8575,12 +8707,13 @@ class EnhancedSelect extends HTMLElement {
8575
8707
  classMap: true,
8576
8708
  optionRenderer: true,
8577
8709
  groupHeaderRenderer: true,
8710
+ emptyStateSlot: true,
8578
8711
  cssCustomProperties: true,
8579
8712
  shadowParts: true,
8580
8713
  globalStyleMirroring: true,
8581
8714
  },
8582
8715
  events: {
8583
- emitted: ['select', 'open', 'close', 'search', 'change', 'loadMore', 'remove', 'clear', 'error', 'diagnostic'],
8716
+ emitted: ['select', 'open', 'close', 'search', 'change', 'loadMore', 'remove', 'clear', 'emptyStateChange', 'error', 'diagnostic'],
8584
8717
  diagnosticEvent: true,
8585
8718
  },
8586
8719
  functionality: {
@@ -8596,6 +8729,36 @@ class EnhancedSelect extends HTMLElement {
8596
8729
  limitations: this.getKnownLimitations(),
8597
8730
  };
8598
8731
  }
8732
+ /** Return a detached immutable snapshot of the currently rendered empty state. */
8733
+ getEmptyStateContext() {
8734
+ const context = this._emptyStateContext;
8735
+ return context ? Object.freeze({ query: context.query, reason: context.reason }) : null;
8736
+ }
8737
+ _syncEmptyStateContext(context) {
8738
+ const current = this._emptyStateContext;
8739
+ if (current?.query === context?.query
8740
+ && current?.reason === context?.reason
8741
+ && Boolean(current) === Boolean(context))
8742
+ return;
8743
+ this._emptyStateContext = context
8744
+ ? Object.freeze({ query: context.query, reason: context.reason })
8745
+ : null;
8746
+ const detail = Object.freeze({ context: this.getEmptyStateContext() });
8747
+ this._emit('emptyStateChange', detail);
8748
+ }
8749
+ _appendEmptyState(context) {
8750
+ const empty = document.createElement('div');
8751
+ empty.setAttribute('part', 'no-results');
8752
+ empty.className = 'empty-state';
8753
+ const slot = document.createElement('slot');
8754
+ slot.name = 'empty-state';
8755
+ slot.appendChild(document.createTextNode(context.reason === 'no-results'
8756
+ ? `No results found for "${context.query}"`
8757
+ : 'No options available'));
8758
+ empty.appendChild(slot);
8759
+ this._optionsContainer.appendChild(empty);
8760
+ this._syncEmptyStateContext(context);
8761
+ }
8599
8762
  _emitChange() {
8600
8763
  const selectedEntries = this._getSelectedEntries();
8601
8764
  const selectedItems = selectedEntries.map(({ item }) => item);
@@ -9761,11 +9924,27 @@ class EnhancedSelect extends HTMLElement {
9761
9924
  this._state.isBusy = snapshot.busy;
9762
9925
  if (!snapshot.busy) {
9763
9926
  this._syncBusyPresentation();
9764
- if (!this._busyPresentationVisible) {
9765
- this._optionsContainer.querySelector('.busy-bucket')?.remove();
9927
+ const shouldReconcileEmptyState = snapshot.status === 'exhausted'
9928
+ && this._renderPlan.filteredKeys.length === 0;
9929
+ if (shouldReconcileEmptyState) {
9930
+ // The first projection rendered while logically busy, so the empty
9931
+ // state was intentionally suppressed. Reconcile only this terminal
9932
+ // empty case after settlement; keep any minimum-duration busy
9933
+ // presentation and its timer intact.
9934
+ this._renderOptions();
9935
+ const emptyStateRenderCycleId = this._renderCycleId;
9936
+ const emptyStateRenderCompleted = await this._waitForRenderCycle(emptyStateRenderCycleId);
9937
+ if (!emptyStateRenderCompleted) {
9938
+ throw new Error('Acknowledged empty-state render was superseded before completion');
9939
+ }
9940
+ }
9941
+ else {
9942
+ if (!this._busyPresentationVisible) {
9943
+ this._optionsContainer.querySelector('.busy-bucket')?.remove();
9944
+ }
9945
+ if (this._shouldRenderLoadMoreTrigger())
9946
+ this._addLoadMoreTrigger();
9766
9947
  }
9767
- if (this._shouldRenderLoadMoreTrigger())
9768
- this._addLoadMoreTrigger();
9769
9948
  }
9770
9949
  this._optionsContainer.scrollTop = this._acknowledgedProjectionScrollTop;
9771
9950
  this._syncListboxAccessibility();
@@ -10092,7 +10271,7 @@ class EnhancedSelect extends HTMLElement {
10092
10271
  this._busyPresentationGeneration += 1;
10093
10272
  this._clearBusyPresentationHideTimer();
10094
10273
  this._busyPresentationVisible = this._config.busyBucket.enabled;
10095
- this._busyPresentationStartedAt = this._busyBucket?.isConnected ? Date.now() : null;
10274
+ this._busyPresentationStartedAt = Date.now();
10096
10275
  }
10097
10276
  _syncBusyPresentation() {
10098
10277
  if (!this._config.busyBucket.enabled) {
@@ -10260,6 +10439,7 @@ class EnhancedSelect extends HTMLElement {
10260
10439
  this._optionsContainer.innerHTML = '';
10261
10440
  let filteredIndicesForScope = [];
10262
10441
  let guardedScrollTopToRestore = null;
10442
+ let renderedEmptyStateContext = null;
10263
10443
  // Ensure dropdown only contains options container (cleanup legacy direct children)
10264
10444
  // We need to preserve optionsContainer, so we can't just clear dropdown.innerHTML
10265
10445
  // But we can check if there are other children and remove them
@@ -10283,6 +10463,7 @@ class EnhancedSelect extends HTMLElement {
10283
10463
  searching.setAttribute('part', 'loading');
10284
10464
  searching.textContent = 'Searching...';
10285
10465
  this._optionsContainer.appendChild(searching);
10466
+ this._syncEmptyStateContext(null);
10286
10467
  this._resolveRenderCycle(renderCycleId, true);
10287
10468
  return;
10288
10469
  }
@@ -10386,17 +10567,11 @@ class EnhancedSelect extends HTMLElement {
10386
10567
  const filteredIndices = this._renderPlan.filteredIndices;
10387
10568
  this._refreshVirtualizationScope(filteredIndicesForScope);
10388
10569
  this._appendGuardedTopSpacer();
10389
- if (filteredIndices.length === 0 && !this._state.isBusy) {
10390
- const empty = document.createElement('div');
10391
- empty.setAttribute('part', 'no-results');
10392
- empty.className = 'empty-state';
10393
- if (query) {
10394
- empty.textContent = `No results found for "${this._state.searchQuery}"`;
10395
- }
10396
- else {
10397
- empty.textContent = 'No options available';
10398
- }
10399
- this._optionsContainer.appendChild(empty);
10570
+ if (this._renderPlan.filteredKeys.length === 0 && !this._state.isBusy) {
10571
+ renderedEmptyStateContext = this._state.searchQuery
10572
+ ? { query: this._state.searchQuery, reason: 'no-results' }
10573
+ : { query: '', reason: 'no-options' };
10574
+ this._appendEmptyState(renderedEmptyStateContext);
10400
10575
  }
10401
10576
  else {
10402
10577
  const shouldIncrementalRender = !this._guardedVirtualizer
@@ -10458,6 +10633,7 @@ class EnhancedSelect extends HTMLElement {
10458
10633
  }
10459
10634
  };
10460
10635
  renderChunk();
10636
+ this._syncEmptyStateContext(null);
10461
10637
  return;
10462
10638
  }
10463
10639
  filteredIndices.forEach((itemIndex) => {
@@ -10482,6 +10658,7 @@ class EnhancedSelect extends HTMLElement {
10482
10658
  if (guardedScrollTopToRestore !== null) {
10483
10659
  this._optionsContainer.scrollTop = guardedScrollTopToRestore;
10484
10660
  }
10661
+ this._syncEmptyStateContext(renderedEmptyStateContext);
10485
10662
  this._syncRenderedOptionAccessibility();
10486
10663
  this._syncLegacyActiveAria();
10487
10664
  this._syncGuardedOptionResizeTargets();