@adea-ai/ui 0.162.1 → 0.162.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.
- package/dist/lib/tooltip-focus-gate.d.ts +9 -0
- package/dist/lib/tooltip-focus-gate.d.ts.map +1 -1
- package/dist/lib/tooltip-focus-gate.js +14 -2
- package/dist/lib/tooltip-focus-gate.js.map +1 -1
- package/dist/r/src/lib/tooltip-focus-gate.ts +22 -1
- package/package.json +1 -1
- package/src/lib/tooltip-focus-gate.ts +22 -1
|
@@ -19,6 +19,15 @@ export declare function shouldSuppressTooltipFocus(input: {
|
|
|
19
19
|
isButtonLike: boolean;
|
|
20
20
|
keyboardIntentActive: boolean;
|
|
21
21
|
}): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Whether a `pointermove` comes from a pointer that moved, rather than one a
|
|
24
|
+
* browser dispatched at the same position because content scrolled under it.
|
|
25
|
+
* An event without movement fields counts as travel.
|
|
26
|
+
*/
|
|
27
|
+
export declare function pointerMoveTravelled(event: {
|
|
28
|
+
movementX?: number;
|
|
29
|
+
movementY?: number;
|
|
30
|
+
}): boolean;
|
|
22
31
|
/**
|
|
23
32
|
* Install the gate on a document. Idempotent per document; returns a disposer
|
|
24
33
|
* that removes the listeners (used by tests and by the tooltip root, which
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tooltip-focus-gate.d.ts","sourceRoot":"","sources":["../../src/lib/tooltip-focus-gate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"tooltip-focus-gate.d.ts","sourceRoot":"","sources":["../../src/lib/tooltip-focus-gate.ts"],"names":[],"mappings":"AAqFA;;;2BAG2B;AAC3B,eAAO,MAAM,8BAA8B,MAAM,CAAA;AAiBjD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAEzD;AAED,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAOhE;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE;IAChD,YAAY,EAAE,OAAO,CAAA;IACrB,oBAAoB,EAAE,OAAO,CAAA;CAC9B,GAAG,OAAO,CAEV;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAI/F;AAID;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,GAAE,QAAmB,GAAG,MAAM,IAAI,CAgF5E;AAWD,wBAAgB,uBAAuB,CAAC,SAAS,CAAC,EAAE,QAAQ,GAAG,IAAI,CAMlE;AAED,wBAAgB,uBAAuB,CAAC,SAAS,CAAC,EAAE,QAAQ,GAAG,IAAI,CAYlE"}
|
|
@@ -38,6 +38,16 @@ function isButtonLikeFocusTarget(target) {
|
|
|
38
38
|
function shouldSuppressTooltipFocus(input) {
|
|
39
39
|
return input.isButtonLike && !input.keyboardIntentActive;
|
|
40
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Whether a `pointermove` comes from a pointer that moved, rather than one a
|
|
43
|
+
* browser dispatched at the same position because content scrolled under it.
|
|
44
|
+
* An event without movement fields counts as travel.
|
|
45
|
+
*/
|
|
46
|
+
function pointerMoveTravelled(event) {
|
|
47
|
+
const { movementX, movementY } = event;
|
|
48
|
+
if (typeof movementX !== "number" || typeof movementY !== "number") return true;
|
|
49
|
+
return movementX !== 0 || movementY !== 0;
|
|
50
|
+
}
|
|
41
51
|
var installedDocuments = /* @__PURE__ */ new WeakSet();
|
|
42
52
|
/**
|
|
43
53
|
* Install the gate on a document. Idempotent per document; returns a disposer
|
|
@@ -102,7 +112,9 @@ function installTooltipFocusGate(doc = document) {
|
|
|
102
112
|
if (doc.activeElement !== phantom) return;
|
|
103
113
|
if (typeof phantom.blur === "function") phantom.blur();
|
|
104
114
|
};
|
|
105
|
-
const onPointerMove = () =>
|
|
115
|
+
const onPointerMove = (event) => {
|
|
116
|
+
if (pointerMoveTravelled(event)) releasePhantomFocus();
|
|
117
|
+
};
|
|
106
118
|
doc.addEventListener("keydown", onKeyDown, true);
|
|
107
119
|
doc.addEventListener("focus", onFocus, true);
|
|
108
120
|
doc.addEventListener("pointerdown", onPointerDown, true);
|
|
@@ -147,6 +159,6 @@ function releaseTooltipFocusGate(targetDoc) {
|
|
|
147
159
|
}
|
|
148
160
|
gateHolders.set(doc, holders - 1);
|
|
149
161
|
}
|
|
150
|
-
export { TOOLTIP_FOCUS_INTENT_WINDOW_MS, acquireTooltipFocusGate, installTooltipFocusGate, isButtonLikeFocusTarget, isTabFocusTarget, releaseTooltipFocusGate, shouldSuppressTooltipFocus };
|
|
162
|
+
export { TOOLTIP_FOCUS_INTENT_WINDOW_MS, acquireTooltipFocusGate, installTooltipFocusGate, isButtonLikeFocusTarget, isTabFocusTarget, pointerMoveTravelled, releaseTooltipFocusGate, shouldSuppressTooltipFocus };
|
|
151
163
|
|
|
152
164
|
//# sourceMappingURL=tooltip-focus-gate.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tooltip-focus-gate.js","names":[],"sources":["../../src/lib/tooltip-focus-gate.ts"],"sourcesContent":["/*\n * Tooltips open on active hover over the control, or on keyboard focus that\n * follows a Tab key press. They must never open from programmatic focus: the\n * autofocus a dialog or sheet gives its first control, and the focus a closing\n * overlay restores to its opener, both land on buttons that carry tooltips,\n * and the tooltip primitive opens immediately on focus and pins the tooltip\n * until blur. That is the \"tooltip auto-displays when a sheet opens\" report,\n * and the pinned tooltip is the \"tooltip gets stuck and never disappears\"\n * report: the pointer is nowhere near the control, so no pointer leave ever\n * closes it.\n *\n * The trigger listens for the native `focus` event on the trigger element, and\n * composites build tooltips internally (ActionButton renders one from its\n * `tooltip` prop), so the single interception point that covers every tooltip\n * is a capture-phase `focus` listener on `document`. When focus lands without a\n * keyboard-intent signal on a button-like element, the event is stopped before\n * any element listener runs: the tooltip never learns about the focus. Hover\n * opens are untouched (they ride pointer events), and the separate `focusin`\n * event still flows, so dialog focus-cycle tracking, `:focus-visible` styling,\n * and focus restoration all keep working. Focus still lands, only the event\n * is stopped, so `document.activeElement` and scripted `.blur()` behave.\n *\n * The keyboard-intent signal is a Tab key press immediately before the focus,\n * the only key that moves focus natively. Enter and Space open overlays too,\n * so any \"recent key\" window wide enough for those would let an overlay's own\n * autofocus back through; Tab-only keeps autofocus suppressed no matter how\n * the overlay was opened, while normal Tab navigation still announces each\n * control's tooltip (hover descriptions stay reachable from the keyboard).\n *\n * Suppression is scoped to button-like targets (`button`, `a[href]`,\n * `[role=\"button\"]`, `[role=\"link\"]`). Composite widgets that move focus\n * programmatically, menu items, listbox options, grid cells, are other\n * element types whose roving-focus highlighting must keep receiving focus\n * events, and text controls keep their own focus behaviour (select on focus,\n * caret placement).\n *\n * Suppressed focus is real as far as the DOM is concerned (`activeElement`\n * moves; only the event was stopped), and the tooltip interaction model treats\n * a trigger that is `activeElement` as keyboard-held: while a tooltip on it is\n * open, pointer movement away only *requests* a close and the request is\n * refused for as long as the pointer keeps moving. Left alone, a later real\n * hover on that control would open the tip and never let it leave, the\n * residual \"stuck\" case. The gate therefore treats a suppressed focus as a\n * phantom and releases it: the first pointer activity after the focus (a\n * pointer session taking over from a script) blurs the element. Keyboard\n * sessions never release it and never need to, keyboard focus moves by Tab,\n * which is never suppressed, so nothing pins; and the release only runs for\n * elements still holding `activeElement`, so a Tab already taken from the\n * phantom is left alone.\n *\n * Focus that arrives while a pointer button is held belongs to a pointer\n * gesture, not to a tooltip: the pixel resize handle focuses its handle when a\n * drag starts and cancels the drag on blur, so a gesture focus must pass\n * through untouched (no suppression, no phantom, no release). A gesture's own\n * click behaviour already keeps tooltips closed, and the autofocus cases this\n * gate exists for all happen with the pointer up.\n *\n * A tab in a roving-focus tab list is the one button-like element whose\n * programmatic focus is the widget working as designed: a dialog's focus scope\n * and the tab list both put focus on the selected tab, and the tab list scrolls\n * it into view on every focus-in. Releasing that focus would hand it straight\n * back (the scope restores it), so every pointer move would re-suppress,\n * re-release and re-scroll the rail to the top, and a tab below the fold of a\n * large-font or short-window rail could not be clicked. A focus on `role=\"tab\"`\n * is therefore still hidden from the tooltip, so no tooltip opens from it, but\n * it is not tracked as a phantom and the pointer never blurs it.\n *\n * Independently of the gesture window, resize grips render as\n * `<button role=\"separator\">` and carry no tooltip, so they are excluded from\n * the button-like scope entirely (see `isButtonLikeFocusTarget`).\n *\n * The gate is installed by the `Tooltip` root for the lifetime of the first\n * tooltip in a document and removed when the last one unmounts, so every\n * consumer of the primitive, including composites that build their own\n * tooltips, inherits the contract without wiring of its own.\n */\n\n/** How long after a Tab key press a resulting focus event still counts as\n * keyboard navigation. The native focus move follows the key press\n * immediately; the window only has to bridge that dispatch, not a later\n * script-driven focus. */\nexport const TOOLTIP_FOCUS_INTENT_WINDOW_MS = 250\n\n/** Elements a tooltip-bearing control is rendered as. Everything else keeps\n * its native focus behaviour, and so do resize grips (see below). */\nconst BUTTON_LIKE_SELECTOR = 'button, a[href], [role=\"button\"], [role=\"link\"]'\n\n/**\n * Whether a focus event landing on this target could open a tooltip on a\n * button-like control. Accepts any Element (an SVG glyph inside a control,\n * for instance) and duck-types so the decision is testable without a DOM.\n */\nfunction buttonLikeControl(target: unknown): Element | null {\n const element = target as Element | null | undefined\n if (typeof element?.closest !== 'function') return null\n return element.closest(BUTTON_LIKE_SELECTOR)\n}\n\n/**\n * Whether the focus target is a tab of a roving-focus tab list. Its focus is\n * hidden from tooltips like any other programmatic focus, but it is never\n * released by the pointer (see the header).\n */\nexport function isTabFocusTarget(target: unknown): boolean {\n return buttonLikeControl(target)?.getAttribute?.('role') === 'tab'\n}\n\nexport function isButtonLikeFocusTarget(target: unknown): boolean {\n const control = buttonLikeControl(target)\n if (control === null) return false\n // Resize grips render as `<button role=\"separator\">` and focus themselves\n // when a pointer drag starts. They carry no tooltip, and the blur that\n // releases a suppressed focus would end the drag on its first move.\n return control.getAttribute?.('role') !== 'separator'\n}\n\n/**\n * The suppression decision: a button-like focus target hides a tooltip only\n * when no keyboard navigation is in flight. Anything else, a Tab-driven\n * focus, or focus on a non-button element, passes through untouched.\n */\nexport function shouldSuppressTooltipFocus(input: {\n isButtonLike: boolean\n keyboardIntentActive: boolean\n}): boolean {\n return input.isButtonLike && !input.keyboardIntentActive\n}\n\nconst installedDocuments = new WeakSet<Document>()\n\n/**\n * Install the gate on a document. Idempotent per document; returns a disposer\n * that removes the listeners (used by tests and by the tooltip root, which\n * releases the gate when its document's last tooltip unmounts).\n */\nexport function installTooltipFocusGate(doc: Document = document): () => void {\n if (installedDocuments.has(doc)) return () => {}\n\n let lastTabKeyDownAt = Number.NEGATIVE_INFINITY\n /** True between a pointerdown and its pointerup/pointercancel: focus events\n * inside that window belong to a pointer gesture (drag bootstrap, press)\n * and must reach their element untouched. */\n let pointerGestureActive = false\n /** The element holding a suppressed programmatic focus, if it still holds\n * `activeElement`. One slot: a newer suppressed focus replaces an older\n * one, and anything that already lost focus needs no release. */\n let phantomFocusTarget: Element | undefined\n /** The phantom-release `pointermove` listener exists only while a phantom\n * does: a gate that parks a permanent pointer listener on every document\n * with a tooltip in it costs every page a standing listener for a state\n * that is live for a few hundred milliseconds at most. */\n let phantomReleaseAttached = false\n const onKeyDown = (event: KeyboardEvent) => {\n // A later activation key starts a different interaction: its overlay\n // autofocus must not inherit the preceding Tab navigation window.\n lastTabKeyDownAt = event.key === 'Tab' ? Date.now() : Number.NEGATIVE_INFINITY\n }\n const onPointerDown = () => {\n lastTabKeyDownAt = Number.NEGATIVE_INFINITY\n pointerGestureActive = true\n releasePhantomFocus()\n }\n const onPointerGestureEnd = () => {\n pointerGestureActive = false\n }\n const dropPhantom = () => {\n phantomFocusTarget = undefined\n if (phantomReleaseAttached) {\n phantomReleaseAttached = false\n doc.removeEventListener('pointermove', onPointerMove, true)\n }\n }\n const onFocus = (event: FocusEvent) => {\n // Focus that moves on retires the phantom: there is nothing left to\n // release, so the parked pointer listener goes with it.\n if (phantomFocusTarget && event.target !== phantomFocusTarget) dropPhantom()\n if (pointerGestureActive) return\n if (!isButtonLikeFocusTarget(event.target)) return\n const keyboardIntentActive = Date.now() - lastTabKeyDownAt <= TOOLTIP_FOCUS_INTENT_WINDOW_MS\n if (!shouldSuppressTooltipFocus({ isButtonLike: true, keyboardIntentActive })) return\n event.stopPropagation()\n if (isTabFocusTarget(event.target)) return\n phantomFocusTarget = event.target as Element\n if (!phantomReleaseAttached) {\n phantomReleaseAttached = true\n doc.addEventListener('pointermove', onPointerMove, true)\n }\n }\n const releasePhantomFocus = () => {\n if (pointerGestureActive) return\n if (!phantomFocusTarget) return\n const phantom = phantomFocusTarget\n dropPhantom()\n if ((doc as Document).activeElement !== phantom) return\n if (typeof (phantom as HTMLElement).blur === 'function') (phantom as HTMLElement).blur()\n }\n const onPointerMove = () => releasePhantomFocus()\n\n doc.addEventListener('keydown', onKeyDown, true)\n doc.addEventListener('focus', onFocus, true)\n doc.addEventListener('pointerdown', onPointerDown, true)\n doc.addEventListener('pointerup', onPointerGestureEnd, true)\n doc.addEventListener('pointercancel', onPointerGestureEnd, true)\n installedDocuments.add(doc)\n return () => {\n doc.removeEventListener('keydown', onKeyDown, true)\n doc.removeEventListener('focus', onFocus, true)\n doc.removeEventListener('pointerdown', onPointerDown, true)\n doc.removeEventListener('pointerup', onPointerGestureEnd, true)\n doc.removeEventListener('pointercancel', onPointerGestureEnd, true)\n doc.removeEventListener('pointermove', onPointerMove, true)\n installedDocuments.delete(doc)\n }\n}\n\n/**\n * Reference-counted install for the tooltip root: the first tooltip in a\n * document acquires the gate, the last one to unmount releases it. The document\n * parameter exists so tests can drive the counter without a global DOM; the\n * production call site guards on the server render before calling.\n */\nconst gateHolders = new WeakMap<Document, number>()\nconst gateDisposers = new WeakMap<Document, () => void>()\n\nexport function acquireTooltipFocusGate(targetDoc?: Document): void {\n if (typeof document === 'undefined' && !targetDoc) return\n const doc = targetDoc ?? document\n const holders = gateHolders.get(doc) ?? 0\n gateHolders.set(doc, holders + 1)\n if (holders === 0) gateDisposers.set(doc, installTooltipFocusGate(doc))\n}\n\nexport function releaseTooltipFocusGate(targetDoc?: Document): void {\n if (typeof document === 'undefined' && !targetDoc) return\n const doc = targetDoc ?? document\n const holders = gateHolders.get(doc) ?? 0\n if (holders === 0) return\n if (holders === 1) {\n gateHolders.delete(doc)\n gateDisposers.get(doc)?.()\n gateDisposers.delete(doc)\n return\n }\n gateHolders.set(doc, holders - 1)\n}\n"],"mappings":";;;;;AAiFA,IAAa,iCAAiC;;;AAI9C,IAAM,uBAAuB;;;;;;AAO7B,SAAS,kBAAkB,QAAiC;CAC1D,MAAM,UAAU;CAChB,IAAI,OAAO,SAAS,YAAY,YAAY,OAAO;CACnD,OAAO,QAAQ,QAAQ,oBAAoB;AAC7C;;;;;;AAOA,SAAgB,iBAAiB,QAA0B;CACzD,OAAO,kBAAkB,MAAM,CAAC,EAAE,eAAe,MAAM,MAAM;AAC/D;AAEA,SAAgB,wBAAwB,QAA0B;CAChE,MAAM,UAAU,kBAAkB,MAAM;CACxC,IAAI,YAAY,MAAM,OAAO;CAI7B,OAAO,QAAQ,eAAe,MAAM,MAAM;AAC5C;;;;;;AAOA,SAAgB,2BAA2B,OAG/B;CACV,OAAO,MAAM,gBAAgB,CAAC,MAAM;AACtC;AAEA,IAAM,qCAAqB,IAAI,QAAkB;;;;;;AAOjD,SAAgB,wBAAwB,MAAgB,UAAsB;CAC5E,IAAI,mBAAmB,IAAI,GAAG,GAAG,aAAa,CAAC;CAE/C,IAAI,mBAAmB,OAAO;;;;CAI9B,IAAI,uBAAuB;;;;CAI3B,IAAI;;;;;CAKJ,IAAI,yBAAyB;CAC7B,MAAM,aAAa,UAAyB;EAG1C,mBAAmB,MAAM,QAAQ,QAAQ,KAAK,IAAI,IAAI,OAAO;CAC/D;CACA,MAAM,sBAAsB;EAC1B,mBAAmB,OAAO;EAC1B,uBAAuB;EACvB,oBAAoB;CACtB;CACA,MAA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AClE,IAAI,oBAAoB,eAAe,eAAe,IAAI;EAC1D,mBAAmB,OAAO,GAAG;CAC/B;AACF;;;;;;;AAQA,IAAM,8BAAc,IAAI,QAA0B;AAClD,IAAM,gCAAgB,IAAI,QAA8B;AAExD,SAAgB,wBAAwB,WAA4B;CAClE,IAAI,OAAO,aAAa,eAAe,CAAC,WAAW;CACnD,MAAM,MAAM,aAAa;CACzB,MAAM,UAAU,YAAY,IAAI,GAAG,KAAK;CACxC,YAAY,IAAI,KAAK,UAAU,CAAC;CAChC,IAAI,YAAY,GAAG,cAAc,IAAI,KAAK,wBAAwB,GAAG,CAAC;AACxE;AAEA,SAAgB,wBAAwB,WAA4B;CAClE,IAAI,OAAO,aAAa,eAAe,CAAC,WAAW;CACnD,MAAM,MAAM,aAAa;CACzB,MAAM,UAAU,YAAY,IAAI,GAAG,KAAK;CACxC,IAAI,YAAY,GAAG;CACnB,IAAI,YAAY,GAAG;EACjB,YAAY,OAAO,GAAG;EACtB,cAAc,IAAI,GAAG,CAAC,GAAG;EACzB,cAAc,OAAO,GAAG;EACxB;CACF;CACA,YAAY,IAAI,KAAK,UAAU,CAAC;AAClC"}
|
|
1
|
+
{"version":3,"file":"tooltip-focus-gate.js","names":[],"sources":["../../src/lib/tooltip-focus-gate.ts"],"sourcesContent":["/*\n * Tooltips open on active hover over the control, or on keyboard focus that\n * follows a Tab key press. They must never open from programmatic focus: the\n * autofocus a dialog or sheet gives its first control, and the focus a closing\n * overlay restores to its opener, both land on buttons that carry tooltips,\n * and the tooltip primitive opens immediately on focus and pins the tooltip\n * until blur. That is the \"tooltip auto-displays when a sheet opens\" report,\n * and the pinned tooltip is the \"tooltip gets stuck and never disappears\"\n * report: the pointer is nowhere near the control, so no pointer leave ever\n * closes it.\n *\n * The trigger listens for the native `focus` event on the trigger element, and\n * composites build tooltips internally (ActionButton renders one from its\n * `tooltip` prop), so the single interception point that covers every tooltip\n * is a capture-phase `focus` listener on `document`. When focus lands without a\n * keyboard-intent signal on a button-like element, the event is stopped before\n * any element listener runs: the tooltip never learns about the focus. Hover\n * opens are untouched (they ride pointer events), and the separate `focusin`\n * event still flows, so dialog focus-cycle tracking, `:focus-visible` styling,\n * and focus restoration all keep working. Focus still lands, only the event\n * is stopped, so `document.activeElement` and scripted `.blur()` behave.\n *\n * The keyboard-intent signal is a Tab key press immediately before the focus,\n * the only key that moves focus natively. Enter and Space open overlays too,\n * so any \"recent key\" window wide enough for those would let an overlay's own\n * autofocus back through; Tab-only keeps autofocus suppressed no matter how\n * the overlay was opened, while normal Tab navigation still announces each\n * control's tooltip (hover descriptions stay reachable from the keyboard).\n *\n * Suppression is scoped to button-like targets (`button`, `a[href]`,\n * `[role=\"button\"]`, `[role=\"link\"]`). Composite widgets that move focus\n * programmatically, menu items, listbox options, grid cells, are other\n * element types whose roving-focus highlighting must keep receiving focus\n * events, and text controls keep their own focus behaviour (select on focus,\n * caret placement).\n *\n * Suppressed focus is real as far as the DOM is concerned (`activeElement`\n * moves; only the event was stopped), and the tooltip interaction model treats\n * a trigger that is `activeElement` as keyboard-held: while a tooltip on it is\n * open, pointer movement away only *requests* a close and the request is\n * refused for as long as the pointer keeps moving. Left alone, a later real\n * hover on that control would open the tip and never let it leave, the\n * residual \"stuck\" case. The gate therefore treats a suppressed focus as a\n * phantom and releases it: the first pointer activity after the focus (a\n * pointer session taking over from a script) blurs the element. Keyboard\n * sessions never release it and never need to, keyboard focus moves by Tab,\n * which is never suppressed, so nothing pins; and the release only runs for\n * elements still holding `activeElement`, so a Tab already taken from the\n * phantom is left alone.\n *\n * Only a pointer that actually travels releases the phantom. WebKit (Safari,\n * WKWebView) dispatches a `pointermove` at the unchanged position whenever a\n * scroll moves content under a resting mouse, and a dialog's restored focus or\n * an autofocus usually scrolls its control into view, so releasing on any\n * move blurred focus no one had touched. That move reports no travel\n * (`movementX` and `movementY` are both 0); a move that reports no movement\n * fields at all still counts as travel (see `pointerMoveTravelled`).\n *\n * Focus that arrives while a pointer button is held belongs to a pointer\n * gesture, not to a tooltip: the pixel resize handle focuses its handle when a\n * drag starts and cancels the drag on blur, so a gesture focus must pass\n * through untouched (no suppression, no phantom, no release). A gesture's own\n * click behaviour already keeps tooltips closed, and the autofocus cases this\n * gate exists for all happen with the pointer up.\n *\n * A tab in a roving-focus tab list is the one button-like element whose\n * programmatic focus is the widget working as designed: a dialog's focus scope\n * and the tab list both put focus on the selected tab, and the tab list scrolls\n * it into view on every focus-in. Releasing that focus would hand it straight\n * back (the scope restores it), so every pointer move would re-suppress,\n * re-release and re-scroll the rail to the top, and a tab below the fold of a\n * large-font or short-window rail could not be clicked. A focus on `role=\"tab\"`\n * is therefore still hidden from the tooltip, so no tooltip opens from it, but\n * it is not tracked as a phantom and the pointer never blurs it.\n *\n * Independently of the gesture window, resize grips render as\n * `<button role=\"separator\">` and carry no tooltip, so they are excluded from\n * the button-like scope entirely (see `isButtonLikeFocusTarget`).\n *\n * The gate is installed by the `Tooltip` root for the lifetime of the first\n * tooltip in a document and removed when the last one unmounts, so every\n * consumer of the primitive, including composites that build their own\n * tooltips, inherits the contract without wiring of its own.\n */\n\n/** How long after a Tab key press a resulting focus event still counts as\n * keyboard navigation. The native focus move follows the key press\n * immediately; the window only has to bridge that dispatch, not a later\n * script-driven focus. */\nexport const TOOLTIP_FOCUS_INTENT_WINDOW_MS = 250\n\n/** Elements a tooltip-bearing control is rendered as. Everything else keeps\n * its native focus behaviour, and so do resize grips (see below). */\nconst BUTTON_LIKE_SELECTOR = 'button, a[href], [role=\"button\"], [role=\"link\"]'\n\n/**\n * Whether a focus event landing on this target could open a tooltip on a\n * button-like control. Accepts any Element (an SVG glyph inside a control,\n * for instance) and duck-types so the decision is testable without a DOM.\n */\nfunction buttonLikeControl(target: unknown): Element | null {\n const element = target as Element | null | undefined\n if (typeof element?.closest !== 'function') return null\n return element.closest(BUTTON_LIKE_SELECTOR)\n}\n\n/**\n * Whether the focus target is a tab of a roving-focus tab list. Its focus is\n * hidden from tooltips like any other programmatic focus, but it is never\n * released by the pointer (see the header).\n */\nexport function isTabFocusTarget(target: unknown): boolean {\n return buttonLikeControl(target)?.getAttribute?.('role') === 'tab'\n}\n\nexport function isButtonLikeFocusTarget(target: unknown): boolean {\n const control = buttonLikeControl(target)\n if (control === null) return false\n // Resize grips render as `<button role=\"separator\">` and focus themselves\n // when a pointer drag starts. They carry no tooltip, and the blur that\n // releases a suppressed focus would end the drag on its first move.\n return control.getAttribute?.('role') !== 'separator'\n}\n\n/**\n * The suppression decision: a button-like focus target hides a tooltip only\n * when no keyboard navigation is in flight. Anything else, a Tab-driven\n * focus, or focus on a non-button element, passes through untouched.\n */\nexport function shouldSuppressTooltipFocus(input: {\n isButtonLike: boolean\n keyboardIntentActive: boolean\n}): boolean {\n return input.isButtonLike && !input.keyboardIntentActive\n}\n\n/**\n * Whether a `pointermove` comes from a pointer that moved, rather than one a\n * browser dispatched at the same position because content scrolled under it.\n * An event without movement fields counts as travel.\n */\nexport function pointerMoveTravelled(event: { movementX?: number; movementY?: number }): boolean {\n const { movementX, movementY } = event\n if (typeof movementX !== 'number' || typeof movementY !== 'number') return true\n return movementX !== 0 || movementY !== 0\n}\n\nconst installedDocuments = new WeakSet<Document>()\n\n/**\n * Install the gate on a document. Idempotent per document; returns a disposer\n * that removes the listeners (used by tests and by the tooltip root, which\n * releases the gate when its document's last tooltip unmounts).\n */\nexport function installTooltipFocusGate(doc: Document = document): () => void {\n if (installedDocuments.has(doc)) return () => {}\n\n let lastTabKeyDownAt = Number.NEGATIVE_INFINITY\n /** True between a pointerdown and its pointerup/pointercancel: focus events\n * inside that window belong to a pointer gesture (drag bootstrap, press)\n * and must reach their element untouched. */\n let pointerGestureActive = false\n /** The element holding a suppressed programmatic focus, if it still holds\n * `activeElement`. One slot: a newer suppressed focus replaces an older\n * one, and anything that already lost focus needs no release. */\n let phantomFocusTarget: Element | undefined\n /** The phantom-release `pointermove` listener exists only while a phantom\n * does: a gate that parks a permanent pointer listener on every document\n * with a tooltip in it costs every page a standing listener for a state\n * that is live for a few hundred milliseconds at most. */\n let phantomReleaseAttached = false\n const onKeyDown = (event: KeyboardEvent) => {\n // A later activation key starts a different interaction: its overlay\n // autofocus must not inherit the preceding Tab navigation window.\n lastTabKeyDownAt = event.key === 'Tab' ? Date.now() : Number.NEGATIVE_INFINITY\n }\n const onPointerDown = () => {\n lastTabKeyDownAt = Number.NEGATIVE_INFINITY\n pointerGestureActive = true\n releasePhantomFocus()\n }\n const onPointerGestureEnd = () => {\n pointerGestureActive = false\n }\n const dropPhantom = () => {\n phantomFocusTarget = undefined\n if (phantomReleaseAttached) {\n phantomReleaseAttached = false\n doc.removeEventListener('pointermove', onPointerMove, true)\n }\n }\n const onFocus = (event: FocusEvent) => {\n // Focus that moves on retires the phantom: there is nothing left to\n // release, so the parked pointer listener goes with it.\n if (phantomFocusTarget && event.target !== phantomFocusTarget) dropPhantom()\n if (pointerGestureActive) return\n if (!isButtonLikeFocusTarget(event.target)) return\n const keyboardIntentActive = Date.now() - lastTabKeyDownAt <= TOOLTIP_FOCUS_INTENT_WINDOW_MS\n if (!shouldSuppressTooltipFocus({ isButtonLike: true, keyboardIntentActive })) return\n event.stopPropagation()\n if (isTabFocusTarget(event.target)) return\n phantomFocusTarget = event.target as Element\n if (!phantomReleaseAttached) {\n phantomReleaseAttached = true\n doc.addEventListener('pointermove', onPointerMove, true)\n }\n }\n const releasePhantomFocus = () => {\n if (pointerGestureActive) return\n if (!phantomFocusTarget) return\n const phantom = phantomFocusTarget\n dropPhantom()\n if ((doc as Document).activeElement !== phantom) return\n if (typeof (phantom as HTMLElement).blur === 'function') (phantom as HTMLElement).blur()\n }\n const onPointerMove = (event: PointerEvent) => {\n if (pointerMoveTravelled(event)) releasePhantomFocus()\n }\n\n doc.addEventListener('keydown', onKeyDown, true)\n doc.addEventListener('focus', onFocus, true)\n doc.addEventListener('pointerdown', onPointerDown, true)\n doc.addEventListener('pointerup', onPointerGestureEnd, true)\n doc.addEventListener('pointercancel', onPointerGestureEnd, true)\n installedDocuments.add(doc)\n return () => {\n doc.removeEventListener('keydown', onKeyDown, true)\n doc.removeEventListener('focus', onFocus, true)\n doc.removeEventListener('pointerdown', onPointerDown, true)\n doc.removeEventListener('pointerup', onPointerGestureEnd, true)\n doc.removeEventListener('pointercancel', onPointerGestureEnd, true)\n doc.removeEventListener('pointermove', onPointerMove, true)\n installedDocuments.delete(doc)\n }\n}\n\n/**\n * Reference-counted install for the tooltip root: the first tooltip in a\n * document acquires the gate, the last one to unmount releases it. The document\n * parameter exists so tests can drive the counter without a global DOM; the\n * production call site guards on the server render before calling.\n */\nconst gateHolders = new WeakMap<Document, number>()\nconst gateDisposers = new WeakMap<Document, () => void>()\n\nexport function acquireTooltipFocusGate(targetDoc?: Document): void {\n if (typeof document === 'undefined' && !targetDoc) return\n const doc = targetDoc ?? document\n const holders = gateHolders.get(doc) ?? 0\n gateHolders.set(doc, holders + 1)\n if (holders === 0) gateDisposers.set(doc, installTooltipFocusGate(doc))\n}\n\nexport function releaseTooltipFocusGate(targetDoc?: Document): void {\n if (typeof document === 'undefined' && !targetDoc) return\n const doc = targetDoc ?? document\n const holders = gateHolders.get(doc) ?? 0\n if (holders === 0) return\n if (holders === 1) {\n gateHolders.delete(doc)\n gateDisposers.get(doc)?.()\n gateDisposers.delete(doc)\n return\n }\n gateHolders.set(doc, holders - 1)\n}\n"],"mappings":";;;;;AAyFA,IAAa,iCAAiC;;;AAI9C,IAAM,uBAAuB;;;;;;AAO7B,SAAS,kBAAkB,QAAiC;CAC1D,MAAM,UAAU;CAChB,IAAI,OAAO,SAAS,YAAY,YAAY,OAAO;CACnD,OAAO,QAAQ,QAAQ,oBAAoB;AAC7C;;;;;;AAOA,SAAgB,iBAAiB,QAA0B;CACzD,OAAO,kBAAkB,MAAM,CAAC,EAAE,eAAe,MAAM,MAAM;AAC/D;AAEA,SAAgB,wBAAwB,QAA0B;CAChE,MAAM,UAAU,kBAAkB,MAAM;CACxC,IAAI,YAAY,MAAM,OAAO;CAI7B,OAAO,QAAQ,eAA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KAAK;CACxC,IAAI,YAAY,GAAG;CACnB,IAAI,YAAY,GAAG;EACjB,YAAY,OAAO,GAAG;EACtB,cAAc,IAAI,GAAG,CAAC,GAAG;EACzB,cAAc,OAAO,GAAG;EACxB;CACF;CACA,YAAY,IAAI,KAAK,UAAU,CAAC;AAClC"}
|
|
@@ -48,6 +48,14 @@
|
|
|
48
48
|
* elements still holding `activeElement`, so a Tab already taken from the
|
|
49
49
|
* phantom is left alone.
|
|
50
50
|
*
|
|
51
|
+
* Only a pointer that actually travels releases the phantom. WebKit (Safari,
|
|
52
|
+
* WKWebView) dispatches a `pointermove` at the unchanged position whenever a
|
|
53
|
+
* scroll moves content under a resting mouse, and a dialog's restored focus or
|
|
54
|
+
* an autofocus usually scrolls its control into view, so releasing on any
|
|
55
|
+
* move blurred focus no one had touched. That move reports no travel
|
|
56
|
+
* (`movementX` and `movementY` are both 0); a move that reports no movement
|
|
57
|
+
* fields at all still counts as travel (see `pointerMoveTravelled`).
|
|
58
|
+
*
|
|
51
59
|
* Focus that arrives while a pointer button is held belongs to a pointer
|
|
52
60
|
* gesture, not to a tooltip: the pixel resize handle focuses its handle when a
|
|
53
61
|
* drag starts and cancels the drag on blur, so a gesture focus must pass
|
|
@@ -126,6 +134,17 @@ export function shouldSuppressTooltipFocus(input: {
|
|
|
126
134
|
return input.isButtonLike && !input.keyboardIntentActive
|
|
127
135
|
}
|
|
128
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Whether a `pointermove` comes from a pointer that moved, rather than one a
|
|
139
|
+
* browser dispatched at the same position because content scrolled under it.
|
|
140
|
+
* An event without movement fields counts as travel.
|
|
141
|
+
*/
|
|
142
|
+
export function pointerMoveTravelled(event: { movementX?: number; movementY?: number }): boolean {
|
|
143
|
+
const { movementX, movementY } = event
|
|
144
|
+
if (typeof movementX !== 'number' || typeof movementY !== 'number') return true
|
|
145
|
+
return movementX !== 0 || movementY !== 0
|
|
146
|
+
}
|
|
147
|
+
|
|
129
148
|
const installedDocuments = new WeakSet<Document>()
|
|
130
149
|
|
|
131
150
|
/**
|
|
@@ -194,7 +213,9 @@ export function installTooltipFocusGate(doc: Document = document): () => void {
|
|
|
194
213
|
if ((doc as Document).activeElement !== phantom) return
|
|
195
214
|
if (typeof (phantom as HTMLElement).blur === 'function') (phantom as HTMLElement).blur()
|
|
196
215
|
}
|
|
197
|
-
const onPointerMove = () =>
|
|
216
|
+
const onPointerMove = (event: PointerEvent) => {
|
|
217
|
+
if (pointerMoveTravelled(event)) releasePhantomFocus()
|
|
218
|
+
}
|
|
198
219
|
|
|
199
220
|
doc.addEventListener('keydown', onKeyDown, true)
|
|
200
221
|
doc.addEventListener('focus', onFocus, true)
|
package/package.json
CHANGED
|
@@ -48,6 +48,14 @@
|
|
|
48
48
|
* elements still holding `activeElement`, so a Tab already taken from the
|
|
49
49
|
* phantom is left alone.
|
|
50
50
|
*
|
|
51
|
+
* Only a pointer that actually travels releases the phantom. WebKit (Safari,
|
|
52
|
+
* WKWebView) dispatches a `pointermove` at the unchanged position whenever a
|
|
53
|
+
* scroll moves content under a resting mouse, and a dialog's restored focus or
|
|
54
|
+
* an autofocus usually scrolls its control into view, so releasing on any
|
|
55
|
+
* move blurred focus no one had touched. That move reports no travel
|
|
56
|
+
* (`movementX` and `movementY` are both 0); a move that reports no movement
|
|
57
|
+
* fields at all still counts as travel (see `pointerMoveTravelled`).
|
|
58
|
+
*
|
|
51
59
|
* Focus that arrives while a pointer button is held belongs to a pointer
|
|
52
60
|
* gesture, not to a tooltip: the pixel resize handle focuses its handle when a
|
|
53
61
|
* drag starts and cancels the drag on blur, so a gesture focus must pass
|
|
@@ -126,6 +134,17 @@ export function shouldSuppressTooltipFocus(input: {
|
|
|
126
134
|
return input.isButtonLike && !input.keyboardIntentActive
|
|
127
135
|
}
|
|
128
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Whether a `pointermove` comes from a pointer that moved, rather than one a
|
|
139
|
+
* browser dispatched at the same position because content scrolled under it.
|
|
140
|
+
* An event without movement fields counts as travel.
|
|
141
|
+
*/
|
|
142
|
+
export function pointerMoveTravelled(event: { movementX?: number; movementY?: number }): boolean {
|
|
143
|
+
const { movementX, movementY } = event
|
|
144
|
+
if (typeof movementX !== 'number' || typeof movementY !== 'number') return true
|
|
145
|
+
return movementX !== 0 || movementY !== 0
|
|
146
|
+
}
|
|
147
|
+
|
|
129
148
|
const installedDocuments = new WeakSet<Document>()
|
|
130
149
|
|
|
131
150
|
/**
|
|
@@ -194,7 +213,9 @@ export function installTooltipFocusGate(doc: Document = document): () => void {
|
|
|
194
213
|
if ((doc as Document).activeElement !== phantom) return
|
|
195
214
|
if (typeof (phantom as HTMLElement).blur === 'function') (phantom as HTMLElement).blur()
|
|
196
215
|
}
|
|
197
|
-
const onPointerMove = () =>
|
|
216
|
+
const onPointerMove = (event: PointerEvent) => {
|
|
217
|
+
if (pointerMoveTravelled(event)) releasePhantomFocus()
|
|
218
|
+
}
|
|
198
219
|
|
|
199
220
|
doc.addEventListener('keydown', onKeyDown, true)
|
|
200
221
|
doc.addEventListener('focus', onFocus, true)
|