@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.umd.js CHANGED
@@ -2111,6 +2111,8 @@
2111
2111
  overflowX: 'hidden',
2112
2112
  overflowY: 'auto',
2113
2113
  dragScroll: true,
2114
+ scrollToLatestChip: false,
2115
+ scrollToLatestChipBehavior: 'smooth',
2114
2116
  },
2115
2117
  scrollToSelected: {
2116
2118
  enabled: true,
@@ -2976,23 +2978,23 @@
2976
2978
  }
2977
2979
  }
2978
2980
  _attachEventListeners() {
2979
- // Click handler for selection
2981
+ // One delegated handler survives all SelectOption re-renders. In
2982
+ // particular, SVG descendants of the remove icon must remove the option,
2983
+ // not accidentally activate the surrounding option.
2980
2984
  this._container.addEventListener('click', (e) => {
2981
- // Don't trigger selection if clicking remove button
2982
- if (e.target === this._removeButton) {
2985
+ const target = e.target;
2986
+ if (this._removeButton
2987
+ && target instanceof Node
2988
+ && this._removeButton.contains(target)) {
2989
+ e.preventDefault();
2990
+ e.stopPropagation();
2991
+ this._handleRemove();
2983
2992
  return;
2984
2993
  }
2985
2994
  if (!this._isDisabledSelectionBlocked()) {
2986
2995
  this._handleSelect();
2987
2996
  }
2988
2997
  });
2989
- // Remove button handler
2990
- if (this._removeButton) {
2991
- this._removeButton.addEventListener('click', (e) => {
2992
- e.stopPropagation();
2993
- this._handleRemove();
2994
- });
2995
- }
2996
2998
  // Keyboard handler
2997
2999
  this.addEventListener('keydown', (e) => {
2998
3000
  if (this._isDisabledSelectionBlocked())
@@ -3055,7 +3057,6 @@
3055
3057
  updateConfig(updates) {
3056
3058
  this._config = { ...this._config, ...updates };
3057
3059
  this._render();
3058
- this._attachEventListeners();
3059
3060
  }
3060
3061
  /**
3061
3062
  * Get current configuration
@@ -4070,6 +4071,7 @@
4070
4071
  intendedRenderKeys: [],
4071
4072
  filteredIndexByKey: new Map(),
4072
4073
  };
4074
+ this._emptyStateContext = null;
4073
4075
  // Null keeps the public/default renderer on its legacy full path. This
4074
4076
  // private state is only used by guarded virtualization integration.
4075
4077
  this._guardedVirtualWindow = null;
@@ -4126,6 +4128,10 @@
4126
4128
  this._pendingFirstRenderMark = false;
4127
4129
  this._pendingSearchRenderMark = false;
4128
4130
  this._rangeAnchorIndex = null;
4131
+ /** Chip identity is tracked independently of dropdown option positions. */
4132
+ this._lastRenderedChipKeys = null;
4133
+ this._latestChipScrollGeneration = 0;
4134
+ this._latestChipScrollFrame = null;
4129
4135
  this._customOptionBoundElements = new WeakSet();
4130
4136
  this._customOptionManagedActiveClasses = new WeakMap();
4131
4137
  this._customOptionNormalizedIds = new WeakMap();
@@ -4316,6 +4322,7 @@
4316
4322
  this._syncListboxAccessibility();
4317
4323
  }
4318
4324
  disconnectedCallback() {
4325
+ this._cancelLatestChipScroll();
4319
4326
  this._cancelBusyPresentation();
4320
4327
  if (this._dataRequestCoordinator) {
4321
4328
  const actions = this._dataRequestCoordinator.disconnect();
@@ -7908,6 +7915,12 @@
7908
7915
  : delta >= 0
7909
7916
  ? -1
7910
7917
  : selectableIndices.length;
7918
+ // The final selectable option is the keyboard pagination boundary.
7919
+ // Keep the active option stable while the coordinator requests more data.
7920
+ if (delta === 1
7921
+ && currentPosition === selectableIndices.length - 1
7922
+ && this._requestKeyboardLoadMore())
7923
+ return;
7911
7924
  const nextPosition = Math.max(0, Math.min(selectableIndices.length - 1, basePosition + delta));
7912
7925
  const next = selectableIndices[nextPosition];
7913
7926
  this._setActive(next);
@@ -7920,6 +7933,33 @@
7920
7933
  this._selectRange(anchorIndex, next, { clear: !opts?.toggleKey });
7921
7934
  }
7922
7935
  }
7936
+ /** Return true only when boundary navigation is held for an in-flight/new page. */
7937
+ _requestKeyboardLoadMore() {
7938
+ if (!this._state.isOpen
7939
+ || !this._config.infiniteScroll.enabled
7940
+ || this._dataLoadingConfig.protocol !== 'acknowledged'
7941
+ || !this._dataRequestCoordinator
7942
+ || !this._acknowledgedReady)
7943
+ return false;
7944
+ if (this._acknowledgedProjectionPending || this._acknowledgedProjectionSettling)
7945
+ return true;
7946
+ const snapshot = this._dataRequestCoordinator.getSnapshot();
7947
+ if (!snapshot.connected)
7948
+ return false;
7949
+ if (snapshot.busy)
7950
+ return true;
7951
+ if (!snapshot.hasMore
7952
+ || snapshot.error !== null
7953
+ || snapshot.resourceLimit !== null
7954
+ || (snapshot.status !== 'succeeded' && snapshot.status !== 'empty'))
7955
+ return false;
7956
+ // Delegate identity, pagination, and in-flight protection to the coordinator.
7957
+ const actions = this._dataRequestCoordinator.requestLoadMore('manual');
7958
+ if (actions.length === 0)
7959
+ return false;
7960
+ this._dispatchAcknowledgedActions(actions);
7961
+ return true;
7962
+ }
7923
7963
  _setActive(index, reason = 'keyboard') {
7924
7964
  this._ensureRegistryCurrent();
7925
7965
  const record = this._getRecordByIndex(index);
@@ -8239,6 +8279,19 @@
8239
8279
  _updateInputDisplay() {
8240
8280
  const selectedEntries = this._getSelectedEntries();
8241
8281
  const selectedItems = selectedEntries.map(({ item }) => item);
8282
+ const nextKeys = new Set(selectedEntries.map(({ key }) => key));
8283
+ const previousKeys = this._lastRenderedChipKeys;
8284
+ let newlyAddedKey;
8285
+ if (previousKeys) {
8286
+ for (const key of nextKeys) {
8287
+ if (!previousKeys.has(key))
8288
+ newlyAddedKey = key;
8289
+ }
8290
+ }
8291
+ this._lastRenderedChipKeys = nextKeys;
8292
+ const previousChipScrollLeft = this._inputContent.scrollLeft;
8293
+ // Only a newly added selected key can initiate auto-scroll.
8294
+ this._cancelLatestChipScroll();
8242
8295
  const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
8243
8296
  if (selectedItems.length === 0) {
8244
8297
  this._input.value = '';
@@ -8314,8 +8367,87 @@
8314
8367
  if (this._acknowledgedPendingSearchQuery !== null) {
8315
8368
  this._input.value = this._acknowledgedPendingSearchQuery;
8316
8369
  }
8370
+ // Recreating the chip DOM must not reset manual scrolling. In particular,
8371
+ // remove/clear, config rerenders, and item refreshes are not scroll intents.
8372
+ if (this._config.selection.mode === 'multi') {
8373
+ this._inputContent.scrollLeft = previousChipScrollLeft;
8374
+ if (newlyAddedKey !== undefined)
8375
+ this._scheduleLatestChipScroll(newlyAddedKey);
8376
+ }
8317
8377
  this._syncClearControlState();
8318
8378
  }
8379
+ _cancelLatestChipScroll() {
8380
+ this._latestChipScrollGeneration += 1;
8381
+ if (this._latestChipScrollFrame !== null) {
8382
+ cancelAnimationFrame(this._latestChipScrollFrame);
8383
+ this._latestChipScrollFrame = null;
8384
+ }
8385
+ }
8386
+ _scheduleLatestChipScroll(key) {
8387
+ if (!this.isConnected
8388
+ || this._config.selection.mode !== 'multi'
8389
+ || this._config.multiSelectDisplay.mode !== 'horizontal'
8390
+ || this._config.multiSelectDisplay.scrollToLatestChip !== true
8391
+ || this._config.multiSelectDisplay.overflowX === 'hidden')
8392
+ return;
8393
+ const generation = this._latestChipScrollGeneration;
8394
+ this._latestChipScrollFrame = requestAnimationFrame(() => {
8395
+ this._latestChipScrollFrame = null;
8396
+ if (generation !== this._latestChipScrollGeneration
8397
+ || !this.isConnected
8398
+ || this._multiScrollDrag.active
8399
+ || this._config.selection.mode !== 'multi'
8400
+ || this._config.multiSelectDisplay.mode !== 'horizontal'
8401
+ || this._config.multiSelectDisplay.scrollToLatestChip !== true)
8402
+ return;
8403
+ // Match the canonical selected-key sequence, not option indices:
8404
+ // selected items may be reordered or detached from the loaded page.
8405
+ const entries = this._getSelectedEntries();
8406
+ const index = entries.findIndex((entry) => entry.key === key);
8407
+ if (index < 0)
8408
+ return;
8409
+ const chip = this._inputContent.querySelectorAll('.selection-badge')[index];
8410
+ if (!chip)
8411
+ return;
8412
+ const hostBounds = this._inputContent.getBoundingClientRect();
8413
+ const chipBounds = chip.getBoundingClientRect();
8414
+ const leftEdge = hostBounds.left + this._inputContent.clientLeft;
8415
+ const rightEdge = leftEdge + this._inputContent.clientWidth;
8416
+ let delta;
8417
+ if (chip.parentElement === this._inputContent && chip.offsetWidth > 0) {
8418
+ // getBoundingClientRect() includes the chip's entry animation transform.
8419
+ // Use layout dimensions so the scroll target remains correct while the
8420
+ // chip scales into its final size. In RTL layouts, offsetLeft and
8421
+ // scrollLeft are both negative relative to the inline-start origin, so
8422
+ // the same subtraction maps the chip into the viewport's coordinates.
8423
+ const chipLeft = chip.offsetLeft - this._inputContent.scrollLeft;
8424
+ const chipRight = chipLeft + chip.offsetWidth;
8425
+ delta = chipRight > this._inputContent.clientWidth
8426
+ ? chipRight - this._inputContent.clientWidth
8427
+ : chipLeft < 0
8428
+ ? chipLeft
8429
+ : 0;
8430
+ }
8431
+ else {
8432
+ // DOM test environments without layout use the measurable rectangle.
8433
+ delta = chipBounds.right > rightEdge
8434
+ ? chipBounds.right - rightEdge
8435
+ : chipBounds.left < leftEdge
8436
+ ? chipBounds.left - leftEdge
8437
+ : 0;
8438
+ }
8439
+ if (Math.abs(delta) < 0.5)
8440
+ return;
8441
+ const reducedMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
8442
+ const behavior = reducedMotion ? 'auto' : (this._config.multiSelectDisplay.scrollToLatestChipBehavior ?? 'smooth');
8443
+ if (typeof this._inputContent.scrollBy === 'function') {
8444
+ this._inputContent.scrollBy({ left: delta, behavior });
8445
+ }
8446
+ else {
8447
+ this._inputContent.scrollLeft += delta;
8448
+ }
8449
+ });
8450
+ }
8319
8451
  _isActivelyEditingSearchInput() {
8320
8452
  return this._config.searchable === true
8321
8453
  && this._state.isOpen
@@ -8581,12 +8713,13 @@
8581
8713
  classMap: true,
8582
8714
  optionRenderer: true,
8583
8715
  groupHeaderRenderer: true,
8716
+ emptyStateSlot: true,
8584
8717
  cssCustomProperties: true,
8585
8718
  shadowParts: true,
8586
8719
  globalStyleMirroring: true,
8587
8720
  },
8588
8721
  events: {
8589
- emitted: ['select', 'open', 'close', 'search', 'change', 'loadMore', 'remove', 'clear', 'error', 'diagnostic'],
8722
+ emitted: ['select', 'open', 'close', 'search', 'change', 'loadMore', 'remove', 'clear', 'emptyStateChange', 'error', 'diagnostic'],
8590
8723
  diagnosticEvent: true,
8591
8724
  },
8592
8725
  functionality: {
@@ -8602,6 +8735,36 @@
8602
8735
  limitations: this.getKnownLimitations(),
8603
8736
  };
8604
8737
  }
8738
+ /** Return a detached immutable snapshot of the currently rendered empty state. */
8739
+ getEmptyStateContext() {
8740
+ const context = this._emptyStateContext;
8741
+ return context ? Object.freeze({ query: context.query, reason: context.reason }) : null;
8742
+ }
8743
+ _syncEmptyStateContext(context) {
8744
+ const current = this._emptyStateContext;
8745
+ if (current?.query === context?.query
8746
+ && current?.reason === context?.reason
8747
+ && Boolean(current) === Boolean(context))
8748
+ return;
8749
+ this._emptyStateContext = context
8750
+ ? Object.freeze({ query: context.query, reason: context.reason })
8751
+ : null;
8752
+ const detail = Object.freeze({ context: this.getEmptyStateContext() });
8753
+ this._emit('emptyStateChange', detail);
8754
+ }
8755
+ _appendEmptyState(context) {
8756
+ const empty = document.createElement('div');
8757
+ empty.setAttribute('part', 'no-results');
8758
+ empty.className = 'empty-state';
8759
+ const slot = document.createElement('slot');
8760
+ slot.name = 'empty-state';
8761
+ slot.appendChild(document.createTextNode(context.reason === 'no-results'
8762
+ ? `No results found for "${context.query}"`
8763
+ : 'No options available'));
8764
+ empty.appendChild(slot);
8765
+ this._optionsContainer.appendChild(empty);
8766
+ this._syncEmptyStateContext(context);
8767
+ }
8605
8768
  _emitChange() {
8606
8769
  const selectedEntries = this._getSelectedEntries();
8607
8770
  const selectedItems = selectedEntries.map(({ item }) => item);
@@ -9767,11 +9930,27 @@
9767
9930
  this._state.isBusy = snapshot.busy;
9768
9931
  if (!snapshot.busy) {
9769
9932
  this._syncBusyPresentation();
9770
- if (!this._busyPresentationVisible) {
9771
- this._optionsContainer.querySelector('.busy-bucket')?.remove();
9933
+ const shouldReconcileEmptyState = snapshot.status === 'exhausted'
9934
+ && this._renderPlan.filteredKeys.length === 0;
9935
+ if (shouldReconcileEmptyState) {
9936
+ // The first projection rendered while logically busy, so the empty
9937
+ // state was intentionally suppressed. Reconcile only this terminal
9938
+ // empty case after settlement; keep any minimum-duration busy
9939
+ // presentation and its timer intact.
9940
+ this._renderOptions();
9941
+ const emptyStateRenderCycleId = this._renderCycleId;
9942
+ const emptyStateRenderCompleted = await this._waitForRenderCycle(emptyStateRenderCycleId);
9943
+ if (!emptyStateRenderCompleted) {
9944
+ throw new Error('Acknowledged empty-state render was superseded before completion');
9945
+ }
9946
+ }
9947
+ else {
9948
+ if (!this._busyPresentationVisible) {
9949
+ this._optionsContainer.querySelector('.busy-bucket')?.remove();
9950
+ }
9951
+ if (this._shouldRenderLoadMoreTrigger())
9952
+ this._addLoadMoreTrigger();
9772
9953
  }
9773
- if (this._shouldRenderLoadMoreTrigger())
9774
- this._addLoadMoreTrigger();
9775
9954
  }
9776
9955
  this._optionsContainer.scrollTop = this._acknowledgedProjectionScrollTop;
9777
9956
  this._syncListboxAccessibility();
@@ -10098,7 +10277,7 @@
10098
10277
  this._busyPresentationGeneration += 1;
10099
10278
  this._clearBusyPresentationHideTimer();
10100
10279
  this._busyPresentationVisible = this._config.busyBucket.enabled;
10101
- this._busyPresentationStartedAt = this._busyBucket?.isConnected ? Date.now() : null;
10280
+ this._busyPresentationStartedAt = Date.now();
10102
10281
  }
10103
10282
  _syncBusyPresentation() {
10104
10283
  if (!this._config.busyBucket.enabled) {
@@ -10266,6 +10445,7 @@
10266
10445
  this._optionsContainer.innerHTML = '';
10267
10446
  let filteredIndicesForScope = [];
10268
10447
  let guardedScrollTopToRestore = null;
10448
+ let renderedEmptyStateContext = null;
10269
10449
  // Ensure dropdown only contains options container (cleanup legacy direct children)
10270
10450
  // We need to preserve optionsContainer, so we can't just clear dropdown.innerHTML
10271
10451
  // But we can check if there are other children and remove them
@@ -10289,6 +10469,7 @@
10289
10469
  searching.setAttribute('part', 'loading');
10290
10470
  searching.textContent = 'Searching...';
10291
10471
  this._optionsContainer.appendChild(searching);
10472
+ this._syncEmptyStateContext(null);
10292
10473
  this._resolveRenderCycle(renderCycleId, true);
10293
10474
  return;
10294
10475
  }
@@ -10392,17 +10573,11 @@
10392
10573
  const filteredIndices = this._renderPlan.filteredIndices;
10393
10574
  this._refreshVirtualizationScope(filteredIndicesForScope);
10394
10575
  this._appendGuardedTopSpacer();
10395
- if (filteredIndices.length === 0 && !this._state.isBusy) {
10396
- const empty = document.createElement('div');
10397
- empty.setAttribute('part', 'no-results');
10398
- empty.className = 'empty-state';
10399
- if (query) {
10400
- empty.textContent = `No results found for "${this._state.searchQuery}"`;
10401
- }
10402
- else {
10403
- empty.textContent = 'No options available';
10404
- }
10405
- this._optionsContainer.appendChild(empty);
10576
+ if (this._renderPlan.filteredKeys.length === 0 && !this._state.isBusy) {
10577
+ renderedEmptyStateContext = this._state.searchQuery
10578
+ ? { query: this._state.searchQuery, reason: 'no-results' }
10579
+ : { query: '', reason: 'no-options' };
10580
+ this._appendEmptyState(renderedEmptyStateContext);
10406
10581
  }
10407
10582
  else {
10408
10583
  const shouldIncrementalRender = !this._guardedVirtualizer
@@ -10464,6 +10639,7 @@
10464
10639
  }
10465
10640
  };
10466
10641
  renderChunk();
10642
+ this._syncEmptyStateContext(null);
10467
10643
  return;
10468
10644
  }
10469
10645
  filteredIndices.forEach((itemIndex) => {
@@ -10488,6 +10664,7 @@
10488
10664
  if (guardedScrollTopToRestore !== null) {
10489
10665
  this._optionsContainer.scrollTop = guardedScrollTopToRestore;
10490
10666
  }
10667
+ this._syncEmptyStateContext(renderedEmptyStateContext);
10491
10668
  this._syncRenderedOptionAccessibility();
10492
10669
  this._syncLegacyActiveAria();
10493
10670
  this._syncGuardedOptionResizeTargets();