@smilodon/core 1.20.0 → 1.22.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
@@ -2944,6 +2944,15 @@
2944
2944
  pointer-events: none;
2945
2945
  }
2946
2946
 
2947
+ .remove-button-icon > [data-smilodon-fallback-icon],
2948
+ .remove-button-icon > [data-smilodon-react-icon-root] {
2949
+ display: contents;
2950
+ }
2951
+
2952
+ .remove-button-icon[data-smilodon-icon-owner="react"] > [data-smilodon-fallback-icon] {
2953
+ display: none;
2954
+ }
2955
+
2947
2956
  .remove-button-icon svg {
2948
2957
  width: 100%;
2949
2958
  height: 100%;
@@ -3066,15 +3075,20 @@
3066
3075
  const removeIcon = document.createElement('span');
3067
3076
  removeIcon.className = 'remove-button-icon';
3068
3077
  removeIcon.setAttribute('part', 'chip-remove-icon');
3078
+ const fallbackIcon = document.createElement('span');
3079
+ fallbackIcon.setAttribute('data-smilodon-fallback-icon', '');
3080
+ const portalRoot = document.createElement('span');
3081
+ portalRoot.setAttribute('data-smilodon-react-icon-root', '');
3069
3082
  const iconMarkup = this._config.removeButtonIcon && this._config.removeButtonIcon.trim()
3070
3083
  ? this._config.removeButtonIcon
3071
3084
  : '×';
3072
3085
  if (iconMarkup.trim().startsWith('<')) {
3073
- removeIcon.innerHTML = iconMarkup;
3086
+ fallbackIcon.innerHTML = iconMarkup;
3074
3087
  }
3075
3088
  else {
3076
- removeIcon.textContent = iconMarkup;
3089
+ fallbackIcon.textContent = iconMarkup;
3077
3090
  }
3091
+ removeIcon.append(fallbackIcon, portalRoot);
3078
3092
  this._removeButton.appendChild(removeIcon);
3079
3093
  this._container.appendChild(this._removeButton);
3080
3094
  }
@@ -4449,6 +4463,15 @@
4449
4463
  super();
4450
4464
  this._dataLoadingConfig = { ...LEGACY_DATA_LOADING_CONFIG };
4451
4465
  this._hasLastInvalidDataLoadingCandidate = false;
4466
+ this._selectedLabelTooltipTrigger = null;
4467
+ this._selectedLabelTooltipPointerTrigger = null;
4468
+ this._selectedLabelTooltipFocusTrigger = null;
4469
+ this._selectedLabelTooltipPointerInside = false;
4470
+ this._selectedLabelTooltipDismissedTrigger = null;
4471
+ this._selectedLabelTooltipHideTimer = null;
4472
+ this._selectedLabelTooltipResizeObserver = null;
4473
+ this._selectedLabelTooltipMeasureFrame = null;
4474
+ this._selectedLabelKeys = new WeakMap();
4452
4475
  this._busyPresentationVisible = false;
4453
4476
  this._busyPresentationStartedAt = null;
4454
4477
  this._busyPresentationGeneration = 0;
@@ -4548,6 +4571,9 @@
4548
4571
  this._rangeAnchorKey = null;
4549
4572
  /** Chip identity is tracked independently of dropdown option positions. */
4550
4573
  this._lastRenderedChipKeys = null;
4574
+ /** Stable presentation nodes keyed by the engine's selected-item identity. */
4575
+ this._chipElements = new Map();
4576
+ this._chipExitStates = new Map();
4551
4577
  this._latestChipScrollGeneration = 0;
4552
4578
  this._latestChipScrollFrame = null;
4553
4579
  this._customOptionBoundElements = new WeakSet();
@@ -4633,6 +4659,14 @@
4633
4659
  this._handleClose('outside-pointer');
4634
4660
  }
4635
4661
  };
4662
+ this._scheduleSelectedLabelTooltipMeasure = () => {
4663
+ if (this._selectedLabelTooltipMeasureFrame !== null || !this.isConnected)
4664
+ return;
4665
+ this._selectedLabelTooltipMeasureFrame = this._requestLifecycleFrame(() => {
4666
+ this._selectedLabelTooltipMeasureFrame = null;
4667
+ this._refreshSelectedLabelTooltipMeasurements();
4668
+ });
4669
+ };
4636
4670
  this._shadow = this.attachShadow({ mode: 'open' });
4637
4671
  this._uniqueId = `enhanced-select-${Math.random().toString(36).substr(2, 9)}`;
4638
4672
  this._rendererHelpers = this._buildRendererHelpers();
@@ -4667,6 +4701,8 @@
4667
4701
  this._inputContainer = this._createInputContainer();
4668
4702
  this._inputContent = this._createInputContent();
4669
4703
  this._input = this._createInput();
4704
+ this._selectedLabelTooltip = this._createSelectedLabelTooltip();
4705
+ this._selectedLabelTooltipTextMeasure = this._createSelectedLabelTooltipTextMeasure();
4670
4706
  this._arrowContainer = this._createArrowContainer();
4671
4707
  this._clearControl = this._createClearControl();
4672
4708
  this._dropdown = this._createDropdown();
@@ -4686,15 +4722,29 @@
4686
4722
  }
4687
4723
  connectedCallback() {
4688
4724
  this._hasEverConnected = true;
4725
+ // Adapter capability marker without adding a field to the public class
4726
+ // declaration. React-node remove icons require the isolated root contract.
4727
+ this.setAttribute('data-smilodon-react-icon-portals', 'isolated-v1');
4689
4728
  // register instance
4690
4729
  EnhancedSelect._instances.add(this);
4691
4730
  this._runEngineEffects(this._engine.dispatch({ type: 'CONNECT' }));
4692
4731
  this._runEngineEffects(this._engine.dispatch({ type: 'DISABLED_CHANGED', disabled: !this._config.enabled }));
4693
4732
  this._connectionController?.abort();
4694
4733
  this._connectionController = new AbortController();
4734
+ this._observeSelectedLabelTooltipTargets();
4735
+ this._inputContent.addEventListener('scroll', this._scheduleSelectedLabelTooltipMeasure, { passive: true });
4695
4736
  document.addEventListener('pointerdown', this._onDocumentPointerDown, {
4696
4737
  signal: this._connectionController.signal,
4697
4738
  });
4739
+ window.addEventListener('resize', this._scheduleSelectedLabelTooltipMeasure, {
4740
+ signal: this._connectionController.signal,
4741
+ passive: true,
4742
+ });
4743
+ window.addEventListener('scroll', this._scheduleSelectedLabelTooltipMeasure, {
4744
+ signal: this._connectionController.signal,
4745
+ passive: true,
4746
+ capture: true,
4747
+ });
4698
4748
  // WORKAROUND: Force display style on host element for Angular compatibility
4699
4749
  // Angular's rendering seems to not apply :host styles correctly in some cases
4700
4750
  // Must be done in connectedCallback when element is attached to DOM
@@ -4745,6 +4795,7 @@
4745
4795
  disconnectedCallback() {
4746
4796
  this._cancelOpenSelectedScroll();
4747
4797
  this._cancelLatestChipScroll();
4798
+ this._clearChipPresentation();
4748
4799
  this._cancelBusyPresentation();
4749
4800
  if (this._dataRequestCoordinator) {
4750
4801
  const actions = this._dataRequestCoordinator.disconnect();
@@ -4765,8 +4816,16 @@
4765
4816
  this._handleClose('disconnect');
4766
4817
  }
4767
4818
  this._runEngineEffects(this._engine.dispatch({ type: 'DISCONNECT' }));
4819
+ this._inputContent.removeEventListener('scroll', this._scheduleSelectedLabelTooltipMeasure);
4768
4820
  this._connectionController?.abort();
4769
4821
  this._connectionController = undefined;
4822
+ this._hideSelectedLabelTooltip(true);
4823
+ this._selectedLabelTooltipResizeObserver?.disconnect();
4824
+ this._selectedLabelTooltipResizeObserver = null;
4825
+ if (this._selectedLabelTooltipMeasureFrame !== null) {
4826
+ cancelAnimationFrame(this._selectedLabelTooltipMeasureFrame);
4827
+ this._selectedLabelTooltipMeasureFrame = null;
4828
+ }
4770
4829
  this._asyncGeneration += 1;
4771
4830
  this._cancelGuardedVirtualWindowUpdate();
4772
4831
  this._clearLifecycleTasks();
@@ -5698,12 +5757,25 @@
5698
5757
  }
5699
5758
  _setIconContent(target, markup, fallback) {
5700
5759
  const content = markup && markup.trim() ? markup : fallback;
5701
- target.innerHTML = '';
5760
+ let fallbackTarget = target.querySelector(':scope > [data-smilodon-fallback-icon]');
5761
+ if (!fallbackTarget) {
5762
+ fallbackTarget = document.createElement('span');
5763
+ fallbackTarget.setAttribute('data-smilodon-fallback-icon', '');
5764
+ target.prepend(fallbackTarget);
5765
+ }
5766
+ if (!target.querySelector(':scope > [data-smilodon-react-icon-root]')) {
5767
+ const portalRoot = document.createElement('span');
5768
+ portalRoot.setAttribute('data-smilodon-react-icon-root', '');
5769
+ target.append(portalRoot);
5770
+ }
5771
+ // Only Core's fallback subtree is replaceable. React may reconcile a
5772
+ // separate portal root beneath this stable icon target.
5773
+ fallbackTarget.replaceChildren();
5702
5774
  if (content.trim().startsWith('<')) {
5703
- target.innerHTML = content;
5775
+ fallbackTarget.innerHTML = content;
5704
5776
  return;
5705
5777
  }
5706
- target.textContent = content;
5778
+ fallbackTarget.textContent = content;
5707
5779
  }
5708
5780
  _syncInputContainerMode() {
5709
5781
  if (!this._inputContainer || !this._input)
@@ -5918,6 +5990,35 @@
5918
5990
  input.setAttribute('aria-haspopup', 'listbox');
5919
5991
  return input;
5920
5992
  }
5993
+ _createSelectedLabelTooltip() {
5994
+ const tooltip = document.createElement('div');
5995
+ tooltip.id = `${this._uniqueId}-selected-label-tooltip`;
5996
+ tooltip.className = 'selected-label-tooltip';
5997
+ tooltip.setAttribute('role', 'tooltip');
5998
+ tooltip.hidden = true;
5999
+ return tooltip;
6000
+ }
6001
+ _createSelectedLabelTooltipTextMeasure() {
6002
+ const measure = document.createElement('span');
6003
+ measure.setAttribute('aria-hidden', 'true');
6004
+ measure.dataset.selectInternal = 'tooltip-measure';
6005
+ measure.style.cssText = [
6006
+ 'all: initial',
6007
+ 'position: fixed',
6008
+ 'left: -100000px',
6009
+ 'top: 0',
6010
+ 'display: inline-block',
6011
+ 'width: max-content',
6012
+ 'height: auto',
6013
+ 'padding: 0',
6014
+ 'margin: 0',
6015
+ 'border: 0',
6016
+ 'white-space: pre',
6017
+ 'visibility: hidden',
6018
+ 'pointer-events: none',
6019
+ ].join(';');
6020
+ return measure;
6021
+ }
5921
6022
  _createDropdown() {
5922
6023
  const dropdown = document.createElement('div');
5923
6024
  dropdown.className = 'select-dropdown';
@@ -6012,6 +6113,8 @@
6012
6113
  if (this._errorDescription) {
6013
6114
  this._shadow.appendChild(this._errorDescription);
6014
6115
  }
6116
+ this._shadow.appendChild(this._selectedLabelTooltip);
6117
+ this._shadow.appendChild(this._selectedLabelTooltipTextMeasure);
6015
6118
  // Set ARIA relationships
6016
6119
  const listboxId = `${this._uniqueId}-listbox`;
6017
6120
  this._dropdown.id = listboxId;
@@ -6026,6 +6129,12 @@
6026
6129
  ELEGANT SELECT COMPONENT — Refined Design System
6027
6130
  Formal aesthetics with sophisticated microinteractions
6028
6131
  ═══════════════════════════════════════════════════════════════════════════ */
6132
+
6133
+ @layer smilodon-input-content-fallback {
6134
+ .input-container.input-container--multi:not([data-multi-scroll-mode="horizontal"]) .input-content {
6135
+ padding-inline-end: var(--select-multi-input-end-gap, 12px);
6136
+ }
6137
+ }
6029
6138
 
6030
6139
  :host {
6031
6140
  --select-primary: #1a1a2e;
@@ -6050,6 +6159,10 @@
6050
6159
  --select-transition-smooth: 250ms cubic-bezier(0.4, 0, 0.2, 1);
6051
6160
  --select-transition-bounce: 350ms cubic-bezier(0.34, 1.56, 0.64, 1);
6052
6161
  --select-badge-animation: badgeEnter 300ms cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
6162
+ --select-badge-exit-from-opacity: 1;
6163
+ --select-badge-exit-from-transform: scale(1) translateY(0);
6164
+ --select-badge-exit-to-opacity: 0;
6165
+ --select-badge-exit-to-transform: scale(0.8) translateY(-4px);
6053
6166
  --select-badge-enter-from-opacity: 0;
6054
6167
  --select-badge-enter-from-transform: scale(0.8) translateY(-4px);
6055
6168
  --select-badge-enter-to-opacity: 1;
@@ -6251,7 +6364,13 @@
6251
6364
  transform var(--select-transition-bounce),
6252
6365
  box-shadow var(--select-transition-fast),
6253
6366
  background var(--select-transition-fast);
6254
- animation: var(--select-badge-animation);
6367
+ animation: var(--select-badge-enter-animation, var(--select-badge-animation));
6368
+ }
6369
+
6370
+ .selection-badge[data-motion="exiting"] {
6371
+ animation: var(--select-badge-exit-animation, none);
6372
+ pointer-events: none;
6373
+ transition: none;
6255
6374
  }
6256
6375
 
6257
6376
  @keyframes badgeEnter {
@@ -6264,6 +6383,17 @@
6264
6383
  transform: var(--select-badge-enter-to-transform);
6265
6384
  }
6266
6385
  }
6386
+
6387
+ @keyframes badgeExit {
6388
+ 0% {
6389
+ opacity: var(--select-badge-exit-from-opacity, 1);
6390
+ transform: var(--select-badge-exit-from-transform, scale(1) translateY(0));
6391
+ }
6392
+ 100% {
6393
+ opacity: var(--select-badge-exit-to-opacity, 0);
6394
+ transform: var(--select-badge-exit-to-transform, scale(0.8) translateY(-4px));
6395
+ }
6396
+ }
6267
6397
 
6268
6398
  .selection-badge:hover {
6269
6399
  background: var(--select-badge-hover-bg, var(--select-badge-bg, linear-gradient(135deg, var(--select-accent) 0%, var(--select-primary-light) 100%)));
@@ -6336,6 +6466,15 @@
6336
6466
  pointer-events: none;
6337
6467
  }
6338
6468
 
6469
+ .badge-remove-icon > [data-smilodon-fallback-icon],
6470
+ .badge-remove-icon > [data-smilodon-react-icon-root] {
6471
+ display: contents;
6472
+ }
6473
+
6474
+ .badge-remove-icon[data-smilodon-icon-owner="react"] > [data-smilodon-fallback-icon] {
6475
+ display: none;
6476
+ }
6477
+
6339
6478
  .badge-remove-icon svg {
6340
6479
  width: 100%;
6341
6480
  height: 100%;
@@ -6376,6 +6515,29 @@
6376
6515
  position: relative;
6377
6516
  width: 100%;
6378
6517
  }
6518
+
6519
+ .selected-label-tooltip {
6520
+ position: fixed;
6521
+ z-index: 2147483000;
6522
+ box-sizing: border-box;
6523
+ width: max-content;
6524
+ max-width: min(24rem, calc(100vw - 16px));
6525
+ padding: 8px 10px;
6526
+ border: 1px solid rgba(255, 255, 255, 0.18);
6527
+ border-radius: 6px;
6528
+ background: #18202e;
6529
+ color: #fff;
6530
+ box-shadow: 0 4px 16px rgba(0, 0, 0, 0.24);
6531
+ font: 500 13px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
6532
+ white-space: normal;
6533
+ overflow-wrap: anywhere;
6534
+ pointer-events: auto;
6535
+ direction: inherit;
6536
+ }
6537
+
6538
+ .selected-label-tooltip[hidden] {
6539
+ display: none;
6540
+ }
6379
6541
 
6380
6542
  .input-container {
6381
6543
  position: relative;
@@ -6424,6 +6586,7 @@
6424
6586
  overflow-y: var(--select-input-overflow-y, hidden);
6425
6587
  scrollbar-width: thin;
6426
6588
  box-sizing: border-box;
6589
+ padding: var(--select-input-content-padding, revert-layer);
6427
6590
  }
6428
6591
 
6429
6592
  .input-container:hover {
@@ -6449,7 +6612,6 @@
6449
6612
  align-content: var(--select-multi-input-align-content, var(--select-multi-input-align-content-config, flex-start));
6450
6613
  overflow-x: var(--select-multi-input-overflow-x, var(--select-multi-input-overflow-x-config, hidden));
6451
6614
  overflow-y: var(--select-multi-input-overflow-y, var(--select-multi-input-overflow-y-config, auto));
6452
- padding-inline-end: var(--select-multi-input-end-gap, 12px);
6453
6615
  }
6454
6616
 
6455
6617
  .input-container.input-container--multi .input-content .selection-badge {
@@ -6487,7 +6649,7 @@
6487
6649
  overflow-y: hidden;
6488
6650
  overscroll-behavior-x: contain;
6489
6651
  overscroll-behavior-y: none;
6490
- padding-inline-end: 0;
6652
+ padding: var(--select-input-content-padding, 0);
6491
6653
  scroll-padding-inline-end: var(--select-multi-action-area-width, var(--select-arrow-width, 42px));
6492
6654
  }
6493
6655
 
@@ -6590,6 +6752,7 @@
6590
6752
  bottom: 0;
6591
6753
  width: var(--select-arrow-width, var(--select-arrow-size, 42px));
6592
6754
  height: var(--select-arrow-height, var(--select-arrow-size, auto));
6755
+ padding: var(--select-dropdown-arrow-container-padding, 0);
6593
6756
  display: flex;
6594
6757
  align-items: center;
6595
6758
  justify-content: center;
@@ -7469,7 +7632,24 @@
7469
7632
  this._shadow.appendChild(style);
7470
7633
  }
7471
7634
  }
7635
+ _isArrowOrClearControlEvent(event) {
7636
+ const path = event.composedPath();
7637
+ return ((this._arrowContainer != null && path.includes(this._arrowContainer)) ||
7638
+ (this._clearControl != null && path.includes(this._clearControl)));
7639
+ }
7472
7640
  _attachEventListeners() {
7641
+ this._shadow.addEventListener('pointerover', (event) => this._handleSelectedLabelPointerOver(event));
7642
+ this._shadow.addEventListener('pointerout', (event) => this._handleSelectedLabelPointerOut(event));
7643
+ this._shadow.addEventListener('focusin', (event) => this._handleSelectedLabelFocusIn(event));
7644
+ this._shadow.addEventListener('focusout', (event) => this._handleSelectedLabelFocusOut(event));
7645
+ this._shadow.addEventListener('keydown', (event) => {
7646
+ const keyboardEvent = event;
7647
+ if (keyboardEvent.key !== 'Escape' || this._selectedLabelTooltip.hidden)
7648
+ return;
7649
+ keyboardEvent.preventDefault();
7650
+ keyboardEvent.stopPropagation();
7651
+ this._dismissSelectedLabelTooltip();
7652
+ });
7473
7653
  // Arrow click handler
7474
7654
  if (this._arrowContainer) {
7475
7655
  this._boundArrowClick = (e) => {
@@ -7505,13 +7685,11 @@
7505
7685
  // Prevent propagation to document click listener but do NOT preventDefault.
7506
7686
  // Allow default so browser events (click) on newly opened options still fire.
7507
7687
  // e.stopPropagation(); // BUG: By stopping propagation here, the document click listener doesn't see it, which is fine for not closing it. But be very careful.
7508
- const target = e.target;
7509
7688
  if (!this._config.enabled)
7510
7689
  return;
7511
- if (target && target.closest('.dropdown-arrow-container'))
7512
- return;
7513
- if (target && target.closest('.clear-control-button'))
7690
+ if (this._isArrowOrClearControlEvent(e))
7514
7691
  return;
7692
+ const target = e.target;
7515
7693
  if (this._isPointerOnInputScrollbar(e)) {
7516
7694
  this._suppressNextOpenClick = true;
7517
7695
  return;
@@ -7558,7 +7736,6 @@
7558
7736
  }
7559
7737
  });
7560
7738
  this._inputContainer.addEventListener('click', (e) => {
7561
- const target = e.target;
7562
7739
  const isHorizontalMultiMode = this._config.selection.mode === 'multi'
7563
7740
  && (this._config.multiSelectDisplay?.mode ?? 'wrap') === 'horizontal';
7564
7741
  if (!this._config.enabled || !isHorizontalMultiMode) {
@@ -7566,7 +7743,7 @@
7566
7743
  this._multiScrollDrag.moved = false;
7567
7744
  return;
7568
7745
  }
7569
- if (target && target.closest('.dropdown-arrow-container, .clear-control-button')) {
7746
+ if (this._isArrowOrClearControlEvent(e)) {
7570
7747
  this._suppressNextOpenClick = false;
7571
7748
  this._multiScrollDrag.moved = false;
7572
7749
  return;
@@ -7641,6 +7818,9 @@
7641
7818
  if (!this._config.searchable)
7642
7819
  return;
7643
7820
  const query = e.target.value;
7821
+ if (this._selectedLabelTooltipDismissedTrigger === this._input) {
7822
+ this._selectedLabelTooltipDismissedTrigger = null;
7823
+ }
7644
7824
  this._handleSearch(query);
7645
7825
  });
7646
7826
  // If the user presses down inside the options container we should
@@ -7803,6 +7983,340 @@
7803
7983
  return index === undefined ? null : this._getOptionElementByIndex(index);
7804
7984
  },
7805
7985
  });
7986
+ this._syncSelectedLabelTooltipDescription();
7987
+ }
7988
+ _selectedLabelTriggerFromTarget(target) {
7989
+ if (!(target instanceof HTMLElement))
7990
+ return null;
7991
+ if (target === this._input)
7992
+ return target;
7993
+ const label = target.closest('.selection-badge-label');
7994
+ return label && this._inputContent.contains(label) ? label : null;
7995
+ }
7996
+ _getSelectedLabelForTrigger(trigger) {
7997
+ const entries = this._getSelectedEntries();
7998
+ if (trigger === this._input) {
7999
+ if (this._config.selection.mode !== 'single')
8000
+ return null;
8001
+ const item = entries[0]?.item;
8002
+ if (item === undefined)
8003
+ return null;
8004
+ const label = this._getItemLabel(item);
8005
+ return this._input.value === label ? label : null;
8006
+ }
8007
+ if (!trigger.classList.contains('selection-badge-label'))
8008
+ return null;
8009
+ const key = this._selectedLabelKeys.get(trigger);
8010
+ if (key === undefined)
8011
+ return null;
8012
+ const entry = entries.find((candidate) => candidate.key === key);
8013
+ if (!entry)
8014
+ return null;
8015
+ const label = this._getItemLabel(entry.item);
8016
+ return trigger.textContent === label ? label : null;
8017
+ }
8018
+ _isSelectedLabelTruncated(trigger) {
8019
+ if (!trigger.isConnected)
8020
+ return false;
8021
+ return trigger.clientWidth > 0 && trigger.scrollWidth > trigger.clientWidth + 1;
8022
+ }
8023
+ _getSelectedLabelTooltipContent(trigger) {
8024
+ if (trigger === this._input && this._config.searchable) {
8025
+ const visibleText = this._input.value;
8026
+ const pendingQuery = this._acknowledgedPendingSearchQuery;
8027
+ const committedQuery = this._state.searchQuery;
8028
+ const isPendingQuery = pendingQuery !== null && pendingQuery.length > 0 && visibleText === pendingQuery;
8029
+ const isCommittedQuery = committedQuery.length > 0 && visibleText === committedQuery;
8030
+ if (visibleText.length > 0 && (isPendingQuery || isCommittedQuery)) {
8031
+ return { kind: 'search-query', text: visibleText };
8032
+ }
8033
+ }
8034
+ const selectedLabel = this._getSelectedLabelForTrigger(trigger);
8035
+ return selectedLabel === null ? null : { kind: 'selected-label', text: selectedLabel };
8036
+ }
8037
+ _isSelectedLabelTooltipContentTruncated(trigger, content) {
8038
+ if (trigger !== this._input)
8039
+ return this._isSelectedLabelTruncated(trigger);
8040
+ if (!trigger.isConnected || !content.text)
8041
+ return false;
8042
+ const style = getComputedStyle(this._input);
8043
+ const measure = this._selectedLabelTooltipTextMeasure;
8044
+ measure.textContent = content.text;
8045
+ const mirroredProperties = [
8046
+ 'font',
8047
+ 'font-family',
8048
+ 'font-size',
8049
+ 'font-style',
8050
+ 'font-weight',
8051
+ 'font-stretch',
8052
+ 'font-variant',
8053
+ 'font-kerning',
8054
+ 'font-feature-settings',
8055
+ 'font-variation-settings',
8056
+ 'letter-spacing',
8057
+ 'word-spacing',
8058
+ 'text-transform',
8059
+ 'direction',
8060
+ 'unicode-bidi',
8061
+ ];
8062
+ for (const property of mirroredProperties) {
8063
+ const value = style.getPropertyValue(property);
8064
+ if (value)
8065
+ measure.style.setProperty(property, value);
8066
+ }
8067
+ const contentWidth = measure.getBoundingClientRect().width;
8068
+ const inputStyle = style;
8069
+ const horizontalPadding = Number.parseFloat(inputStyle.paddingLeft) + Number.parseFloat(inputStyle.paddingRight);
8070
+ const textIndent = Number.parseFloat(inputStyle.textIndent) || 0;
8071
+ const availableWidth = this._input.clientWidth - (Number.isFinite(horizontalPadding) ? horizontalPadding : 0) - textIndent;
8072
+ return availableWidth > 0 && contentWidth > availableWidth + 1;
8073
+ }
8074
+ _handleSelectedLabelPointerOver(event) {
8075
+ const target = event.target;
8076
+ if (target?.closest('.selected-label-tooltip') === this._selectedLabelTooltip) {
8077
+ this._selectedLabelTooltipPointerInside = true;
8078
+ this._clearSelectedLabelTooltipHideTimer();
8079
+ return;
8080
+ }
8081
+ const trigger = this._selectedLabelTriggerFromTarget(target);
8082
+ if (!trigger || (event.relatedTarget instanceof Node && trigger.contains(event.relatedTarget)))
8083
+ return;
8084
+ this._selectedLabelTooltipPointerTrigger = trigger;
8085
+ this._selectedLabelTooltipDismissedTrigger = null;
8086
+ this._showSelectedLabelTooltip(trigger);
8087
+ }
8088
+ _handleSelectedLabelPointerOut(event) {
8089
+ const target = event.target;
8090
+ if (target === this._selectedLabelTooltip || this._selectedLabelTooltip.contains(target)) {
8091
+ if (event.relatedTarget instanceof Node && this._selectedLabelTooltip.contains(event.relatedTarget))
8092
+ return;
8093
+ this._selectedLabelTooltipPointerInside = false;
8094
+ this._scheduleSelectedLabelTooltipHide();
8095
+ return;
8096
+ }
8097
+ const trigger = this._selectedLabelTriggerFromTarget(target);
8098
+ if (!trigger || (event.relatedTarget instanceof Node && trigger.contains(event.relatedTarget)))
8099
+ return;
8100
+ if (this._selectedLabelTooltipPointerTrigger === trigger) {
8101
+ this._selectedLabelTooltipPointerTrigger = null;
8102
+ if (this._selectedLabelTooltipDismissedTrigger === trigger) {
8103
+ this._selectedLabelTooltipDismissedTrigger = null;
8104
+ }
8105
+ }
8106
+ if (event.relatedTarget === this._selectedLabelTooltip) {
8107
+ this._selectedLabelTooltipPointerInside = true;
8108
+ this._clearSelectedLabelTooltipHideTimer();
8109
+ return;
8110
+ }
8111
+ this._scheduleSelectedLabelTooltipHide();
8112
+ }
8113
+ _handleSelectedLabelFocusIn(event) {
8114
+ const trigger = this._selectedLabelTriggerFromTarget(event.target);
8115
+ if (!trigger)
8116
+ return;
8117
+ this._selectedLabelTooltipFocusTrigger = trigger;
8118
+ this._selectedLabelTooltipDismissedTrigger = null;
8119
+ this._showSelectedLabelTooltip(trigger);
8120
+ }
8121
+ _handleSelectedLabelFocusOut(event) {
8122
+ const trigger = this._selectedLabelTriggerFromTarget(event.target);
8123
+ if (!trigger || this._selectedLabelTooltipFocusTrigger !== trigger)
8124
+ return;
8125
+ this._selectedLabelTooltipFocusTrigger = null;
8126
+ if (this._selectedLabelTooltipDismissedTrigger === trigger) {
8127
+ this._selectedLabelTooltipDismissedTrigger = null;
8128
+ }
8129
+ this._scheduleSelectedLabelTooltipHide();
8130
+ }
8131
+ _showSelectedLabelTooltip(trigger) {
8132
+ if (this._selectedLabelTooltipDismissedTrigger === trigger)
8133
+ return;
8134
+ if (!trigger.isConnected || !this._isSelectedLabelVisibleInViewport(trigger)) {
8135
+ if (this._selectedLabelTooltipTrigger === trigger)
8136
+ this._hideSelectedLabelTooltip();
8137
+ return;
8138
+ }
8139
+ const content = this._getSelectedLabelTooltipContent(trigger);
8140
+ if (content === null || !this._isSelectedLabelTooltipContentTruncated(trigger, content)) {
8141
+ if (trigger.classList.contains('selection-badge-label'))
8142
+ trigger.tabIndex = -1;
8143
+ if (this._selectedLabelTooltipTrigger === trigger)
8144
+ this._hideSelectedLabelTooltip();
8145
+ return;
8146
+ }
8147
+ if (trigger.classList.contains('selection-badge-label'))
8148
+ trigger.tabIndex = 0;
8149
+ if (this._selectedLabelTooltipTrigger !== trigger) {
8150
+ this._removeSelectedLabelTooltipDescription(this._selectedLabelTooltipTrigger);
8151
+ this._selectedLabelTooltipTrigger = trigger;
8152
+ }
8153
+ this._selectedLabelTooltip.textContent = content.text;
8154
+ this._selectedLabelTooltip.hidden = false;
8155
+ this._positionSelectedLabelTooltip(trigger);
8156
+ this._syncSelectedLabelTooltipDescription();
8157
+ this._clearSelectedLabelTooltipHideTimer();
8158
+ }
8159
+ _positionSelectedLabelTooltip(trigger) {
8160
+ if (this._selectedLabelTooltip.hidden || !trigger.isConnected)
8161
+ return;
8162
+ const triggerRect = trigger.getBoundingClientRect();
8163
+ const tooltip = this._selectedLabelTooltip;
8164
+ tooltip.style.maxWidth = `min(24rem, calc(100vw - 16px))`;
8165
+ tooltip.style.direction = getComputedStyle(trigger).direction;
8166
+ const tooltipRect = tooltip.getBoundingClientRect();
8167
+ const viewportWidth = document.documentElement.clientWidth || window.innerWidth;
8168
+ const viewportHeight = document.documentElement.clientHeight || window.innerHeight;
8169
+ const rtl = getComputedStyle(trigger).direction === 'rtl';
8170
+ const desiredLeft = rtl ? triggerRect.right - tooltipRect.width : triggerRect.left;
8171
+ const left = Math.max(8, Math.min(desiredLeft, viewportWidth - tooltipRect.width - 8));
8172
+ const above = triggerRect.top - tooltipRect.height - 6;
8173
+ const top = above >= 8 ? above : Math.min(triggerRect.bottom + 6, viewportHeight - tooltipRect.height - 8);
8174
+ tooltip.style.left = `${left}px`;
8175
+ tooltip.style.top = `${Math.max(8, top)}px`;
8176
+ }
8177
+ _syncSelectedLabelTooltipDescription() {
8178
+ const trigger = this._selectedLabelTooltipTrigger;
8179
+ this._removeSelectedLabelTooltipDescription(trigger);
8180
+ if (trigger && !this._selectedLabelTooltip.hidden) {
8181
+ const ids = new Set((trigger.getAttribute('aria-describedby') ?? '').split(/\s+/).filter(Boolean));
8182
+ ids.add(this._selectedLabelTooltip.id);
8183
+ trigger.setAttribute('aria-describedby', [...ids].join(' '));
8184
+ }
8185
+ }
8186
+ _removeSelectedLabelTooltipDescription(trigger) {
8187
+ if (!trigger)
8188
+ return;
8189
+ const ids = (trigger.getAttribute('aria-describedby') ?? '').split(/\s+/).filter(Boolean);
8190
+ const remaining = ids.filter((id) => id !== this._selectedLabelTooltip.id);
8191
+ if (remaining.length)
8192
+ trigger.setAttribute('aria-describedby', remaining.join(' '));
8193
+ else
8194
+ trigger.removeAttribute('aria-describedby');
8195
+ }
8196
+ _dismissSelectedLabelTooltip() {
8197
+ this._selectedLabelTooltipDismissedTrigger = this._selectedLabelTooltipFocusTrigger
8198
+ ?? this._selectedLabelTooltipTrigger
8199
+ ?? this._selectedLabelTooltipPointerTrigger;
8200
+ this._hideSelectedLabelTooltip();
8201
+ }
8202
+ _hideSelectedLabelTooltip(clearInteractionState = false) {
8203
+ this._clearSelectedLabelTooltipHideTimer();
8204
+ this._removeSelectedLabelTooltipDescription(this._selectedLabelTooltipTrigger);
8205
+ this._selectedLabelTooltipTrigger = null;
8206
+ this._selectedLabelTooltip.hidden = true;
8207
+ this._selectedLabelTooltipTextMeasure.textContent = '';
8208
+ if (clearInteractionState) {
8209
+ this._selectedLabelTooltipPointerTrigger = null;
8210
+ this._selectedLabelTooltipFocusTrigger = null;
8211
+ this._selectedLabelTooltipPointerInside = false;
8212
+ this._selectedLabelTooltipDismissedTrigger = null;
8213
+ }
8214
+ }
8215
+ _clearSelectedLabelTooltipHideTimer() {
8216
+ if (this._selectedLabelTooltipHideTimer === null)
8217
+ return;
8218
+ window.clearTimeout(this._selectedLabelTooltipHideTimer);
8219
+ this._lifecycleTimers.delete(this._selectedLabelTooltipHideTimer);
8220
+ this._selectedLabelTooltipHideTimer = null;
8221
+ }
8222
+ _scheduleSelectedLabelTooltipHide() {
8223
+ this._clearSelectedLabelTooltipHideTimer();
8224
+ this._selectedLabelTooltipHideTimer = this._setLifecycleTimeout(() => {
8225
+ this._selectedLabelTooltipHideTimer = null;
8226
+ if (this._selectedLabelTooltipPointerInside)
8227
+ return;
8228
+ const trigger = this._selectedLabelTooltipTrigger;
8229
+ if (trigger && (this._selectedLabelTooltipFocusTrigger === trigger
8230
+ || this._selectedLabelTooltipPointerTrigger === trigger))
8231
+ return;
8232
+ const focusedTrigger = this._selectedLabelTooltipFocusTrigger;
8233
+ if (focusedTrigger && focusedTrigger !== this._selectedLabelTooltipDismissedTrigger
8234
+ && focusedTrigger.isConnected
8235
+ && this._isSelectedLabelVisibleInViewport(focusedTrigger)) {
8236
+ this._showSelectedLabelTooltip(focusedTrigger);
8237
+ return;
8238
+ }
8239
+ this._hideSelectedLabelTooltip();
8240
+ }, 180);
8241
+ }
8242
+ _isSelectedLabelVisibleInViewport(trigger) {
8243
+ if (!trigger.classList.contains('selection-badge-label'))
8244
+ return true;
8245
+ const viewport = this._inputContent;
8246
+ const triggerRect = trigger.getBoundingClientRect();
8247
+ const viewportRect = viewport.getBoundingClientRect();
8248
+ // DOM-only test environments do not calculate layout rectangles. Leave
8249
+ // visibility indeterminate there; browser measurement handles real layout.
8250
+ if (triggerRect.width === 0 && triggerRect.height === 0
8251
+ && viewportRect.width === 0 && viewportRect.height === 0)
8252
+ return true;
8253
+ const left = viewportRect.left + viewport.clientLeft;
8254
+ const top = viewportRect.top + viewport.clientTop;
8255
+ const right = left + viewport.clientWidth;
8256
+ const bottom = top + viewport.clientHeight;
8257
+ return triggerRect.right > left && triggerRect.left < right
8258
+ && triggerRect.bottom > top && triggerRect.top < bottom;
8259
+ }
8260
+ _observeSelectedLabelTooltipTargets() {
8261
+ if (typeof ResizeObserver === 'undefined')
8262
+ return;
8263
+ const hasSingleSelection = this._config.selection.mode === 'single'
8264
+ && this._getSelectedEntries().length > 0;
8265
+ const inputContent = this._getSelectedLabelTooltipContent(this._input);
8266
+ const shouldObserveInput = hasSingleSelection || inputContent?.kind === 'search-query';
8267
+ const chipLabels = this._getActiveChipBadges()
8268
+ .map((badge) => badge.querySelector('.selection-badge-label'))
8269
+ .filter((label) => label !== null);
8270
+ if (!shouldObserveInput && chipLabels.length === 0) {
8271
+ this._selectedLabelTooltipResizeObserver?.disconnect();
8272
+ this._selectedLabelTooltipResizeObserver = null;
8273
+ return;
8274
+ }
8275
+ if (!this._selectedLabelTooltipResizeObserver) {
8276
+ this._selectedLabelTooltipResizeObserver = new ResizeObserver(() => this._scheduleSelectedLabelTooltipMeasure());
8277
+ }
8278
+ this._selectedLabelTooltipResizeObserver.disconnect();
8279
+ if (shouldObserveInput)
8280
+ this._selectedLabelTooltipResizeObserver.observe(this._input);
8281
+ chipLabels.forEach((label) => {
8282
+ this._selectedLabelTooltipResizeObserver?.observe(label);
8283
+ });
8284
+ }
8285
+ _refreshSelectedLabelTooltipMeasurements() {
8286
+ this._getActiveChipBadges().forEach((badge) => {
8287
+ const label = badge.querySelector('.selection-badge-label');
8288
+ if (!label)
8289
+ return;
8290
+ label.tabIndex = this._isSelectedLabelTruncated(label) ? 0 : -1;
8291
+ });
8292
+ const currentTrigger = this._selectedLabelTooltipTrigger;
8293
+ if (currentTrigger?.classList.contains('selection-badge-label')
8294
+ && !this._isSelectedLabelVisibleInViewport(currentTrigger)) {
8295
+ if (this._selectedLabelTooltipPointerTrigger === currentTrigger) {
8296
+ this._selectedLabelTooltipPointerTrigger = null;
8297
+ }
8298
+ this._hideSelectedLabelTooltip();
8299
+ }
8300
+ const trigger = this._selectedLabelTooltipTrigger
8301
+ ?? this._selectedLabelTooltipFocusTrigger
8302
+ ?? this._selectedLabelTooltipPointerTrigger;
8303
+ if (!trigger)
8304
+ return;
8305
+ if (!trigger.isConnected || !this._isSelectedLabelVisibleInViewport(trigger)) {
8306
+ if (this._selectedLabelTooltipPointerTrigger === trigger) {
8307
+ this._selectedLabelTooltipPointerTrigger = null;
8308
+ }
8309
+ if (this._selectedLabelTooltipTrigger === trigger)
8310
+ this._hideSelectedLabelTooltip();
8311
+ return;
8312
+ }
8313
+ const content = this._getSelectedLabelTooltipContent(trigger);
8314
+ if (content === null || !this._isSelectedLabelTooltipContentTruncated(trigger, content)) {
8315
+ if (this._selectedLabelTooltipTrigger === trigger)
8316
+ this._hideSelectedLabelTooltip();
8317
+ return;
8318
+ }
8319
+ this._showSelectedLabelTooltip(trigger);
7806
8320
  }
7807
8321
  _syncListboxAccessibility() {
7808
8322
  if (!this._dropdown || !this._engine || !this._state)
@@ -8399,6 +8913,8 @@
8399
8913
  return results;
8400
8914
  }
8401
8915
  _handleSearch(query) {
8916
+ const previousQuery = this._acknowledgedPendingSearchQuery ?? this._state.searchQuery;
8917
+ const queryPresenceChanged = Boolean(previousQuery) !== Boolean(query);
8402
8918
  if (this._dataRequestCoordinator
8403
8919
  && (this._acknowledgedProjectionPending || this._acknowledgedProjectionSettling)) {
8404
8920
  // Keep the committed query/generation stable until the accepted snapshot
@@ -8407,6 +8923,9 @@
8407
8923
  this._acknowledgedPendingSearchQuery = query;
8408
8924
  this._input.value = query;
8409
8925
  this._syncClearControlState();
8926
+ if (queryPresenceChanged)
8927
+ this._observeSelectedLabelTooltipTargets();
8928
+ this._refreshSelectedLabelTooltipMeasurements();
8410
8929
  return;
8411
8930
  }
8412
8931
  if (this._dataLoadingConfig.protocol === 'acknowledged'
@@ -8418,6 +8937,9 @@
8418
8937
  this._cancelBusyPresentation();
8419
8938
  this._state.searchQuery = query;
8420
8939
  this._syncClearControlState();
8940
+ if (queryPresenceChanged)
8941
+ this._observeSelectedLabelTooltipTargets();
8942
+ this._refreshSelectedLabelTooltipMeasurements();
8421
8943
  this._state.lastNotifiedQuery = query;
8422
8944
  this._state.lastNotifiedResultCount = 0;
8423
8945
  if (this._dataRequestCoordinator) {
@@ -8456,6 +8978,9 @@
8456
8978
  }
8457
8979
  this._state.searchQuery = query;
8458
8980
  this._syncClearControlState();
8981
+ if (queryPresenceChanged)
8982
+ this._observeSelectedLabelTooltipTargets();
8983
+ this._refreshSelectedLabelTooltipMeasurements();
8459
8984
  if (this._dataLoadingConfig.protocol === 'acknowledged' && this._dataRequestCoordinator) {
8460
8985
  // Local acknowledged search has no remote request, but pagination still
8461
8986
  // belongs to the coordinator's current query generation.
@@ -8503,6 +9028,12 @@
8503
9028
  }
8504
9029
  }
8505
9030
  _handleKeydown(e) {
9031
+ if (e.key === 'Escape' && !this._selectedLabelTooltip.hidden) {
9032
+ e.preventDefault();
9033
+ e.stopPropagation();
9034
+ this._dismissSelectedLabelTooltip();
9035
+ return;
9036
+ }
8506
9037
  if (!this._config.enabled)
8507
9038
  return;
8508
9039
  switch (e.key) {
@@ -8576,6 +9107,9 @@
8576
9107
  break;
8577
9108
  case 'Enter':
8578
9109
  case ' ':
9110
+ if (e.key === ' ' && this._config.searchable && e.target === this._input) {
9111
+ break;
9112
+ }
8579
9113
  e.preventDefault();
8580
9114
  if (!this._state.isOpen) {
8581
9115
  this._handleOpen();
@@ -8599,7 +9133,8 @@
8599
9133
  break;
8600
9134
  case 'a':
8601
9135
  case 'A':
8602
- if ((e.ctrlKey || e.metaKey) && this._config.selection.mode === 'multi') {
9136
+ if ((e.ctrlKey || e.metaKey) && !e.altKey
9137
+ && this._config.selection.mode === 'multi' && !this._config.searchable) {
8603
9138
  e.preventDefault();
8604
9139
  this._selectAll();
8605
9140
  }
@@ -9070,7 +9605,7 @@
9070
9605
  : null;
9071
9606
  const hadFocusedChipRemove = focusedBadge !== null;
9072
9607
  const focusedBadgeIndex = focusedBadge
9073
- ? Array.from(this._inputContent.querySelectorAll('.selection-badge')).indexOf(focusedBadge)
9608
+ ? this._getActiveChipBadges().indexOf(focusedBadge)
9074
9609
  : -1;
9075
9610
  const option = this._itemRegistry.keyToRecord.has(entry.key)
9076
9611
  ? this._getOptionElementByIndex(entry.index)
@@ -9096,7 +9631,7 @@
9096
9631
  this._renderOptions();
9097
9632
  this._emitChange();
9098
9633
  if (hadFocusedChipRemove && this.isConnected) {
9099
- const removeButtons = this._inputContent.querySelectorAll('[part="chip-remove"]');
9634
+ const removeButtons = this._getActiveChipRemoveButtons();
9100
9635
  if (removeButtons.length > 0) {
9101
9636
  const nextFocusIndex = Math.min(Math.max(focusedBadgeIndex, 0), removeButtons.length - 1);
9102
9637
  removeButtons[nextFocusIndex].focus();
@@ -9110,8 +9645,248 @@
9110
9645
  : undefined;
9111
9646
  this._emit('remove', { item: config?.item ?? item, index: entry.index });
9112
9647
  }
9648
+ _getActiveChipBadges() {
9649
+ return Array.from(this._inputContent.querySelectorAll('.selection-badge'))
9650
+ .filter((badge) => badge.dataset.motion !== 'exiting' && !badge.hasAttribute('inert'));
9651
+ }
9652
+ _getActiveChipRemoveButtons() {
9653
+ return this._getActiveChipBadges()
9654
+ .map((badge) => badge.querySelector('[part="chip-remove"]'))
9655
+ .filter((button) => button !== null && !button.disabled);
9656
+ }
9657
+ _clearChipPresentation() {
9658
+ const badges = new Set(this._chipElements.values());
9659
+ const tooltipTriggers = [
9660
+ this._selectedLabelTooltipTrigger,
9661
+ this._selectedLabelTooltipPointerTrigger,
9662
+ this._selectedLabelTooltipFocusTrigger,
9663
+ this._selectedLabelTooltipDismissedTrigger,
9664
+ ];
9665
+ if (tooltipTriggers.some((trigger) => trigger !== null
9666
+ && Array.from(badges).some((badge) => badge.contains(trigger)))) {
9667
+ this._hideSelectedLabelTooltip(true);
9668
+ }
9669
+ for (const [key, state] of this._chipExitStates) {
9670
+ window.clearTimeout(state.timer);
9671
+ this._chipExitStates.delete(key);
9672
+ state.animations.forEach((animation) => animation.cancel());
9673
+ }
9674
+ for (const badge of this._chipElements.values()) {
9675
+ badge.removeAttribute('inert');
9676
+ badge.remove();
9677
+ }
9678
+ this._chipExitStates.clear();
9679
+ this._chipElements.clear();
9680
+ }
9681
+ _cancelChipExit(key, badge) {
9682
+ const state = this._chipExitStates.get(key);
9683
+ if (!state || state.badge !== badge)
9684
+ return;
9685
+ window.clearTimeout(state.timer);
9686
+ this._chipExitStates.delete(key);
9687
+ state.animations.forEach((animation) => animation.cancel());
9688
+ badge.removeAttribute('data-motion');
9689
+ badge.removeAttribute('aria-hidden');
9690
+ badge.removeAttribute('inert');
9691
+ badge.setAttribute('part', 'chip');
9692
+ const removeButton = badge.querySelector('[part="chip-remove"]');
9693
+ if (removeButton)
9694
+ removeButton.disabled = false;
9695
+ }
9696
+ _finishChipExit(key, expected) {
9697
+ const state = this._chipExitStates.get(key);
9698
+ if (!state || state.badge !== expected)
9699
+ return;
9700
+ window.clearTimeout(state.timer);
9701
+ this._chipExitStates.delete(key);
9702
+ state.animations.forEach((animation) => {
9703
+ if (animation.playState !== 'finished')
9704
+ animation.cancel();
9705
+ });
9706
+ this._chipElements.delete(key);
9707
+ expected.remove();
9708
+ if (this.isConnected) {
9709
+ this._observeSelectedLabelTooltipTargets();
9710
+ this._refreshSelectedLabelTooltipMeasurements();
9711
+ }
9712
+ }
9713
+ _moveFocusOutOfChip(badge) {
9714
+ const activeElement = this._shadow.activeElement;
9715
+ if (!(activeElement instanceof HTMLElement) || !badge.contains(activeElement))
9716
+ return;
9717
+ const badges = this._getActiveChipBadges().filter((candidate) => candidate !== badge);
9718
+ const oldIndex = Array.from(this._inputContent.querySelectorAll('.selection-badge')).indexOf(badge);
9719
+ const buttons = badges
9720
+ .map((candidate) => candidate.querySelector('[part="chip-remove"]'))
9721
+ .filter((button) => button !== null && !button.disabled);
9722
+ const next = buttons[Math.min(Math.max(oldIndex, 0), buttons.length - 1)];
9723
+ if (next)
9724
+ next.focus({ preventScroll: true });
9725
+ else
9726
+ this._input.focus({ preventScroll: true });
9727
+ }
9728
+ _beginChipExit(key, badge) {
9729
+ if (this._chipExitStates.has(key))
9730
+ return;
9731
+ this._moveFocusOutOfChip(badge);
9732
+ const label = badge.querySelector('.selection-badge-label');
9733
+ if (label) {
9734
+ this._removeSelectedLabelTooltipDescription(label);
9735
+ label.tabIndex = -1;
9736
+ if (this._selectedLabelTooltipPointerTrigger === label)
9737
+ this._selectedLabelTooltipPointerTrigger = null;
9738
+ if (this._selectedLabelTooltipFocusTrigger === label)
9739
+ this._selectedLabelTooltipFocusTrigger = null;
9740
+ if (this._selectedLabelTooltipDismissedTrigger === label)
9741
+ this._selectedLabelTooltipDismissedTrigger = null;
9742
+ if (this._selectedLabelTooltipTrigger === label)
9743
+ this._hideSelectedLabelTooltip();
9744
+ }
9745
+ badge.dataset.motion = 'exiting';
9746
+ badge.setAttribute('aria-hidden', 'true');
9747
+ badge.setAttribute('inert', '');
9748
+ badge.setAttribute('part', 'chip-exiting');
9749
+ const removeButton = badge.querySelector('[part="chip-remove"]');
9750
+ if (removeButton)
9751
+ removeButton.disabled = true;
9752
+ // Force style resolution after the exiting part is exposed. This also
9753
+ // verifies that a consumer keyframe is actually available across Shadow DOM.
9754
+ badge.getBoundingClientRect();
9755
+ const animations = (typeof badge.getAnimations === 'function' ? badge.getAnimations() : []).filter((animation) => {
9756
+ const cssAnimation = animation;
9757
+ return typeof cssAnimation.animationName === 'string'
9758
+ && cssAnimation.animationName !== 'none'
9759
+ && animation.playState !== 'idle';
9760
+ });
9761
+ const reducedMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
9762
+ if (reducedMotion || animations.length === 0) {
9763
+ badge.removeAttribute('data-motion');
9764
+ this._chipExitStates.set(key, { badge, animations: [], timer: 0 });
9765
+ this._finishChipExit(key, badge);
9766
+ return;
9767
+ }
9768
+ const endTimes = animations.map((animation) => Number(animation.effect?.getComputedTiming().endTime ?? 0));
9769
+ const maxEndTime = Math.max(0, ...endTimes);
9770
+ const fallbackDelay = Math.min(Number.isFinite(maxEndTime) ? maxEndTime + 100 : 1000, 5000);
9771
+ const timer = window.setTimeout(() => this._finishChipExit(key, badge), fallbackDelay);
9772
+ this._chipExitStates.set(key, { badge, animations, timer });
9773
+ void Promise.allSettled(animations.map((animation) => animation.finished))
9774
+ .then(() => this._finishChipExit(key, badge));
9775
+ }
9776
+ _createChip(key) {
9777
+ const badge = document.createElement('span');
9778
+ badge.className = 'selection-badge';
9779
+ badge.setAttribute('part', 'chip');
9780
+ const label = document.createElement('span');
9781
+ label.className = 'selection-badge-label';
9782
+ label.setAttribute('part', 'chip-label');
9783
+ this._selectedLabelKeys.set(label, key);
9784
+ badge.appendChild(label);
9785
+ this._chipElements.set(key, badge);
9786
+ return badge;
9787
+ }
9788
+ _updateChip(key, index, item, badge) {
9789
+ const getLabel = this._config.serverSide.getLabelFromItem || ((value) => value?.label ?? String(value));
9790
+ const labelText = getLabel(item);
9791
+ badge.className = 'selection-badge';
9792
+ if (this._config.styles.classNames?.badge) {
9793
+ badge.classList.add(...this._config.styles.classNames.badge.split(' ').filter(Boolean));
9794
+ }
9795
+ badge.setAttribute('part', 'chip');
9796
+ badge.removeAttribute('aria-hidden');
9797
+ badge.removeAttribute('inert');
9798
+ badge.removeAttribute('data-motion');
9799
+ const label = badge.querySelector('.selection-badge-label');
9800
+ label.className = 'selection-badge-label';
9801
+ if (this._config.styles.classNames?.badgeLabel) {
9802
+ label.classList.add(...this._config.styles.classNames.badgeLabel.split(' ').filter(Boolean));
9803
+ }
9804
+ label.textContent = labelText;
9805
+ this._selectedLabelKeys.set(label, key);
9806
+ const shouldShowRemove = this._config.selection.showRemoveButton !== false;
9807
+ let removeButton = badge.querySelector('[part="chip-remove"]');
9808
+ if (!shouldShowRemove) {
9809
+ removeButton?.remove();
9810
+ return;
9811
+ }
9812
+ if (!removeButton) {
9813
+ removeButton = document.createElement('button');
9814
+ removeButton.type = 'button';
9815
+ removeButton.addEventListener('pointerdown', (event) => {
9816
+ event.stopPropagation();
9817
+ event.preventDefault();
9818
+ });
9819
+ removeButton.addEventListener('click', (event) => {
9820
+ event.stopPropagation();
9821
+ event.preventDefault();
9822
+ this._handleOptionRemove(index, key);
9823
+ });
9824
+ const removeIcon = document.createElement('span');
9825
+ removeIcon.className = 'badge-remove-icon';
9826
+ removeIcon.setAttribute('part', 'chip-remove-icon');
9827
+ removeButton.appendChild(removeIcon);
9828
+ badge.appendChild(removeButton);
9829
+ }
9830
+ removeButton.className = 'badge-remove';
9831
+ if (this._config.styles.classNames?.removeButton) {
9832
+ removeButton.classList.add(...this._config.styles.classNames.removeButton.split(' ').filter(Boolean));
9833
+ }
9834
+ if (this._config.styles.classNames?.badgeRemove) {
9835
+ removeButton.classList.add(...this._config.styles.classNames.badgeRemove.split(' ').filter(Boolean));
9836
+ }
9837
+ removeButton.setAttribute('part', 'chip-remove');
9838
+ removeButton.setAttribute('aria-label', `Remove ${labelText}`);
9839
+ removeButton.disabled = false;
9840
+ const removeIcon = removeButton.querySelector('[part="chip-remove-icon"]');
9841
+ removeIcon.className = 'badge-remove-icon';
9842
+ if (this._config.styles.classNames?.badgeRemoveIcon) {
9843
+ removeIcon.classList.add(...this._config.styles.classNames.badgeRemoveIcon.split(' ').filter(Boolean));
9844
+ }
9845
+ this._setIconContent(removeIcon, this._config.selection.removeButtonIcon, '×');
9846
+ }
9847
+ _renderSelectedChips(entries) {
9848
+ const selectedKeys = new Set(entries.map(({ key }) => key));
9849
+ for (const { key, index, item } of entries) {
9850
+ let badge = this._chipElements.get(key);
9851
+ if (!badge)
9852
+ badge = this._createChip(key);
9853
+ this._cancelChipExit(key, badge);
9854
+ this._updateChip(key, index, item, badge);
9855
+ }
9856
+ for (const [key, badge] of this._chipElements) {
9857
+ if (!selectedKeys.has(key))
9858
+ this._beginChipExit(key, badge);
9859
+ }
9860
+ // Do not move chips merely because another chip entered its exit period.
9861
+ // That keeps the exiting chip in its original layout slot while the active
9862
+ // sequence remains correct. Reorder only when selected-key order changed.
9863
+ const desiredBadges = entries
9864
+ .map(({ key }) => this._chipElements.get(key))
9865
+ .filter((badge) => badge !== undefined);
9866
+ const currentBadges = this._getActiveChipBadges();
9867
+ const orderChanged = desiredBadges.length !== currentBadges.length
9868
+ || desiredBadges.some((badge, index) => badge !== currentBadges[index]);
9869
+ if (orderChanged) {
9870
+ let nextSibling = this._input;
9871
+ for (let index = desiredBadges.length - 1; index >= 0; index -= 1) {
9872
+ const badge = desiredBadges[index];
9873
+ if (badge.nextSibling !== nextSibling)
9874
+ this._inputContent.insertBefore(badge, nextSibling);
9875
+ nextSibling = badge;
9876
+ }
9877
+ }
9878
+ }
9113
9879
  _updateInputDisplay() {
9114
9880
  const selectedEntries = this._getSelectedEntries();
9881
+ const focusedElement = this._shadow.activeElement;
9882
+ const focusedSelectedKey = focusedElement?.classList.contains('selection-badge-label')
9883
+ ? this._selectedLabelKeys.get(focusedElement) ?? null
9884
+ : null;
9885
+ const focusedBadge = focusedElement?.closest('.selection-badge');
9886
+ const focusedChipKey = focusedBadge
9887
+ ? Array.from(this._chipElements.entries()).find(([, badge]) => badge === focusedBadge)?.[0] ?? null
9888
+ : null;
9889
+ const focusedBadgeIndex = focusedBadge ? this._getActiveChipBadges().indexOf(focusedBadge) : -1;
9115
9890
  const selectedItems = selectedEntries.map(({ item }) => item);
9116
9891
  const nextKeys = new Set(selectedEntries.map(({ key }) => key));
9117
9892
  const previousKeys = this._lastRenderedChipKeys;
@@ -9127,73 +9902,27 @@
9127
9902
  // Only a newly added selected key can initiate auto-scroll.
9128
9903
  this._cancelLatestChipScroll();
9129
9904
  const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
9130
- if (selectedItems.length === 0) {
9131
- this._input.value = '';
9132
- this._input.placeholder = this._config.placeholder || 'Select an option...';
9133
- // Clear any badges
9134
- const existingBadges = this._inputContent.querySelectorAll('.selection-badge');
9135
- existingBadges.forEach(badge => badge.remove());
9136
- }
9137
- else if (this._config.selection.mode === 'single') {
9138
- this._input.value = getLabel(selectedItems[0]);
9905
+ if (this._config.selection.mode === 'multi') {
9906
+ if (selectedItems.length === 0) {
9907
+ this._input.value = '';
9908
+ this._input.placeholder = this._config.placeholder || 'Select an option...';
9909
+ }
9910
+ else {
9911
+ this._input.value = '';
9912
+ this._input.placeholder = '';
9913
+ }
9914
+ this._renderSelectedChips(selectedEntries);
9139
9915
  }
9140
9916
  else {
9141
- // Multi-select: show badges instead of text in input
9142
- this._input.value = '';
9143
- this._input.placeholder = '';
9144
- // Clear existing badges
9145
- const existingBadges = this._inputContent.querySelectorAll('.selection-badge');
9146
- existingBadges.forEach(badge => badge.remove());
9147
- // Create badges for each selected item
9148
- selectedEntries.forEach(({ key, index, item }) => {
9149
- const badge = document.createElement('span');
9150
- badge.className = 'selection-badge';
9151
- if (this._config.styles.classNames?.badge) {
9152
- badge.classList.add(...this._config.styles.classNames.badge.split(' ').filter(Boolean));
9153
- }
9154
- badge.setAttribute('part', 'chip');
9155
- const badgeLabel = document.createElement('span');
9156
- badgeLabel.className = 'selection-badge-label';
9157
- if (this._config.styles.classNames?.badgeLabel) {
9158
- badgeLabel.classList.add(...this._config.styles.classNames.badgeLabel.split(' ').filter(Boolean));
9159
- }
9160
- badgeLabel.setAttribute('part', 'chip-label');
9161
- badgeLabel.textContent = getLabel(item);
9162
- badge.appendChild(badgeLabel);
9163
- // Add remove button to badge
9164
- if (this._config.selection.showRemoveButton !== false) {
9165
- const removeBtn = document.createElement('button');
9166
- removeBtn.type = 'button';
9167
- removeBtn.className = 'badge-remove';
9168
- if (this._config.styles.classNames?.removeButton) {
9169
- removeBtn.classList.add(...this._config.styles.classNames.removeButton.split(' ').filter(Boolean));
9170
- }
9171
- if (this._config.styles.classNames?.badgeRemove) {
9172
- removeBtn.classList.add(...this._config.styles.classNames.badgeRemove.split(' ').filter(Boolean));
9173
- }
9174
- removeBtn.setAttribute('part', 'chip-remove');
9175
- removeBtn.setAttribute('aria-label', `Remove ${getLabel(item)}`);
9176
- const removeIcon = document.createElement('span');
9177
- removeIcon.className = 'badge-remove-icon';
9178
- if (this._config.styles.classNames?.badgeRemoveIcon) {
9179
- removeIcon.classList.add(...this._config.styles.classNames.badgeRemoveIcon.split(' ').filter(Boolean));
9180
- }
9181
- removeIcon.setAttribute('part', 'chip-remove-icon');
9182
- this._setIconContent(removeIcon, this._config.selection.removeButtonIcon, '×');
9183
- removeBtn.appendChild(removeIcon);
9184
- removeBtn.addEventListener('pointerdown', (e) => {
9185
- e.stopPropagation();
9186
- e.preventDefault();
9187
- });
9188
- removeBtn.addEventListener('click', (e) => {
9189
- e.stopPropagation();
9190
- e.preventDefault();
9191
- this._handleOptionRemove(index, key);
9192
- });
9193
- badge.appendChild(removeBtn);
9194
- }
9195
- this._inputContent.insertBefore(badge, this._input);
9196
- });
9917
+ // A mode switch ends visual exits immediately; a single-select has no chip presentation.
9918
+ this._clearChipPresentation();
9919
+ if (selectedItems.length === 0) {
9920
+ this._input.value = '';
9921
+ this._input.placeholder = this._config.placeholder || 'Select an option...';
9922
+ }
9923
+ else {
9924
+ this._input.value = getLabel(selectedItems[0]);
9925
+ }
9197
9926
  }
9198
9927
  // A query typed during an accepted-snapshot projection is a pending user
9199
9928
  // intent, not part of the committed projection state. Keep it visible even
@@ -9201,14 +9930,60 @@
9201
9930
  if (this._acknowledgedPendingSearchQuery !== null) {
9202
9931
  this._input.value = this._acknowledgedPendingSearchQuery;
9203
9932
  }
9204
- // Recreating the chip DOM must not reset manual scrolling. In particular,
9205
- // remove/clear, config rerenders, and item refreshes are not scroll intents.
9933
+ // Preserve manual scrolling. Only a newly selected key can request auto-scroll.
9206
9934
  if (this._config.selection.mode === 'multi') {
9207
9935
  this._inputContent.scrollLeft = previousChipScrollLeft;
9208
9936
  if (newlyAddedKey !== undefined)
9209
9937
  this._scheduleLatestChipScroll(newlyAddedKey);
9210
9938
  }
9211
9939
  this._syncClearControlState();
9940
+ this._observeSelectedLabelTooltipTargets();
9941
+ this._refreshSelectedLabelTooltipMeasurements();
9942
+ if (focusedChipKey !== null && !nextKeys.has(focusedChipKey)) {
9943
+ const removeButtons = this._getActiveChipRemoveButtons();
9944
+ if (removeButtons.length > 0) {
9945
+ const nextFocusIndex = Math.min(Math.max(focusedBadgeIndex, 0), removeButtons.length - 1);
9946
+ removeButtons[nextFocusIndex].focus({ preventScroll: true });
9947
+ }
9948
+ else {
9949
+ this._input.focus({ preventScroll: true });
9950
+ }
9951
+ }
9952
+ else if (focusedChipKey !== null && focusedElement && !focusedElement.isConnected) {
9953
+ const badge = this._chipElements.get(focusedChipKey);
9954
+ const removeButton = badge?.querySelector('[part="chip-remove"]');
9955
+ if (removeButton && !removeButton.disabled)
9956
+ removeButton.focus({ preventScroll: true });
9957
+ else
9958
+ this._input.focus({ preventScroll: true });
9959
+ }
9960
+ else if (focusedSelectedKey !== null) {
9961
+ const replacement = this._getActiveChipBadges()
9962
+ .map((badge) => badge.querySelector('.selection-badge-label'))
9963
+ .find((label) => label && this._selectedLabelKeys.get(label) === focusedSelectedKey);
9964
+ if (!replacement) {
9965
+ const removeButtons = this._getActiveChipRemoveButtons();
9966
+ if (removeButtons.length > 0) {
9967
+ const nextFocusIndex = Math.min(Math.max(focusedBadgeIndex, 0), removeButtons.length - 1);
9968
+ removeButtons[nextFocusIndex].focus({ preventScroll: true });
9969
+ }
9970
+ else {
9971
+ this._input.focus({ preventScroll: true });
9972
+ }
9973
+ }
9974
+ else if (this._isSelectedLabelTruncated(replacement)) {
9975
+ // The measurement pass assigns the tab stop before focus restoration.
9976
+ replacement.tabIndex = 0;
9977
+ replacement.focus({ preventScroll: true });
9978
+ }
9979
+ else {
9980
+ this._input.focus({ preventScroll: true });
9981
+ }
9982
+ }
9983
+ if (this._selectedLabelTooltipFocusTrigger === this._input
9984
+ || this._selectedLabelTooltipPointerTrigger === this._input) {
9985
+ this._showSelectedLabelTooltip(this._input);
9986
+ }
9212
9987
  }
9213
9988
  _cancelLatestChipScroll() {
9214
9989
  this._latestChipScrollGeneration += 1;
@@ -9240,7 +10015,7 @@
9240
10015
  const index = entries.findIndex((entry) => entry.key === key);
9241
10016
  if (index < 0)
9242
10017
  return;
9243
- const chip = this._inputContent.querySelectorAll('.selection-badge')[index];
10018
+ const chip = this._getActiveChipBadges()[index];
9244
10019
  if (!chip)
9245
10020
  return;
9246
10021
  const hostBounds = this._inputContent.getBoundingClientRect();
@@ -9828,6 +10603,8 @@
9828
10603
  }
9829
10604
  this._renderOptions();
9830
10605
  this._syncClearControlState();
10606
+ this._observeSelectedLabelTooltipTargets();
10607
+ this._refreshSelectedLabelTooltipMeasurements();
9831
10608
  }
9832
10609
  /**
9833
10610
  * Open dropdown
@@ -10036,6 +10813,8 @@
10036
10813
  }
10037
10814
  }
10038
10815
  }
10816
+ this._observeSelectedLabelTooltipTargets();
10817
+ this._refreshSelectedLabelTooltipMeasurements();
10039
10818
  if (configErrorDetail) {
10040
10819
  this._emit('dataLoadingConfigError', configErrorDetail);
10041
10820
  try {