@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/web/index.mjs CHANGED
@@ -1282,7 +1282,8 @@ var useContextMenuPosition = ({
1282
1282
  panelRef,
1283
1283
  isOpen,
1284
1284
  placement = "bottom-start",
1285
- offset = 4
1285
+ offset = 4,
1286
+ matchTriggerWidth = false
1286
1287
  }) => {
1287
1288
  const [resolved, setResolved] = useState3();
1288
1289
  const lockedVerticalRef = useRef2(null);
@@ -1293,19 +1294,20 @@ var useContextMenuPosition = ({
1293
1294
  return;
1294
1295
  }
1295
1296
  const compute = (keepSide) => {
1296
- const trigger = triggerRef.current;
1297
+ const trigger2 = triggerRef.current;
1297
1298
  const panel2 = panelRef.current;
1298
- if (!trigger || !panel2) return;
1299
- const triggerRect = trigger.getBoundingClientRect();
1299
+ if (!trigger2 || !panel2) return;
1300
+ const triggerRect = trigger2.getBoundingClientRect();
1300
1301
  const panelRect = panel2.getBoundingClientRect();
1302
+ const panelWidth = matchTriggerWidth ? triggerRect.width : panelRect.width;
1301
1303
  const viewportWidth = window.innerWidth;
1302
1304
  const viewportHeight = window.innerHeight;
1303
1305
  let { vertical, horizontal } = splitPlacement(placement);
1304
1306
  const computeTop = (v) => v === "bottom" ? triggerRect.bottom + offset : triggerRect.top - panelRect.height - offset;
1305
1307
  const computeLeft = (h) => {
1306
1308
  if (h === "center")
1307
- return triggerRect.left + triggerRect.width / 2 - panelRect.width / 2;
1308
- return h === "start" ? triggerRect.left : triggerRect.right - panelRect.width;
1309
+ return triggerRect.left + triggerRect.width / 2 - panelWidth / 2;
1310
+ return h === "start" ? triggerRect.left : triggerRect.right - panelWidth;
1309
1311
  };
1310
1312
  let top;
1311
1313
  if (keepSide && lockedVerticalRef.current) {
@@ -1327,14 +1329,14 @@ var useContextMenuPosition = ({
1327
1329
  }
1328
1330
  let left = computeLeft(horizontal);
1329
1331
  if (horizontal === "center") {
1330
- const maxLeft = viewportWidth - panelRect.width;
1332
+ const maxLeft = viewportWidth - panelWidth;
1331
1333
  left = maxLeft >= 0 ? Math.min(Math.max(left, 0), maxLeft) : 0;
1332
1334
  } else {
1333
- const wantedRight = left + panelRect.width;
1335
+ const wantedRight = left + panelWidth;
1334
1336
  if (left < 0 || wantedRight > viewportWidth) {
1335
1337
  const flipped = horizontal === "start" ? "end" : "start";
1336
1338
  const flippedLeft = computeLeft(flipped);
1337
- const flippedRight = flippedLeft + panelRect.width;
1339
+ const flippedRight = flippedLeft + panelWidth;
1338
1340
  if (flippedLeft >= 0 && flippedRight <= viewportWidth) {
1339
1341
  horizontal = flipped;
1340
1342
  left = flippedLeft;
@@ -1344,10 +1346,11 @@ var useContextMenuPosition = ({
1344
1346
  const next = {
1345
1347
  top,
1346
1348
  left,
1347
- placement: joinPlacement(vertical, horizontal)
1349
+ placement: joinPlacement(vertical, horizontal),
1350
+ triggerWidth: triggerRect.width
1348
1351
  };
1349
1352
  setResolved(
1350
- (prev) => prev && prev.top === next.top && prev.left === next.left && prev.placement === next.placement ? prev : next
1353
+ (prev) => prev && prev.top === next.top && prev.left === next.left && prev.placement === next.placement && prev.triggerWidth === next.triggerWidth ? prev : next
1351
1354
  );
1352
1355
  };
1353
1356
  compute(false);
@@ -1359,11 +1362,18 @@ var useContextMenuPosition = ({
1359
1362
  observer = new ResizeObserver(() => compute(true));
1360
1363
  observer.observe(panel);
1361
1364
  }
1365
+ let triggerObserver;
1366
+ const trigger = triggerRef.current;
1367
+ if (trigger && typeof ResizeObserver !== "undefined") {
1368
+ triggerObserver = new ResizeObserver(() => compute(true));
1369
+ triggerObserver.observe(trigger);
1370
+ }
1362
1371
  return () => {
1363
1372
  window.removeEventListener("resize", onResize);
1364
1373
  observer?.disconnect();
1374
+ triggerObserver?.disconnect();
1365
1375
  };
1366
- }, [isOpen, placement, offset, triggerRef, panelRef]);
1376
+ }, [isOpen, placement, offset, matchTriggerWidth, triggerRef, panelRef]);
1367
1377
  return resolved;
1368
1378
  };
1369
1379
 
@@ -1542,6 +1552,7 @@ var filterItems = (items, query) => {
1542
1552
  return result;
1543
1553
  };
1544
1554
  var isOption = (item) => item.type === "option";
1555
+ var isSearchCellElement = (node) => isValidElement(node) && node.type === ContextMenuItem && node.props.type === "search";
1545
1556
  var ContextMenuRoot = forwardRef(
1546
1557
  ({
1547
1558
  children,
@@ -1557,6 +1568,7 @@ var ContextMenuRoot = forwardRef(
1557
1568
  onOpenChange,
1558
1569
  trigger,
1559
1570
  triggerFullWidth = false,
1571
+ matchTriggerWidth = false,
1560
1572
  placement = "bottom-start",
1561
1573
  offset = 4,
1562
1574
  position,
@@ -1601,14 +1613,17 @@ var ContextMenuRoot = forwardRef(
1601
1613
  const shouldCloseOnSelect = closeOnSelect ?? (type === "checkbox" ? false : true);
1602
1614
  const surfaceColor = background ?? xuiTheme.colors.layer.float;
1603
1615
  const positionOffset = withArrow ? offset + ARROW_TIP_PROTRUSION : offset;
1616
+ const shouldMatchTriggerWidth = matchTriggerWidth && !!trigger && !position && width === void 0;
1604
1617
  const positioned = useContextMenuPosition({
1605
1618
  triggerRef,
1606
1619
  panelRef,
1607
1620
  isOpen: isOpen && !!trigger && !position,
1608
1621
  placement,
1609
- offset: positionOffset
1622
+ offset: positionOffset,
1623
+ matchTriggerWidth: shouldMatchTriggerWidth
1610
1624
  });
1611
1625
  const resolvedPlacement = positioned?.placement ?? placement;
1626
+ const matchedWidthStyle = shouldMatchTriggerWidth && positioned ? { width: positioned.triggerWidth, minWidth: 0 } : void 0;
1612
1627
  const getArrowStyle = (arrowPlacement) => {
1613
1628
  const isTop = arrowPlacement.startsWith("top");
1614
1629
  const arrowStyle = {
@@ -1818,7 +1833,7 @@ var ContextMenuRoot = forwardRef(
1818
1833
  );
1819
1834
  };
1820
1835
  const renderedItems = searchable ? filterItems(items, debouncedQuery) : [...items];
1821
- const searchHeader = searchable ? /* @__PURE__ */ jsx4(
1836
+ const renderStickySearch = (node, key) => /* @__PURE__ */ jsx4(
1822
1837
  "div",
1823
1838
  {
1824
1839
  "data-sticky": "top",
@@ -1828,21 +1843,34 @@ var ContextMenuRoot = forwardRef(
1828
1843
  zIndex: 1,
1829
1844
  backgroundColor: surfaceColor
1830
1845
  },
1831
- children: /* @__PURE__ */ jsx4(
1832
- ContextMenuItem,
1833
- {
1834
- type: "search",
1835
- value: query,
1836
- onValueChange: setQuery,
1837
- placeholder: "Search",
1838
- autoFocus: true,
1839
- themeMode,
1840
- themeProductContext
1841
- }
1842
- )
1846
+ children: node
1843
1847
  },
1848
+ key
1849
+ );
1850
+ const searchHeader = searchable ? renderStickySearch(
1851
+ /* @__PURE__ */ jsx4(
1852
+ ContextMenuItem,
1853
+ {
1854
+ type: "search",
1855
+ value: query,
1856
+ onValueChange: setQuery,
1857
+ placeholder: "Search",
1858
+ autoFocus: true,
1859
+ themeMode,
1860
+ themeProductContext
1861
+ }
1862
+ ),
1844
1863
  "context-menu-search"
1845
1864
  ) : null;
1865
+ const renderChildren = () => {
1866
+ if (!React5.Children.toArray(children).some(isSearchCellElement)) {
1867
+ return children;
1868
+ }
1869
+ return React5.Children.map(
1870
+ children,
1871
+ (child, index2) => isSearchCellElement(child) ? renderStickySearch(child, `context-menu-search-${index2}`) : child
1872
+ );
1873
+ };
1846
1874
  const renderContent = () => {
1847
1875
  if (loading || isLoading || type === "loading") {
1848
1876
  const brandColor = xuiTheme.colors.control.brand.primary.bg;
@@ -1857,7 +1885,7 @@ var ContextMenuRoot = forwardRef(
1857
1885
  }
1858
1886
  );
1859
1887
  }
1860
- if (children) return children;
1888
+ if (children) return renderChildren();
1861
1889
  if (renderedItems.length === 0) {
1862
1890
  const emptyNode = empty ?? /* @__PURE__ */ jsx4(
1863
1891
  "div",
@@ -1954,7 +1982,9 @@ var ContextMenuRoot = forwardRef(
1954
1982
  ref: containerRef,
1955
1983
  style: {
1956
1984
  position: trigger || position ? "relative" : void 0,
1957
- display: trigger ? "inline-block" : void 0
1985
+ // An inline-block container shrinks to the trigger, which would
1986
+ // cancel the trigger's 100% width; a block one fills the parent.
1987
+ display: trigger ? triggerFullWidth ? "block" : "inline-block" : void 0
1958
1988
  },
1959
1989
  children: [
1960
1990
  triggerNode,
@@ -1981,6 +2011,7 @@ var ContextMenuRoot = forwardRef(
1981
2011
  onMouseLeave: () => setActiveIndex(-1),
1982
2012
  style: {
1983
2013
  ...positionStyle,
2014
+ ...matchedWidthStyle,
1984
2015
  ...style,
1985
2016
  zIndex: overlayLayer + LAYER_OFFSET3.menu,
1986
2017
  boxShadow: PANEL_SHADOW,