@smilodon/core 1.20.0 → 1.21.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
@@ -4449,6 +4449,15 @@
4449
4449
  super();
4450
4450
  this._dataLoadingConfig = { ...LEGACY_DATA_LOADING_CONFIG };
4451
4451
  this._hasLastInvalidDataLoadingCandidate = false;
4452
+ this._selectedLabelTooltipTrigger = null;
4453
+ this._selectedLabelTooltipPointerTrigger = null;
4454
+ this._selectedLabelTooltipFocusTrigger = null;
4455
+ this._selectedLabelTooltipPointerInside = false;
4456
+ this._selectedLabelTooltipDismissedTrigger = null;
4457
+ this._selectedLabelTooltipHideTimer = null;
4458
+ this._selectedLabelTooltipResizeObserver = null;
4459
+ this._selectedLabelTooltipMeasureFrame = null;
4460
+ this._selectedLabelKeys = new WeakMap();
4452
4461
  this._busyPresentationVisible = false;
4453
4462
  this._busyPresentationStartedAt = null;
4454
4463
  this._busyPresentationGeneration = 0;
@@ -4548,6 +4557,9 @@
4548
4557
  this._rangeAnchorKey = null;
4549
4558
  /** Chip identity is tracked independently of dropdown option positions. */
4550
4559
  this._lastRenderedChipKeys = null;
4560
+ /** Stable presentation nodes keyed by the engine's selected-item identity. */
4561
+ this._chipElements = new Map();
4562
+ this._chipExitStates = new Map();
4551
4563
  this._latestChipScrollGeneration = 0;
4552
4564
  this._latestChipScrollFrame = null;
4553
4565
  this._customOptionBoundElements = new WeakSet();
@@ -4633,6 +4645,14 @@
4633
4645
  this._handleClose('outside-pointer');
4634
4646
  }
4635
4647
  };
4648
+ this._scheduleSelectedLabelTooltipMeasure = () => {
4649
+ if (this._selectedLabelTooltipMeasureFrame !== null || !this.isConnected)
4650
+ return;
4651
+ this._selectedLabelTooltipMeasureFrame = this._requestLifecycleFrame(() => {
4652
+ this._selectedLabelTooltipMeasureFrame = null;
4653
+ this._refreshSelectedLabelTooltipMeasurements();
4654
+ });
4655
+ };
4636
4656
  this._shadow = this.attachShadow({ mode: 'open' });
4637
4657
  this._uniqueId = `enhanced-select-${Math.random().toString(36).substr(2, 9)}`;
4638
4658
  this._rendererHelpers = this._buildRendererHelpers();
@@ -4667,6 +4687,7 @@
4667
4687
  this._inputContainer = this._createInputContainer();
4668
4688
  this._inputContent = this._createInputContent();
4669
4689
  this._input = this._createInput();
4690
+ this._selectedLabelTooltip = this._createSelectedLabelTooltip();
4670
4691
  this._arrowContainer = this._createArrowContainer();
4671
4692
  this._clearControl = this._createClearControl();
4672
4693
  this._dropdown = this._createDropdown();
@@ -4692,9 +4713,19 @@
4692
4713
  this._runEngineEffects(this._engine.dispatch({ type: 'DISABLED_CHANGED', disabled: !this._config.enabled }));
4693
4714
  this._connectionController?.abort();
4694
4715
  this._connectionController = new AbortController();
4716
+ this._observeSelectedLabelTooltipTargets();
4695
4717
  document.addEventListener('pointerdown', this._onDocumentPointerDown, {
4696
4718
  signal: this._connectionController.signal,
4697
4719
  });
4720
+ window.addEventListener('resize', this._scheduleSelectedLabelTooltipMeasure, {
4721
+ signal: this._connectionController.signal,
4722
+ passive: true,
4723
+ });
4724
+ window.addEventListener('scroll', this._scheduleSelectedLabelTooltipMeasure, {
4725
+ signal: this._connectionController.signal,
4726
+ passive: true,
4727
+ capture: true,
4728
+ });
4698
4729
  // WORKAROUND: Force display style on host element for Angular compatibility
4699
4730
  // Angular's rendering seems to not apply :host styles correctly in some cases
4700
4731
  // Must be done in connectedCallback when element is attached to DOM
@@ -4745,6 +4776,7 @@
4745
4776
  disconnectedCallback() {
4746
4777
  this._cancelOpenSelectedScroll();
4747
4778
  this._cancelLatestChipScroll();
4779
+ this._clearChipPresentation();
4748
4780
  this._cancelBusyPresentation();
4749
4781
  if (this._dataRequestCoordinator) {
4750
4782
  const actions = this._dataRequestCoordinator.disconnect();
@@ -4767,6 +4799,13 @@
4767
4799
  this._runEngineEffects(this._engine.dispatch({ type: 'DISCONNECT' }));
4768
4800
  this._connectionController?.abort();
4769
4801
  this._connectionController = undefined;
4802
+ this._hideSelectedLabelTooltip(true);
4803
+ this._selectedLabelTooltipResizeObserver?.disconnect();
4804
+ this._selectedLabelTooltipResizeObserver = null;
4805
+ if (this._selectedLabelTooltipMeasureFrame !== null) {
4806
+ cancelAnimationFrame(this._selectedLabelTooltipMeasureFrame);
4807
+ this._selectedLabelTooltipMeasureFrame = null;
4808
+ }
4770
4809
  this._asyncGeneration += 1;
4771
4810
  this._cancelGuardedVirtualWindowUpdate();
4772
4811
  this._clearLifecycleTasks();
@@ -5918,6 +5957,14 @@
5918
5957
  input.setAttribute('aria-haspopup', 'listbox');
5919
5958
  return input;
5920
5959
  }
5960
+ _createSelectedLabelTooltip() {
5961
+ const tooltip = document.createElement('div');
5962
+ tooltip.id = `${this._uniqueId}-selected-label-tooltip`;
5963
+ tooltip.className = 'selected-label-tooltip';
5964
+ tooltip.setAttribute('role', 'tooltip');
5965
+ tooltip.hidden = true;
5966
+ return tooltip;
5967
+ }
5921
5968
  _createDropdown() {
5922
5969
  const dropdown = document.createElement('div');
5923
5970
  dropdown.className = 'select-dropdown';
@@ -6012,6 +6059,7 @@
6012
6059
  if (this._errorDescription) {
6013
6060
  this._shadow.appendChild(this._errorDescription);
6014
6061
  }
6062
+ this._shadow.appendChild(this._selectedLabelTooltip);
6015
6063
  // Set ARIA relationships
6016
6064
  const listboxId = `${this._uniqueId}-listbox`;
6017
6065
  this._dropdown.id = listboxId;
@@ -6026,6 +6074,12 @@
6026
6074
  ELEGANT SELECT COMPONENT — Refined Design System
6027
6075
  Formal aesthetics with sophisticated microinteractions
6028
6076
  ═══════════════════════════════════════════════════════════════════════════ */
6077
+
6078
+ @layer smilodon-input-content-fallback {
6079
+ .input-container.input-container--multi:not([data-multi-scroll-mode="horizontal"]) .input-content {
6080
+ padding-inline-end: var(--select-multi-input-end-gap, 12px);
6081
+ }
6082
+ }
6029
6083
 
6030
6084
  :host {
6031
6085
  --select-primary: #1a1a2e;
@@ -6050,6 +6104,10 @@
6050
6104
  --select-transition-smooth: 250ms cubic-bezier(0.4, 0, 0.2, 1);
6051
6105
  --select-transition-bounce: 350ms cubic-bezier(0.34, 1.56, 0.64, 1);
6052
6106
  --select-badge-animation: badgeEnter 300ms cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
6107
+ --select-badge-exit-from-opacity: 1;
6108
+ --select-badge-exit-from-transform: scale(1) translateY(0);
6109
+ --select-badge-exit-to-opacity: 0;
6110
+ --select-badge-exit-to-transform: scale(0.8) translateY(-4px);
6053
6111
  --select-badge-enter-from-opacity: 0;
6054
6112
  --select-badge-enter-from-transform: scale(0.8) translateY(-4px);
6055
6113
  --select-badge-enter-to-opacity: 1;
@@ -6251,7 +6309,13 @@
6251
6309
  transform var(--select-transition-bounce),
6252
6310
  box-shadow var(--select-transition-fast),
6253
6311
  background var(--select-transition-fast);
6254
- animation: var(--select-badge-animation);
6312
+ animation: var(--select-badge-enter-animation, var(--select-badge-animation));
6313
+ }
6314
+
6315
+ .selection-badge[data-motion="exiting"] {
6316
+ animation: var(--select-badge-exit-animation, none);
6317
+ pointer-events: none;
6318
+ transition: none;
6255
6319
  }
6256
6320
 
6257
6321
  @keyframes badgeEnter {
@@ -6264,6 +6328,17 @@
6264
6328
  transform: var(--select-badge-enter-to-transform);
6265
6329
  }
6266
6330
  }
6331
+
6332
+ @keyframes badgeExit {
6333
+ 0% {
6334
+ opacity: var(--select-badge-exit-from-opacity, 1);
6335
+ transform: var(--select-badge-exit-from-transform, scale(1) translateY(0));
6336
+ }
6337
+ 100% {
6338
+ opacity: var(--select-badge-exit-to-opacity, 0);
6339
+ transform: var(--select-badge-exit-to-transform, scale(0.8) translateY(-4px));
6340
+ }
6341
+ }
6267
6342
 
6268
6343
  .selection-badge:hover {
6269
6344
  background: var(--select-badge-hover-bg, var(--select-badge-bg, linear-gradient(135deg, var(--select-accent) 0%, var(--select-primary-light) 100%)));
@@ -6376,6 +6451,29 @@
6376
6451
  position: relative;
6377
6452
  width: 100%;
6378
6453
  }
6454
+
6455
+ .selected-label-tooltip {
6456
+ position: fixed;
6457
+ z-index: 2147483000;
6458
+ box-sizing: border-box;
6459
+ width: max-content;
6460
+ max-width: min(24rem, calc(100vw - 16px));
6461
+ padding: 8px 10px;
6462
+ border: 1px solid rgba(255, 255, 255, 0.18);
6463
+ border-radius: 6px;
6464
+ background: #18202e;
6465
+ color: #fff;
6466
+ box-shadow: 0 4px 16px rgba(0, 0, 0, 0.24);
6467
+ font: 500 13px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
6468
+ white-space: normal;
6469
+ overflow-wrap: anywhere;
6470
+ pointer-events: auto;
6471
+ direction: inherit;
6472
+ }
6473
+
6474
+ .selected-label-tooltip[hidden] {
6475
+ display: none;
6476
+ }
6379
6477
 
6380
6478
  .input-container {
6381
6479
  position: relative;
@@ -6424,6 +6522,7 @@
6424
6522
  overflow-y: var(--select-input-overflow-y, hidden);
6425
6523
  scrollbar-width: thin;
6426
6524
  box-sizing: border-box;
6525
+ padding: var(--select-input-content-padding, revert-layer);
6427
6526
  }
6428
6527
 
6429
6528
  .input-container:hover {
@@ -6449,7 +6548,6 @@
6449
6548
  align-content: var(--select-multi-input-align-content, var(--select-multi-input-align-content-config, flex-start));
6450
6549
  overflow-x: var(--select-multi-input-overflow-x, var(--select-multi-input-overflow-x-config, hidden));
6451
6550
  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
6551
  }
6454
6552
 
6455
6553
  .input-container.input-container--multi .input-content .selection-badge {
@@ -6487,7 +6585,7 @@
6487
6585
  overflow-y: hidden;
6488
6586
  overscroll-behavior-x: contain;
6489
6587
  overscroll-behavior-y: none;
6490
- padding-inline-end: 0;
6588
+ padding: var(--select-input-content-padding, 0);
6491
6589
  scroll-padding-inline-end: var(--select-multi-action-area-width, var(--select-arrow-width, 42px));
6492
6590
  }
6493
6591
 
@@ -6590,6 +6688,7 @@
6590
6688
  bottom: 0;
6591
6689
  width: var(--select-arrow-width, var(--select-arrow-size, 42px));
6592
6690
  height: var(--select-arrow-height, var(--select-arrow-size, auto));
6691
+ padding: var(--select-dropdown-arrow-container-padding, 0);
6593
6692
  display: flex;
6594
6693
  align-items: center;
6595
6694
  justify-content: center;
@@ -7469,7 +7568,25 @@
7469
7568
  this._shadow.appendChild(style);
7470
7569
  }
7471
7570
  }
7571
+ _isArrowOrClearControlEvent(event) {
7572
+ const path = event.composedPath();
7573
+ return ((this._arrowContainer != null && path.includes(this._arrowContainer)) ||
7574
+ (this._clearControl != null && path.includes(this._clearControl)));
7575
+ }
7472
7576
  _attachEventListeners() {
7577
+ this._shadow.addEventListener('pointerover', (event) => this._handleSelectedLabelPointerOver(event));
7578
+ this._shadow.addEventListener('pointerout', (event) => this._handleSelectedLabelPointerOut(event));
7579
+ this._shadow.addEventListener('focusin', (event) => this._handleSelectedLabelFocusIn(event));
7580
+ this._shadow.addEventListener('focusout', (event) => this._handleSelectedLabelFocusOut(event));
7581
+ this._shadow.addEventListener('keydown', (event) => {
7582
+ const keyboardEvent = event;
7583
+ if (keyboardEvent.key !== 'Escape' || this._selectedLabelTooltip.hidden)
7584
+ return;
7585
+ keyboardEvent.preventDefault();
7586
+ keyboardEvent.stopPropagation();
7587
+ this._dismissSelectedLabelTooltip();
7588
+ });
7589
+ this._inputContent.addEventListener('scroll', this._scheduleSelectedLabelTooltipMeasure, { passive: true });
7473
7590
  // Arrow click handler
7474
7591
  if (this._arrowContainer) {
7475
7592
  this._boundArrowClick = (e) => {
@@ -7505,13 +7622,11 @@
7505
7622
  // Prevent propagation to document click listener but do NOT preventDefault.
7506
7623
  // Allow default so browser events (click) on newly opened options still fire.
7507
7624
  // 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
7625
  if (!this._config.enabled)
7510
7626
  return;
7511
- if (target && target.closest('.dropdown-arrow-container'))
7512
- return;
7513
- if (target && target.closest('.clear-control-button'))
7627
+ if (this._isArrowOrClearControlEvent(e))
7514
7628
  return;
7629
+ const target = e.target;
7515
7630
  if (this._isPointerOnInputScrollbar(e)) {
7516
7631
  this._suppressNextOpenClick = true;
7517
7632
  return;
@@ -7558,7 +7673,6 @@
7558
7673
  }
7559
7674
  });
7560
7675
  this._inputContainer.addEventListener('click', (e) => {
7561
- const target = e.target;
7562
7676
  const isHorizontalMultiMode = this._config.selection.mode === 'multi'
7563
7677
  && (this._config.multiSelectDisplay?.mode ?? 'wrap') === 'horizontal';
7564
7678
  if (!this._config.enabled || !isHorizontalMultiMode) {
@@ -7566,7 +7680,7 @@
7566
7680
  this._multiScrollDrag.moved = false;
7567
7681
  return;
7568
7682
  }
7569
- if (target && target.closest('.dropdown-arrow-container, .clear-control-button')) {
7683
+ if (this._isArrowOrClearControlEvent(e)) {
7570
7684
  this._suppressNextOpenClick = false;
7571
7685
  this._multiScrollDrag.moved = false;
7572
7686
  return;
@@ -7641,6 +7755,10 @@
7641
7755
  if (!this._config.searchable)
7642
7756
  return;
7643
7757
  const query = e.target.value;
7758
+ if (this._selectedLabelTooltipTrigger === this._input
7759
+ && this._getSelectedLabelForTrigger(this._input) === null) {
7760
+ this._hideSelectedLabelTooltip();
7761
+ }
7644
7762
  this._handleSearch(query);
7645
7763
  });
7646
7764
  // If the user presses down inside the options container we should
@@ -7803,6 +7921,286 @@
7803
7921
  return index === undefined ? null : this._getOptionElementByIndex(index);
7804
7922
  },
7805
7923
  });
7924
+ this._syncSelectedLabelTooltipDescription();
7925
+ }
7926
+ _selectedLabelTriggerFromTarget(target) {
7927
+ if (!(target instanceof HTMLElement))
7928
+ return null;
7929
+ if (target === this._input)
7930
+ return target;
7931
+ const label = target.closest('.selection-badge-label');
7932
+ return label && this._inputContent.contains(label) ? label : null;
7933
+ }
7934
+ _getSelectedLabelForTrigger(trigger) {
7935
+ const entries = this._getSelectedEntries();
7936
+ if (trigger === this._input) {
7937
+ if (this._config.selection.mode !== 'single' || this._state.searchQuery.length > 0)
7938
+ return null;
7939
+ const item = entries[0]?.item;
7940
+ if (item === undefined)
7941
+ return null;
7942
+ const label = this._getItemLabel(item);
7943
+ return this._input.value === label ? label : null;
7944
+ }
7945
+ if (!trigger.classList.contains('selection-badge-label'))
7946
+ return null;
7947
+ const key = this._selectedLabelKeys.get(trigger);
7948
+ if (key === undefined)
7949
+ return null;
7950
+ const entry = entries.find((candidate) => candidate.key === key);
7951
+ if (!entry)
7952
+ return null;
7953
+ const label = this._getItemLabel(entry.item);
7954
+ return trigger.textContent === label ? label : null;
7955
+ }
7956
+ _isSelectedLabelTruncated(trigger) {
7957
+ if (!trigger.isConnected)
7958
+ return false;
7959
+ return trigger.clientWidth > 0 && trigger.scrollWidth > trigger.clientWidth + 1;
7960
+ }
7961
+ _handleSelectedLabelPointerOver(event) {
7962
+ const target = event.target;
7963
+ if (target?.closest('.selected-label-tooltip') === this._selectedLabelTooltip) {
7964
+ this._selectedLabelTooltipPointerInside = true;
7965
+ this._clearSelectedLabelTooltipHideTimer();
7966
+ return;
7967
+ }
7968
+ const trigger = this._selectedLabelTriggerFromTarget(target);
7969
+ if (!trigger || (event.relatedTarget instanceof Node && trigger.contains(event.relatedTarget)))
7970
+ return;
7971
+ this._selectedLabelTooltipPointerTrigger = trigger;
7972
+ this._selectedLabelTooltipDismissedTrigger = null;
7973
+ this._showSelectedLabelTooltip(trigger);
7974
+ }
7975
+ _handleSelectedLabelPointerOut(event) {
7976
+ const target = event.target;
7977
+ if (target === this._selectedLabelTooltip || this._selectedLabelTooltip.contains(target)) {
7978
+ if (event.relatedTarget instanceof Node && this._selectedLabelTooltip.contains(event.relatedTarget))
7979
+ return;
7980
+ this._selectedLabelTooltipPointerInside = false;
7981
+ this._scheduleSelectedLabelTooltipHide();
7982
+ return;
7983
+ }
7984
+ const trigger = this._selectedLabelTriggerFromTarget(target);
7985
+ if (!trigger || (event.relatedTarget instanceof Node && trigger.contains(event.relatedTarget)))
7986
+ return;
7987
+ if (this._selectedLabelTooltipPointerTrigger === trigger) {
7988
+ this._selectedLabelTooltipPointerTrigger = null;
7989
+ if (this._selectedLabelTooltipDismissedTrigger === trigger) {
7990
+ this._selectedLabelTooltipDismissedTrigger = null;
7991
+ }
7992
+ }
7993
+ if (event.relatedTarget === this._selectedLabelTooltip) {
7994
+ this._selectedLabelTooltipPointerInside = true;
7995
+ this._clearSelectedLabelTooltipHideTimer();
7996
+ return;
7997
+ }
7998
+ this._scheduleSelectedLabelTooltipHide();
7999
+ }
8000
+ _handleSelectedLabelFocusIn(event) {
8001
+ const trigger = this._selectedLabelTriggerFromTarget(event.target);
8002
+ if (!trigger)
8003
+ return;
8004
+ this._selectedLabelTooltipFocusTrigger = trigger;
8005
+ this._selectedLabelTooltipDismissedTrigger = null;
8006
+ this._showSelectedLabelTooltip(trigger);
8007
+ }
8008
+ _handleSelectedLabelFocusOut(event) {
8009
+ const trigger = this._selectedLabelTriggerFromTarget(event.target);
8010
+ if (!trigger || this._selectedLabelTooltipFocusTrigger !== trigger)
8011
+ return;
8012
+ this._selectedLabelTooltipFocusTrigger = null;
8013
+ if (this._selectedLabelTooltipDismissedTrigger === trigger) {
8014
+ this._selectedLabelTooltipDismissedTrigger = null;
8015
+ }
8016
+ this._scheduleSelectedLabelTooltipHide();
8017
+ }
8018
+ _showSelectedLabelTooltip(trigger) {
8019
+ if (this._selectedLabelTooltipDismissedTrigger === trigger)
8020
+ return;
8021
+ if (!trigger.isConnected || !this._isSelectedLabelVisibleInViewport(trigger)) {
8022
+ if (this._selectedLabelTooltipTrigger === trigger)
8023
+ this._hideSelectedLabelTooltip();
8024
+ return;
8025
+ }
8026
+ const label = this._getSelectedLabelForTrigger(trigger);
8027
+ if (label === null || !this._isSelectedLabelTruncated(trigger)) {
8028
+ if (trigger.classList.contains('selection-badge-label'))
8029
+ trigger.tabIndex = -1;
8030
+ if (this._selectedLabelTooltipTrigger === trigger)
8031
+ this._hideSelectedLabelTooltip();
8032
+ return;
8033
+ }
8034
+ if (trigger.classList.contains('selection-badge-label'))
8035
+ trigger.tabIndex = 0;
8036
+ if (this._selectedLabelTooltipTrigger !== trigger) {
8037
+ this._removeSelectedLabelTooltipDescription(this._selectedLabelTooltipTrigger);
8038
+ this._selectedLabelTooltipTrigger = trigger;
8039
+ }
8040
+ this._selectedLabelTooltip.textContent = label;
8041
+ this._selectedLabelTooltip.hidden = false;
8042
+ this._positionSelectedLabelTooltip(trigger);
8043
+ this._syncSelectedLabelTooltipDescription();
8044
+ this._clearSelectedLabelTooltipHideTimer();
8045
+ }
8046
+ _positionSelectedLabelTooltip(trigger) {
8047
+ if (this._selectedLabelTooltip.hidden || !trigger.isConnected)
8048
+ return;
8049
+ const triggerRect = trigger.getBoundingClientRect();
8050
+ const tooltip = this._selectedLabelTooltip;
8051
+ tooltip.style.maxWidth = `min(24rem, calc(100vw - 16px))`;
8052
+ tooltip.style.direction = getComputedStyle(trigger).direction;
8053
+ const tooltipRect = tooltip.getBoundingClientRect();
8054
+ const viewportWidth = document.documentElement.clientWidth || window.innerWidth;
8055
+ const viewportHeight = document.documentElement.clientHeight || window.innerHeight;
8056
+ const rtl = getComputedStyle(trigger).direction === 'rtl';
8057
+ const desiredLeft = rtl ? triggerRect.right - tooltipRect.width : triggerRect.left;
8058
+ const left = Math.max(8, Math.min(desiredLeft, viewportWidth - tooltipRect.width - 8));
8059
+ const above = triggerRect.top - tooltipRect.height - 6;
8060
+ const top = above >= 8 ? above : Math.min(triggerRect.bottom + 6, viewportHeight - tooltipRect.height - 8);
8061
+ tooltip.style.left = `${left}px`;
8062
+ tooltip.style.top = `${Math.max(8, top)}px`;
8063
+ }
8064
+ _syncSelectedLabelTooltipDescription() {
8065
+ const trigger = this._selectedLabelTooltipTrigger;
8066
+ this._removeSelectedLabelTooltipDescription(trigger);
8067
+ if (trigger && !this._selectedLabelTooltip.hidden) {
8068
+ const ids = new Set((trigger.getAttribute('aria-describedby') ?? '').split(/\s+/).filter(Boolean));
8069
+ ids.add(this._selectedLabelTooltip.id);
8070
+ trigger.setAttribute('aria-describedby', [...ids].join(' '));
8071
+ }
8072
+ }
8073
+ _removeSelectedLabelTooltipDescription(trigger) {
8074
+ if (!trigger)
8075
+ return;
8076
+ const ids = (trigger.getAttribute('aria-describedby') ?? '').split(/\s+/).filter(Boolean);
8077
+ const remaining = ids.filter((id) => id !== this._selectedLabelTooltip.id);
8078
+ if (remaining.length)
8079
+ trigger.setAttribute('aria-describedby', remaining.join(' '));
8080
+ else
8081
+ trigger.removeAttribute('aria-describedby');
8082
+ }
8083
+ _dismissSelectedLabelTooltip() {
8084
+ this._selectedLabelTooltipDismissedTrigger = this._selectedLabelTooltipFocusTrigger
8085
+ ?? this._selectedLabelTooltipTrigger
8086
+ ?? this._selectedLabelTooltipPointerTrigger;
8087
+ this._hideSelectedLabelTooltip();
8088
+ }
8089
+ _hideSelectedLabelTooltip(clearInteractionState = false) {
8090
+ this._clearSelectedLabelTooltipHideTimer();
8091
+ this._removeSelectedLabelTooltipDescription(this._selectedLabelTooltipTrigger);
8092
+ this._selectedLabelTooltipTrigger = null;
8093
+ this._selectedLabelTooltip.hidden = true;
8094
+ if (clearInteractionState) {
8095
+ this._selectedLabelTooltipPointerTrigger = null;
8096
+ this._selectedLabelTooltipFocusTrigger = null;
8097
+ this._selectedLabelTooltipPointerInside = false;
8098
+ this._selectedLabelTooltipDismissedTrigger = null;
8099
+ }
8100
+ }
8101
+ _clearSelectedLabelTooltipHideTimer() {
8102
+ if (this._selectedLabelTooltipHideTimer === null)
8103
+ return;
8104
+ window.clearTimeout(this._selectedLabelTooltipHideTimer);
8105
+ this._lifecycleTimers.delete(this._selectedLabelTooltipHideTimer);
8106
+ this._selectedLabelTooltipHideTimer = null;
8107
+ }
8108
+ _scheduleSelectedLabelTooltipHide() {
8109
+ this._clearSelectedLabelTooltipHideTimer();
8110
+ this._selectedLabelTooltipHideTimer = this._setLifecycleTimeout(() => {
8111
+ this._selectedLabelTooltipHideTimer = null;
8112
+ if (this._selectedLabelTooltipPointerInside)
8113
+ return;
8114
+ const trigger = this._selectedLabelTooltipTrigger;
8115
+ if (trigger && (this._selectedLabelTooltipFocusTrigger === trigger
8116
+ || this._selectedLabelTooltipPointerTrigger === trigger))
8117
+ return;
8118
+ const focusedTrigger = this._selectedLabelTooltipFocusTrigger;
8119
+ if (focusedTrigger && focusedTrigger !== this._selectedLabelTooltipDismissedTrigger
8120
+ && focusedTrigger.isConnected
8121
+ && this._isSelectedLabelVisibleInViewport(focusedTrigger)) {
8122
+ this._showSelectedLabelTooltip(focusedTrigger);
8123
+ return;
8124
+ }
8125
+ this._hideSelectedLabelTooltip();
8126
+ }, 180);
8127
+ }
8128
+ _isSelectedLabelVisibleInViewport(trigger) {
8129
+ if (!trigger.classList.contains('selection-badge-label'))
8130
+ return true;
8131
+ const viewport = this._inputContent;
8132
+ const triggerRect = trigger.getBoundingClientRect();
8133
+ const viewportRect = viewport.getBoundingClientRect();
8134
+ // DOM-only test environments do not calculate layout rectangles. Leave
8135
+ // visibility indeterminate there; browser measurement handles real layout.
8136
+ if (triggerRect.width === 0 && triggerRect.height === 0
8137
+ && viewportRect.width === 0 && viewportRect.height === 0)
8138
+ return true;
8139
+ const left = viewportRect.left + viewport.clientLeft;
8140
+ const top = viewportRect.top + viewport.clientTop;
8141
+ const right = left + viewport.clientWidth;
8142
+ const bottom = top + viewport.clientHeight;
8143
+ return triggerRect.right > left && triggerRect.left < right
8144
+ && triggerRect.bottom > top && triggerRect.top < bottom;
8145
+ }
8146
+ _observeSelectedLabelTooltipTargets() {
8147
+ if (typeof ResizeObserver === 'undefined')
8148
+ return;
8149
+ const hasSingleSelection = this._config.selection.mode === 'single'
8150
+ && this._getSelectedEntries().length > 0;
8151
+ const chipLabels = this._getActiveChipBadges()
8152
+ .map((badge) => badge.querySelector('.selection-badge-label'))
8153
+ .filter((label) => label !== null);
8154
+ if (!hasSingleSelection && chipLabels.length === 0) {
8155
+ this._selectedLabelTooltipResizeObserver?.disconnect();
8156
+ this._selectedLabelTooltipResizeObserver = null;
8157
+ return;
8158
+ }
8159
+ if (!this._selectedLabelTooltipResizeObserver) {
8160
+ this._selectedLabelTooltipResizeObserver = new ResizeObserver(() => this._scheduleSelectedLabelTooltipMeasure());
8161
+ }
8162
+ this._selectedLabelTooltipResizeObserver.disconnect();
8163
+ if (hasSingleSelection)
8164
+ this._selectedLabelTooltipResizeObserver.observe(this._input);
8165
+ chipLabels.forEach((label) => {
8166
+ this._selectedLabelTooltipResizeObserver?.observe(label);
8167
+ });
8168
+ }
8169
+ _refreshSelectedLabelTooltipMeasurements() {
8170
+ this._getActiveChipBadges().forEach((badge) => {
8171
+ const label = badge.querySelector('.selection-badge-label');
8172
+ if (!label)
8173
+ return;
8174
+ label.tabIndex = this._isSelectedLabelTruncated(label) ? 0 : -1;
8175
+ });
8176
+ const currentTrigger = this._selectedLabelTooltipTrigger;
8177
+ if (currentTrigger?.classList.contains('selection-badge-label')
8178
+ && !this._isSelectedLabelVisibleInViewport(currentTrigger)) {
8179
+ if (this._selectedLabelTooltipPointerTrigger === currentTrigger) {
8180
+ this._selectedLabelTooltipPointerTrigger = null;
8181
+ }
8182
+ this._hideSelectedLabelTooltip();
8183
+ }
8184
+ const trigger = this._selectedLabelTooltipTrigger
8185
+ ?? this._selectedLabelTooltipFocusTrigger
8186
+ ?? this._selectedLabelTooltipPointerTrigger;
8187
+ if (!trigger)
8188
+ return;
8189
+ if (!trigger.isConnected || !this._isSelectedLabelVisibleInViewport(trigger)) {
8190
+ if (this._selectedLabelTooltipPointerTrigger === trigger) {
8191
+ this._selectedLabelTooltipPointerTrigger = null;
8192
+ }
8193
+ if (this._selectedLabelTooltipTrigger === trigger)
8194
+ this._hideSelectedLabelTooltip();
8195
+ return;
8196
+ }
8197
+ const label = this._getSelectedLabelForTrigger(trigger);
8198
+ if (label === null || !this._isSelectedLabelTruncated(trigger)) {
8199
+ if (this._selectedLabelTooltipTrigger === trigger)
8200
+ this._hideSelectedLabelTooltip();
8201
+ return;
8202
+ }
8203
+ this._showSelectedLabelTooltip(trigger);
7806
8204
  }
7807
8205
  _syncListboxAccessibility() {
7808
8206
  if (!this._dropdown || !this._engine || !this._state)
@@ -8399,6 +8797,10 @@
8399
8797
  return results;
8400
8798
  }
8401
8799
  _handleSearch(query) {
8800
+ if (this._selectedLabelTooltipTrigger === this._input
8801
+ && (query.length > 0 || this._getSelectedLabelForTrigger(this._input) === null)) {
8802
+ this._hideSelectedLabelTooltip();
8803
+ }
8402
8804
  if (this._dataRequestCoordinator
8403
8805
  && (this._acknowledgedProjectionPending || this._acknowledgedProjectionSettling)) {
8404
8806
  // Keep the committed query/generation stable until the accepted snapshot
@@ -8503,6 +8905,12 @@
8503
8905
  }
8504
8906
  }
8505
8907
  _handleKeydown(e) {
8908
+ if (e.key === 'Escape' && !this._selectedLabelTooltip.hidden) {
8909
+ e.preventDefault();
8910
+ e.stopPropagation();
8911
+ this._dismissSelectedLabelTooltip();
8912
+ return;
8913
+ }
8506
8914
  if (!this._config.enabled)
8507
8915
  return;
8508
8916
  switch (e.key) {
@@ -8576,6 +8984,9 @@
8576
8984
  break;
8577
8985
  case 'Enter':
8578
8986
  case ' ':
8987
+ if (e.key === ' ' && this._config.searchable && e.target === this._input) {
8988
+ break;
8989
+ }
8579
8990
  e.preventDefault();
8580
8991
  if (!this._state.isOpen) {
8581
8992
  this._handleOpen();
@@ -8599,7 +9010,8 @@
8599
9010
  break;
8600
9011
  case 'a':
8601
9012
  case 'A':
8602
- if ((e.ctrlKey || e.metaKey) && this._config.selection.mode === 'multi') {
9013
+ if ((e.ctrlKey || e.metaKey) && !e.altKey
9014
+ && this._config.selection.mode === 'multi' && !this._config.searchable) {
8603
9015
  e.preventDefault();
8604
9016
  this._selectAll();
8605
9017
  }
@@ -9070,7 +9482,7 @@
9070
9482
  : null;
9071
9483
  const hadFocusedChipRemove = focusedBadge !== null;
9072
9484
  const focusedBadgeIndex = focusedBadge
9073
- ? Array.from(this._inputContent.querySelectorAll('.selection-badge')).indexOf(focusedBadge)
9485
+ ? this._getActiveChipBadges().indexOf(focusedBadge)
9074
9486
  : -1;
9075
9487
  const option = this._itemRegistry.keyToRecord.has(entry.key)
9076
9488
  ? this._getOptionElementByIndex(entry.index)
@@ -9096,7 +9508,7 @@
9096
9508
  this._renderOptions();
9097
9509
  this._emitChange();
9098
9510
  if (hadFocusedChipRemove && this.isConnected) {
9099
- const removeButtons = this._inputContent.querySelectorAll('[part="chip-remove"]');
9511
+ const removeButtons = this._getActiveChipRemoveButtons();
9100
9512
  if (removeButtons.length > 0) {
9101
9513
  const nextFocusIndex = Math.min(Math.max(focusedBadgeIndex, 0), removeButtons.length - 1);
9102
9514
  removeButtons[nextFocusIndex].focus();
@@ -9110,8 +9522,248 @@
9110
9522
  : undefined;
9111
9523
  this._emit('remove', { item: config?.item ?? item, index: entry.index });
9112
9524
  }
9525
+ _getActiveChipBadges() {
9526
+ return Array.from(this._inputContent.querySelectorAll('.selection-badge'))
9527
+ .filter((badge) => badge.dataset.motion !== 'exiting' && !badge.hasAttribute('inert'));
9528
+ }
9529
+ _getActiveChipRemoveButtons() {
9530
+ return this._getActiveChipBadges()
9531
+ .map((badge) => badge.querySelector('[part="chip-remove"]'))
9532
+ .filter((button) => button !== null && !button.disabled);
9533
+ }
9534
+ _clearChipPresentation() {
9535
+ const badges = new Set(this._chipElements.values());
9536
+ const tooltipTriggers = [
9537
+ this._selectedLabelTooltipTrigger,
9538
+ this._selectedLabelTooltipPointerTrigger,
9539
+ this._selectedLabelTooltipFocusTrigger,
9540
+ this._selectedLabelTooltipDismissedTrigger,
9541
+ ];
9542
+ if (tooltipTriggers.some((trigger) => trigger !== null
9543
+ && Array.from(badges).some((badge) => badge.contains(trigger)))) {
9544
+ this._hideSelectedLabelTooltip(true);
9545
+ }
9546
+ for (const [key, state] of this._chipExitStates) {
9547
+ window.clearTimeout(state.timer);
9548
+ this._chipExitStates.delete(key);
9549
+ state.animations.forEach((animation) => animation.cancel());
9550
+ }
9551
+ for (const badge of this._chipElements.values()) {
9552
+ badge.removeAttribute('inert');
9553
+ badge.remove();
9554
+ }
9555
+ this._chipExitStates.clear();
9556
+ this._chipElements.clear();
9557
+ }
9558
+ _cancelChipExit(key, badge) {
9559
+ const state = this._chipExitStates.get(key);
9560
+ if (!state || state.badge !== badge)
9561
+ return;
9562
+ window.clearTimeout(state.timer);
9563
+ this._chipExitStates.delete(key);
9564
+ state.animations.forEach((animation) => animation.cancel());
9565
+ badge.removeAttribute('data-motion');
9566
+ badge.removeAttribute('aria-hidden');
9567
+ badge.removeAttribute('inert');
9568
+ badge.setAttribute('part', 'chip');
9569
+ const removeButton = badge.querySelector('[part="chip-remove"]');
9570
+ if (removeButton)
9571
+ removeButton.disabled = false;
9572
+ }
9573
+ _finishChipExit(key, expected) {
9574
+ const state = this._chipExitStates.get(key);
9575
+ if (!state || state.badge !== expected)
9576
+ return;
9577
+ window.clearTimeout(state.timer);
9578
+ this._chipExitStates.delete(key);
9579
+ state.animations.forEach((animation) => {
9580
+ if (animation.playState !== 'finished')
9581
+ animation.cancel();
9582
+ });
9583
+ this._chipElements.delete(key);
9584
+ expected.remove();
9585
+ if (this.isConnected) {
9586
+ this._observeSelectedLabelTooltipTargets();
9587
+ this._refreshSelectedLabelTooltipMeasurements();
9588
+ }
9589
+ }
9590
+ _moveFocusOutOfChip(badge) {
9591
+ const activeElement = this._shadow.activeElement;
9592
+ if (!(activeElement instanceof HTMLElement) || !badge.contains(activeElement))
9593
+ return;
9594
+ const badges = this._getActiveChipBadges().filter((candidate) => candidate !== badge);
9595
+ const oldIndex = Array.from(this._inputContent.querySelectorAll('.selection-badge')).indexOf(badge);
9596
+ const buttons = badges
9597
+ .map((candidate) => candidate.querySelector('[part="chip-remove"]'))
9598
+ .filter((button) => button !== null && !button.disabled);
9599
+ const next = buttons[Math.min(Math.max(oldIndex, 0), buttons.length - 1)];
9600
+ if (next)
9601
+ next.focus({ preventScroll: true });
9602
+ else
9603
+ this._input.focus({ preventScroll: true });
9604
+ }
9605
+ _beginChipExit(key, badge) {
9606
+ if (this._chipExitStates.has(key))
9607
+ return;
9608
+ this._moveFocusOutOfChip(badge);
9609
+ const label = badge.querySelector('.selection-badge-label');
9610
+ if (label) {
9611
+ this._removeSelectedLabelTooltipDescription(label);
9612
+ label.tabIndex = -1;
9613
+ if (this._selectedLabelTooltipPointerTrigger === label)
9614
+ this._selectedLabelTooltipPointerTrigger = null;
9615
+ if (this._selectedLabelTooltipFocusTrigger === label)
9616
+ this._selectedLabelTooltipFocusTrigger = null;
9617
+ if (this._selectedLabelTooltipDismissedTrigger === label)
9618
+ this._selectedLabelTooltipDismissedTrigger = null;
9619
+ if (this._selectedLabelTooltipTrigger === label)
9620
+ this._hideSelectedLabelTooltip();
9621
+ }
9622
+ badge.dataset.motion = 'exiting';
9623
+ badge.setAttribute('aria-hidden', 'true');
9624
+ badge.setAttribute('inert', '');
9625
+ badge.setAttribute('part', 'chip-exiting');
9626
+ const removeButton = badge.querySelector('[part="chip-remove"]');
9627
+ if (removeButton)
9628
+ removeButton.disabled = true;
9629
+ // Force style resolution after the exiting part is exposed. This also
9630
+ // verifies that a consumer keyframe is actually available across Shadow DOM.
9631
+ badge.getBoundingClientRect();
9632
+ const animations = (typeof badge.getAnimations === 'function' ? badge.getAnimations() : []).filter((animation) => {
9633
+ const cssAnimation = animation;
9634
+ return typeof cssAnimation.animationName === 'string'
9635
+ && cssAnimation.animationName !== 'none'
9636
+ && animation.playState !== 'idle';
9637
+ });
9638
+ const reducedMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
9639
+ if (reducedMotion || animations.length === 0) {
9640
+ badge.removeAttribute('data-motion');
9641
+ this._chipExitStates.set(key, { badge, animations: [], timer: 0 });
9642
+ this._finishChipExit(key, badge);
9643
+ return;
9644
+ }
9645
+ const endTimes = animations.map((animation) => Number(animation.effect?.getComputedTiming().endTime ?? 0));
9646
+ const maxEndTime = Math.max(0, ...endTimes);
9647
+ const fallbackDelay = Math.min(Number.isFinite(maxEndTime) ? maxEndTime + 100 : 1000, 5000);
9648
+ const timer = window.setTimeout(() => this._finishChipExit(key, badge), fallbackDelay);
9649
+ this._chipExitStates.set(key, { badge, animations, timer });
9650
+ void Promise.allSettled(animations.map((animation) => animation.finished))
9651
+ .then(() => this._finishChipExit(key, badge));
9652
+ }
9653
+ _createChip(key) {
9654
+ const badge = document.createElement('span');
9655
+ badge.className = 'selection-badge';
9656
+ badge.setAttribute('part', 'chip');
9657
+ const label = document.createElement('span');
9658
+ label.className = 'selection-badge-label';
9659
+ label.setAttribute('part', 'chip-label');
9660
+ this._selectedLabelKeys.set(label, key);
9661
+ badge.appendChild(label);
9662
+ this._chipElements.set(key, badge);
9663
+ return badge;
9664
+ }
9665
+ _updateChip(key, index, item, badge) {
9666
+ const getLabel = this._config.serverSide.getLabelFromItem || ((value) => value?.label ?? String(value));
9667
+ const labelText = getLabel(item);
9668
+ badge.className = 'selection-badge';
9669
+ if (this._config.styles.classNames?.badge) {
9670
+ badge.classList.add(...this._config.styles.classNames.badge.split(' ').filter(Boolean));
9671
+ }
9672
+ badge.setAttribute('part', 'chip');
9673
+ badge.removeAttribute('aria-hidden');
9674
+ badge.removeAttribute('inert');
9675
+ badge.removeAttribute('data-motion');
9676
+ const label = badge.querySelector('.selection-badge-label');
9677
+ label.className = 'selection-badge-label';
9678
+ if (this._config.styles.classNames?.badgeLabel) {
9679
+ label.classList.add(...this._config.styles.classNames.badgeLabel.split(' ').filter(Boolean));
9680
+ }
9681
+ label.textContent = labelText;
9682
+ this._selectedLabelKeys.set(label, key);
9683
+ const shouldShowRemove = this._config.selection.showRemoveButton !== false;
9684
+ let removeButton = badge.querySelector('[part="chip-remove"]');
9685
+ if (!shouldShowRemove) {
9686
+ removeButton?.remove();
9687
+ return;
9688
+ }
9689
+ if (!removeButton) {
9690
+ removeButton = document.createElement('button');
9691
+ removeButton.type = 'button';
9692
+ removeButton.addEventListener('pointerdown', (event) => {
9693
+ event.stopPropagation();
9694
+ event.preventDefault();
9695
+ });
9696
+ removeButton.addEventListener('click', (event) => {
9697
+ event.stopPropagation();
9698
+ event.preventDefault();
9699
+ this._handleOptionRemove(index, key);
9700
+ });
9701
+ const removeIcon = document.createElement('span');
9702
+ removeIcon.className = 'badge-remove-icon';
9703
+ removeIcon.setAttribute('part', 'chip-remove-icon');
9704
+ removeButton.appendChild(removeIcon);
9705
+ badge.appendChild(removeButton);
9706
+ }
9707
+ removeButton.className = 'badge-remove';
9708
+ if (this._config.styles.classNames?.removeButton) {
9709
+ removeButton.classList.add(...this._config.styles.classNames.removeButton.split(' ').filter(Boolean));
9710
+ }
9711
+ if (this._config.styles.classNames?.badgeRemove) {
9712
+ removeButton.classList.add(...this._config.styles.classNames.badgeRemove.split(' ').filter(Boolean));
9713
+ }
9714
+ removeButton.setAttribute('part', 'chip-remove');
9715
+ removeButton.setAttribute('aria-label', `Remove ${labelText}`);
9716
+ removeButton.disabled = false;
9717
+ const removeIcon = removeButton.querySelector('[part="chip-remove-icon"]');
9718
+ removeIcon.className = 'badge-remove-icon';
9719
+ if (this._config.styles.classNames?.badgeRemoveIcon) {
9720
+ removeIcon.classList.add(...this._config.styles.classNames.badgeRemoveIcon.split(' ').filter(Boolean));
9721
+ }
9722
+ this._setIconContent(removeIcon, this._config.selection.removeButtonIcon, '×');
9723
+ }
9724
+ _renderSelectedChips(entries) {
9725
+ const selectedKeys = new Set(entries.map(({ key }) => key));
9726
+ for (const { key, index, item } of entries) {
9727
+ let badge = this._chipElements.get(key);
9728
+ if (!badge)
9729
+ badge = this._createChip(key);
9730
+ this._cancelChipExit(key, badge);
9731
+ this._updateChip(key, index, item, badge);
9732
+ }
9733
+ for (const [key, badge] of this._chipElements) {
9734
+ if (!selectedKeys.has(key))
9735
+ this._beginChipExit(key, badge);
9736
+ }
9737
+ // Do not move chips merely because another chip entered its exit period.
9738
+ // That keeps the exiting chip in its original layout slot while the active
9739
+ // sequence remains correct. Reorder only when selected-key order changed.
9740
+ const desiredBadges = entries
9741
+ .map(({ key }) => this._chipElements.get(key))
9742
+ .filter((badge) => badge !== undefined);
9743
+ const currentBadges = this._getActiveChipBadges();
9744
+ const orderChanged = desiredBadges.length !== currentBadges.length
9745
+ || desiredBadges.some((badge, index) => badge !== currentBadges[index]);
9746
+ if (orderChanged) {
9747
+ let nextSibling = this._input;
9748
+ for (let index = desiredBadges.length - 1; index >= 0; index -= 1) {
9749
+ const badge = desiredBadges[index];
9750
+ if (badge.nextSibling !== nextSibling)
9751
+ this._inputContent.insertBefore(badge, nextSibling);
9752
+ nextSibling = badge;
9753
+ }
9754
+ }
9755
+ }
9113
9756
  _updateInputDisplay() {
9114
9757
  const selectedEntries = this._getSelectedEntries();
9758
+ const focusedElement = this._shadow.activeElement;
9759
+ const focusedSelectedKey = focusedElement?.classList.contains('selection-badge-label')
9760
+ ? this._selectedLabelKeys.get(focusedElement) ?? null
9761
+ : null;
9762
+ const focusedBadge = focusedElement?.closest('.selection-badge');
9763
+ const focusedChipKey = focusedBadge
9764
+ ? Array.from(this._chipElements.entries()).find(([, badge]) => badge === focusedBadge)?.[0] ?? null
9765
+ : null;
9766
+ const focusedBadgeIndex = focusedBadge ? this._getActiveChipBadges().indexOf(focusedBadge) : -1;
9115
9767
  const selectedItems = selectedEntries.map(({ item }) => item);
9116
9768
  const nextKeys = new Set(selectedEntries.map(({ key }) => key));
9117
9769
  const previousKeys = this._lastRenderedChipKeys;
@@ -9127,73 +9779,27 @@
9127
9779
  // Only a newly added selected key can initiate auto-scroll.
9128
9780
  this._cancelLatestChipScroll();
9129
9781
  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]);
9782
+ if (this._config.selection.mode === 'multi') {
9783
+ if (selectedItems.length === 0) {
9784
+ this._input.value = '';
9785
+ this._input.placeholder = this._config.placeholder || 'Select an option...';
9786
+ }
9787
+ else {
9788
+ this._input.value = '';
9789
+ this._input.placeholder = '';
9790
+ }
9791
+ this._renderSelectedChips(selectedEntries);
9139
9792
  }
9140
9793
  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
- });
9794
+ // A mode switch ends visual exits immediately; a single-select has no chip presentation.
9795
+ this._clearChipPresentation();
9796
+ if (selectedItems.length === 0) {
9797
+ this._input.value = '';
9798
+ this._input.placeholder = this._config.placeholder || 'Select an option...';
9799
+ }
9800
+ else {
9801
+ this._input.value = getLabel(selectedItems[0]);
9802
+ }
9197
9803
  }
9198
9804
  // A query typed during an accepted-snapshot projection is a pending user
9199
9805
  // intent, not part of the committed projection state. Keep it visible even
@@ -9201,14 +9807,60 @@
9201
9807
  if (this._acknowledgedPendingSearchQuery !== null) {
9202
9808
  this._input.value = this._acknowledgedPendingSearchQuery;
9203
9809
  }
9204
- // Recreating the chip DOM must not reset manual scrolling. In particular,
9205
- // remove/clear, config rerenders, and item refreshes are not scroll intents.
9810
+ // Preserve manual scrolling. Only a newly selected key can request auto-scroll.
9206
9811
  if (this._config.selection.mode === 'multi') {
9207
9812
  this._inputContent.scrollLeft = previousChipScrollLeft;
9208
9813
  if (newlyAddedKey !== undefined)
9209
9814
  this._scheduleLatestChipScroll(newlyAddedKey);
9210
9815
  }
9211
9816
  this._syncClearControlState();
9817
+ this._observeSelectedLabelTooltipTargets();
9818
+ this._refreshSelectedLabelTooltipMeasurements();
9819
+ if (focusedChipKey !== null && !nextKeys.has(focusedChipKey)) {
9820
+ const removeButtons = this._getActiveChipRemoveButtons();
9821
+ if (removeButtons.length > 0) {
9822
+ const nextFocusIndex = Math.min(Math.max(focusedBadgeIndex, 0), removeButtons.length - 1);
9823
+ removeButtons[nextFocusIndex].focus({ preventScroll: true });
9824
+ }
9825
+ else {
9826
+ this._input.focus({ preventScroll: true });
9827
+ }
9828
+ }
9829
+ else if (focusedChipKey !== null && focusedElement && !focusedElement.isConnected) {
9830
+ const badge = this._chipElements.get(focusedChipKey);
9831
+ const removeButton = badge?.querySelector('[part="chip-remove"]');
9832
+ if (removeButton && !removeButton.disabled)
9833
+ removeButton.focus({ preventScroll: true });
9834
+ else
9835
+ this._input.focus({ preventScroll: true });
9836
+ }
9837
+ else if (focusedSelectedKey !== null) {
9838
+ const replacement = this._getActiveChipBadges()
9839
+ .map((badge) => badge.querySelector('.selection-badge-label'))
9840
+ .find((label) => label && this._selectedLabelKeys.get(label) === focusedSelectedKey);
9841
+ if (!replacement) {
9842
+ const removeButtons = this._getActiveChipRemoveButtons();
9843
+ if (removeButtons.length > 0) {
9844
+ const nextFocusIndex = Math.min(Math.max(focusedBadgeIndex, 0), removeButtons.length - 1);
9845
+ removeButtons[nextFocusIndex].focus({ preventScroll: true });
9846
+ }
9847
+ else {
9848
+ this._input.focus({ preventScroll: true });
9849
+ }
9850
+ }
9851
+ else if (this._isSelectedLabelTruncated(replacement)) {
9852
+ // The measurement pass assigns the tab stop before focus restoration.
9853
+ replacement.tabIndex = 0;
9854
+ replacement.focus({ preventScroll: true });
9855
+ }
9856
+ else {
9857
+ this._input.focus({ preventScroll: true });
9858
+ }
9859
+ }
9860
+ if (this._selectedLabelTooltipFocusTrigger === this._input
9861
+ || this._selectedLabelTooltipPointerTrigger === this._input) {
9862
+ this._showSelectedLabelTooltip(this._input);
9863
+ }
9212
9864
  }
9213
9865
  _cancelLatestChipScroll() {
9214
9866
  this._latestChipScrollGeneration += 1;
@@ -9240,7 +9892,7 @@
9240
9892
  const index = entries.findIndex((entry) => entry.key === key);
9241
9893
  if (index < 0)
9242
9894
  return;
9243
- const chip = this._inputContent.querySelectorAll('.selection-badge')[index];
9895
+ const chip = this._getActiveChipBadges()[index];
9244
9896
  if (!chip)
9245
9897
  return;
9246
9898
  const hostBounds = this._inputContent.getBoundingClientRect();