@xsolla/xui-context-menu 0.226.0 → 0.228.0

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/native/index.mjs CHANGED
@@ -1256,7 +1256,8 @@ var useContextMenuPosition = ({
1256
1256
  panelRef,
1257
1257
  isOpen,
1258
1258
  placement = "bottom-start",
1259
- offset = 4
1259
+ offset = 4,
1260
+ matchTriggerWidth = false
1260
1261
  }) => {
1261
1262
  const [resolved, setResolved] = useState3();
1262
1263
  const lockedVerticalRef = useRef2(null);
@@ -1267,19 +1268,20 @@ var useContextMenuPosition = ({
1267
1268
  return;
1268
1269
  }
1269
1270
  const compute = (keepSide) => {
1270
- const trigger = triggerRef.current;
1271
+ const trigger2 = triggerRef.current;
1271
1272
  const panel2 = panelRef.current;
1272
- if (!trigger || !panel2) return;
1273
- const triggerRect = trigger.getBoundingClientRect();
1273
+ if (!trigger2 || !panel2) return;
1274
+ const triggerRect = trigger2.getBoundingClientRect();
1274
1275
  const panelRect = panel2.getBoundingClientRect();
1276
+ const panelWidth = matchTriggerWidth ? triggerRect.width : panelRect.width;
1275
1277
  const viewportWidth = window.innerWidth;
1276
1278
  const viewportHeight = window.innerHeight;
1277
1279
  let { vertical, horizontal } = splitPlacement(placement);
1278
1280
  const computeTop = (v) => v === "bottom" ? triggerRect.bottom + offset : triggerRect.top - panelRect.height - offset;
1279
1281
  const computeLeft = (h) => {
1280
1282
  if (h === "center")
1281
- return triggerRect.left + triggerRect.width / 2 - panelRect.width / 2;
1282
- return h === "start" ? triggerRect.left : triggerRect.right - panelRect.width;
1283
+ return triggerRect.left + triggerRect.width / 2 - panelWidth / 2;
1284
+ return h === "start" ? triggerRect.left : triggerRect.right - panelWidth;
1283
1285
  };
1284
1286
  let top;
1285
1287
  if (keepSide && lockedVerticalRef.current) {
@@ -1301,14 +1303,14 @@ var useContextMenuPosition = ({
1301
1303
  }
1302
1304
  let left = computeLeft(horizontal);
1303
1305
  if (horizontal === "center") {
1304
- const maxLeft = viewportWidth - panelRect.width;
1306
+ const maxLeft = viewportWidth - panelWidth;
1305
1307
  left = maxLeft >= 0 ? Math.min(Math.max(left, 0), maxLeft) : 0;
1306
1308
  } else {
1307
- const wantedRight = left + panelRect.width;
1309
+ const wantedRight = left + panelWidth;
1308
1310
  if (left < 0 || wantedRight > viewportWidth) {
1309
1311
  const flipped = horizontal === "start" ? "end" : "start";
1310
1312
  const flippedLeft = computeLeft(flipped);
1311
- const flippedRight = flippedLeft + panelRect.width;
1313
+ const flippedRight = flippedLeft + panelWidth;
1312
1314
  if (flippedLeft >= 0 && flippedRight <= viewportWidth) {
1313
1315
  horizontal = flipped;
1314
1316
  left = flippedLeft;
@@ -1318,10 +1320,11 @@ var useContextMenuPosition = ({
1318
1320
  const next = {
1319
1321
  top,
1320
1322
  left,
1321
- placement: joinPlacement(vertical, horizontal)
1323
+ placement: joinPlacement(vertical, horizontal),
1324
+ triggerWidth: triggerRect.width
1322
1325
  };
1323
1326
  setResolved(
1324
- (prev) => prev && prev.top === next.top && prev.left === next.left && prev.placement === next.placement ? prev : next
1327
+ (prev) => prev && prev.top === next.top && prev.left === next.left && prev.placement === next.placement && prev.triggerWidth === next.triggerWidth ? prev : next
1325
1328
  );
1326
1329
  };
1327
1330
  compute(false);
@@ -1333,11 +1336,18 @@ var useContextMenuPosition = ({
1333
1336
  observer = new ResizeObserver(() => compute(true));
1334
1337
  observer.observe(panel);
1335
1338
  }
1339
+ let triggerObserver;
1340
+ const trigger = triggerRef.current;
1341
+ if (trigger && typeof ResizeObserver !== "undefined") {
1342
+ triggerObserver = new ResizeObserver(() => compute(true));
1343
+ triggerObserver.observe(trigger);
1344
+ }
1336
1345
  return () => {
1337
1346
  window.removeEventListener("resize", onResize);
1338
1347
  observer?.disconnect();
1348
+ triggerObserver?.disconnect();
1339
1349
  };
1340
- }, [isOpen, placement, offset, triggerRef, panelRef]);
1350
+ }, [isOpen, placement, offset, matchTriggerWidth, triggerRef, panelRef]);
1341
1351
  return resolved;
1342
1352
  };
1343
1353
 
@@ -1516,6 +1526,7 @@ var filterItems = (items, query) => {
1516
1526
  return result;
1517
1527
  };
1518
1528
  var isOption = (item) => item.type === "option";
1529
+ var isSearchCellElement = (node) => isValidElement(node) && node.type === ContextMenuItem && node.props.type === "search";
1519
1530
  var ContextMenuRoot = forwardRef(
1520
1531
  ({
1521
1532
  children,
@@ -1531,6 +1542,7 @@ var ContextMenuRoot = forwardRef(
1531
1542
  onOpenChange,
1532
1543
  trigger,
1533
1544
  triggerFullWidth = false,
1545
+ matchTriggerWidth = false,
1534
1546
  placement = "bottom-start",
1535
1547
  offset = 4,
1536
1548
  position,
@@ -1575,14 +1587,17 @@ var ContextMenuRoot = forwardRef(
1575
1587
  const shouldCloseOnSelect = closeOnSelect ?? (type === "checkbox" ? false : true);
1576
1588
  const surfaceColor = background ?? xuiTheme.colors.layer.float;
1577
1589
  const positionOffset = withArrow ? offset + ARROW_TIP_PROTRUSION : offset;
1590
+ const shouldMatchTriggerWidth = matchTriggerWidth && !!trigger && !position && width === void 0;
1578
1591
  const positioned = useContextMenuPosition({
1579
1592
  triggerRef,
1580
1593
  panelRef,
1581
1594
  isOpen: isOpen && !!trigger && !position,
1582
1595
  placement,
1583
- offset: positionOffset
1596
+ offset: positionOffset,
1597
+ matchTriggerWidth: shouldMatchTriggerWidth
1584
1598
  });
1585
1599
  const resolvedPlacement = positioned?.placement ?? placement;
1600
+ const matchedWidthStyle = shouldMatchTriggerWidth && positioned ? { width: positioned.triggerWidth, minWidth: 0 } : void 0;
1586
1601
  const getArrowStyle = (arrowPlacement) => {
1587
1602
  const isTop = arrowPlacement.startsWith("top");
1588
1603
  const arrowStyle = {
@@ -1792,7 +1807,7 @@ var ContextMenuRoot = forwardRef(
1792
1807
  );
1793
1808
  };
1794
1809
  const renderedItems = searchable ? filterItems(items, debouncedQuery) : [...items];
1795
- const searchHeader = searchable ? /* @__PURE__ */ jsx4(
1810
+ const renderStickySearch = (node, key) => /* @__PURE__ */ jsx4(
1796
1811
  "div",
1797
1812
  {
1798
1813
  "data-sticky": "top",
@@ -1802,21 +1817,34 @@ var ContextMenuRoot = forwardRef(
1802
1817
  zIndex: 1,
1803
1818
  backgroundColor: surfaceColor
1804
1819
  },
1805
- children: /* @__PURE__ */ jsx4(
1806
- ContextMenuItem,
1807
- {
1808
- type: "search",
1809
- value: query,
1810
- onValueChange: setQuery,
1811
- placeholder: "Search",
1812
- autoFocus: true,
1813
- themeMode,
1814
- themeProductContext
1815
- }
1816
- )
1820
+ children: node
1817
1821
  },
1822
+ key
1823
+ );
1824
+ const searchHeader = searchable ? renderStickySearch(
1825
+ /* @__PURE__ */ jsx4(
1826
+ ContextMenuItem,
1827
+ {
1828
+ type: "search",
1829
+ value: query,
1830
+ onValueChange: setQuery,
1831
+ placeholder: "Search",
1832
+ autoFocus: true,
1833
+ themeMode,
1834
+ themeProductContext
1835
+ }
1836
+ ),
1818
1837
  "context-menu-search"
1819
1838
  ) : null;
1839
+ const renderChildren = () => {
1840
+ if (!React3.Children.toArray(children).some(isSearchCellElement)) {
1841
+ return children;
1842
+ }
1843
+ return React3.Children.map(
1844
+ children,
1845
+ (child, index) => isSearchCellElement(child) ? renderStickySearch(child, `context-menu-search-${index}`) : child
1846
+ );
1847
+ };
1820
1848
  const renderContent = () => {
1821
1849
  if (loading || isLoading || type === "loading") {
1822
1850
  const brandColor = xuiTheme.colors.control.brand.primary.bg;
@@ -1831,7 +1859,7 @@ var ContextMenuRoot = forwardRef(
1831
1859
  }
1832
1860
  );
1833
1861
  }
1834
- if (children) return children;
1862
+ if (children) return renderChildren();
1835
1863
  if (renderedItems.length === 0) {
1836
1864
  const emptyNode = empty ?? /* @__PURE__ */ jsx4(
1837
1865
  "div",
@@ -1928,7 +1956,9 @@ var ContextMenuRoot = forwardRef(
1928
1956
  ref: containerRef,
1929
1957
  style: {
1930
1958
  position: trigger || position ? "relative" : void 0,
1931
- display: trigger ? "inline-block" : void 0
1959
+ // An inline-block container shrinks to the trigger, which would
1960
+ // cancel the trigger's 100% width; a block one fills the parent.
1961
+ display: trigger ? triggerFullWidth ? "block" : "inline-block" : void 0
1932
1962
  },
1933
1963
  children: [
1934
1964
  triggerNode,
@@ -1955,6 +1985,7 @@ var ContextMenuRoot = forwardRef(
1955
1985
  onMouseLeave: () => setActiveIndex(-1),
1956
1986
  style: {
1957
1987
  ...positionStyle,
1988
+ ...matchedWidthStyle,
1958
1989
  ...style,
1959
1990
  zIndex: overlayLayer + LAYER_OFFSET3.menu,
1960
1991
  boxShadow: PANEL_SHADOW,