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