@smilodon/core 1.0.6 → 1.0.7

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.cjs CHANGED
@@ -1910,26 +1910,44 @@ class EnhancedSelect extends HTMLElement {
1910
1910
  return;
1911
1911
  const target = this._config.scrollToSelected.multiSelectTarget;
1912
1912
  const indices = Array.from(this._state.selectedIndices).sort((a, b) => a - b);
1913
- const targetIndex = target === 'first' ? indices[0] : indices[indices.length - 1];
1914
- // FIX: Find the option element by ID instead of index in children
1915
- // because children list might be filtered or reordered
1913
+ // For multi-select, find the closest selected item to the current scroll position
1914
+ let targetIndex;
1915
+ if (this._config.selection.mode === 'multi' && indices.length > 1) {
1916
+ // Calculate which selected item is closest to the center of the viewport
1917
+ const dropdownRect = this._dropdown.getBoundingClientRect();
1918
+ const viewportCenter = this._dropdown.scrollTop + (dropdownRect.height / 2);
1919
+ // Find the selected item closest to viewport center
1920
+ let closestIndex = indices[0];
1921
+ let closestDistance = Infinity;
1922
+ for (const index of indices) {
1923
+ const optionId = `${this._uniqueId}-option-${index}`;
1924
+ const option = this._optionsContainer.querySelector(`[id="${optionId}"]`);
1925
+ if (option) {
1926
+ const optionTop = option.offsetTop;
1927
+ const distance = Math.abs(optionTop - viewportCenter);
1928
+ if (distance < closestDistance) {
1929
+ closestDistance = distance;
1930
+ closestIndex = index;
1931
+ }
1932
+ }
1933
+ }
1934
+ targetIndex = closestIndex;
1935
+ }
1936
+ else {
1937
+ // For single select or only one selected item, use the configured target
1938
+ targetIndex = target === 'first' ? indices[0] : indices[indices.length - 1];
1939
+ }
1940
+ // Find and scroll to the target option
1916
1941
  const optionId = `${this._uniqueId}-option-${targetIndex}`;
1917
- // We need to search in shadow root or options container
1918
- // Since options are custom elements, we can find them by ID if we set it (we do)
1919
- // But wait, we set ID on the element instance, but is it in the DOM?
1920
- // If filtered out, it won't be in the DOM.
1921
- // If we are searching, we might not want to scroll to selected if it's not visible
1922
- // But if we just opened the dropdown, we usually want to see the selected item.
1923
- // If the selected item is filtered out, we can't scroll to it.
1924
- // Try to find the element in the options container
1925
- // Note: querySelector on shadowRoot works if we set the ID attribute
1926
- // In _renderOptions we set: option.id = ...
1927
1942
  const option = this._optionsContainer.querySelector(`[id="${optionId}"]`);
1928
1943
  if (option) {
1944
+ // Use smooth scrolling with center alignment for better UX
1929
1945
  option.scrollIntoView({
1930
1946
  block: this._config.scrollToSelected.block || 'center',
1931
1947
  behavior: 'smooth',
1932
1948
  });
1949
+ // Also set it as active for keyboard navigation
1950
+ this._setActive(targetIndex);
1933
1951
  }
1934
1952
  }
1935
1953
  async _loadMoreItems() {