@hansenexus/hud 0.5.1 → 0.5.2

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,163 @@ 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
+ /** Extra cost of a top corner when every corner needs a shift: host navs live up there. */
798
+ const TOP_PENALTY = 48;
799
+ const isBottom = (corner) => corner.startsWith("bottom");
800
+ const isRight = (corner) => corner.endsWith("right");
801
+ function pillRect({ corner, shift }, size, viewport, reserve) {
802
+ return {
803
+ left: isRight(corner) ? viewport.w - reserve.right - 12 - size.width : reserve.left + 12,
804
+ top: isBottom(corner) ? viewport.h - reserve.bottom - 12 - shift - size.height : 12 + shift,
805
+ width: size.width,
806
+ height: size.height
807
+ };
808
+ }
809
+ function intersects(a, b) {
810
+ 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;
811
+ }
812
+ /**
813
+ * Shifts the pill away from its corner's edge until it clears every obstacle,
814
+ * or returns null when that would take it past the shift cap.
815
+ */
816
+ function shiftWithin(corner, options) {
817
+ const { viewport, size, obstacles, reserve } = options;
818
+ const max = viewport.h * MAX_SHIFT_SHARE;
819
+ let placement = {
820
+ corner,
821
+ shift: 0
822
+ };
823
+ for (let round = 0; round <= obstacles.length; round++) {
824
+ const rect = pillRect(placement, size, viewport, reserve);
825
+ const hits = obstacles.filter((o) => intersects(rect, o));
826
+ if (hits.length === 0) return placement;
827
+ const shift = Math.max(...hits.map((o) => isBottom(corner) ? viewport.h - reserve.bottom - 12 - o.top + CLEARANCE : o.top + o.height + CLEARANCE - 12));
828
+ if (shift > max || shift <= placement.shift) return null;
829
+ placement = {
830
+ corner,
831
+ shift
832
+ };
833
+ }
834
+ return null;
835
+ }
836
+ /**
837
+ * Where the pill goes. A dragged corner is kept and only shifted. Otherwise the
838
+ * first corner that is free as is wins; when none is, the corner that needs
839
+ * the smallest shift, with top corners penalised. Null when no corner clears its obstacles within the cap:
840
+ * the caller then shrinks the pill to compact mode and tries again.
841
+ */
842
+ function placePill(options) {
843
+ if (options.pinned) return shiftWithin(options.pinned, options);
844
+ const cost = ({ corner, shift }) => shift + (isBottom(corner) ? 0 : TOP_PENALTY);
845
+ let best = null;
846
+ for (const corner of CORNERS) {
847
+ const placement = shiftWithin(corner, options);
848
+ if (placement?.shift === 0) return placement;
849
+ if (placement && (!best || cost(placement) < cost(best))) best = placement;
850
+ }
851
+ return best;
852
+ }
853
+ /** The corner a dragged pill lands in: the viewport quadrant of the drop point. */
854
+ function nearestCorner(x, y, viewport) {
855
+ return `${y > viewport.h / 2 ? "bottom" : "top"}-${x > viewport.w / 2 ? "right" : "left"}`;
856
+ }
857
+ /**
858
+ * Which plugin items fit in `budget` pixels. Higher priority first, then pill
859
+ * order; an item that does not fit is skipped, so a smaller one after it can
860
+ * still take the remaining room. `compact` keeps only prioritised items.
861
+ */
862
+ function fitItems(items, budget, { compact = false, gap = 0 } = {}) {
863
+ const ranked = items.map((item, index) => ({
864
+ item,
865
+ index
866
+ })).filter(({ item }) => !compact || item.priority > 0).sort((a, b) => b.item.priority - a.item.priority || a.index - b.index);
867
+ const visible = /* @__PURE__ */ new Set();
868
+ let left = budget;
869
+ for (const { item } of ranked) {
870
+ const cost = item.width + gap;
871
+ if (cost > left) continue;
872
+ visible.add(item.id);
873
+ left -= cost;
874
+ }
875
+ return visible;
876
+ }
877
+ /**
878
+ * How far to move `rect` so it lies inside `area`, `margin` off each edge.
879
+ * On an axis where it is too big to fit, its start is aligned with the area's.
880
+ */
881
+ function clampOffset(rect, area, margin) {
882
+ const axis = (start, size, areaStart, areaSize) => {
883
+ const min = areaStart + margin;
884
+ const max = areaStart + areaSize - margin - size;
885
+ if (max < min || start < min) return min - start;
886
+ return start > max ? max - start : 0;
887
+ };
888
+ return {
889
+ x: axis(rect.left, rect.width, area.left, area.width),
890
+ y: axis(rect.top, rect.height, area.top, area.height)
891
+ };
892
+ }
893
+ //#endregion
894
+ //#region src/shell/pill/visual-viewport.ts
895
+ function subscribe(listener) {
896
+ const vv = window.visualViewport;
897
+ window.addEventListener("resize", listener);
898
+ vv?.addEventListener("resize", listener);
899
+ vv?.addEventListener("scroll", listener);
900
+ return () => {
901
+ window.removeEventListener("resize", listener);
902
+ vv?.removeEventListener("resize", listener);
903
+ vv?.removeEventListener("scroll", listener);
904
+ };
905
+ }
906
+ let cached = {
907
+ x: 0,
908
+ y: 0,
909
+ w: 0,
910
+ h: 0,
911
+ detached: false
912
+ };
913
+ /**
914
+ * On a phone, a page wider than the screen (an image with a fixed width is
915
+ * enough) grows the layout viewport past the visible area, and a pill fixed
916
+ * to the bottom edge ends up below the fold. The visual viewport is what the
917
+ * user sees; the pill places itself in it.
918
+ */
919
+ function read() {
920
+ const root = document.documentElement;
921
+ const layoutW = root.clientWidth || window.innerWidth;
922
+ const layoutH = root.clientHeight || window.innerHeight;
923
+ const vv = window.visualViewport;
924
+ const x = vv?.offsetLeft ?? 0;
925
+ const y = vv?.offsetTop ?? 0;
926
+ const w = Math.round(vv?.width ?? layoutW);
927
+ const h = Math.round(vv?.height ?? layoutH);
928
+ const detached = x !== 0 || y !== 0 || window.innerWidth > w + 1 || window.innerHeight > h + 1;
929
+ const next = {
930
+ x,
931
+ y,
932
+ w: detached ? w : layoutW,
933
+ h: detached ? h : layoutH,
934
+ detached
935
+ };
936
+ if (next.x !== cached.x || next.y !== cached.y || next.w !== cached.w || next.h !== cached.h || next.detached !== cached.detached) cached = next;
937
+ return cached;
938
+ }
939
+ function useVisualViewport() {
940
+ return useSyncExternalStore(subscribe, read, read);
941
+ }
793
942
  //#endregion
794
943
  //#region src/shell/panels/pill-menu.tsx
944
+ /** Distance the open menu keeps from the edges of the visible area. */
945
+ const MENU_MARGIN = 8;
946
+ const NO_OFFSET = {
947
+ x: 0,
948
+ y: 0
949
+ };
795
950
  /**
796
951
  * The pill's "⋯" menu: every panel, every command with its shortcut, and Reset
797
952
  * layout. `overflow` counts the pill items that did not fit; their panels are
@@ -803,6 +958,34 @@ function PillMenu({ overflow = 0 }) {
803
958
  const apple = useMemo(isApplePlatform, []);
804
959
  const [open, setOpen] = useState(false);
805
960
  const ref = useRef(null);
961
+ const menuRef = useRef(null);
962
+ const visual = useVisualViewport();
963
+ const [offset, setOffset] = useState(NO_OFFSET);
964
+ useLayoutEffect(() => {
965
+ const menu = menuRef.current;
966
+ if (!open || !menu) {
967
+ setOffset(NO_OFFSET);
968
+ return;
969
+ }
970
+ const box = menu.getBoundingClientRect();
971
+ if (box.width === 0 && box.height === 0) return;
972
+ const next = clampOffset({
973
+ left: box.left - offset.x,
974
+ top: box.top - offset.y,
975
+ width: box.width,
976
+ height: box.height
977
+ }, {
978
+ left: visual.x,
979
+ top: visual.y,
980
+ width: visual.w,
981
+ height: visual.h
982
+ }, MENU_MARGIN);
983
+ if (next.x !== offset.x || next.y !== offset.y) setOffset(next);
984
+ }, [
985
+ open,
986
+ visual,
987
+ offset
988
+ ]);
806
989
  useEffect(() => {
807
990
  if (!open) return;
808
991
  const onPointerDown = (e) => {
@@ -833,9 +1016,11 @@ function PillMenu({ overflow = 0 }) {
833
1016
  children: ["+", overflow]
834
1017
  })]
835
1018
  }), open && /* @__PURE__ */ jsxs("div", {
1019
+ ref: menuRef,
836
1020
  className: "menu",
837
1021
  role: "menu",
838
1022
  "aria-label": "HUD menu",
1023
+ style: offset.x || offset.y ? { transform: `translate(${offset.x}px, ${offset.y}px)` } : void 0,
839
1024
  children: [
840
1025
  /* @__PURE__ */ jsx("div", {
841
1026
  className: "menu-label",
@@ -1232,139 +1417,6 @@ function useObstacles() {
1232
1417
  }, []);
1233
1418
  return rects;
1234
1419
  }
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
1420
  //#endregion
1369
1421
  //#region src/shell/pill/pill.tsx
1370
1422
  /** `.pill` gap, padding and border, which the item widths do not include. */
@@ -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.5.2",
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",