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