@smilodon/core 1.0.6 → 1.0.8

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
@@ -1559,8 +1559,13 @@ class EnhancedSelect extends HTMLElement {
1559
1559
  this._config.callbacks.onOpen?.();
1560
1560
  // Scroll to selected if configured
1561
1561
  if (this._config.scrollToSelected.enabled) {
1562
- // Use setTimeout to allow render to complete
1563
- setTimeout(() => this._scrollToSelected(), 0);
1562
+ // Use requestAnimationFrame for better timing after render
1563
+ requestAnimationFrame(() => {
1564
+ // Double RAF to ensure layout is complete
1565
+ requestAnimationFrame(() => {
1566
+ this._scrollToSelected();
1567
+ });
1568
+ });
1564
1569
  }
1565
1570
  }
1566
1571
  _handleClose() {
@@ -1910,26 +1915,44 @@ class EnhancedSelect extends HTMLElement {
1910
1915
  return;
1911
1916
  const target = this._config.scrollToSelected.multiSelectTarget;
1912
1917
  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
1918
+ // For multi-select, find the closest selected item to the current scroll position
1919
+ let targetIndex;
1920
+ if (this._config.selection.mode === 'multi' && indices.length > 1) {
1921
+ // Calculate which selected item is closest to the center of the viewport
1922
+ const dropdownRect = this._dropdown.getBoundingClientRect();
1923
+ const viewportCenter = this._dropdown.scrollTop + (dropdownRect.height / 2);
1924
+ // Find the selected item closest to viewport center
1925
+ let closestIndex = indices[0];
1926
+ let closestDistance = Infinity;
1927
+ for (const index of indices) {
1928
+ const optionId = `${this._uniqueId}-option-${index}`;
1929
+ const option = this._optionsContainer.querySelector(`[id="${optionId}"]`);
1930
+ if (option) {
1931
+ const optionTop = option.offsetTop;
1932
+ const distance = Math.abs(optionTop - viewportCenter);
1933
+ if (distance < closestDistance) {
1934
+ closestDistance = distance;
1935
+ closestIndex = index;
1936
+ }
1937
+ }
1938
+ }
1939
+ targetIndex = closestIndex;
1940
+ }
1941
+ else {
1942
+ // For single select or only one selected item, use the configured target
1943
+ targetIndex = target === 'first' ? indices[0] : indices[indices.length - 1];
1944
+ }
1945
+ // Find and scroll to the target option
1916
1946
  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
1947
  const option = this._optionsContainer.querySelector(`[id="${optionId}"]`);
1928
1948
  if (option) {
1949
+ // Use smooth scrolling with center alignment for better UX
1929
1950
  option.scrollIntoView({
1930
1951
  block: this._config.scrollToSelected.block || 'center',
1931
1952
  behavior: 'smooth',
1932
1953
  });
1954
+ // Also set it as active for keyboard navigation
1955
+ this._setActive(targetIndex);
1933
1956
  }
1934
1957
  }
1935
1958
  async _loadMoreItems() {