@xsolla/xui-context-menu 0.227.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
@@ -205,6 +205,8 @@ A horizontal rule with `role="separator"`. No content props.
205
205
  | `emptyMessage` | `string` | Custom message for the default empty state. |
206
206
  | `empty` | `ReactNode` | Replace the empty state entirely. |
207
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`. |
208
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. |
209
211
  | `isOpen` | `boolean` | Controlled open state. |
210
212
  | `onOpenChange` | `(open: boolean) => void` | Open-state callback. |
@@ -219,6 +221,30 @@ A horizontal rule with `role="separator"`. No content props.
219
221
  | `testID` | `string` | Testing handle; forwarded as `data-testid` when `data-testid` is not provided. |
220
222
  | `data-testid` | `string` | Testing handle. |
221
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
+
222
248
  ### Panel padding
223
249
 
224
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
 
@@ -1540,6 +1550,7 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1540
1550
  onOpenChange,
1541
1551
  trigger,
1542
1552
  triggerFullWidth = false,
1553
+ matchTriggerWidth = false,
1543
1554
  placement = "bottom-start",
1544
1555
  offset = 4,
1545
1556
  position,
@@ -1584,14 +1595,17 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1584
1595
  const shouldCloseOnSelect = closeOnSelect ?? (type === "checkbox" ? false : true);
1585
1596
  const surfaceColor = background ?? xuiTheme.colors.layer.float;
1586
1597
  const positionOffset = withArrow ? offset + ARROW_TIP_PROTRUSION : offset;
1598
+ const shouldMatchTriggerWidth = matchTriggerWidth && !!trigger && !position && width === void 0;
1587
1599
  const positioned = useContextMenuPosition({
1588
1600
  triggerRef,
1589
1601
  panelRef,
1590
1602
  isOpen: isOpen && !!trigger && !position,
1591
1603
  placement,
1592
- offset: positionOffset
1604
+ offset: positionOffset,
1605
+ matchTriggerWidth: shouldMatchTriggerWidth
1593
1606
  });
1594
1607
  const resolvedPlacement = positioned?.placement ?? placement;
1608
+ const matchedWidthStyle = shouldMatchTriggerWidth && positioned ? { width: positioned.triggerWidth, minWidth: 0 } : void 0;
1595
1609
  const getArrowStyle = (arrowPlacement) => {
1596
1610
  const isTop = arrowPlacement.startsWith("top");
1597
1611
  const arrowStyle = {
@@ -1950,7 +1964,9 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1950
1964
  ref: containerRef,
1951
1965
  style: {
1952
1966
  position: trigger || position ? "relative" : void 0,
1953
- 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
1954
1970
  },
1955
1971
  children: [
1956
1972
  triggerNode,
@@ -1977,6 +1993,7 @@ var ContextMenuRoot = (0, import_react6.forwardRef)(
1977
1993
  onMouseLeave: () => setActiveIndex(-1),
1978
1994
  style: {
1979
1995
  ...positionStyle,
1996
+ ...matchedWidthStyle,
1980
1997
  ...style,
1981
1998
  zIndex: overlayLayer + import_xui_core3.LAYER_OFFSET.menu,
1982
1999
  boxShadow: PANEL_SHADOW,