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