@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 +26 -0
- package/native/index.d.mts +15 -1
- package/native/index.d.ts +15 -1
- package/native/index.js +31 -14
- package/native/index.js.map +1 -1
- package/native/index.mjs +31 -14
- package/native/index.mjs.map +1 -1
- package/package.json +10 -10
- package/web/index.d.mts +15 -1
- package/web/index.d.ts +15 -1
- package/web/index.js +31 -14
- package/web/index.js.map +1 -1
- package/web/index.mjs +31 -14
- package/web/index.mjs.map +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsolla/xui-context-menu",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.228.0",
|
|
4
4
|
"main": "./web/index.js",
|
|
5
5
|
"module": "./web/index.mjs",
|
|
6
6
|
"types": "./web/index.d.ts",
|
|
@@ -13,15 +13,15 @@
|
|
|
13
13
|
"test:coverage": "vitest run --coverage"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@xsolla/xui-checkbox": "0.
|
|
17
|
-
"@xsolla/xui-core": "0.
|
|
18
|
-
"@xsolla/xui-divider": "0.
|
|
19
|
-
"@xsolla/xui-icons": "0.
|
|
20
|
-
"@xsolla/xui-icons-base": "0.
|
|
21
|
-
"@xsolla/xui-primitives-core": "0.
|
|
22
|
-
"@xsolla/xui-radio": "0.
|
|
23
|
-
"@xsolla/xui-spinner": "0.
|
|
24
|
-
"@xsolla/xui-typography": "0.
|
|
16
|
+
"@xsolla/xui-checkbox": "0.228.0",
|
|
17
|
+
"@xsolla/xui-core": "0.228.0",
|
|
18
|
+
"@xsolla/xui-divider": "0.228.0",
|
|
19
|
+
"@xsolla/xui-icons": "0.228.0",
|
|
20
|
+
"@xsolla/xui-icons-base": "0.228.0",
|
|
21
|
+
"@xsolla/xui-primitives-core": "0.228.0",
|
|
22
|
+
"@xsolla/xui-radio": "0.228.0",
|
|
23
|
+
"@xsolla/xui-spinner": "0.228.0",
|
|
24
|
+
"@xsolla/xui-typography": "0.228.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"react": ">=16.8.0",
|
package/web/index.d.mts
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/web/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/web/index.js
CHANGED
|
@@ -1294,7 +1294,8 @@ var useContextMenuPosition = ({
|
|
|
1294
1294
|
panelRef,
|
|
1295
1295
|
isOpen,
|
|
1296
1296
|
placement = "bottom-start",
|
|
1297
|
-
offset = 4
|
|
1297
|
+
offset = 4,
|
|
1298
|
+
matchTriggerWidth = false
|
|
1298
1299
|
}) => {
|
|
1299
1300
|
const [resolved, setResolved] = (0, import_react6.useState)();
|
|
1300
1301
|
const lockedVerticalRef = (0, import_react6.useRef)(null);
|
|
@@ -1305,19 +1306,20 @@ var useContextMenuPosition = ({
|
|
|
1305
1306
|
return;
|
|
1306
1307
|
}
|
|
1307
1308
|
const compute = (keepSide) => {
|
|
1308
|
-
const
|
|
1309
|
+
const trigger2 = triggerRef.current;
|
|
1309
1310
|
const panel2 = panelRef.current;
|
|
1310
|
-
if (!
|
|
1311
|
-
const triggerRect =
|
|
1311
|
+
if (!trigger2 || !panel2) return;
|
|
1312
|
+
const triggerRect = trigger2.getBoundingClientRect();
|
|
1312
1313
|
const panelRect = panel2.getBoundingClientRect();
|
|
1314
|
+
const panelWidth = matchTriggerWidth ? triggerRect.width : panelRect.width;
|
|
1313
1315
|
const viewportWidth = window.innerWidth;
|
|
1314
1316
|
const viewportHeight = window.innerHeight;
|
|
1315
1317
|
let { vertical, horizontal } = splitPlacement(placement);
|
|
1316
1318
|
const computeTop = (v) => v === "bottom" ? triggerRect.bottom + offset : triggerRect.top - panelRect.height - offset;
|
|
1317
1319
|
const computeLeft = (h) => {
|
|
1318
1320
|
if (h === "center")
|
|
1319
|
-
return triggerRect.left + triggerRect.width / 2 -
|
|
1320
|
-
return h === "start" ? triggerRect.left : triggerRect.right -
|
|
1321
|
+
return triggerRect.left + triggerRect.width / 2 - panelWidth / 2;
|
|
1322
|
+
return h === "start" ? triggerRect.left : triggerRect.right - panelWidth;
|
|
1321
1323
|
};
|
|
1322
1324
|
let top;
|
|
1323
1325
|
if (keepSide && lockedVerticalRef.current) {
|
|
@@ -1339,14 +1341,14 @@ var useContextMenuPosition = ({
|
|
|
1339
1341
|
}
|
|
1340
1342
|
let left = computeLeft(horizontal);
|
|
1341
1343
|
if (horizontal === "center") {
|
|
1342
|
-
const maxLeft = viewportWidth -
|
|
1344
|
+
const maxLeft = viewportWidth - panelWidth;
|
|
1343
1345
|
left = maxLeft >= 0 ? Math.min(Math.max(left, 0), maxLeft) : 0;
|
|
1344
1346
|
} else {
|
|
1345
|
-
const wantedRight = left +
|
|
1347
|
+
const wantedRight = left + panelWidth;
|
|
1346
1348
|
if (left < 0 || wantedRight > viewportWidth) {
|
|
1347
1349
|
const flipped = horizontal === "start" ? "end" : "start";
|
|
1348
1350
|
const flippedLeft = computeLeft(flipped);
|
|
1349
|
-
const flippedRight = flippedLeft +
|
|
1351
|
+
const flippedRight = flippedLeft + panelWidth;
|
|
1350
1352
|
if (flippedLeft >= 0 && flippedRight <= viewportWidth) {
|
|
1351
1353
|
horizontal = flipped;
|
|
1352
1354
|
left = flippedLeft;
|
|
@@ -1356,10 +1358,11 @@ var useContextMenuPosition = ({
|
|
|
1356
1358
|
const next = {
|
|
1357
1359
|
top,
|
|
1358
1360
|
left,
|
|
1359
|
-
placement: joinPlacement(vertical, horizontal)
|
|
1361
|
+
placement: joinPlacement(vertical, horizontal),
|
|
1362
|
+
triggerWidth: triggerRect.width
|
|
1360
1363
|
};
|
|
1361
1364
|
setResolved(
|
|
1362
|
-
(prev) => prev && prev.top === next.top && prev.left === next.left && prev.placement === next.placement ? prev : next
|
|
1365
|
+
(prev) => prev && prev.top === next.top && prev.left === next.left && prev.placement === next.placement && prev.triggerWidth === next.triggerWidth ? prev : next
|
|
1363
1366
|
);
|
|
1364
1367
|
};
|
|
1365
1368
|
compute(false);
|
|
@@ -1371,11 +1374,18 @@ var useContextMenuPosition = ({
|
|
|
1371
1374
|
observer = new ResizeObserver(() => compute(true));
|
|
1372
1375
|
observer.observe(panel);
|
|
1373
1376
|
}
|
|
1377
|
+
let triggerObserver;
|
|
1378
|
+
const trigger = triggerRef.current;
|
|
1379
|
+
if (trigger && typeof ResizeObserver !== "undefined") {
|
|
1380
|
+
triggerObserver = new ResizeObserver(() => compute(true));
|
|
1381
|
+
triggerObserver.observe(trigger);
|
|
1382
|
+
}
|
|
1374
1383
|
return () => {
|
|
1375
1384
|
window.removeEventListener("resize", onResize);
|
|
1376
1385
|
observer?.disconnect();
|
|
1386
|
+
triggerObserver?.disconnect();
|
|
1377
1387
|
};
|
|
1378
|
-
}, [isOpen, placement, offset, triggerRef, panelRef]);
|
|
1388
|
+
}, [isOpen, placement, offset, matchTriggerWidth, triggerRef, panelRef]);
|
|
1379
1389
|
return resolved;
|
|
1380
1390
|
};
|
|
1381
1391
|
|
|
@@ -1570,6 +1580,7 @@ var ContextMenuRoot = (0, import_react8.forwardRef)(
|
|
|
1570
1580
|
onOpenChange,
|
|
1571
1581
|
trigger,
|
|
1572
1582
|
triggerFullWidth = false,
|
|
1583
|
+
matchTriggerWidth = false,
|
|
1573
1584
|
placement = "bottom-start",
|
|
1574
1585
|
offset = 4,
|
|
1575
1586
|
position,
|
|
@@ -1614,14 +1625,17 @@ var ContextMenuRoot = (0, import_react8.forwardRef)(
|
|
|
1614
1625
|
const shouldCloseOnSelect = closeOnSelect ?? (type === "checkbox" ? false : true);
|
|
1615
1626
|
const surfaceColor = background ?? xuiTheme.colors.layer.float;
|
|
1616
1627
|
const positionOffset = withArrow ? offset + ARROW_TIP_PROTRUSION : offset;
|
|
1628
|
+
const shouldMatchTriggerWidth = matchTriggerWidth && !!trigger && !position && width === void 0;
|
|
1617
1629
|
const positioned = useContextMenuPosition({
|
|
1618
1630
|
triggerRef,
|
|
1619
1631
|
panelRef,
|
|
1620
1632
|
isOpen: isOpen && !!trigger && !position,
|
|
1621
1633
|
placement,
|
|
1622
|
-
offset: positionOffset
|
|
1634
|
+
offset: positionOffset,
|
|
1635
|
+
matchTriggerWidth: shouldMatchTriggerWidth
|
|
1623
1636
|
});
|
|
1624
1637
|
const resolvedPlacement = positioned?.placement ?? placement;
|
|
1638
|
+
const matchedWidthStyle = shouldMatchTriggerWidth && positioned ? { width: positioned.triggerWidth, minWidth: 0 } : void 0;
|
|
1625
1639
|
const getArrowStyle = (arrowPlacement) => {
|
|
1626
1640
|
const isTop = arrowPlacement.startsWith("top");
|
|
1627
1641
|
const arrowStyle = {
|
|
@@ -1980,7 +1994,9 @@ var ContextMenuRoot = (0, import_react8.forwardRef)(
|
|
|
1980
1994
|
ref: containerRef,
|
|
1981
1995
|
style: {
|
|
1982
1996
|
position: trigger || position ? "relative" : void 0,
|
|
1983
|
-
|
|
1997
|
+
// An inline-block container shrinks to the trigger, which would
|
|
1998
|
+
// cancel the trigger's 100% width; a block one fills the parent.
|
|
1999
|
+
display: trigger ? triggerFullWidth ? "block" : "inline-block" : void 0
|
|
1984
2000
|
},
|
|
1985
2001
|
children: [
|
|
1986
2002
|
triggerNode,
|
|
@@ -2007,6 +2023,7 @@ var ContextMenuRoot = (0, import_react8.forwardRef)(
|
|
|
2007
2023
|
onMouseLeave: () => setActiveIndex(-1),
|
|
2008
2024
|
style: {
|
|
2009
2025
|
...positionStyle,
|
|
2026
|
+
...matchedWidthStyle,
|
|
2010
2027
|
...style,
|
|
2011
2028
|
zIndex: overlayLayer + import_xui_core3.LAYER_OFFSET.menu,
|
|
2012
2029
|
boxShadow: PANEL_SHADOW,
|