@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/README.md CHANGED
@@ -165,6 +165,21 @@ Every cell lays its slots out on a single centred row, which reads correctly whi
165
165
  | `autoFocus` | `boolean` | Focuses the input on mount. |
166
166
  | `aria-label` | `string` | Defaults to `"Search options"`. |
167
167
 
168
+ #### Pinned on scroll
169
+
170
+ When the option list overflows `maxHeight`, the search cell stays pinned to the top of the panel while the options scroll beneath it. This works on both API paths: the `searchable` preset, and a search cell passed as a **direct child** of `<ContextMenu>` on the custom path.
171
+
172
+ ```tsx
173
+ <ContextMenu trigger={<Button>Select domain</Button>} maxHeight={240}>
174
+ <ContextMenuItem type="search" value={query} onValueChange={setQuery} />
175
+ {domains.map((domain) => (
176
+ <ContextMenuItem key={domain} type="option" label={domain} />
177
+ ))}
178
+ </ContextMenu>
179
+ ```
180
+
181
+ The panel detects the search cell among its direct children only. A search cell nested inside a fragment or your own wrapper component is not pinned — render it directly under `<ContextMenu>`.
182
+
168
183
  ### `type="heading"`
169
184
 
170
185
  | Prop | Type | Purpose |
@@ -190,6 +205,8 @@ A horizontal rule with `role="separator"`. No content props.
190
205
  | `emptyMessage` | `string` | Custom message for the default empty state. |
191
206
  | `empty` | `ReactNode` | Replace the empty state entirely. |
192
207
  | `trigger` | `ReactNode` | Element that toggles the panel; receives `aria-haspopup` / `aria-expanded`. |
208
+ | `triggerFullWidth` | `boolean` | The trigger fills its parent's width instead of hugging its content. Defaults to `false`. |
209
+ | `matchTriggerWidth` | `boolean` | Sizes the panel to the measured trigger width and follows it live while open. Overrides the per-size minimum width; an explicit `width` still wins. Ignored without a `trigger` or when `position` is set. Defaults to `false`. |
193
210
  | `placement` | `"bottom-start" \| "top-start" \| "bottom-end" \| "top-end"` | Initial placement. Auto-flips when clipped on open; the chosen side is kept while the panel resizes. |
194
211
  | `isOpen` | `boolean` | Controlled open state. |
195
212
  | `onOpenChange` | `(open: boolean) => void` | Open-state callback. |
@@ -204,6 +221,30 @@ A horizontal rule with `role="separator"`. No content props.
204
221
  | `testID` | `string` | Testing handle; forwarded as `data-testid` when `data-testid` is not provided. |
205
222
  | `data-testid` | `string` | Testing handle. |
206
223
 
224
+ ### Matching the trigger width
225
+
226
+ By default the panel width comes from `size` (a per-size minimum of 132 / 172 / 220 / 276 px for `sm` / `md` / `lg` / `xl`) or from its content. It has no relation to the trigger. When the trigger is field-like — a MultiSelect in external-menu mode (`dropdownMenu={false}`), or a `<Button fullWidth>` — set `matchTriggerWidth` so the options line up with the field, the way Select and MultiSelect size their own dropdown:
227
+
228
+ ```tsx
229
+ <div style={{ width: 360 }}>
230
+ <ContextMenu
231
+ trigger={<Button fullWidth>Choose a region</Button>}
232
+ triggerFullWidth
233
+ matchTriggerWidth
234
+ type="list"
235
+ items={items}
236
+ aria-label="Region"
237
+ />
238
+ </div>
239
+ ```
240
+
241
+ - The panel takes the trigger's measured width and the per-size `minWidth` no longer applies.
242
+ - An explicit `width` still wins over `matchTriggerWidth`.
243
+ - `matchTriggerWidth` is ignored without a `trigger`, and when `position` is set.
244
+ - The panel follows the trigger width live while open: on a window resize and when only the trigger's parent resizes (layout change, flexible container). This tracking uses `ResizeObserver` where the browser provides it.
245
+
246
+ `triggerFullWidth` makes the menu's own container a block, so the trigger can fill its parent. Pair the two props when the trigger should span its container and the panel should follow.
247
+
207
248
  ### Panel padding
208
249
 
209
250
  The panel pads its own content by `8px` on every side at every `size`, so cells never touch the rounded border. Set `noPadding` when a child should be full-bleed — a banner, a sticky footer row, or a custom list that draws its own insets:
@@ -98,6 +98,16 @@ interface ContextMenuProps extends ThemeOverrideProps {
98
98
  trigger?: ReactNode;
99
99
  /** Trigger fills its parent's width instead of hugging its content. Defaults to `false`. */
100
100
  triggerFullWidth?: boolean;
101
+ /**
102
+ * Sizes the panel to the measured trigger width, the way Select and
103
+ * MultiSelect size their own dropdown. Overrides the per-size minimum width
104
+ * and follows the trigger live while the menu is open.
105
+ *
106
+ * An explicit `width` still wins. Ignored without a `trigger` or when
107
+ * `position` is set. Pair with `triggerFullWidth` for a field-width trigger.
108
+ * Defaults to `false`.
109
+ */
110
+ matchTriggerWidth?: boolean;
101
111
  /** Placement relative to the trigger; auto-flips to stay in the viewport. Ignored when `position` is set. Defaults to `"bottom-start"`. */
102
112
  placement?: ContextMenuPlacement;
103
113
  /** Gap in px to the panel edge, or to the arrow tip when `withArrow` is set. Defaults to `4`. */
@@ -191,12 +201,16 @@ interface UseContextMenuPositionOptions {
191
201
  isOpen: boolean;
192
202
  placement?: ContextMenuPlacement;
193
203
  offset?: number;
204
+ /** Lay the panel out at the trigger's width instead of its own measured width. */
205
+ matchTriggerWidth?: boolean;
194
206
  }
195
207
  interface ResolvedPosition {
196
208
  top: number;
197
209
  left: number;
198
210
  placement: ContextMenuPlacement;
211
+ /** Measured trigger width, so the panel can size itself to it. */
212
+ triggerWidth: number;
199
213
  }
200
- declare const useContextMenuPosition: ({ triggerRef, panelRef, isOpen, placement, offset, }: UseContextMenuPositionOptions) => ResolvedPosition | undefined;
214
+ declare const useContextMenuPosition: ({ triggerRef, panelRef, isOpen, placement, offset, matchTriggerWidth, }: UseContextMenuPositionOptions) => ResolvedPosition | undefined;
201
215
 
202
216
  export { ContextMenu, ContextMenuContext, type ContextMenuContextValue, type ContextMenuDividerItemProps, type ContextMenuHeadingItemProps, ContextMenuItem, type ContextMenuItemProps, type ContextMenuOptionItemProps, type ContextMenuPlacement, type ContextMenuPosition, type ContextMenuPresetType, type ContextMenuProps, type ContextMenuSearchItemProps, type ContextMenuSize, type ContextMenuSizing, ContextMenuSubmenu, type ContextMenuSubmenuProps, useContextMenu, useContextMenuPosition, useContextMenuRequired, useKeyboardNavigation };
package/native/index.d.ts CHANGED
@@ -98,6 +98,16 @@ interface ContextMenuProps extends ThemeOverrideProps {
98
98
  trigger?: ReactNode;
99
99
  /** Trigger fills its parent's width instead of hugging its content. Defaults to `false`. */
100
100
  triggerFullWidth?: boolean;
101
+ /**
102
+ * Sizes the panel to the measured trigger width, the way Select and
103
+ * MultiSelect size their own dropdown. Overrides the per-size minimum width
104
+ * and follows the trigger live while the menu is open.
105
+ *
106
+ * An explicit `width` still wins. Ignored without a `trigger` or when
107
+ * `position` is set. Pair with `triggerFullWidth` for a field-width trigger.
108
+ * Defaults to `false`.
109
+ */
110
+ matchTriggerWidth?: boolean;
101
111
  /** Placement relative to the trigger; auto-flips to stay in the viewport. Ignored when `position` is set. Defaults to `"bottom-start"`. */
102
112
  placement?: ContextMenuPlacement;
103
113
  /** Gap in px to the panel edge, or to the arrow tip when `withArrow` is set. Defaults to `4`. */
@@ -191,12 +201,16 @@ interface UseContextMenuPositionOptions {
191
201
  isOpen: boolean;
192
202
  placement?: ContextMenuPlacement;
193
203
  offset?: number;
204
+ /** Lay the panel out at the trigger's width instead of its own measured width. */
205
+ matchTriggerWidth?: boolean;
194
206
  }
195
207
  interface ResolvedPosition {
196
208
  top: number;
197
209
  left: number;
198
210
  placement: ContextMenuPlacement;
211
+ /** Measured trigger width, so the panel can size itself to it. */
212
+ triggerWidth: number;
199
213
  }
200
- declare const useContextMenuPosition: ({ triggerRef, panelRef, isOpen, placement, offset, }: UseContextMenuPositionOptions) => ResolvedPosition | undefined;
214
+ declare const useContextMenuPosition: ({ triggerRef, panelRef, isOpen, placement, offset, matchTriggerWidth, }: UseContextMenuPositionOptions) => ResolvedPosition | undefined;
201
215
 
202
216
  export { ContextMenu, ContextMenuContext, type ContextMenuContextValue, type ContextMenuDividerItemProps, type ContextMenuHeadingItemProps, ContextMenuItem, type ContextMenuItemProps, type ContextMenuOptionItemProps, type ContextMenuPlacement, type ContextMenuPosition, type ContextMenuPresetType, type ContextMenuProps, type ContextMenuSearchItemProps, type ContextMenuSize, type ContextMenuSizing, ContextMenuSubmenu, type ContextMenuSubmenuProps, useContextMenu, useContextMenuPosition, useContextMenuRequired, useKeyboardNavigation };
package/native/index.js CHANGED
@@ -1264,7 +1264,8 @@ var useContextMenuPosition = ({
1264
1264
  panelRef,
1265
1265
  isOpen,
1266
1266
  placement = "bottom-start",
1267
- offset = 4
1267
+ offset = 4,
1268
+ matchTriggerWidth = false
1268
1269
  }) => {
1269
1270
  const [resolved, setResolved] = (0, import_react4.useState)();
1270
1271
  const lockedVerticalRef = (0, import_react4.useRef)(null);
@@ -1275,19 +1276,20 @@ var useContextMenuPosition = ({
1275
1276
  return;
1276
1277
  }
1277
1278
  const compute = (keepSide) => {
1278
- const trigger = triggerRef.current;
1279
+ const trigger2 = triggerRef.current;
1279
1280
  const panel2 = panelRef.current;
1280
- if (!trigger || !panel2) return;
1281
- const triggerRect = trigger.getBoundingClientRect();
1281
+ if (!trigger2 || !panel2) return;
1282
+ const triggerRect = trigger2.getBoundingClientRect();
1282
1283
  const panelRect = panel2.getBoundingClientRect();
1284
+ const panelWidth = matchTriggerWidth ? triggerRect.width : panelRect.width;
1283
1285
  const viewportWidth = window.innerWidth;
1284
1286
  const viewportHeight = window.innerHeight;
1285
1287
  let { vertical, horizontal } = splitPlacement(placement);
1286
1288
  const computeTop = (v) => v === "bottom" ? triggerRect.bottom + offset : triggerRect.top - panelRect.height - offset;
1287
1289
  const computeLeft = (h) => {
1288
1290
  if (h === "center")
1289
- return triggerRect.left + triggerRect.width / 2 - panelRect.width / 2;
1290
- return h === "start" ? triggerRect.left : triggerRect.right - panelRect.width;
1291
+ return triggerRect.left + triggerRect.width / 2 - panelWidth / 2;
1292
+ return h === "start" ? triggerRect.left : triggerRect.right - panelWidth;
1291
1293
  };
1292
1294
  let top;
1293
1295
  if (keepSide && lockedVerticalRef.current) {
@@ -1309,14 +1311,14 @@ var useContextMenuPosition = ({
1309
1311
  }
1310
1312
  let left = computeLeft(horizontal);
1311
1313
  if (horizontal === "center") {
1312
- const maxLeft = viewportWidth - panelRect.width;
1314
+ const maxLeft = viewportWidth - panelWidth;
1313
1315
  left = maxLeft >= 0 ? Math.min(Math.max(left, 0), maxLeft) : 0;
1314
1316
  } else {
1315
- const wantedRight = left + panelRect.width;
1317
+ const wantedRight = left + panelWidth;
1316
1318
  if (left < 0 || wantedRight > viewportWidth) {
1317
1319
  const flipped = horizontal === "start" ? "end" : "start";
1318
1320
  const flippedLeft = computeLeft(flipped);
1319
- const flippedRight = flippedLeft + panelRect.width;
1321
+ const flippedRight = flippedLeft + panelWidth;
1320
1322
  if (flippedLeft >= 0 && flippedRight <= viewportWidth) {
1321
1323
  horizontal = flipped;
1322
1324
  left = flippedLeft;
@@ -1326,10 +1328,11 @@ var useContextMenuPosition = ({
1326
1328
  const next = {
1327
1329
  top,
1328
1330
  left,
1329
- placement: joinPlacement(vertical, horizontal)
1331
+ placement: joinPlacement(vertical, horizontal),
1332
+ triggerWidth: triggerRect.width
1330
1333
  };
1331
1334
  setResolved(
1332
- (prev) => prev && prev.top === next.top && prev.left === next.left && prev.placement === next.placement ? prev : next
1335
+ (prev) => prev && prev.top === next.top && prev.left === next.left && prev.placement === next.placement && prev.triggerWidth === next.triggerWidth ? prev : next
1333
1336
  );
1334
1337
  };
1335
1338
  compute(false);
@@ -1341,11 +1344,18 @@ var useContextMenuPosition = ({
1341
1344
  observer = new ResizeObserver(() => compute(true));
1342
1345
  observer.observe(panel);
1343
1346
  }
1347
+ let triggerObserver;
1348
+ const trigger = triggerRef.current;
1349
+ if (trigger && typeof ResizeObserver !== "undefined") {
1350
+ triggerObserver = new ResizeObserver(() => compute(true));
1351
+ triggerObserver.observe(trigger);
1352
+ }
1344
1353
  return () => {
1345
1354
  window.removeEventListener("resize", onResize);
1346
1355
  observer?.disconnect();
1356
+ triggerObserver?.disconnect();
1347
1357
  };
1348
- }, [isOpen, placement, offset, triggerRef, panelRef]);
1358
+ }, [isOpen, placement, offset, matchTriggerWidth, triggerRef, panelRef]);
1349
1359
  return resolved;
1350
1360
  };
1351
1361
 
@@ -1524,6 +1534,7 @@ var filterItems = (items, query) => {
1524
1534
  return result;
1525
1535
  };
1526
1536
  var isOption = (item) => item.type === "option";
1537
+ var isSearchCellElement = (node) => (0, import_react6.isValidElement)(node) && node.type === ContextMenuItem && node.props.type === "search";
1527
1538
  var ContextMenuRoot = (0, import_react6.forwardRef)(
1528
1539
  ({
1529
1540
  children,
@@ -1539,6 +1550,7 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1539
1550
  onOpenChange,
1540
1551
  trigger,
1541
1552
  triggerFullWidth = false,
1553
+ matchTriggerWidth = false,
1542
1554
  placement = "bottom-start",
1543
1555
  offset = 4,
1544
1556
  position,
@@ -1583,14 +1595,17 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1583
1595
  const shouldCloseOnSelect = closeOnSelect ?? (type === "checkbox" ? false : true);
1584
1596
  const surfaceColor = background ?? xuiTheme.colors.layer.float;
1585
1597
  const positionOffset = withArrow ? offset + ARROW_TIP_PROTRUSION : offset;
1598
+ const shouldMatchTriggerWidth = matchTriggerWidth && !!trigger && !position && width === void 0;
1586
1599
  const positioned = useContextMenuPosition({
1587
1600
  triggerRef,
1588
1601
  panelRef,
1589
1602
  isOpen: isOpen && !!trigger && !position,
1590
1603
  placement,
1591
- offset: positionOffset
1604
+ offset: positionOffset,
1605
+ matchTriggerWidth: shouldMatchTriggerWidth
1592
1606
  });
1593
1607
  const resolvedPlacement = positioned?.placement ?? placement;
1608
+ const matchedWidthStyle = shouldMatchTriggerWidth && positioned ? { width: positioned.triggerWidth, minWidth: 0 } : void 0;
1594
1609
  const getArrowStyle = (arrowPlacement) => {
1595
1610
  const isTop = arrowPlacement.startsWith("top");
1596
1611
  const arrowStyle = {
@@ -1800,7 +1815,7 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1800
1815
  );
1801
1816
  };
1802
1817
  const renderedItems = searchable ? filterItems(items, debouncedQuery) : [...items];
1803
- const searchHeader = searchable ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1818
+ const renderStickySearch = (node, key) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1804
1819
  "div",
1805
1820
  {
1806
1821
  "data-sticky": "top",
@@ -1810,21 +1825,34 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1810
1825
  zIndex: 1,
1811
1826
  backgroundColor: surfaceColor
1812
1827
  },
1813
- children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1814
- ContextMenuItem,
1815
- {
1816
- type: "search",
1817
- value: query,
1818
- onValueChange: setQuery,
1819
- placeholder: "Search",
1820
- autoFocus: true,
1821
- themeMode,
1822
- themeProductContext
1823
- }
1824
- )
1828
+ children: node
1825
1829
  },
1830
+ key
1831
+ );
1832
+ const searchHeader = searchable ? renderStickySearch(
1833
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1834
+ ContextMenuItem,
1835
+ {
1836
+ type: "search",
1837
+ value: query,
1838
+ onValueChange: setQuery,
1839
+ placeholder: "Search",
1840
+ autoFocus: true,
1841
+ themeMode,
1842
+ themeProductContext
1843
+ }
1844
+ ),
1826
1845
  "context-menu-search"
1827
1846
  ) : null;
1847
+ const renderChildren = () => {
1848
+ if (!import_react6.default.Children.toArray(children).some(isSearchCellElement)) {
1849
+ return children;
1850
+ }
1851
+ return import_react6.default.Children.map(
1852
+ children,
1853
+ (child, index) => isSearchCellElement(child) ? renderStickySearch(child, `context-menu-search-${index}`) : child
1854
+ );
1855
+ };
1828
1856
  const renderContent = () => {
1829
1857
  if (loading || isLoading || type === "loading") {
1830
1858
  const brandColor = xuiTheme.colors.control.brand.primary.bg;
@@ -1839,7 +1867,7 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1839
1867
  }
1840
1868
  );
1841
1869
  }
1842
- if (children) return children;
1870
+ if (children) return renderChildren();
1843
1871
  if (renderedItems.length === 0) {
1844
1872
  const emptyNode = empty ?? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1845
1873
  "div",
@@ -1936,7 +1964,9 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1936
1964
  ref: containerRef,
1937
1965
  style: {
1938
1966
  position: trigger || position ? "relative" : void 0,
1939
- display: trigger ? "inline-block" : void 0
1967
+ // An inline-block container shrinks to the trigger, which would
1968
+ // cancel the trigger's 100% width; a block one fills the parent.
1969
+ display: trigger ? triggerFullWidth ? "block" : "inline-block" : void 0
1940
1970
  },
1941
1971
  children: [
1942
1972
  triggerNode,
@@ -1963,6 +1993,7 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1963
1993
  onMouseLeave: () => setActiveIndex(-1),
1964
1994
  style: {
1965
1995
  ...positionStyle,
1996
+ ...matchedWidthStyle,
1966
1997
  ...style,
1967
1998
  zIndex: overlayLayer + import_xui_core3.LAYER_OFFSET.menu,
1968
1999
  boxShadow: PANEL_SHADOW,