@torpor/ui 1.3.3 → 1.3.4

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.
@@ -47,6 +47,7 @@ export function createPopoutContent(options: PopoutContentOptions): {
47
47
  // mouse-opened popover never takes focus, so it must not yank focus back to
48
48
  // its anchor when it hides.
49
49
  let hadFocus = false;
50
+ let resizeObserver: ResizeObserver | null = null;
50
51
 
51
52
  function setPosition() {
52
53
  const anchor = options.getAnchor();
@@ -60,6 +61,22 @@ export function createPopoutContent(options: PopoutContentOptions): {
60
61
  }
61
62
  }
62
63
 
64
+ /**
65
+ * Content often renders before its data (or images/layout) arrive, which
66
+ * changes the size the placement was computed from. Watch the content and
67
+ * reposition whenever it resizes while open.
68
+ */
69
+ function observeContent() {
70
+ if (typeof ResizeObserver === "undefined") return;
71
+ const content = options.getContent();
72
+ if (!content) return;
73
+ resizeObserver ??= new ResizeObserver(() => {
74
+ if (shown) setPosition();
75
+ });
76
+ resizeObserver.disconnect();
77
+ resizeObserver.observe(content);
78
+ }
79
+
63
80
  function handleDocumentMouseDown() {
64
81
  closeOnClick = true;
65
82
  }
@@ -108,6 +125,7 @@ export function createPopoutContent(options: PopoutContentOptions): {
108
125
 
109
126
  // Set the position and listen for window resize and scroll to reset the position
110
127
  setPosition();
128
+ observeContent();
111
129
  addEventListener("resize", setPosition);
112
130
  const anchor = options.getAnchor();
113
131
  if (anchor) {
@@ -125,6 +143,7 @@ export function createPopoutContent(options: PopoutContentOptions): {
125
143
  removeDocumentEvent("click", handleDocumentClick);
126
144
  removeDocumentEvent("focusin", handleDocumentFocusIn);
127
145
  removeDocumentEvent("keydown", handleDocumentKeyDown);
146
+ resizeObserver?.disconnect();
128
147
  removeEventListener("resize", setPosition);
129
148
  const anchor = options.getAnchor();
130
149
  if (anchor) {
@@ -54,22 +54,26 @@ export default function setPopoverPosition(
54
54
 
55
55
  switch (calculatedSide) {
56
56
  case "top": {
57
- content.style.top = anchorRect.top - contentRect.height + "px";
57
+ // Clamp the axis the placement chose too: when neither side has
58
+ // enough room, the requested side is kept and must stay on screen.
59
+ content.style.top =
60
+ clamp(anchorRect.top - contentRect.height, window.innerHeight - contentRect.height) + "px";
58
61
  setVerticalAlignment(content, anchorRect, contentRect, alignment);
59
62
  break;
60
63
  }
61
64
  case "right": {
62
- content.style.left = anchorRect.right + "px";
65
+ content.style.left = clamp(anchorRect.right, window.innerWidth - contentRect.width) + "px";
63
66
  setHorizontalAlignment(content, anchorRect, contentRect, alignment);
64
67
  break;
65
68
  }
66
69
  case "bottom": {
67
- content.style.top = anchorRect.bottom + "px";
70
+ content.style.top = clamp(anchorRect.bottom, window.innerHeight - contentRect.height) + "px";
68
71
  setVerticalAlignment(content, anchorRect, contentRect, alignment);
69
72
  break;
70
73
  }
71
74
  case "left": {
72
- content.style.left = anchorRect.left - contentRect.width + "px";
75
+ content.style.left =
76
+ clamp(anchorRect.left - contentRect.width, window.innerWidth - contentRect.width) + "px";
73
77
  setHorizontalAlignment(content, anchorRect, contentRect, alignment);
74
78
  break;
75
79
  }
@@ -80,6 +84,11 @@ export default function setPopoverPosition(
80
84
  }
81
85
  }
82
86
 
87
+ /** Clamps a position to the viewport, keeping it non-negative. */
88
+ function clamp(value: number, max: number): number {
89
+ return Math.max(Math.min(value, max), 0);
90
+ }
91
+
83
92
  function setVerticalAlignment(
84
93
  content: HTMLElement,
85
94
  anchorRect: DOMRect,
@@ -107,10 +116,7 @@ function setVerticalAlignment(
107
116
  }
108
117
 
109
118
  // Try to make sure the content is entirely on the screen horizontally
110
- left = Math.min(left, window.innerWidth - contentRect.width);
111
- left = Math.max(left, 0);
112
-
113
- content.style.left = left + "px";
119
+ content.style.left = clamp(left, window.innerWidth - contentRect.width) + "px";
114
120
  }
115
121
 
116
122
  function setHorizontalAlignment(
@@ -140,8 +146,5 @@ function setHorizontalAlignment(
140
146
  }
141
147
 
142
148
  // Try to make sure the content is entirely on the screen vertically
143
- top = Math.min(top, window.innerHeight - contentRect.height);
144
- top = Math.max(top, 0);
145
-
146
- content.style.top = top + "px";
149
+ content.style.top = clamp(top, window.innerHeight - contentRect.height) + "px";
147
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torpor/ui",
3
- "version": "1.3.3",
3
+ "version": "1.3.4",
4
4
  "description": "Accessible, unstyled components for use in your Torpor front-end",
5
5
  "type": "module",
6
6
  "torpor": true,
@@ -181,15 +181,15 @@
181
181
  "author": "",
182
182
  "license": "ISC",
183
183
  "dependencies": {
184
- "@torpor/unplugin": "^1.2.8",
185
- "@torpor/view": "^1.2.5"
184
+ "@torpor/unplugin": "^1.2.9",
185
+ "@torpor/view": "^1.2.6"
186
186
  },
187
187
  "devDependencies": {
188
188
  "@testing-library/dom": "^10.4.2",
189
189
  "@testing-library/jest-dom": "^7.0.1",
190
190
  "@testing-library/user-event": "^14.6.8",
191
- "@torpor/check": "^1.0.13",
192
- "@torpor/prettier": "^1.0.13",
191
+ "@torpor/check": "^1.0.14",
192
+ "@torpor/prettier": "^1.0.14",
193
193
  "@types/jsdom": "^30.0.0",
194
194
  "@types/node": "^26.6.5",
195
195
  "@typescript/native-preview": "7.0.0-dev.20260707.2",