@zag-js/focus-visible 2.0.0-next.3 → 2.0.0-next.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.
- package/dist/index.d.mts +38 -37
- package/dist/index.d.ts +38 -37
- package/dist/index.js +236 -224
- package/dist/index.mjs +225 -202
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,44 +1,45 @@
|
|
|
1
|
-
type Modality = "keyboard" | "pointer" | "virtual";
|
|
1
|
+
export type Modality = "keyboard" | "pointer" | "virtual";
|
|
2
2
|
type RootNode = Document | ShadowRoot | Node;
|
|
3
3
|
interface GlobalListenerData {
|
|
4
|
-
|
|
4
|
+
/** Unset when the prototype's `focus` could not be read */
|
|
5
|
+
focus: VoidFunction | undefined;
|
|
6
|
+
/** The unreadable accessor to restore */
|
|
7
|
+
descriptor?: PropertyDescriptor | undefined;
|
|
5
8
|
}
|
|
6
|
-
declare let listenerMap: Map<Window, GlobalListenerData>;
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
interface InteractionModalityChangeDetails {
|
|
15
|
-
/** The modality of the interaction that caused the focus to be visible. */
|
|
16
|
-
modality: Modality | null;
|
|
9
|
+
export declare let listenerMap: Map<Window, GlobalListenerData>;
|
|
10
|
+
/** Skip the next focus event, so preventFocus() doesn't show focus rings */
|
|
11
|
+
export declare let ignoreFocusEvent: boolean;
|
|
12
|
+
export declare function getInteractionModality(): Modality | null;
|
|
13
|
+
export declare function setInteractionModality(modality: Modality): void;
|
|
14
|
+
export interface InteractionModalityChangeDetails {
|
|
15
|
+
/** The modality of the interaction that caused the focus to be visible. */
|
|
16
|
+
modality: Modality | null;
|
|
17
17
|
}
|
|
18
|
-
interface InteractionModalityProps {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
export interface InteractionModalityProps {
|
|
19
|
+
/** The root element to track focus visibility for. */
|
|
20
|
+
root?: RootNode | undefined;
|
|
21
|
+
/** Callback to be called when the interaction modality changes. */
|
|
22
|
+
onChange: (details: InteractionModalityChangeDetails) => void;
|
|
23
23
|
}
|
|
24
|
-
declare function trackInteractionModality(props: InteractionModalityProps): VoidFunction;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
export declare function trackInteractionModality(props: InteractionModalityProps): VoidFunction;
|
|
25
|
+
/** Whether the latest focus was the browser restoring it as the window regained focus */
|
|
26
|
+
export declare function isFocusRestoredByWindow(): boolean;
|
|
27
|
+
export declare function isFocusVisible(): boolean;
|
|
28
|
+
export interface FocusVisibleChangeDetails {
|
|
29
|
+
/** Whether keyboard focus is visible globally. */
|
|
30
|
+
isFocusVisible: boolean;
|
|
31
|
+
/** The modality of the interaction that caused the focus to be visible. */
|
|
32
|
+
modality: Modality | null;
|
|
31
33
|
}
|
|
32
|
-
interface FocusVisibleProps {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
export interface FocusVisibleProps {
|
|
35
|
+
/** The root element to track focus visibility for. */
|
|
36
|
+
root?: RootNode | undefined;
|
|
37
|
+
/** Whether the element is a text input. */
|
|
38
|
+
isTextInput?: boolean | undefined;
|
|
39
|
+
/** Whether the element will be auto focused. */
|
|
40
|
+
autoFocus?: boolean | undefined;
|
|
41
|
+
/** Callback to be called when the focus visibility changes. */
|
|
42
|
+
onChange?: ((details: FocusVisibleChangeDetails) => void) | undefined;
|
|
41
43
|
}
|
|
42
|
-
declare function trackFocusVisible(props?: FocusVisibleProps): VoidFunction;
|
|
43
|
-
|
|
44
|
-
export { type FocusVisibleChangeDetails, type FocusVisibleProps, type InteractionModalityChangeDetails, type InteractionModalityProps, type Modality, getInteractionModality, ignoreFocusEvent, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
|
|
44
|
+
export declare function trackFocusVisible(props?: FocusVisibleProps): VoidFunction;
|
|
45
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,44 +1,45 @@
|
|
|
1
|
-
type Modality = "keyboard" | "pointer" | "virtual";
|
|
1
|
+
export type Modality = "keyboard" | "pointer" | "virtual";
|
|
2
2
|
type RootNode = Document | ShadowRoot | Node;
|
|
3
3
|
interface GlobalListenerData {
|
|
4
|
-
|
|
4
|
+
/** Unset when the prototype's `focus` could not be read */
|
|
5
|
+
focus: VoidFunction | undefined;
|
|
6
|
+
/** The unreadable accessor to restore */
|
|
7
|
+
descriptor?: PropertyDescriptor | undefined;
|
|
5
8
|
}
|
|
6
|
-
declare let listenerMap: Map<Window, GlobalListenerData>;
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
interface InteractionModalityChangeDetails {
|
|
15
|
-
/** The modality of the interaction that caused the focus to be visible. */
|
|
16
|
-
modality: Modality | null;
|
|
9
|
+
export declare let listenerMap: Map<Window, GlobalListenerData>;
|
|
10
|
+
/** Skip the next focus event, so preventFocus() doesn't show focus rings */
|
|
11
|
+
export declare let ignoreFocusEvent: boolean;
|
|
12
|
+
export declare function getInteractionModality(): Modality | null;
|
|
13
|
+
export declare function setInteractionModality(modality: Modality): void;
|
|
14
|
+
export interface InteractionModalityChangeDetails {
|
|
15
|
+
/** The modality of the interaction that caused the focus to be visible. */
|
|
16
|
+
modality: Modality | null;
|
|
17
17
|
}
|
|
18
|
-
interface InteractionModalityProps {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
export interface InteractionModalityProps {
|
|
19
|
+
/** The root element to track focus visibility for. */
|
|
20
|
+
root?: RootNode | undefined;
|
|
21
|
+
/** Callback to be called when the interaction modality changes. */
|
|
22
|
+
onChange: (details: InteractionModalityChangeDetails) => void;
|
|
23
23
|
}
|
|
24
|
-
declare function trackInteractionModality(props: InteractionModalityProps): VoidFunction;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
export declare function trackInteractionModality(props: InteractionModalityProps): VoidFunction;
|
|
25
|
+
/** Whether the latest focus was the browser restoring it as the window regained focus */
|
|
26
|
+
export declare function isFocusRestoredByWindow(): boolean;
|
|
27
|
+
export declare function isFocusVisible(): boolean;
|
|
28
|
+
export interface FocusVisibleChangeDetails {
|
|
29
|
+
/** Whether keyboard focus is visible globally. */
|
|
30
|
+
isFocusVisible: boolean;
|
|
31
|
+
/** The modality of the interaction that caused the focus to be visible. */
|
|
32
|
+
modality: Modality | null;
|
|
31
33
|
}
|
|
32
|
-
interface FocusVisibleProps {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
export interface FocusVisibleProps {
|
|
35
|
+
/** The root element to track focus visibility for. */
|
|
36
|
+
root?: RootNode | undefined;
|
|
37
|
+
/** Whether the element is a text input. */
|
|
38
|
+
isTextInput?: boolean | undefined;
|
|
39
|
+
/** Whether the element will be auto focused. */
|
|
40
|
+
autoFocus?: boolean | undefined;
|
|
41
|
+
/** Callback to be called when the focus visibility changes. */
|
|
42
|
+
onChange?: ((details: FocusVisibleChangeDetails) => void) | undefined;
|
|
41
43
|
}
|
|
42
|
-
declare function trackFocusVisible(props?: FocusVisibleProps): VoidFunction;
|
|
43
|
-
|
|
44
|
-
export { type FocusVisibleChangeDetails, type FocusVisibleProps, type InteractionModalityChangeDetails, type InteractionModalityProps, type Modality, getInteractionModality, ignoreFocusEvent, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
|
|
44
|
+
export declare function trackFocusVisible(props?: FocusVisibleProps): VoidFunction;
|
|
45
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,252 +1,264 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
getInteractionModality: () => getInteractionModality,
|
|
24
|
-
ignoreFocusEvent: () => ignoreFocusEvent,
|
|
25
|
-
isFocusVisible: () => isFocusVisible,
|
|
26
|
-
listenerMap: () => listenerMap,
|
|
27
|
-
setInteractionModality: () => setInteractionModality,
|
|
28
|
-
trackFocusVisible: () => trackFocusVisible,
|
|
29
|
-
trackInteractionModality: () => trackInteractionModality
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
30
4
|
});
|
|
31
|
-
|
|
32
|
-
|
|
5
|
+
let _zag_js_dom_query = require("@zag-js/dom-query");
|
|
6
|
+
// src/index.ts
|
|
7
|
+
/**
|
|
8
|
+
* Credit: Huge props to the team at Adobe for inspiring this implementation.
|
|
9
|
+
* https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/interactions/src/useFocusVisible.ts
|
|
10
|
+
*/
|
|
33
11
|
function isValidKey(e) {
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
12
|
+
return !(e.metaKey || !(0, _zag_js_dom_query.isMac)() && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
|
|
13
|
+
}
|
|
14
|
+
const nonTextInputTypes = /* @__PURE__ */ new Set([
|
|
15
|
+
"checkbox",
|
|
16
|
+
"radio",
|
|
17
|
+
"range",
|
|
18
|
+
"color",
|
|
19
|
+
"file",
|
|
20
|
+
"image",
|
|
21
|
+
"button",
|
|
22
|
+
"submit",
|
|
23
|
+
"reset"
|
|
24
|
+
]);
|
|
25
|
+
const interactiveContentSelector = [
|
|
26
|
+
"a[href]",
|
|
27
|
+
"audio[controls]",
|
|
28
|
+
"button",
|
|
29
|
+
"details",
|
|
30
|
+
"embed",
|
|
31
|
+
"iframe",
|
|
32
|
+
"img[controls]",
|
|
33
|
+
"img[usemap]",
|
|
34
|
+
"input",
|
|
35
|
+
"select",
|
|
36
|
+
"textarea",
|
|
37
|
+
"video[controls]"
|
|
50
38
|
].join(",");
|
|
51
39
|
function isKeyboardFocusEvent(isTextInput, modality, e) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
40
|
+
const eventTarget = e ? (0, _zag_js_dom_query.getEventTarget)(e) : null;
|
|
41
|
+
const doc = (0, _zag_js_dom_query.getDocument)(eventTarget);
|
|
42
|
+
const win = (0, _zag_js_dom_query.getWindow)(eventTarget);
|
|
43
|
+
const activeElement = (0, _zag_js_dom_query.getActiveElement)(doc);
|
|
44
|
+
isTextInput = isTextInput || activeElement instanceof win.HTMLInputElement && !nonTextInputTypes.has(activeElement?.type) || activeElement instanceof win.HTMLTextAreaElement || activeElement instanceof win.HTMLElement && activeElement.isContentEditable;
|
|
45
|
+
return !(isTextInput && modality === "keyboard" && e instanceof win.KeyboardEvent && !Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key));
|
|
46
|
+
}
|
|
47
|
+
let currentModality = null;
|
|
48
|
+
let changeHandlers = /* @__PURE__ */ new Set();
|
|
49
|
+
let listenerMap = /* @__PURE__ */ new Map();
|
|
50
|
+
let hasEventBeforeFocus = false;
|
|
51
|
+
let pendingLabelControl = null;
|
|
52
|
+
let hasBlurredWindowRecently = false;
|
|
53
|
+
let focusRestoredByWindow = false;
|
|
54
|
+
let lastPointerPosition = null;
|
|
55
|
+
/** Skip the next focus event, so preventFocus() doesn't show focus rings */
|
|
56
|
+
let ignoreFocusEvent = false;
|
|
57
|
+
const FOCUS_VISIBLE_INPUT_KEYS = {
|
|
58
|
+
Tab: true,
|
|
59
|
+
Escape: true
|
|
70
60
|
};
|
|
71
61
|
function triggerChangeHandlers(modality, e) {
|
|
72
|
-
|
|
73
|
-
handler(modality, e);
|
|
74
|
-
}
|
|
62
|
+
for (let handler of changeHandlers) handler(modality, e);
|
|
75
63
|
}
|
|
76
64
|
function handleKeyboardEvent(e) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
65
|
+
pendingLabelControl = null;
|
|
66
|
+
hasEventBeforeFocus = true;
|
|
67
|
+
if (isValidKey(e)) {
|
|
68
|
+
currentModality = "keyboard";
|
|
69
|
+
triggerChangeHandlers("keyboard", e);
|
|
70
|
+
}
|
|
83
71
|
}
|
|
84
72
|
function handlePointerEvent(e) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
73
|
+
if ((e.type === "pointermove" || e.type === "mousemove") && e.isTrusted && lastPointerPosition?.x === e.clientX && lastPointerPosition?.y === e.clientY) return;
|
|
74
|
+
lastPointerPosition = {
|
|
75
|
+
x: e.clientX,
|
|
76
|
+
y: e.clientY
|
|
77
|
+
};
|
|
78
|
+
currentModality = "pointer";
|
|
79
|
+
if (e.type === "mousedown" || e.type === "pointerdown") {
|
|
80
|
+
pendingLabelControl = null;
|
|
81
|
+
hasEventBeforeFocus = true;
|
|
82
|
+
triggerChangeHandlers("pointer", e);
|
|
83
|
+
}
|
|
94
84
|
}
|
|
95
85
|
function handleClickEvent(e) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
86
|
+
if ((0, _zag_js_dom_query.isVirtualClick)(e)) {
|
|
87
|
+
pendingLabelControl = null;
|
|
88
|
+
hasEventBeforeFocus = true;
|
|
89
|
+
currentModality = "virtual";
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
pendingLabelControl = null;
|
|
93
|
+
const target = (0, _zag_js_dom_query.getEventTarget)(e);
|
|
94
|
+
const label = target?.closest("label");
|
|
95
|
+
const control = label?.control;
|
|
96
|
+
if (!label || !control || control.matches(":disabled")) return;
|
|
97
|
+
const interactive = target?.closest(interactiveContentSelector);
|
|
98
|
+
if (interactive && label.contains(interactive) && interactive !== control) return;
|
|
99
|
+
if (control !== (0, _zag_js_dom_query.getActiveElement)(label.ownerDocument)) pendingLabelControl = control;
|
|
110
100
|
}
|
|
111
101
|
function handleFocusEvent(e) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
pendingLabelControl = null;
|
|
128
|
-
hasBlurredWindowRecently = false;
|
|
102
|
+
const target = (0, _zag_js_dom_query.getEventTarget)(e);
|
|
103
|
+
const isLabelActivationFocus = target === pendingLabelControl;
|
|
104
|
+
if ((0, _zag_js_dom_query.isWindow)(target)) {
|
|
105
|
+
hasBlurredWindowRecently = true;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if ((0, _zag_js_dom_query.isDocument)(target) || !e.isTrusted) return;
|
|
109
|
+
if (!hasEventBeforeFocus && !isLabelActivationFocus && !hasBlurredWindowRecently) {
|
|
110
|
+
currentModality = "virtual";
|
|
111
|
+
triggerChangeHandlers("virtual", e);
|
|
112
|
+
}
|
|
113
|
+
focusRestoredByWindow = hasBlurredWindowRecently && !hasEventBeforeFocus;
|
|
114
|
+
hasEventBeforeFocus = false;
|
|
115
|
+
pendingLabelControl = null;
|
|
116
|
+
hasBlurredWindowRecently = false;
|
|
129
117
|
}
|
|
130
118
|
function handleWindowBlur() {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
hasBlurredWindowRecently = true;
|
|
119
|
+
hasEventBeforeFocus = false;
|
|
120
|
+
pendingLabelControl = null;
|
|
121
|
+
hasBlurredWindowRecently = true;
|
|
135
122
|
}
|
|
123
|
+
function readPrototypeFocus(proto) {
|
|
124
|
+
try {
|
|
125
|
+
return proto.focus;
|
|
126
|
+
} catch {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Patch `HTMLElement.prototype.focus` to mark focus as programmatic. If `focus` is an accessor that
|
|
132
|
+
* throws off the prototype (e.g. Storybook's), resolve it per element instead. Never throws.
|
|
133
|
+
*/
|
|
134
|
+
function patchFocusMethod(win) {
|
|
135
|
+
const proto = win.HTMLElement.prototype;
|
|
136
|
+
try {
|
|
137
|
+
const nativeFocus = readPrototypeFocus(proto);
|
|
138
|
+
const descriptor = nativeFocus ? void 0 : Object.getOwnPropertyDescriptor(proto, "focus");
|
|
139
|
+
const getFocus = descriptor?.get;
|
|
140
|
+
if (!nativeFocus && !getFocus) return { focus: void 0 };
|
|
141
|
+
function patchedFocus() {
|
|
142
|
+
hasEventBeforeFocus = true;
|
|
143
|
+
(nativeFocus ?? getFocus.call(this)).apply(this, arguments);
|
|
144
|
+
}
|
|
145
|
+
Object.defineProperty(win.HTMLElement.prototype, "focus", {
|
|
146
|
+
configurable: true,
|
|
147
|
+
writable: true,
|
|
148
|
+
value: patchedFocus
|
|
149
|
+
});
|
|
150
|
+
return {
|
|
151
|
+
focus: nativeFocus,
|
|
152
|
+
descriptor
|
|
153
|
+
};
|
|
154
|
+
} catch {
|
|
155
|
+
return { focus: void 0 };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Setup global event listeners to control when keyboard focus style should be visible.
|
|
160
|
+
*/
|
|
136
161
|
function setupGlobalFocusEvents(root) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
Object.defineProperty(win.HTMLElement.prototype, "focus", {
|
|
189
|
-
configurable: true,
|
|
190
|
-
value: listenerData.focus
|
|
191
|
-
});
|
|
192
|
-
} catch {
|
|
193
|
-
}
|
|
194
|
-
doc.removeEventListener("keydown", handleKeyboardEvent, true);
|
|
195
|
-
doc.removeEventListener("keyup", handleKeyboardEvent, true);
|
|
196
|
-
doc.removeEventListener("click", handleClickEvent, true);
|
|
197
|
-
win.removeEventListener("focus", handleFocusEvent, true);
|
|
198
|
-
win.removeEventListener("blur", handleWindowBlur, false);
|
|
199
|
-
if (typeof win.PointerEvent !== "undefined") {
|
|
200
|
-
doc.removeEventListener("pointerdown", handlePointerEvent, true);
|
|
201
|
-
doc.removeEventListener("pointermove", handlePointerEvent, true);
|
|
202
|
-
doc.removeEventListener("pointerup", handlePointerEvent, true);
|
|
203
|
-
} else {
|
|
204
|
-
doc.removeEventListener("mousedown", handlePointerEvent, true);
|
|
205
|
-
doc.removeEventListener("mousemove", handlePointerEvent, true);
|
|
206
|
-
doc.removeEventListener("mouseup", handlePointerEvent, true);
|
|
207
|
-
}
|
|
208
|
-
listenerMap.delete(win);
|
|
162
|
+
if (typeof window === "undefined" || listenerMap.get((0, _zag_js_dom_query.getWindow)(root))) return;
|
|
163
|
+
const win = (0, _zag_js_dom_query.getWindow)(root);
|
|
164
|
+
const doc = (0, _zag_js_dom_query.getDocument)(root);
|
|
165
|
+
const focusData = patchFocusMethod(win);
|
|
166
|
+
doc.addEventListener("keydown", handleKeyboardEvent, true);
|
|
167
|
+
doc.addEventListener("keyup", handleKeyboardEvent, true);
|
|
168
|
+
doc.addEventListener("click", handleClickEvent, true);
|
|
169
|
+
win.addEventListener("focus", handleFocusEvent, true);
|
|
170
|
+
win.addEventListener("blur", handleWindowBlur, false);
|
|
171
|
+
if (typeof win.PointerEvent !== "undefined") {
|
|
172
|
+
doc.addEventListener("pointerdown", handlePointerEvent, true);
|
|
173
|
+
doc.addEventListener("pointermove", handlePointerEvent, true);
|
|
174
|
+
doc.addEventListener("pointerup", handlePointerEvent, true);
|
|
175
|
+
} else {
|
|
176
|
+
doc.addEventListener("mousedown", handlePointerEvent, true);
|
|
177
|
+
doc.addEventListener("mousemove", handlePointerEvent, true);
|
|
178
|
+
doc.addEventListener("mouseup", handlePointerEvent, true);
|
|
179
|
+
}
|
|
180
|
+
win.addEventListener("beforeunload", () => {
|
|
181
|
+
tearDownWindowFocusTracking(root);
|
|
182
|
+
}, { once: true });
|
|
183
|
+
listenerMap.set(win, focusData);
|
|
184
|
+
}
|
|
185
|
+
const tearDownWindowFocusTracking = (root, loadListener) => {
|
|
186
|
+
const win = (0, _zag_js_dom_query.getWindow)(root);
|
|
187
|
+
const doc = (0, _zag_js_dom_query.getDocument)(root);
|
|
188
|
+
if (loadListener) doc.removeEventListener("DOMContentLoaded", loadListener);
|
|
189
|
+
const listenerData = listenerMap.get(win);
|
|
190
|
+
if (!listenerData) return;
|
|
191
|
+
if (listenerData.focus || listenerData.descriptor) try {
|
|
192
|
+
Object.defineProperty(win.HTMLElement.prototype, "focus", listenerData.descriptor ?? {
|
|
193
|
+
configurable: true,
|
|
194
|
+
writable: true,
|
|
195
|
+
value: listenerData.focus
|
|
196
|
+
});
|
|
197
|
+
} catch {}
|
|
198
|
+
doc.removeEventListener("keydown", handleKeyboardEvent, true);
|
|
199
|
+
doc.removeEventListener("keyup", handleKeyboardEvent, true);
|
|
200
|
+
doc.removeEventListener("click", handleClickEvent, true);
|
|
201
|
+
win.removeEventListener("focus", handleFocusEvent, true);
|
|
202
|
+
win.removeEventListener("blur", handleWindowBlur, false);
|
|
203
|
+
if (typeof win.PointerEvent !== "undefined") {
|
|
204
|
+
doc.removeEventListener("pointerdown", handlePointerEvent, true);
|
|
205
|
+
doc.removeEventListener("pointermove", handlePointerEvent, true);
|
|
206
|
+
doc.removeEventListener("pointerup", handlePointerEvent, true);
|
|
207
|
+
} else {
|
|
208
|
+
doc.removeEventListener("mousedown", handlePointerEvent, true);
|
|
209
|
+
doc.removeEventListener("mousemove", handlePointerEvent, true);
|
|
210
|
+
doc.removeEventListener("mouseup", handlePointerEvent, true);
|
|
211
|
+
}
|
|
212
|
+
listenerMap.delete(win);
|
|
209
213
|
};
|
|
210
214
|
function getInteractionModality() {
|
|
211
|
-
|
|
215
|
+
return currentModality;
|
|
212
216
|
}
|
|
213
217
|
function setInteractionModality(modality) {
|
|
214
|
-
|
|
215
|
-
|
|
218
|
+
currentModality = modality;
|
|
219
|
+
triggerChangeHandlers(modality, null);
|
|
216
220
|
}
|
|
217
221
|
function trackInteractionModality(props) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
222
|
+
const { onChange, root } = props;
|
|
223
|
+
setupGlobalFocusEvents(root);
|
|
224
|
+
onChange({ modality: currentModality });
|
|
225
|
+
const handler = () => onChange({ modality: currentModality });
|
|
226
|
+
changeHandlers.add(handler);
|
|
227
|
+
return () => {
|
|
228
|
+
changeHandlers.delete(handler);
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/** Whether the latest focus was the browser restoring it as the window regained focus */
|
|
232
|
+
function isFocusRestoredByWindow() {
|
|
233
|
+
return focusRestoredByWindow;
|
|
226
234
|
}
|
|
227
235
|
function isFocusVisible() {
|
|
228
|
-
|
|
236
|
+
return currentModality === "keyboard" || currentModality === "virtual";
|
|
229
237
|
}
|
|
230
238
|
function trackFocusVisible(props = {}) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
239
|
+
const { isTextInput, autoFocus, onChange, root } = props;
|
|
240
|
+
setupGlobalFocusEvents(root);
|
|
241
|
+
onChange?.({
|
|
242
|
+
isFocusVisible: autoFocus || isFocusVisible(),
|
|
243
|
+
modality: currentModality
|
|
244
|
+
});
|
|
245
|
+
const handler = (modality, e) => {
|
|
246
|
+
if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
|
|
247
|
+
onChange?.({
|
|
248
|
+
isFocusVisible: isFocusVisible(),
|
|
249
|
+
modality
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
changeHandlers.add(handler);
|
|
253
|
+
return () => {
|
|
254
|
+
changeHandlers.delete(handler);
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
exports.getInteractionModality = getInteractionModality;
|
|
258
|
+
exports.ignoreFocusEvent = ignoreFocusEvent;
|
|
259
|
+
exports.isFocusRestoredByWindow = isFocusRestoredByWindow;
|
|
260
|
+
exports.isFocusVisible = isFocusVisible;
|
|
261
|
+
exports.listenerMap = listenerMap;
|
|
262
|
+
exports.setInteractionModality = setInteractionModality;
|
|
263
|
+
exports.trackFocusVisible = trackFocusVisible;
|
|
264
|
+
exports.trackInteractionModality = trackInteractionModality;
|
package/dist/index.mjs
CHANGED
|
@@ -1,230 +1,253 @@
|
|
|
1
|
+
import { getActiveElement, getDocument, getEventTarget, getWindow, isDocument, isMac, isVirtualClick, isWindow } from "@zag-js/dom-query";
|
|
1
2
|
// src/index.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
getWindow,
|
|
7
|
-
isDocument,
|
|
8
|
-
isMac,
|
|
9
|
-
isVirtualClick,
|
|
10
|
-
isWindow
|
|
11
|
-
} from "@zag-js/dom-query";
|
|
3
|
+
/**
|
|
4
|
+
* Credit: Huge props to the team at Adobe for inspiring this implementation.
|
|
5
|
+
* https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/interactions/src/useFocusVisible.ts
|
|
6
|
+
*/
|
|
12
7
|
function isValidKey(e) {
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
return !(e.metaKey || !isMac() && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
|
|
9
|
+
}
|
|
10
|
+
const nonTextInputTypes = /* @__PURE__ */ new Set([
|
|
11
|
+
"checkbox",
|
|
12
|
+
"radio",
|
|
13
|
+
"range",
|
|
14
|
+
"color",
|
|
15
|
+
"file",
|
|
16
|
+
"image",
|
|
17
|
+
"button",
|
|
18
|
+
"submit",
|
|
19
|
+
"reset"
|
|
20
|
+
]);
|
|
21
|
+
const interactiveContentSelector = [
|
|
22
|
+
"a[href]",
|
|
23
|
+
"audio[controls]",
|
|
24
|
+
"button",
|
|
25
|
+
"details",
|
|
26
|
+
"embed",
|
|
27
|
+
"iframe",
|
|
28
|
+
"img[controls]",
|
|
29
|
+
"img[usemap]",
|
|
30
|
+
"input",
|
|
31
|
+
"select",
|
|
32
|
+
"textarea",
|
|
33
|
+
"video[controls]"
|
|
29
34
|
].join(",");
|
|
30
35
|
function isKeyboardFocusEvent(isTextInput, modality, e) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
36
|
+
const eventTarget = e ? getEventTarget(e) : null;
|
|
37
|
+
const doc = getDocument(eventTarget);
|
|
38
|
+
const win = getWindow(eventTarget);
|
|
39
|
+
const activeElement = getActiveElement(doc);
|
|
40
|
+
isTextInput = isTextInput || activeElement instanceof win.HTMLInputElement && !nonTextInputTypes.has(activeElement?.type) || activeElement instanceof win.HTMLTextAreaElement || activeElement instanceof win.HTMLElement && activeElement.isContentEditable;
|
|
41
|
+
return !(isTextInput && modality === "keyboard" && e instanceof win.KeyboardEvent && !Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key));
|
|
42
|
+
}
|
|
43
|
+
let currentModality = null;
|
|
44
|
+
let changeHandlers = /* @__PURE__ */ new Set();
|
|
45
|
+
let listenerMap = /* @__PURE__ */ new Map();
|
|
46
|
+
let hasEventBeforeFocus = false;
|
|
47
|
+
let pendingLabelControl = null;
|
|
48
|
+
let hasBlurredWindowRecently = false;
|
|
49
|
+
let focusRestoredByWindow = false;
|
|
50
|
+
let lastPointerPosition = null;
|
|
51
|
+
/** Skip the next focus event, so preventFocus() doesn't show focus rings */
|
|
52
|
+
let ignoreFocusEvent = false;
|
|
53
|
+
const FOCUS_VISIBLE_INPUT_KEYS = {
|
|
54
|
+
Tab: true,
|
|
55
|
+
Escape: true
|
|
49
56
|
};
|
|
50
57
|
function triggerChangeHandlers(modality, e) {
|
|
51
|
-
|
|
52
|
-
handler(modality, e);
|
|
53
|
-
}
|
|
58
|
+
for (let handler of changeHandlers) handler(modality, e);
|
|
54
59
|
}
|
|
55
60
|
function handleKeyboardEvent(e) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
pendingLabelControl = null;
|
|
62
|
+
hasEventBeforeFocus = true;
|
|
63
|
+
if (isValidKey(e)) {
|
|
64
|
+
currentModality = "keyboard";
|
|
65
|
+
triggerChangeHandlers("keyboard", e);
|
|
66
|
+
}
|
|
62
67
|
}
|
|
63
68
|
function handlePointerEvent(e) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
if ((e.type === "pointermove" || e.type === "mousemove") && e.isTrusted && lastPointerPosition?.x === e.clientX && lastPointerPosition?.y === e.clientY) return;
|
|
70
|
+
lastPointerPosition = {
|
|
71
|
+
x: e.clientX,
|
|
72
|
+
y: e.clientY
|
|
73
|
+
};
|
|
74
|
+
currentModality = "pointer";
|
|
75
|
+
if (e.type === "mousedown" || e.type === "pointerdown") {
|
|
76
|
+
pendingLabelControl = null;
|
|
77
|
+
hasEventBeforeFocus = true;
|
|
78
|
+
triggerChangeHandlers("pointer", e);
|
|
79
|
+
}
|
|
73
80
|
}
|
|
74
81
|
function handleClickEvent(e) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
82
|
+
if (isVirtualClick(e)) {
|
|
83
|
+
pendingLabelControl = null;
|
|
84
|
+
hasEventBeforeFocus = true;
|
|
85
|
+
currentModality = "virtual";
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
pendingLabelControl = null;
|
|
89
|
+
const target = getEventTarget(e);
|
|
90
|
+
const label = target?.closest("label");
|
|
91
|
+
const control = label?.control;
|
|
92
|
+
if (!label || !control || control.matches(":disabled")) return;
|
|
93
|
+
const interactive = target?.closest(interactiveContentSelector);
|
|
94
|
+
if (interactive && label.contains(interactive) && interactive !== control) return;
|
|
95
|
+
if (control !== getActiveElement(label.ownerDocument)) pendingLabelControl = control;
|
|
89
96
|
}
|
|
90
97
|
function handleFocusEvent(e) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
pendingLabelControl = null;
|
|
107
|
-
hasBlurredWindowRecently = false;
|
|
98
|
+
const target = getEventTarget(e);
|
|
99
|
+
const isLabelActivationFocus = target === pendingLabelControl;
|
|
100
|
+
if (isWindow(target)) {
|
|
101
|
+
hasBlurredWindowRecently = true;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (isDocument(target) || !e.isTrusted) return;
|
|
105
|
+
if (!hasEventBeforeFocus && !isLabelActivationFocus && !hasBlurredWindowRecently) {
|
|
106
|
+
currentModality = "virtual";
|
|
107
|
+
triggerChangeHandlers("virtual", e);
|
|
108
|
+
}
|
|
109
|
+
focusRestoredByWindow = hasBlurredWindowRecently && !hasEventBeforeFocus;
|
|
110
|
+
hasEventBeforeFocus = false;
|
|
111
|
+
pendingLabelControl = null;
|
|
112
|
+
hasBlurredWindowRecently = false;
|
|
108
113
|
}
|
|
109
114
|
function handleWindowBlur() {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
115
|
+
hasEventBeforeFocus = false;
|
|
116
|
+
pendingLabelControl = null;
|
|
117
|
+
hasBlurredWindowRecently = true;
|
|
118
|
+
}
|
|
119
|
+
function readPrototypeFocus(proto) {
|
|
120
|
+
try {
|
|
121
|
+
return proto.focus;
|
|
122
|
+
} catch {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Patch `HTMLElement.prototype.focus` to mark focus as programmatic. If `focus` is an accessor that
|
|
128
|
+
* throws off the prototype (e.g. Storybook's), resolve it per element instead. Never throws.
|
|
129
|
+
*/
|
|
130
|
+
function patchFocusMethod(win) {
|
|
131
|
+
const proto = win.HTMLElement.prototype;
|
|
132
|
+
try {
|
|
133
|
+
const nativeFocus = readPrototypeFocus(proto);
|
|
134
|
+
const descriptor = nativeFocus ? void 0 : Object.getOwnPropertyDescriptor(proto, "focus");
|
|
135
|
+
const getFocus = descriptor?.get;
|
|
136
|
+
if (!nativeFocus && !getFocus) return { focus: void 0 };
|
|
137
|
+
function patchedFocus() {
|
|
138
|
+
hasEventBeforeFocus = true;
|
|
139
|
+
(nativeFocus ?? getFocus.call(this)).apply(this, arguments);
|
|
140
|
+
}
|
|
141
|
+
Object.defineProperty(win.HTMLElement.prototype, "focus", {
|
|
142
|
+
configurable: true,
|
|
143
|
+
writable: true,
|
|
144
|
+
value: patchedFocus
|
|
145
|
+
});
|
|
146
|
+
return {
|
|
147
|
+
focus: nativeFocus,
|
|
148
|
+
descriptor
|
|
149
|
+
};
|
|
150
|
+
} catch {
|
|
151
|
+
return { focus: void 0 };
|
|
152
|
+
}
|
|
114
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Setup global event listeners to control when keyboard focus style should be visible.
|
|
156
|
+
*/
|
|
115
157
|
function setupGlobalFocusEvents(root) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
Object.defineProperty(win.HTMLElement.prototype, "focus", {
|
|
168
|
-
configurable: true,
|
|
169
|
-
value: listenerData.focus
|
|
170
|
-
});
|
|
171
|
-
} catch {
|
|
172
|
-
}
|
|
173
|
-
doc.removeEventListener("keydown", handleKeyboardEvent, true);
|
|
174
|
-
doc.removeEventListener("keyup", handleKeyboardEvent, true);
|
|
175
|
-
doc.removeEventListener("click", handleClickEvent, true);
|
|
176
|
-
win.removeEventListener("focus", handleFocusEvent, true);
|
|
177
|
-
win.removeEventListener("blur", handleWindowBlur, false);
|
|
178
|
-
if (typeof win.PointerEvent !== "undefined") {
|
|
179
|
-
doc.removeEventListener("pointerdown", handlePointerEvent, true);
|
|
180
|
-
doc.removeEventListener("pointermove", handlePointerEvent, true);
|
|
181
|
-
doc.removeEventListener("pointerup", handlePointerEvent, true);
|
|
182
|
-
} else {
|
|
183
|
-
doc.removeEventListener("mousedown", handlePointerEvent, true);
|
|
184
|
-
doc.removeEventListener("mousemove", handlePointerEvent, true);
|
|
185
|
-
doc.removeEventListener("mouseup", handlePointerEvent, true);
|
|
186
|
-
}
|
|
187
|
-
listenerMap.delete(win);
|
|
158
|
+
if (typeof window === "undefined" || listenerMap.get(getWindow(root))) return;
|
|
159
|
+
const win = getWindow(root);
|
|
160
|
+
const doc = getDocument(root);
|
|
161
|
+
const focusData = patchFocusMethod(win);
|
|
162
|
+
doc.addEventListener("keydown", handleKeyboardEvent, true);
|
|
163
|
+
doc.addEventListener("keyup", handleKeyboardEvent, true);
|
|
164
|
+
doc.addEventListener("click", handleClickEvent, true);
|
|
165
|
+
win.addEventListener("focus", handleFocusEvent, true);
|
|
166
|
+
win.addEventListener("blur", handleWindowBlur, false);
|
|
167
|
+
if (typeof win.PointerEvent !== "undefined") {
|
|
168
|
+
doc.addEventListener("pointerdown", handlePointerEvent, true);
|
|
169
|
+
doc.addEventListener("pointermove", handlePointerEvent, true);
|
|
170
|
+
doc.addEventListener("pointerup", handlePointerEvent, true);
|
|
171
|
+
} else {
|
|
172
|
+
doc.addEventListener("mousedown", handlePointerEvent, true);
|
|
173
|
+
doc.addEventListener("mousemove", handlePointerEvent, true);
|
|
174
|
+
doc.addEventListener("mouseup", handlePointerEvent, true);
|
|
175
|
+
}
|
|
176
|
+
win.addEventListener("beforeunload", () => {
|
|
177
|
+
tearDownWindowFocusTracking(root);
|
|
178
|
+
}, { once: true });
|
|
179
|
+
listenerMap.set(win, focusData);
|
|
180
|
+
}
|
|
181
|
+
const tearDownWindowFocusTracking = (root, loadListener) => {
|
|
182
|
+
const win = getWindow(root);
|
|
183
|
+
const doc = getDocument(root);
|
|
184
|
+
if (loadListener) doc.removeEventListener("DOMContentLoaded", loadListener);
|
|
185
|
+
const listenerData = listenerMap.get(win);
|
|
186
|
+
if (!listenerData) return;
|
|
187
|
+
if (listenerData.focus || listenerData.descriptor) try {
|
|
188
|
+
Object.defineProperty(win.HTMLElement.prototype, "focus", listenerData.descriptor ?? {
|
|
189
|
+
configurable: true,
|
|
190
|
+
writable: true,
|
|
191
|
+
value: listenerData.focus
|
|
192
|
+
});
|
|
193
|
+
} catch {}
|
|
194
|
+
doc.removeEventListener("keydown", handleKeyboardEvent, true);
|
|
195
|
+
doc.removeEventListener("keyup", handleKeyboardEvent, true);
|
|
196
|
+
doc.removeEventListener("click", handleClickEvent, true);
|
|
197
|
+
win.removeEventListener("focus", handleFocusEvent, true);
|
|
198
|
+
win.removeEventListener("blur", handleWindowBlur, false);
|
|
199
|
+
if (typeof win.PointerEvent !== "undefined") {
|
|
200
|
+
doc.removeEventListener("pointerdown", handlePointerEvent, true);
|
|
201
|
+
doc.removeEventListener("pointermove", handlePointerEvent, true);
|
|
202
|
+
doc.removeEventListener("pointerup", handlePointerEvent, true);
|
|
203
|
+
} else {
|
|
204
|
+
doc.removeEventListener("mousedown", handlePointerEvent, true);
|
|
205
|
+
doc.removeEventListener("mousemove", handlePointerEvent, true);
|
|
206
|
+
doc.removeEventListener("mouseup", handlePointerEvent, true);
|
|
207
|
+
}
|
|
208
|
+
listenerMap.delete(win);
|
|
188
209
|
};
|
|
189
210
|
function getInteractionModality() {
|
|
190
|
-
|
|
211
|
+
return currentModality;
|
|
191
212
|
}
|
|
192
213
|
function setInteractionModality(modality) {
|
|
193
|
-
|
|
194
|
-
|
|
214
|
+
currentModality = modality;
|
|
215
|
+
triggerChangeHandlers(modality, null);
|
|
195
216
|
}
|
|
196
217
|
function trackInteractionModality(props) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
218
|
+
const { onChange, root } = props;
|
|
219
|
+
setupGlobalFocusEvents(root);
|
|
220
|
+
onChange({ modality: currentModality });
|
|
221
|
+
const handler = () => onChange({ modality: currentModality });
|
|
222
|
+
changeHandlers.add(handler);
|
|
223
|
+
return () => {
|
|
224
|
+
changeHandlers.delete(handler);
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/** Whether the latest focus was the browser restoring it as the window regained focus */
|
|
228
|
+
function isFocusRestoredByWindow() {
|
|
229
|
+
return focusRestoredByWindow;
|
|
205
230
|
}
|
|
206
231
|
function isFocusVisible() {
|
|
207
|
-
|
|
232
|
+
return currentModality === "keyboard" || currentModality === "virtual";
|
|
208
233
|
}
|
|
209
234
|
function trackFocusVisible(props = {}) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
trackInteractionModality
|
|
230
|
-
};
|
|
235
|
+
const { isTextInput, autoFocus, onChange, root } = props;
|
|
236
|
+
setupGlobalFocusEvents(root);
|
|
237
|
+
onChange?.({
|
|
238
|
+
isFocusVisible: autoFocus || isFocusVisible(),
|
|
239
|
+
modality: currentModality
|
|
240
|
+
});
|
|
241
|
+
const handler = (modality, e) => {
|
|
242
|
+
if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
|
|
243
|
+
onChange?.({
|
|
244
|
+
isFocusVisible: isFocusVisible(),
|
|
245
|
+
modality
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
changeHandlers.add(handler);
|
|
249
|
+
return () => {
|
|
250
|
+
changeHandlers.delete(handler);
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
export { getInteractionModality, ignoreFocusEvent, isFocusRestoredByWindow, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/focus-visible",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.4",
|
|
4
4
|
"description": "Focus visible polyfill utility based on WICG",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"clean-package": "../../../clean-package.config.json",
|
|
29
29
|
"main": "dist/index.js",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@zag-js/dom-query": "2.0.0-next.
|
|
31
|
+
"@zag-js/dom-query": "2.0.0-next.4"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"clean-package": "2.2.0"
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"./package.json": "./package.json"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
|
-
"build": "
|
|
52
|
+
"build": "tsdown",
|
|
53
53
|
"test": "vitest",
|
|
54
54
|
"lint": "oxlint src",
|
|
55
55
|
"test-ci": "pnpm test --ci --runInBand -u",
|