@torpor/ui 1.3.1 → 1.3.3
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.
- package/dist/Popover/PopoverContent.torp +17 -1
- package/dist/Popover/index.js +1 -1
- package/dist/Popover/index.js.map +1 -1
- package/dist/Popover/showPopover.ts +5 -2
- package/dist/utils/hoverReveal.ts +62 -3
- package/dist/utils/popoutContent.ts +24 -9
- package/dist/utils/setPopoverPosition.ts +7 -2
- package/package.json +5 -5
|
@@ -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={(
|
|
75
|
+
onmount={(el) => {
|
|
76
|
+
portal(el);
|
|
77
|
+
popout.show();
|
|
78
|
+
}}
|
|
63
79
|
>
|
|
64
80
|
<slot />
|
|
65
81
|
</div>
|
package/dist/Popover/index.js
CHANGED
|
@@ -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 ||
|
|
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 ||
|
|
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 ||
|
|
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
|
-
|
|
197
|
+
reveal();
|
|
141
198
|
return;
|
|
142
199
|
}
|
|
143
200
|
if (openTimer === undefined) {
|
|
144
201
|
openTimer = setTimeout(() => {
|
|
145
202
|
openTimer = undefined;
|
|
146
|
-
|
|
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
|
-
|
|
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
|
}
|
|
@@ -43,6 +43,10 @@ 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;
|
|
46
50
|
|
|
47
51
|
function setPosition() {
|
|
48
52
|
const anchor = options.getAnchor();
|
|
@@ -83,12 +87,21 @@ export function createPopoutContent(options: PopoutContentOptions): {
|
|
|
83
87
|
options.close(e);
|
|
84
88
|
}
|
|
85
89
|
|
|
90
|
+
function handleDocumentFocusIn(e: FocusEvent) {
|
|
91
|
+
const content = options.getContent();
|
|
92
|
+
if (content && content.contains(e.target as Node)) {
|
|
93
|
+
hadFocus = true;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
86
97
|
function show() {
|
|
87
98
|
shown = true;
|
|
88
99
|
|
|
89
100
|
closeOnClick = false;
|
|
101
|
+
hadFocus = false;
|
|
90
102
|
addDocumentEvent("mousedown", handleDocumentMouseDown);
|
|
91
103
|
addDocumentEvent("click", handleDocumentClick);
|
|
104
|
+
addDocumentEvent("focusin", handleDocumentFocusIn);
|
|
92
105
|
if (options.closeOnEscape) {
|
|
93
106
|
addDocumentEvent("keydown", handleDocumentKeyDown);
|
|
94
107
|
}
|
|
@@ -110,6 +123,7 @@ export function createPopoutContent(options: PopoutContentOptions): {
|
|
|
110
123
|
|
|
111
124
|
removeDocumentEvent("mousedown", handleDocumentMouseDown);
|
|
112
125
|
removeDocumentEvent("click", handleDocumentClick);
|
|
126
|
+
removeDocumentEvent("focusin", handleDocumentFocusIn);
|
|
113
127
|
removeDocumentEvent("keydown", handleDocumentKeyDown);
|
|
114
128
|
removeEventListener("resize", setPosition);
|
|
115
129
|
const anchor = options.getAnchor();
|
|
@@ -117,17 +131,18 @@ export function createPopoutContent(options: PopoutContentOptions): {
|
|
|
117
131
|
getScrollParent(anchor).removeEventListener("scroll", setPosition);
|
|
118
132
|
}
|
|
119
133
|
|
|
120
|
-
// Focus the anchor element again, per the WAI guidelines,
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
//
|
|
134
|
+
// Focus the anchor element again, per the WAI guidelines, when the
|
|
135
|
+
// popout itself had focus. A mouse-opened hover popover never does, so
|
|
136
|
+
// closing it must not steal focus (and show a focus ring). Focus that
|
|
137
|
+
// has already moved elsewhere (e.g. to an adjacent menubar item's
|
|
138
|
+
// menu) is left alone; focus that fell back to the body when the
|
|
139
|
+
// focused content was removed still returns to the anchor.
|
|
124
140
|
if (options.refocusAnchorOnHide) {
|
|
125
141
|
const content = options.getContent();
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
) {
|
|
142
|
+
const active = document.activeElement;
|
|
143
|
+
const focusInContent = content !== undefined && active !== null && content.contains(active);
|
|
144
|
+
const focusFellBack = hadFocus && (active === null || active === document.body);
|
|
145
|
+
if (focusInContent || focusFellBack) {
|
|
131
146
|
const el = options.getAnchor();
|
|
132
147
|
if (el) {
|
|
133
148
|
// The refocus is script-driven, not user intent: arm the
|
|
@@ -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.
|
|
3
|
+
"version": "1.3.3",
|
|
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.
|
|
185
|
-
"@torpor/view": "^1.2.
|
|
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.
|
|
192
|
-
"@torpor/prettier": "^1.0.
|
|
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",
|