@torpor/ui 1.3.2 → 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.
@@ -43,6 +43,11 @@ export function createPopoutContent(options: PopoutContentOptions): {
43
43
  } {
44
44
  let shown = false;
45
45
  let closeOnClick = false;
46
+ // Whether focus ever moved into the popout while it was shown. A
47
+ // mouse-opened popover never takes focus, so it must not yank focus back to
48
+ // its anchor when it hides.
49
+ let hadFocus = false;
50
+ let resizeObserver: ResizeObserver | null = null;
46
51
 
47
52
  function setPosition() {
48
53
  const anchor = options.getAnchor();
@@ -56,6 +61,22 @@ export function createPopoutContent(options: PopoutContentOptions): {
56
61
  }
57
62
  }
58
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
+
59
80
  function handleDocumentMouseDown() {
60
81
  closeOnClick = true;
61
82
  }
@@ -83,18 +104,28 @@ export function createPopoutContent(options: PopoutContentOptions): {
83
104
  options.close(e);
84
105
  }
85
106
 
107
+ function handleDocumentFocusIn(e: FocusEvent) {
108
+ const content = options.getContent();
109
+ if (content && content.contains(e.target as Node)) {
110
+ hadFocus = true;
111
+ }
112
+ }
113
+
86
114
  function show() {
87
115
  shown = true;
88
116
 
89
117
  closeOnClick = false;
118
+ hadFocus = false;
90
119
  addDocumentEvent("mousedown", handleDocumentMouseDown);
91
120
  addDocumentEvent("click", handleDocumentClick);
121
+ addDocumentEvent("focusin", handleDocumentFocusIn);
92
122
  if (options.closeOnEscape) {
93
123
  addDocumentEvent("keydown", handleDocumentKeyDown);
94
124
  }
95
125
 
96
126
  // Set the position and listen for window resize and scroll to reset the position
97
127
  setPosition();
128
+ observeContent();
98
129
  addEventListener("resize", setPosition);
99
130
  const anchor = options.getAnchor();
100
131
  if (anchor) {
@@ -110,24 +141,27 @@ export function createPopoutContent(options: PopoutContentOptions): {
110
141
 
111
142
  removeDocumentEvent("mousedown", handleDocumentMouseDown);
112
143
  removeDocumentEvent("click", handleDocumentClick);
144
+ removeDocumentEvent("focusin", handleDocumentFocusIn);
113
145
  removeDocumentEvent("keydown", handleDocumentKeyDown);
146
+ resizeObserver?.disconnect();
114
147
  removeEventListener("resize", setPosition);
115
148
  const anchor = options.getAnchor();
116
149
  if (anchor) {
117
150
  getScrollParent(anchor).removeEventListener("scroll", setPosition);
118
151
  }
119
152
 
120
- // Focus the anchor element again, per the WAI guidelines, unless focus
121
- // has already moved elsewhere (e.g. to an adjacent menubar item's menu).
122
- // If the focused element was removed with the content, focus has fallen
123
- // back to the body, and the anchor is focused again
153
+ // Focus the anchor element again, per the WAI guidelines, when the
154
+ // popout itself had focus. A mouse-opened hover popover never does, so
155
+ // closing it must not steal focus (and show a focus ring). Focus that
156
+ // has already moved elsewhere (e.g. to an adjacent menubar item's
157
+ // menu) is left alone; focus that fell back to the body when the
158
+ // focused content was removed still returns to the anchor.
124
159
  if (options.refocusAnchorOnHide) {
125
160
  const content = options.getContent();
126
- if (
127
- !content ||
128
- content.contains(document.activeElement) ||
129
- document.activeElement === document.body
130
- ) {
161
+ const active = document.activeElement;
162
+ const focusInContent = content !== undefined && active !== null && content.contains(active);
163
+ const focusFellBack = hadFocus && (active === null || active === document.body);
164
+ if (focusInContent || focusFellBack) {
131
165
  const el = options.getAnchor();
132
166
  if (el) {
133
167
  // The refocus is script-driven, not user intent: arm the
@@ -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.2",
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",