@hansenexus/hud 0.5.1 → 0.6.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.
@@ -66,7 +66,7 @@ const DATA_CSS = `
66
66
  font-family: var(--hud-mono);
67
67
  font-size: 10px;
68
68
  }
69
- .hud .data-segment:hover:not(:disabled) { background: var(--hud-bg-subtle); }
69
+ .hud .data-segment:hover:not(:disabled):not([aria-pressed="true"]) { background: var(--hud-bg-subtle); }
70
70
  .hud .data-segment[aria-pressed="true"] {
71
71
  background: var(--hud-accent);
72
72
  border-color: var(--hud-accent);
package/dist/index.js CHANGED
@@ -790,8 +790,176 @@ function Workspace({ appSlug, panels, apiRef, children }) {
790
790
  })
791
791
  });
792
792
  }
793
+ /** Clearance kept between the pill and a host element it moves away from. */
794
+ const CLEARANCE = 8;
795
+ /** The pill never moves further than this share of the viewport height off its edge. */
796
+ const MAX_SHIFT_SHARE = .4;
797
+ /** On a narrow viewport the pill takes at most this share of the width. */
798
+ const NARROW_SHARE = .4;
799
+ /** Extra cost of a top corner when every corner needs a shift: host navs live up there. */
800
+ const TOP_PENALTY = 48;
801
+ const isBottom = (corner) => corner.startsWith("bottom");
802
+ const isRight = (corner) => corner.endsWith("right");
803
+ function pillRect({ corner, shift }, size, viewport, reserve) {
804
+ return {
805
+ left: isRight(corner) ? viewport.w - reserve.right - 12 - size.width : reserve.left + 12,
806
+ top: isBottom(corner) ? viewport.h - reserve.bottom - 12 - shift - size.height : 12 + shift,
807
+ width: size.width,
808
+ height: size.height
809
+ };
810
+ }
811
+ function intersects(a, b) {
812
+ return a.left < b.left + b.width && b.left < a.left + a.width && a.top < b.top + b.height && b.top < a.top + a.height;
813
+ }
814
+ /**
815
+ * Shifts the pill away from its corner's edge until it clears every obstacle,
816
+ * or returns null when that would take it past the shift cap.
817
+ */
818
+ function shiftWithin(corner, options) {
819
+ const { viewport, size, obstacles, reserve } = options;
820
+ const max = viewport.h * MAX_SHIFT_SHARE;
821
+ let placement = {
822
+ corner,
823
+ shift: 0
824
+ };
825
+ for (let round = 0; round <= obstacles.length; round++) {
826
+ const rect = pillRect(placement, size, viewport, reserve);
827
+ const hits = obstacles.filter((o) => intersects(rect, o));
828
+ if (hits.length === 0) return placement;
829
+ const shift = Math.max(...hits.map((o) => isBottom(corner) ? viewport.h - reserve.bottom - 12 - o.top + CLEARANCE : o.top + o.height + CLEARANCE - 12));
830
+ if (shift > max || shift <= placement.shift) return null;
831
+ placement = {
832
+ corner,
833
+ shift
834
+ };
835
+ }
836
+ return null;
837
+ }
838
+ /**
839
+ * Where the pill goes. A dragged corner is kept and only shifted. Otherwise the
840
+ * first corner that is free as is wins; when none is, the corner that needs
841
+ * the smallest shift, with top corners penalised. Null when no corner clears its obstacles within the cap:
842
+ * the caller then shrinks the pill to compact mode and tries again.
843
+ */
844
+ function placePill(options) {
845
+ if (options.pinned) return shiftWithin(options.pinned, options);
846
+ const cost = ({ corner, shift }) => shift + (isBottom(corner) ? 0 : TOP_PENALTY);
847
+ let best = null;
848
+ for (const corner of CORNERS) {
849
+ const placement = shiftWithin(corner, options);
850
+ if (placement?.shift === 0) return placement;
851
+ if (placement && (!best || cost(placement) < cost(best))) best = placement;
852
+ }
853
+ return best;
854
+ }
855
+ /** The corner a dragged pill lands in: the viewport quadrant of the drop point. */
856
+ function nearestCorner(x, y, viewport) {
857
+ return `${y > viewport.h / 2 ? "bottom" : "top"}-${x > viewport.w / 2 ? "right" : "left"}`;
858
+ }
859
+ const isNarrow = (viewport) => viewport.w < 900;
860
+ /**
861
+ * Width left for plugin items next to the fixed controls of `fixed` pixels
862
+ * (`chrome` is the pill's own padding and border). On a narrow viewport the
863
+ * share cap applies on top of the room pinned panels leave.
864
+ */
865
+ function itemBudget({ viewport, reserve, fixed, chrome }) {
866
+ const room = viewport.w - reserve.left - reserve.right - 24 - chrome - fixed;
867
+ if (!isNarrow(viewport)) return room;
868
+ return Math.min(room, Math.floor(viewport.w * NARROW_SHARE) - chrome - fixed);
869
+ }
870
+ /**
871
+ * Which plugin items fit in `budget` pixels. Higher priority first, then pill
872
+ * order; an item that does not fit is skipped, so a smaller one after it can
873
+ * still take the remaining room. `compact` keeps only prioritised items.
874
+ */
875
+ function fitItems(items, budget, { compact = false, gap = 0 } = {}) {
876
+ const ranked = items.map((item, index) => ({
877
+ item,
878
+ index
879
+ })).filter(({ item }) => !compact || item.priority > 0).sort((a, b) => b.item.priority - a.item.priority || a.index - b.index);
880
+ const visible = /* @__PURE__ */ new Set();
881
+ let left = budget;
882
+ for (const { item } of ranked) {
883
+ const cost = item.width + gap;
884
+ if (cost > left) continue;
885
+ visible.add(item.id);
886
+ left -= cost;
887
+ }
888
+ return visible;
889
+ }
890
+ /**
891
+ * How far to move `rect` so it lies inside `area`, `margin` off each edge.
892
+ * On an axis where it is too big to fit, its start is aligned with the area's.
893
+ */
894
+ function clampOffset(rect, area, margin) {
895
+ const axis = (start, size, areaStart, areaSize) => {
896
+ const min = areaStart + margin;
897
+ const max = areaStart + areaSize - margin - size;
898
+ if (max < min || start < min) return min - start;
899
+ return start > max ? max - start : 0;
900
+ };
901
+ return {
902
+ x: axis(rect.left, rect.width, area.left, area.width),
903
+ y: axis(rect.top, rect.height, area.top, area.height)
904
+ };
905
+ }
906
+ //#endregion
907
+ //#region src/shell/pill/visual-viewport.ts
908
+ function subscribe(listener) {
909
+ const vv = window.visualViewport;
910
+ window.addEventListener("resize", listener);
911
+ vv?.addEventListener("resize", listener);
912
+ vv?.addEventListener("scroll", listener);
913
+ return () => {
914
+ window.removeEventListener("resize", listener);
915
+ vv?.removeEventListener("resize", listener);
916
+ vv?.removeEventListener("scroll", listener);
917
+ };
918
+ }
919
+ let cached = {
920
+ x: 0,
921
+ y: 0,
922
+ w: 0,
923
+ h: 0,
924
+ detached: false
925
+ };
926
+ /**
927
+ * On a phone, a page wider than the screen (an image with a fixed width is
928
+ * enough) grows the layout viewport past the visible area, and a pill fixed
929
+ * to the bottom edge ends up below the fold. The visual viewport is what the
930
+ * user sees; the pill places itself in it.
931
+ */
932
+ function read() {
933
+ const root = document.documentElement;
934
+ const layoutW = root.clientWidth || window.innerWidth;
935
+ const layoutH = root.clientHeight || window.innerHeight;
936
+ const vv = window.visualViewport;
937
+ const x = vv?.offsetLeft ?? 0;
938
+ const y = vv?.offsetTop ?? 0;
939
+ const w = Math.round(vv?.width ?? layoutW);
940
+ const h = Math.round(vv?.height ?? layoutH);
941
+ const detached = x !== 0 || y !== 0 || window.innerWidth > w + 1 || window.innerHeight > h + 1;
942
+ const next = {
943
+ x,
944
+ y,
945
+ w: detached ? w : layoutW,
946
+ h: detached ? h : layoutH,
947
+ detached
948
+ };
949
+ if (next.x !== cached.x || next.y !== cached.y || next.w !== cached.w || next.h !== cached.h || next.detached !== cached.detached) cached = next;
950
+ return cached;
951
+ }
952
+ function useVisualViewport() {
953
+ return useSyncExternalStore(subscribe, read, read);
954
+ }
793
955
  //#endregion
794
956
  //#region src/shell/panels/pill-menu.tsx
957
+ /** Distance the open menu keeps from the edges of the visible area. */
958
+ const MENU_MARGIN = 8;
959
+ const NO_OFFSET = {
960
+ x: 0,
961
+ y: 0
962
+ };
795
963
  /**
796
964
  * The pill's "⋯" menu: every panel, every command with its shortcut, and Reset
797
965
  * layout. `overflow` counts the pill items that did not fit; their panels are
@@ -803,6 +971,34 @@ function PillMenu({ overflow = 0 }) {
803
971
  const apple = useMemo(isApplePlatform, []);
804
972
  const [open, setOpen] = useState(false);
805
973
  const ref = useRef(null);
974
+ const menuRef = useRef(null);
975
+ const visual = useVisualViewport();
976
+ const [offset, setOffset] = useState(NO_OFFSET);
977
+ useLayoutEffect(() => {
978
+ const menu = menuRef.current;
979
+ if (!open || !menu) {
980
+ setOffset(NO_OFFSET);
981
+ return;
982
+ }
983
+ const box = menu.getBoundingClientRect();
984
+ if (box.width === 0 && box.height === 0) return;
985
+ const next = clampOffset({
986
+ left: box.left - offset.x,
987
+ top: box.top - offset.y,
988
+ width: box.width,
989
+ height: box.height
990
+ }, {
991
+ left: visual.x,
992
+ top: visual.y,
993
+ width: visual.w,
994
+ height: visual.h
995
+ }, MENU_MARGIN);
996
+ if (next.x !== offset.x || next.y !== offset.y) setOffset(next);
997
+ }, [
998
+ open,
999
+ visual,
1000
+ offset
1001
+ ]);
806
1002
  useEffect(() => {
807
1003
  if (!open) return;
808
1004
  const onPointerDown = (e) => {
@@ -833,9 +1029,11 @@ function PillMenu({ overflow = 0 }) {
833
1029
  children: ["+", overflow]
834
1030
  })]
835
1031
  }), open && /* @__PURE__ */ jsxs("div", {
1032
+ ref: menuRef,
836
1033
  className: "menu",
837
1034
  role: "menu",
838
1035
  "aria-label": "HUD menu",
1036
+ style: offset.x || offset.y ? { transform: `translate(${offset.x}px, ${offset.y}px)` } : void 0,
839
1037
  children: [
840
1038
  /* @__PURE__ */ jsx("div", {
841
1039
  className: "menu-label",
@@ -1232,139 +1430,6 @@ function useObstacles() {
1232
1430
  }, []);
1233
1431
  return rects;
1234
1432
  }
1235
- /** Clearance kept between the pill and a host element it moves away from. */
1236
- const CLEARANCE = 8;
1237
- /** The pill never moves further than this share of the viewport height off its edge. */
1238
- const MAX_SHIFT_SHARE = .4;
1239
- /** Extra cost of a top corner when every corner needs a shift: host navs live up there. */
1240
- const TOP_PENALTY = 48;
1241
- const isBottom = (corner) => corner.startsWith("bottom");
1242
- const isRight = (corner) => corner.endsWith("right");
1243
- function pillRect({ corner, shift }, size, viewport, reserve) {
1244
- return {
1245
- left: isRight(corner) ? viewport.w - reserve.right - 12 - size.width : reserve.left + 12,
1246
- top: isBottom(corner) ? viewport.h - reserve.bottom - 12 - shift - size.height : 12 + shift,
1247
- width: size.width,
1248
- height: size.height
1249
- };
1250
- }
1251
- function intersects(a, b) {
1252
- return a.left < b.left + b.width && b.left < a.left + a.width && a.top < b.top + b.height && b.top < a.top + a.height;
1253
- }
1254
- /**
1255
- * Shifts the pill away from its corner's edge until it clears every obstacle,
1256
- * or returns null when that would take it past the shift cap.
1257
- */
1258
- function shiftWithin(corner, options) {
1259
- const { viewport, size, obstacles, reserve } = options;
1260
- const max = viewport.h * MAX_SHIFT_SHARE;
1261
- let placement = {
1262
- corner,
1263
- shift: 0
1264
- };
1265
- for (let round = 0; round <= obstacles.length; round++) {
1266
- const rect = pillRect(placement, size, viewport, reserve);
1267
- const hits = obstacles.filter((o) => intersects(rect, o));
1268
- if (hits.length === 0) return placement;
1269
- const shift = Math.max(...hits.map((o) => isBottom(corner) ? viewport.h - reserve.bottom - 12 - o.top + CLEARANCE : o.top + o.height + CLEARANCE - 12));
1270
- if (shift > max || shift <= placement.shift) return null;
1271
- placement = {
1272
- corner,
1273
- shift
1274
- };
1275
- }
1276
- return null;
1277
- }
1278
- /**
1279
- * Where the pill goes. A dragged corner is kept and only shifted. Otherwise the
1280
- * first corner that is free as is wins; when none is, the corner that needs
1281
- * the smallest shift, with top corners penalised. Null when no corner clears its obstacles within the cap:
1282
- * the caller then shrinks the pill to compact mode and tries again.
1283
- */
1284
- function placePill(options) {
1285
- if (options.pinned) return shiftWithin(options.pinned, options);
1286
- const cost = ({ corner, shift }) => shift + (isBottom(corner) ? 0 : TOP_PENALTY);
1287
- let best = null;
1288
- for (const corner of CORNERS) {
1289
- const placement = shiftWithin(corner, options);
1290
- if (placement?.shift === 0) return placement;
1291
- if (placement && (!best || cost(placement) < cost(best))) best = placement;
1292
- }
1293
- return best;
1294
- }
1295
- /** The corner a dragged pill lands in: the viewport quadrant of the drop point. */
1296
- function nearestCorner(x, y, viewport) {
1297
- return `${y > viewport.h / 2 ? "bottom" : "top"}-${x > viewport.w / 2 ? "right" : "left"}`;
1298
- }
1299
- /**
1300
- * Which plugin items fit in `budget` pixels. Higher priority first, then pill
1301
- * order; an item that does not fit is skipped, so a smaller one after it can
1302
- * still take the remaining room. `compact` keeps only prioritised items.
1303
- */
1304
- function fitItems(items, budget, { compact = false, gap = 0 } = {}) {
1305
- const ranked = items.map((item, index) => ({
1306
- item,
1307
- index
1308
- })).filter(({ item }) => !compact || item.priority > 0).sort((a, b) => b.item.priority - a.item.priority || a.index - b.index);
1309
- const visible = /* @__PURE__ */ new Set();
1310
- let left = budget;
1311
- for (const { item } of ranked) {
1312
- const cost = item.width + gap;
1313
- if (cost > left) continue;
1314
- visible.add(item.id);
1315
- left -= cost;
1316
- }
1317
- return visible;
1318
- }
1319
- //#endregion
1320
- //#region src/shell/pill/visual-viewport.ts
1321
- function subscribe(listener) {
1322
- const vv = window.visualViewport;
1323
- window.addEventListener("resize", listener);
1324
- vv?.addEventListener("resize", listener);
1325
- vv?.addEventListener("scroll", listener);
1326
- return () => {
1327
- window.removeEventListener("resize", listener);
1328
- vv?.removeEventListener("resize", listener);
1329
- vv?.removeEventListener("scroll", listener);
1330
- };
1331
- }
1332
- let cached = {
1333
- x: 0,
1334
- y: 0,
1335
- w: 0,
1336
- h: 0,
1337
- detached: false
1338
- };
1339
- /**
1340
- * On a phone, a page wider than the screen (an image with a fixed width is
1341
- * enough) grows the layout viewport past the visible area, and a pill fixed
1342
- * to the bottom edge ends up below the fold. The visual viewport is what the
1343
- * user sees; the pill places itself in it.
1344
- */
1345
- function read() {
1346
- const root = document.documentElement;
1347
- const layoutW = root.clientWidth || window.innerWidth;
1348
- const layoutH = root.clientHeight || window.innerHeight;
1349
- const vv = window.visualViewport;
1350
- const x = vv?.offsetLeft ?? 0;
1351
- const y = vv?.offsetTop ?? 0;
1352
- const w = Math.round(vv?.width ?? layoutW);
1353
- const h = Math.round(vv?.height ?? layoutH);
1354
- const detached = x !== 0 || y !== 0 || window.innerWidth > w + 1 || window.innerHeight > h + 1;
1355
- const next = {
1356
- x,
1357
- y,
1358
- w: detached ? w : layoutW,
1359
- h: detached ? h : layoutH,
1360
- detached
1361
- };
1362
- if (next.x !== cached.x || next.y !== cached.y || next.w !== cached.w || next.h !== cached.h || next.detached !== cached.detached) cached = next;
1363
- return cached;
1364
- }
1365
- function useVisualViewport() {
1366
- return useSyncExternalStore(subscribe, read, read);
1367
- }
1368
1433
  //#endregion
1369
1434
  //#region src/shell/pill/pill.tsx
1370
1435
  /** `.pill` gap, padding and border, which the item widths do not include. */
@@ -1402,8 +1467,9 @@ function sameMeasure(a, b) {
1402
1467
  }
1403
1468
  /**
1404
1469
  * Fits the items into the width left by pinned panels, then places the pill.
1405
- * When no corner is free even after shifting, compact mode keeps only
1406
- * prioritised items and the pill is placed again at that size.
1470
+ * On a narrow viewport, or when no corner is free even after shifting,
1471
+ * compact mode keeps only prioritised items (within 40 percent of the width
1472
+ * on a narrow viewport) and the pill is placed at that size.
1407
1473
  */
1408
1474
  function arrange(slots, measured, options) {
1409
1475
  const { viewport, reserve } = options;
@@ -1412,23 +1478,30 @@ function arrange(slots, measured, options) {
1412
1478
  width: measured.items[plugin.id] ?? 0,
1413
1479
  priority: plugin.pillPriority ?? 0
1414
1480
  }));
1415
- const budget = viewport.w - reserve.left - reserve.right - 24 - CHROME - measured.fixed;
1481
+ const budget = itemBudget({
1482
+ viewport,
1483
+ reserve,
1484
+ fixed: measured.fixed,
1485
+ chrome: CHROME
1486
+ });
1416
1487
  const sizeOf = (visible) => ({
1417
1488
  width: CHROME + measured.fixed + items.reduce((sum, item) => sum + (visible.has(item.id) ? item.width + ITEM_GAP : 0), 0),
1418
1489
  height: measured.height
1419
1490
  });
1420
- const full = fitItems(items, budget, { gap: ITEM_GAP });
1421
- const fullSize = sizeOf(full);
1422
- const placed = placePill({
1423
- ...options,
1424
- size: fullSize
1425
- });
1426
- if (placed) return {
1427
- visible: full,
1428
- placement: placed,
1429
- size: fullSize,
1430
- compact: false
1431
- };
1491
+ if (!isNarrow(viewport)) {
1492
+ const full = fitItems(items, budget, { gap: ITEM_GAP });
1493
+ const fullSize = sizeOf(full);
1494
+ const placed = placePill({
1495
+ ...options,
1496
+ size: fullSize
1497
+ });
1498
+ if (placed) return {
1499
+ visible: full,
1500
+ placement: placed,
1501
+ size: fullSize,
1502
+ compact: false
1503
+ };
1504
+ }
1432
1505
  const compact = fitItems(items, budget, {
1433
1506
  gap: ITEM_GAP,
1434
1507
  compact: true
@@ -25,7 +25,7 @@ function DataPill({ ctx }) {
25
25
  }
26
26
  //#endregion
27
27
  //#region src/plugins/data/index.tsx
28
- const Panel = lazy(() => import("../data-panel-DhxAmP91.js"));
28
+ const Panel = lazy(() => import("../data-panel-BmcHCQnM.js"));
29
29
  /**
30
30
  * Role switch, feature-flag overrides, seeds and mock scenarios. The pill
31
31
  * shows who the page is signed in as, how many flags are overridden and how
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hansenexus/hud",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Modular dev HUD for React apps: status pill, floating panels, element grabber, plugin API.",
5
5
  "license": "MIT",
6
6
  "type": "module",