@xsolla/xui-context-menu 0.223.0 → 0.224.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
@@ -190,7 +190,7 @@ A horizontal rule with `role="separator"`. No content props.
190
190
  | `emptyMessage` | `string` | Custom message for the default empty state. |
191
191
  | `empty` | `ReactNode` | Replace the empty state entirely. |
192
192
  | `trigger` | `ReactNode` | Element that toggles the panel; receives `aria-haspopup` / `aria-expanded`. |
193
- | `placement` | `"bottom-start" \| "top-start" \| "bottom-end" \| "top-end"` | Initial placement. Auto-flips when clipped. |
193
+ | `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
194
  | `isOpen` | `boolean` | Controlled open state. |
195
195
  | `onOpenChange` | `(open: boolean) => void` | Open-state callback. |
196
196
  | `closeOnSelect` | `boolean` | Override the per-preset default. |
@@ -250,6 +250,7 @@ The prop takes any CSS colour string. Prefer a design token from your product th
250
250
  - On open, focus moves to the search input when present, otherwise to the first option.
251
251
  - On close, focus returns to the trigger.
252
252
  - The trigger element receives `aria-haspopup="menu"` and `aria-expanded` synced to the open state.
253
+ - The panel stays anchored to the trigger when its own size changes — search filtering, items loading in, or a different empty state. An upward panel keeps its bottom edge next to the trigger as it shrinks or grows. The side (top or bottom) is chosen on open and on window resize; content changes do not flip it, so the panel does not jump while the user types.
253
254
 
254
255
  ## Keyboard reference
255
256
 
package/native/index.js CHANGED
@@ -1267,17 +1267,19 @@ var useContextMenuPosition = ({
1267
1267
  offset = 4
1268
1268
  }) => {
1269
1269
  const [resolved, setResolved] = (0, import_react4.useState)();
1270
+ const lockedVerticalRef = (0, import_react4.useRef)(null);
1270
1271
  (0, import_react4.useLayoutEffect)(() => {
1271
1272
  if (!isOpen) {
1273
+ lockedVerticalRef.current = null;
1272
1274
  setResolved(void 0);
1273
1275
  return;
1274
1276
  }
1275
- const compute = () => {
1277
+ const compute = (keepSide) => {
1276
1278
  const trigger = triggerRef.current;
1277
- const panel = panelRef.current;
1278
- if (!trigger || !panel) return;
1279
+ const panel2 = panelRef.current;
1280
+ if (!trigger || !panel2) return;
1279
1281
  const triggerRect = trigger.getBoundingClientRect();
1280
- const panelRect = panel.getBoundingClientRect();
1282
+ const panelRect = panel2.getBoundingClientRect();
1281
1283
  const viewportWidth = window.innerWidth;
1282
1284
  const viewportHeight = window.innerHeight;
1283
1285
  let { vertical, horizontal } = splitPlacement(placement);
@@ -1287,16 +1289,23 @@ var useContextMenuPosition = ({
1287
1289
  return triggerRect.left + triggerRect.width / 2 - panelRect.width / 2;
1288
1290
  return h === "start" ? triggerRect.left : triggerRect.right - panelRect.width;
1289
1291
  };
1290
- let top = computeTop(vertical);
1291
- const wantedBottom = top + panelRect.height;
1292
- if (top < 0 || wantedBottom > viewportHeight) {
1293
- const flipped = vertical === "bottom" ? "top" : "bottom";
1294
- const flippedTop = computeTop(flipped);
1295
- const flippedBottom = flippedTop + panelRect.height;
1296
- if (flippedTop >= 0 && flippedBottom <= viewportHeight) {
1297
- vertical = flipped;
1298
- top = flippedTop;
1292
+ let top;
1293
+ if (keepSide && lockedVerticalRef.current) {
1294
+ vertical = lockedVerticalRef.current;
1295
+ top = computeTop(vertical);
1296
+ } else {
1297
+ top = computeTop(vertical);
1298
+ const wantedBottom = top + panelRect.height;
1299
+ if (top < 0 || wantedBottom > viewportHeight) {
1300
+ const flipped = vertical === "bottom" ? "top" : "bottom";
1301
+ const flippedTop = computeTop(flipped);
1302
+ const flippedBottom = flippedTop + panelRect.height;
1303
+ if (flippedTop >= 0 && flippedBottom <= viewportHeight) {
1304
+ vertical = flipped;
1305
+ top = flippedTop;
1306
+ }
1299
1307
  }
1308
+ lockedVerticalRef.current = vertical;
1300
1309
  }
1301
1310
  let left = computeLeft(horizontal);
1302
1311
  if (horizontal === "center") {
@@ -1323,11 +1332,18 @@ var useContextMenuPosition = ({
1323
1332
  (prev) => prev && prev.top === next.top && prev.left === next.left && prev.placement === next.placement ? prev : next
1324
1333
  );
1325
1334
  };
1326
- compute();
1327
- const onResize = () => compute();
1335
+ compute(false);
1336
+ const onResize = () => compute(false);
1328
1337
  window.addEventListener("resize", onResize);
1338
+ let observer;
1339
+ const panel = panelRef.current;
1340
+ if (panel && typeof ResizeObserver !== "undefined") {
1341
+ observer = new ResizeObserver(() => compute(true));
1342
+ observer.observe(panel);
1343
+ }
1329
1344
  return () => {
1330
1345
  window.removeEventListener("resize", onResize);
1346
+ observer?.disconnect();
1331
1347
  };
1332
1348
  }, [isOpen, placement, offset, triggerRef, panelRef]);
1333
1349
  return resolved;