@torpor/ui 1.3.1 → 1.3.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.
@@ -46,6 +46,19 @@ export default function PopoverContent($props?: PopoverContentProps | undefined)
46
46
 
47
47
  let div: HTMLDivElement;
48
48
 
49
+ /**
50
+ * The content is fixed-positioned with viewport coordinates, so it must
51
+ * not stay inside an ancestor that can capture fixed descendants (a
52
+ * transform/filter/contain, or an `overflow`/stacking context). Moving it
53
+ * to the body also keeps its links out of the trigger's link context.
54
+ */
55
+ function portal(el: HTMLElement) {
56
+ const root = el.ownerDocument.body;
57
+ if (root !== null && el.parentNode !== root) {
58
+ root.appendChild(el);
59
+ }
60
+ }
61
+
49
62
  $run(() => {
50
63
  if (!$state.visible) {
51
64
  popout.hide();
@@ -59,7 +72,10 @@ export default function PopoverContent($props?: PopoverContentProps | undefined)
59
72
  class={["torp-popover-content", $props?.class]}
60
73
  {$props?.style}
61
74
  &ref={div}
62
- onmount={(_el) => popout.show()}
75
+ onmount={(el) => {
76
+ portal(el);
77
+ popout.show();
78
+ }}
63
79
  >
64
80
  <slot />
65
81
  </div>
@@ -13,7 +13,7 @@ import PopoverTrigger from "./PopoverTrigger.torp";
13
13
  */
14
14
  function showPopover(popover, anchor, props, target) {
15
15
  const allProps = Object.assign({}, props, { anchor });
16
- popover(target || anchor.parentElement || document.body, null, allProps);
16
+ popover(target || document.body, null, allProps);
17
17
  }
18
18
  //#endregion
19
19
  export { Popover, PopoverContent, PopoverHover, PopoverTrigger, showPopover };
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/Popover/showPopover.ts"],"sourcesContent":["// TODO: Allow passing in a variable -- so that you can show an inline popover\nimport type { Component } from \"@torpor/view\";\n\n/**\n * Shows a popover component in code.\n * @param popover The popover component\n * @param anchor The element to use as an anchor for the popover component\n * @param props The properties for the popover component\n * @param target The target element to show the popover in\n * @returns The value selected by the user\n */\nexport default function showPopover(\n\tpopover: Component,\n\tanchor: HTMLElement,\n\tprops?: Record<PropertyKey, any>,\n\ttarget?: Element,\n): void {\n\t// Add the anchor element to the properties\n\tconst allProps = Object.assign({}, props, { anchor });\n\n\t// Create the popover\n\t// TODO: This needs to clean up after itself, like the mount methods do\n\tpopover(target || anchor.parentElement || document.body, null, allProps);\n}\n"],"mappings":";;;;;;;;;;;;;AAWA,SAAwB,YACvB,SACA,QACA,OACA,QACO;CAEP,MAAM,WAAW,OAAO,OAAO,CAAC,GAAG,OAAO,EAAE,OAAO,CAAC;CAIpD,QAAQ,UAAU,OAAO,iBAAiB,SAAS,MAAM,MAAM,QAAQ;AACxE"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/Popover/showPopover.ts"],"sourcesContent":["// TODO: Allow passing in a variable -- so that you can show an inline popover\nimport type { Component } from \"@torpor/view\";\n\n/**\n * Shows a popover component in code.\n * @param popover The popover component\n * @param anchor The element to use as an anchor for the popover component\n * @param props The properties for the popover component\n * @param target The target element to show the popover in\n * @returns The value selected by the user\n */\nexport default function showPopover(\n\tpopover: Component,\n\tanchor: HTMLElement,\n\tprops?: Record<PropertyKey, any>,\n\ttarget?: Element,\n): void {\n\t// Add the anchor element to the properties\n\tconst allProps = Object.assign({}, props, { anchor });\n\n\t// Create the popover in the portal root by default: fixed-positioned\n\t// content must not stay inside the anchor's ancestors (transforms, filters\n\t// and `contain` capture fixed descendants, and `overflow`/stacking\n\t// contexts can clip it).\n\t// TODO: This needs to clean up after itself, like the mount methods do\n\tpopover(target || document.body, null, allProps);\n}\n"],"mappings":";;;;;;;;;;;;;AAWA,SAAwB,YACvB,SACA,QACA,OACA,QACO;CAEP,MAAM,WAAW,OAAO,OAAO,CAAC,GAAG,OAAO,EAAE,OAAO,CAAC;CAOpD,QAAQ,UAAU,SAAS,MAAM,MAAM,QAAQ;AAChD"}
@@ -18,7 +18,10 @@ export default function showPopover(
18
18
  // Add the anchor element to the properties
19
19
  const allProps = Object.assign({}, props, { anchor });
20
20
 
21
- // Create the popover
21
+ // Create the popover in the portal root by default: fixed-positioned
22
+ // content must not stay inside the anchor's ancestors (transforms, filters
23
+ // and `contain` capture fixed descendants, and `overflow`/stacking
24
+ // contexts can clip it).
22
25
  // TODO: This needs to clean up after itself, like the mount methods do
23
- popover(target || anchor.parentElement || document.body, null, allProps);
26
+ popover(target || document.body, null, allProps);
24
27
  }
@@ -54,6 +54,25 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
54
54
  let openTimer: ReturnType<typeof setTimeout> | undefined;
55
55
  let closeTimer: ReturnType<typeof setTimeout> | undefined;
56
56
 
57
+ // The content element currently watched for pointer/focus leaving it. It
58
+ // is attached once the pointer or focus enters the content (which cancels
59
+ // the trigger's close timer), so that leaving the content arms a close
60
+ // timer of its own -- otherwise the popout never closes after being
61
+ // entered.
62
+ let watchedContent: HTMLElement | null = null;
63
+
64
+ /**
65
+ * Shows the popout and starts watching its content. Watching from the
66
+ * moment it opens (rather than only after a trigger-leave close is
67
+ * canceled) means leaving the content still closes it if the trigger
68
+ * never received a leave event, e.g. when the content opens under the
69
+ * pointer.
70
+ */
71
+ function reveal() {
72
+ show();
73
+ watchContent();
74
+ }
75
+
57
76
  function clearTimers() {
58
77
  if (openTimer !== undefined) {
59
78
  clearTimeout(openTimer);
@@ -79,6 +98,7 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
79
98
  closeTimer = setTimeout(() => {
80
99
  closeTimer = undefined;
81
100
  stopListening();
101
+ unwatchContent();
82
102
  // The content hides with `refocusAnchorOnHide`, which drops focus
83
103
  // back onto this trigger; the popout machinery arms the scripted-
84
104
  // focus guard before it does, so handleFocus ignores it
@@ -104,6 +124,7 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
104
124
  clearTimeout(closeTimer);
105
125
  closeTimer = undefined;
106
126
  stopListening();
127
+ watchContent();
107
128
  }
108
129
  }
109
130
 
@@ -116,6 +137,7 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
116
137
  clearTimeout(closeTimer);
117
138
  closeTimer = undefined;
118
139
  document.removeEventListener("mouseover", handleDocumentOver);
140
+ watchContent();
119
141
  }
120
142
  }
121
143
 
@@ -125,6 +147,40 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
125
147
  return content === target || content.contains(target as Node);
126
148
  }
127
149
 
150
+ function watchContent() {
151
+ const content = getContent();
152
+ if (!content || content === watchedContent) return;
153
+ unwatchContent();
154
+ watchedContent = content;
155
+ content.addEventListener("mouseleave", handleContentLeave);
156
+ content.addEventListener("focusout", handleContentFocusOut);
157
+ }
158
+
159
+ function unwatchContent() {
160
+ if (watchedContent === null) return;
161
+ watchedContent.removeEventListener("mouseleave", handleContentLeave);
162
+ watchedContent.removeEventListener("focusout", handleContentFocusOut);
163
+ watchedContent = null;
164
+ }
165
+
166
+ function handleContentLeave(e: MouseEvent) {
167
+ if (!isVisible()) {
168
+ unwatchContent();
169
+ return;
170
+ }
171
+ if (insideContent(e.relatedTarget)) return;
172
+ scheduleClose();
173
+ }
174
+
175
+ function handleContentFocusOut(e: FocusEvent) {
176
+ if (!isVisible()) {
177
+ unwatchContent();
178
+ return;
179
+ }
180
+ if (insideContent(e.relatedTarget)) return;
181
+ scheduleClose();
182
+ }
183
+
128
184
  return {
129
185
  handleEnter() {
130
186
  if (disabled()) return;
@@ -133,17 +189,18 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
133
189
  clearTimeout(closeTimer);
134
190
  closeTimer = undefined;
135
191
  stopListening();
192
+ unwatchContent();
136
193
  }
137
194
  if (isVisible()) return;
138
195
 
139
196
  if (hoverDelay <= 0) {
140
- show();
197
+ reveal();
141
198
  return;
142
199
  }
143
200
  if (openTimer === undefined) {
144
201
  openTimer = setTimeout(() => {
145
202
  openTimer = undefined;
146
- show();
203
+ reveal();
147
204
  }, hoverDelay);
148
205
  }
149
206
  },
@@ -175,9 +232,10 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
175
232
  clearTimeout(closeTimer);
176
233
  closeTimer = undefined;
177
234
  stopListening();
235
+ unwatchContent();
178
236
  }
179
237
  if (!isVisible()) {
180
- show();
238
+ reveal();
181
239
  }
182
240
  },
183
241
 
@@ -191,6 +249,7 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
191
249
  dispose() {
192
250
  clearTimers();
193
251
  stopListening();
252
+ unwatchContent();
194
253
  },
195
254
  };
196
255
  }
@@ -4,11 +4,16 @@ export default function setPopoverPosition(
4
4
  side: "top" | "right" | "bottom" | "left",
5
5
  alignment: "start" | "center" | "end",
6
6
  ): void {
7
+ // Take the content out of flow BEFORE measuring it. A fixed-position
8
+ // element with `auto` width shrink-wraps its contents, while the same
9
+ // element in normal flow (especially once portaled to `body`) can measure
10
+ // as a full-width block. Measuring first makes the centering/clamping math
11
+ // use that inflated width and clamp the popover to the viewport edge.
12
+ content.style.position = "fixed";
13
+
7
14
  var anchorRect = anchor.getBoundingClientRect();
8
15
  var contentRect = content.getBoundingClientRect();
9
16
 
10
- content.style.position = "fixed";
11
-
12
17
  // Check that we there is enough room to show the content on the side requested
13
18
  // Otherwise, if there is enough room on the other side, show it there instead
14
19
  let calculatedSide = side;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torpor/ui",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
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.7",
185
- "@torpor/view": "^1.2.4"
184
+ "@torpor/unplugin": "^1.2.8",
185
+ "@torpor/view": "^1.2.5"
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.12",
192
- "@torpor/prettier": "^1.0.12",
191
+ "@torpor/check": "^1.0.13",
192
+ "@torpor/prettier": "^1.0.13",
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",