@mirror-physics/fractal-ui 0.2.0-next.84 → 0.2.0-next.85

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
@@ -1497,6 +1497,9 @@ function Dropdown({
1497
1497
  }) {
1498
1498
  const [query, setQuery] = React11.useState("");
1499
1499
  const searchRef = React11.useRef(null);
1500
+ const searchContainerRef = React11.useRef(null);
1501
+ const optionsRef = React11.useRef(null);
1502
+ const [optionsScrolled, setOptionsScrolled] = React11.useState(false);
1500
1503
  const accessibleLabel = triggerLabel ?? fieldLabel ?? "Options";
1501
1504
  const size = controlSize ?? sizeAlias;
1502
1505
  const canClear = clearable && !disabled && (multiple ? selectedValues.length > 0 && Boolean(onSelectionChange) : Boolean(selectedValue) && Boolean(onSelect));
@@ -1534,6 +1537,11 @@ function Dropdown({
1534
1537
  setOpen(false);
1535
1538
  triggerRef.current?.focus();
1536
1539
  }, open);
1540
+ React11.useLayoutEffect(() => {
1541
+ if (!open) return;
1542
+ if (optionsRef.current) optionsRef.current.scrollTop = 0;
1543
+ setOptionsScrolled(false);
1544
+ }, [open, query]);
1537
1545
  React11.useLayoutEffect(() => {
1538
1546
  const root = rootRef.current;
1539
1547
  const trigger = triggerRef.current;
@@ -1571,7 +1579,7 @@ function Dropdown({
1571
1579
  panel.style.maxWidth = `${availableWidth}px`;
1572
1580
  panel.style.minWidth = `${Math.min(triggerRect.width, availableWidth)}px`;
1573
1581
  const panelRect = panel.getBoundingClientRect();
1574
- const desiredHeight = Math.min(heightLimit, panel.scrollHeight + panel.offsetHeight - panel.clientHeight);
1582
+ const desiredHeight = Math.min(heightLimit, (optionsRef.current?.scrollHeight ?? 0) + (searchContainerRef.current?.offsetHeight ?? 0) + parseFloat(getComputedStyle(panel).paddingTop) + parseFloat(getComputedStyle(panel).paddingBottom) + panel.offsetHeight - panel.clientHeight);
1575
1583
  const position = positionDropdown(triggerRect, { width: panelRect.width, height: desiredHeight }, bounds, direction, align);
1576
1584
  const nextStyle = {
1577
1585
  left: position.left - rootRect.left - root.clientLeft + root.scrollLeft,
@@ -1596,6 +1604,7 @@ function Dropdown({
1596
1604
  const resize = new ResizeObserver(updatePosition);
1597
1605
  resize.observe(root);
1598
1606
  resize.observe(panel);
1607
+ if (optionsRef.current) resize.observe(optionsRef.current);
1599
1608
  for (let ancestor = root.parentElement; ancestor; ancestor = ancestor.parentElement) {
1600
1609
  resize.observe(ancestor);
1601
1610
  }
@@ -1695,11 +1704,11 @@ function Dropdown({
1695
1704
  className: cn("fractal-dropdown-panel", `fractal-dropdown-panel--${size}`, panelClassName),
1696
1705
  style: panelStyle,
1697
1706
  children: [
1698
- searchable && /* @__PURE__ */ jsxs17("div", { className: "fractal-dropdown-search", children: [
1707
+ searchable && /* @__PURE__ */ jsx19("div", { ref: searchContainerRef, className: "fractal-dropdown-search-container", "data-scrolled": optionsScrolled, children: /* @__PURE__ */ jsxs17("div", { className: "fractal-dropdown-search", children: [
1699
1708
  /* @__PURE__ */ jsx19(MagnifyingGlass2, { "aria-hidden": "true" }),
1700
1709
  /* @__PURE__ */ jsx19("input", { ref: searchRef, type: "search", "aria-label": searchPlaceholder, placeholder: searchPlaceholder, value: query, onChange: (event) => setQuery(event.target.value) })
1701
- ] }),
1702
- /* @__PURE__ */ jsxs17("div", { role: "listbox", id: `${menuId}-options`, "aria-label": accessibleLabel, "aria-multiselectable": multiple || void 0, children: [
1710
+ ] }) }),
1711
+ /* @__PURE__ */ jsxs17("div", { ref: optionsRef, className: "fractal-dropdown-options", onScroll: (event) => setOptionsScrolled(event.currentTarget.scrollTop > 0), role: "listbox", id: `${menuId}-options`, "aria-label": accessibleLabel, "aria-multiselectable": multiple || void 0, children: [
1703
1712
  label != null && /* @__PURE__ */ jsx19("div", { className: "fractal-dropdown-label", children: label }),
1704
1713
  visibleItems.map((item, index) => {
1705
1714
  const topDivider = item.border === "top" ? /* @__PURE__ */ jsx19("div", { className: "fractal-dropdown-separator" }) : null;
@@ -1739,9 +1748,9 @@ function Dropdown({
1739
1748
  ),
1740
1749
  bottomDivider
1741
1750
  ] }, item.value);
1742
- })
1743
- ] }),
1744
- !selectableItems.length && /* @__PURE__ */ jsx19("p", { role: "status", className: "fractal-dropdown-label", children: emptyMessage })
1751
+ }),
1752
+ !selectableItems.length && /* @__PURE__ */ jsx19("p", { role: "status", className: "fractal-dropdown-label", children: emptyMessage })
1753
+ ] })
1745
1754
  ]
1746
1755
  }
1747
1756
  )