@zag-js/focus-visible 1.47.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 CHANGED
@@ -1,44 +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
- /** Unset when the prototype's `focus` could not be read */
5
- focus: VoidFunction | undefined;
6
- /** The unreadable accessor to restore */
7
- descriptor?: PropertyDescriptor | undefined;
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;
8
8
  }
9
- declare let listenerMap: Map<Window, GlobalListenerData>;
9
+ export declare let listenerMap: Map<Window, GlobalListenerData>;
10
10
  /** Skip the next focus event, so preventFocus() doesn't show focus rings */
11
- declare let ignoreFocusEvent: boolean;
12
- declare function getInteractionModality(): Modality | null;
13
- declare function setInteractionModality(modality: Modality): void;
14
- interface InteractionModalityChangeDetails {
15
- /** The modality of the interaction that caused the focus to be visible. */
16
- modality: Modality | null;
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
- /** 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;
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
- declare function isFocusVisible(): boolean;
26
- 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;
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;
31
31
  }
32
- 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;
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;
41
41
  }
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 };
42
+ export declare function trackFocusVisible(props?: FocusVisibleProps): VoidFunction;
43
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,44 +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
- /** Unset when the prototype's `focus` could not be read */
5
- focus: VoidFunction | undefined;
6
- /** The unreadable accessor to restore */
7
- descriptor?: PropertyDescriptor | undefined;
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;
8
8
  }
9
- declare let listenerMap: Map<Window, GlobalListenerData>;
9
+ export declare let listenerMap: Map<Window, GlobalListenerData>;
10
10
  /** Skip the next focus event, so preventFocus() doesn't show focus rings */
11
- declare let ignoreFocusEvent: boolean;
12
- declare function getInteractionModality(): Modality | null;
13
- declare function setInteractionModality(modality: Modality): void;
14
- interface InteractionModalityChangeDetails {
15
- /** The modality of the interaction that caused the focus to be visible. */
16
- modality: Modality | null;
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
- /** 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;
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
- declare function isFocusVisible(): boolean;
26
- 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;
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;
31
31
  }
32
- 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;
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;
41
41
  }
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 };
42
+ export declare function trackFocusVisible(props?: FocusVisibleProps): VoidFunction;
43
+ export {};
package/dist/index.js CHANGED
@@ -1,269 +1,253 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
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
- module.exports = __toCommonJS(index_exports);
32
- var import_dom_query = require("@zag-js/dom-query");
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
- return !(e.metaKey || !(0, import_dom_query.isMac)() && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
35
- }
36
- var nonTextInputTypes = /* @__PURE__ */ new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"]);
37
- var interactiveContentSelector = [
38
- "a[href]",
39
- "audio[controls]",
40
- "button",
41
- "details",
42
- "embed",
43
- "iframe",
44
- "img[controls]",
45
- "img[usemap]",
46
- "input",
47
- "select",
48
- "textarea",
49
- "video[controls]"
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
- const eventTarget = e ? (0, import_dom_query.getEventTarget)(e) : null;
53
- const doc = (0, import_dom_query.getDocument)(eventTarget);
54
- const win = (0, import_dom_query.getWindow)(eventTarget);
55
- const activeElement = (0, import_dom_query.getActiveElement)(doc);
56
- isTextInput = isTextInput || activeElement instanceof win.HTMLInputElement && !nonTextInputTypes.has(activeElement?.type) || activeElement instanceof win.HTMLTextAreaElement || activeElement instanceof win.HTMLElement && activeElement.isContentEditable;
57
- return !(isTextInput && modality === "keyboard" && e instanceof win.KeyboardEvent && !Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key));
58
- }
59
- var currentModality = null;
60
- var changeHandlers = /* @__PURE__ */ new Set();
61
- var listenerMap = /* @__PURE__ */ new Map();
62
- var hasEventBeforeFocus = false;
63
- var pendingLabelControl = null;
64
- var hasBlurredWindowRecently = false;
65
- var lastPointerPosition = null;
66
- var ignoreFocusEvent = false;
67
- var FOCUS_VISIBLE_INPUT_KEYS = {
68
- Tab: true,
69
- Escape: true
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 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
- for (let handler of changeHandlers) {
73
- handler(modality, e);
74
- }
61
+ for (let handler of changeHandlers) handler(modality, e);
75
62
  }
76
63
  function handleKeyboardEvent(e) {
77
- pendingLabelControl = null;
78
- hasEventBeforeFocus = true;
79
- if (isValidKey(e)) {
80
- currentModality = "keyboard";
81
- triggerChangeHandlers("keyboard", e);
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
- const isMove = e.type === "pointermove" || e.type === "mousemove";
86
- if (isMove && e.isTrusted && lastPointerPosition?.x === e.clientX && lastPointerPosition?.y === e.clientY) return;
87
- lastPointerPosition = { x: e.clientX, y: e.clientY };
88
- currentModality = "pointer";
89
- if (e.type === "mousedown" || e.type === "pointerdown") {
90
- pendingLabelControl = null;
91
- hasEventBeforeFocus = true;
92
- triggerChangeHandlers("pointer", e);
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
- if ((0, import_dom_query.isVirtualClick)(e)) {
97
- pendingLabelControl = null;
98
- hasEventBeforeFocus = true;
99
- currentModality = "virtual";
100
- return;
101
- }
102
- pendingLabelControl = null;
103
- const target = (0, import_dom_query.getEventTarget)(e);
104
- const label = target?.closest("label");
105
- const control = label?.control;
106
- if (!label || !control || control.matches(":disabled")) return;
107
- const interactive = target?.closest(interactiveContentSelector);
108
- if (interactive && label.contains(interactive) && interactive !== control) return;
109
- if (control !== (0, import_dom_query.getActiveElement)(label.ownerDocument)) pendingLabelControl = control;
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
- const target = (0, import_dom_query.getEventTarget)(e);
113
- const isLabelActivationFocus = target === pendingLabelControl;
114
- if (target === (0, import_dom_query.getWindow)(target) || target === (0, import_dom_query.getDocument)(target) || ignoreFocusEvent || !e.isTrusted) {
115
- return;
116
- }
117
- if (!hasEventBeforeFocus && !isLabelActivationFocus && !hasBlurredWindowRecently) {
118
- currentModality = "virtual";
119
- triggerChangeHandlers("virtual", e);
120
- }
121
- hasEventBeforeFocus = false;
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
- if (ignoreFocusEvent) return;
127
- hasEventBeforeFocus = false;
128
- pendingLabelControl = null;
129
- hasBlurredWindowRecently = true;
113
+ hasEventBeforeFocus = false;
114
+ pendingLabelControl = null;
115
+ hasBlurredWindowRecently = true;
130
116
  }
131
117
  function readPrototypeFocus(proto) {
132
- try {
133
- return proto.focus;
134
- } catch {
135
- return void 0;
136
- }
137
- }
118
+ try {
119
+ return proto.focus;
120
+ } catch {
121
+ return;
122
+ }
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
+ */
138
128
  function patchFocusMethod(win) {
139
- const proto = win.HTMLElement.prototype;
140
- try {
141
- let patchedFocus2 = function() {
142
- hasEventBeforeFocus = true;
143
- const focus = nativeFocus ?? getFocus.call(this);
144
- focus.apply(this, arguments);
145
- };
146
- var patchedFocus = patchedFocus2;
147
- const nativeFocus = readPrototypeFocus(proto);
148
- const descriptor = nativeFocus ? void 0 : Object.getOwnPropertyDescriptor(proto, "focus");
149
- const getFocus = descriptor?.get;
150
- if (!nativeFocus && !getFocus) return { focus: void 0 };
151
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
152
- configurable: true,
153
- writable: true,
154
- value: patchedFocus2
155
- });
156
- return { focus: nativeFocus, descriptor };
157
- } catch {
158
- return { focus: void 0 };
159
- }
160
- }
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
+ }
151
+ }
152
+ /**
153
+ * Setup global event listeners to control when keyboard focus style should be visible.
154
+ */
161
155
  function setupGlobalFocusEvents(root) {
162
- if (typeof window === "undefined" || listenerMap.get((0, import_dom_query.getWindow)(root))) {
163
- return;
164
- }
165
- const win = (0, import_dom_query.getWindow)(root);
166
- const doc = (0, import_dom_query.getDocument)(root);
167
- const focusData = patchFocusMethod(win);
168
- doc.addEventListener("keydown", handleKeyboardEvent, true);
169
- doc.addEventListener("keyup", handleKeyboardEvent, true);
170
- doc.addEventListener("click", handleClickEvent, true);
171
- win.addEventListener("focus", handleFocusEvent, true);
172
- win.addEventListener("blur", handleWindowBlur, false);
173
- if (typeof win.PointerEvent !== "undefined") {
174
- doc.addEventListener("pointerdown", handlePointerEvent, true);
175
- doc.addEventListener("pointermove", handlePointerEvent, true);
176
- doc.addEventListener("pointerup", handlePointerEvent, true);
177
- } else {
178
- doc.addEventListener("mousedown", handlePointerEvent, true);
179
- doc.addEventListener("mousemove", handlePointerEvent, true);
180
- doc.addEventListener("mouseup", handlePointerEvent, true);
181
- }
182
- win.addEventListener(
183
- "beforeunload",
184
- () => {
185
- tearDownWindowFocusTracking(root);
186
- },
187
- { once: true }
188
- );
189
- listenerMap.set(win, focusData);
190
- }
191
- var tearDownWindowFocusTracking = (root, loadListener) => {
192
- const win = (0, import_dom_query.getWindow)(root);
193
- const doc = (0, import_dom_query.getDocument)(root);
194
- if (loadListener) {
195
- doc.removeEventListener("DOMContentLoaded", loadListener);
196
- }
197
- const listenerData = listenerMap.get(win);
198
- if (!listenerData) {
199
- return;
200
- }
201
- if (listenerData.focus || listenerData.descriptor) {
202
- try {
203
- Object.defineProperty(
204
- win.HTMLElement.prototype,
205
- "focus",
206
- listenerData.descriptor ?? { configurable: true, writable: true, value: listenerData.focus }
207
- );
208
- } catch {
209
- }
210
- }
211
- doc.removeEventListener("keydown", handleKeyboardEvent, true);
212
- doc.removeEventListener("keyup", handleKeyboardEvent, true);
213
- doc.removeEventListener("click", handleClickEvent, true);
214
- win.removeEventListener("focus", handleFocusEvent, true);
215
- win.removeEventListener("blur", handleWindowBlur, false);
216
- if (typeof win.PointerEvent !== "undefined") {
217
- doc.removeEventListener("pointerdown", handlePointerEvent, true);
218
- doc.removeEventListener("pointermove", handlePointerEvent, true);
219
- doc.removeEventListener("pointerup", handlePointerEvent, true);
220
- } else {
221
- doc.removeEventListener("mousedown", handlePointerEvent, true);
222
- doc.removeEventListener("mousemove", handlePointerEvent, true);
223
- doc.removeEventListener("mouseup", handlePointerEvent, true);
224
- }
225
- listenerMap.delete(win);
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
+ }
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);
226
207
  };
227
208
  function getInteractionModality() {
228
- return currentModality;
209
+ return currentModality;
229
210
  }
230
211
  function setInteractionModality(modality) {
231
- currentModality = modality;
232
- triggerChangeHandlers(modality, null);
212
+ currentModality = modality;
213
+ triggerChangeHandlers(modality, null);
233
214
  }
234
215
  function trackInteractionModality(props) {
235
- const { onChange, root } = props;
236
- setupGlobalFocusEvents(root);
237
- onChange({ modality: currentModality });
238
- const handler = () => onChange({ modality: currentModality });
239
- changeHandlers.add(handler);
240
- return () => {
241
- changeHandlers.delete(handler);
242
- };
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
+ };
243
224
  }
244
225
  function isFocusVisible() {
245
- return currentModality === "keyboard" || currentModality === "virtual";
226
+ return currentModality === "keyboard" || currentModality === "virtual";
246
227
  }
247
228
  function trackFocusVisible(props = {}) {
248
- const { isTextInput, autoFocus, onChange, root } = props;
249
- setupGlobalFocusEvents(root);
250
- onChange?.({ isFocusVisible: autoFocus || isFocusVisible(), modality: currentModality });
251
- const handler = (modality, e) => {
252
- if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
253
- onChange?.({ isFocusVisible: isFocusVisible(), modality });
254
- };
255
- changeHandlers.add(handler);
256
- return () => {
257
- changeHandlers.delete(handler);
258
- };
259
- }
260
- // Annotate the CommonJS export names for ESM import in node:
261
- 0 && (module.exports = {
262
- getInteractionModality,
263
- ignoreFocusEvent,
264
- isFocusVisible,
265
- listenerMap,
266
- setInteractionModality,
267
- trackFocusVisible,
268
- trackInteractionModality
269
- });
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
+ };
246
+ }
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,238 +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
- return !(e.metaKey || !isMac() && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
5
- }
6
- var nonTextInputTypes = /* @__PURE__ */ new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"]);
7
- var interactiveContentSelector = [
8
- "a[href]",
9
- "audio[controls]",
10
- "button",
11
- "details",
12
- "embed",
13
- "iframe",
14
- "img[controls]",
15
- "img[usemap]",
16
- "input",
17
- "select",
18
- "textarea",
19
- "video[controls]"
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
- const eventTarget = e ? getEventTarget(e) : null;
23
- const doc = getDocument(eventTarget);
24
- const win = getWindow(eventTarget);
25
- const activeElement = getActiveElement(doc);
26
- isTextInput = isTextInput || activeElement instanceof win.HTMLInputElement && !nonTextInputTypes.has(activeElement?.type) || activeElement instanceof win.HTMLTextAreaElement || activeElement instanceof win.HTMLElement && activeElement.isContentEditable;
27
- return !(isTextInput && modality === "keyboard" && e instanceof win.KeyboardEvent && !Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key));
28
- }
29
- var currentModality = null;
30
- var changeHandlers = /* @__PURE__ */ new Set();
31
- var listenerMap = /* @__PURE__ */ new Map();
32
- var hasEventBeforeFocus = false;
33
- var pendingLabelControl = null;
34
- var hasBlurredWindowRecently = false;
35
- var lastPointerPosition = null;
36
- var ignoreFocusEvent = false;
37
- var FOCUS_VISIBLE_INPUT_KEYS = {
38
- Tab: true,
39
- Escape: true
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
- for (let handler of changeHandlers) {
43
- handler(modality, e);
44
- }
57
+ for (let handler of changeHandlers) handler(modality, e);
45
58
  }
46
59
  function handleKeyboardEvent(e) {
47
- pendingLabelControl = null;
48
- hasEventBeforeFocus = true;
49
- if (isValidKey(e)) {
50
- currentModality = "keyboard";
51
- triggerChangeHandlers("keyboard", e);
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
- const isMove = e.type === "pointermove" || e.type === "mousemove";
56
- if (isMove && e.isTrusted && lastPointerPosition?.x === e.clientX && lastPointerPosition?.y === e.clientY) return;
57
- lastPointerPosition = { x: e.clientX, y: e.clientY };
58
- currentModality = "pointer";
59
- if (e.type === "mousedown" || e.type === "pointerdown") {
60
- pendingLabelControl = null;
61
- hasEventBeforeFocus = true;
62
- triggerChangeHandlers("pointer", e);
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
- if (isVirtualClick(e)) {
67
- pendingLabelControl = null;
68
- hasEventBeforeFocus = true;
69
- currentModality = "virtual";
70
- return;
71
- }
72
- pendingLabelControl = null;
73
- const target = getEventTarget(e);
74
- const label = target?.closest("label");
75
- const control = label?.control;
76
- if (!label || !control || control.matches(":disabled")) return;
77
- const interactive = target?.closest(interactiveContentSelector);
78
- if (interactive && label.contains(interactive) && interactive !== control) return;
79
- if (control !== getActiveElement(label.ownerDocument)) pendingLabelControl = control;
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
- const target = getEventTarget(e);
83
- const isLabelActivationFocus = target === pendingLabelControl;
84
- if (target === getWindow(target) || target === getDocument(target) || ignoreFocusEvent || !e.isTrusted) {
85
- return;
86
- }
87
- if (!hasEventBeforeFocus && !isLabelActivationFocus && !hasBlurredWindowRecently) {
88
- currentModality = "virtual";
89
- triggerChangeHandlers("virtual", e);
90
- }
91
- hasEventBeforeFocus = false;
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
- if (ignoreFocusEvent) return;
97
- hasEventBeforeFocus = false;
98
- pendingLabelControl = null;
99
- hasBlurredWindowRecently = true;
109
+ hasEventBeforeFocus = false;
110
+ pendingLabelControl = null;
111
+ hasBlurredWindowRecently = true;
100
112
  }
101
113
  function readPrototypeFocus(proto) {
102
- try {
103
- return proto.focus;
104
- } catch {
105
- return void 0;
106
- }
107
- }
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
+ */
108
124
  function patchFocusMethod(win) {
109
- const proto = win.HTMLElement.prototype;
110
- try {
111
- let patchedFocus2 = function() {
112
- hasEventBeforeFocus = true;
113
- const focus = nativeFocus ?? getFocus.call(this);
114
- focus.apply(this, arguments);
115
- };
116
- var patchedFocus = patchedFocus2;
117
- const nativeFocus = readPrototypeFocus(proto);
118
- const descriptor = nativeFocus ? void 0 : Object.getOwnPropertyDescriptor(proto, "focus");
119
- const getFocus = descriptor?.get;
120
- if (!nativeFocus && !getFocus) return { focus: void 0 };
121
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
122
- configurable: true,
123
- writable: true,
124
- value: patchedFocus2
125
- });
126
- return { focus: nativeFocus, descriptor };
127
- } catch {
128
- return { focus: void 0 };
129
- }
130
- }
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
+ }
147
+ }
148
+ /**
149
+ * Setup global event listeners to control when keyboard focus style should be visible.
150
+ */
131
151
  function setupGlobalFocusEvents(root) {
132
- if (typeof window === "undefined" || listenerMap.get(getWindow(root))) {
133
- return;
134
- }
135
- const win = getWindow(root);
136
- const doc = getDocument(root);
137
- const focusData = patchFocusMethod(win);
138
- doc.addEventListener("keydown", handleKeyboardEvent, true);
139
- doc.addEventListener("keyup", handleKeyboardEvent, true);
140
- doc.addEventListener("click", handleClickEvent, true);
141
- win.addEventListener("focus", handleFocusEvent, true);
142
- win.addEventListener("blur", handleWindowBlur, false);
143
- if (typeof win.PointerEvent !== "undefined") {
144
- doc.addEventListener("pointerdown", handlePointerEvent, true);
145
- doc.addEventListener("pointermove", handlePointerEvent, true);
146
- doc.addEventListener("pointerup", handlePointerEvent, true);
147
- } else {
148
- doc.addEventListener("mousedown", handlePointerEvent, true);
149
- doc.addEventListener("mousemove", handlePointerEvent, true);
150
- doc.addEventListener("mouseup", handlePointerEvent, true);
151
- }
152
- win.addEventListener(
153
- "beforeunload",
154
- () => {
155
- tearDownWindowFocusTracking(root);
156
- },
157
- { once: true }
158
- );
159
- listenerMap.set(win, focusData);
160
- }
161
- var tearDownWindowFocusTracking = (root, loadListener) => {
162
- const win = getWindow(root);
163
- const doc = getDocument(root);
164
- if (loadListener) {
165
- doc.removeEventListener("DOMContentLoaded", loadListener);
166
- }
167
- const listenerData = listenerMap.get(win);
168
- if (!listenerData) {
169
- return;
170
- }
171
- if (listenerData.focus || listenerData.descriptor) {
172
- try {
173
- Object.defineProperty(
174
- win.HTMLElement.prototype,
175
- "focus",
176
- listenerData.descriptor ?? { configurable: true, writable: true, value: listenerData.focus }
177
- );
178
- } catch {
179
- }
180
- }
181
- doc.removeEventListener("keydown", handleKeyboardEvent, true);
182
- doc.removeEventListener("keyup", handleKeyboardEvent, true);
183
- doc.removeEventListener("click", handleClickEvent, true);
184
- win.removeEventListener("focus", handleFocusEvent, true);
185
- win.removeEventListener("blur", handleWindowBlur, false);
186
- if (typeof win.PointerEvent !== "undefined") {
187
- doc.removeEventListener("pointerdown", handlePointerEvent, true);
188
- doc.removeEventListener("pointermove", handlePointerEvent, true);
189
- doc.removeEventListener("pointerup", handlePointerEvent, true);
190
- } else {
191
- doc.removeEventListener("mousedown", handlePointerEvent, true);
192
- doc.removeEventListener("mousemove", handlePointerEvent, true);
193
- doc.removeEventListener("mouseup", handlePointerEvent, true);
194
- }
195
- 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);
196
203
  };
197
204
  function getInteractionModality() {
198
- return currentModality;
205
+ return currentModality;
199
206
  }
200
207
  function setInteractionModality(modality) {
201
- currentModality = modality;
202
- triggerChangeHandlers(modality, null);
208
+ currentModality = modality;
209
+ triggerChangeHandlers(modality, null);
203
210
  }
204
211
  function trackInteractionModality(props) {
205
- const { onChange, root } = props;
206
- setupGlobalFocusEvents(root);
207
- onChange({ modality: currentModality });
208
- const handler = () => onChange({ modality: currentModality });
209
- changeHandlers.add(handler);
210
- return () => {
211
- changeHandlers.delete(handler);
212
- };
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
+ };
213
220
  }
214
221
  function isFocusVisible() {
215
- return currentModality === "keyboard" || currentModality === "virtual";
222
+ return currentModality === "keyboard" || currentModality === "virtual";
216
223
  }
217
224
  function trackFocusVisible(props = {}) {
218
- const { isTextInput, autoFocus, onChange, root } = props;
219
- setupGlobalFocusEvents(root);
220
- onChange?.({ isFocusVisible: autoFocus || isFocusVisible(), modality: currentModality });
221
- const handler = (modality, e) => {
222
- if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
223
- onChange?.({ isFocusVisible: isFocusVisible(), modality });
224
- };
225
- changeHandlers.add(handler);
226
- return () => {
227
- changeHandlers.delete(handler);
228
- };
229
- }
230
- export {
231
- getInteractionModality,
232
- ignoreFocusEvent,
233
- isFocusVisible,
234
- listenerMap,
235
- setInteractionModality,
236
- trackFocusVisible,
237
- trackInteractionModality
238
- };
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.47.0",
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.47.0"
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": "tsup",
52
+ "build": "tsdown",
53
53
  "test": "vitest",
54
- "lint": "eslint src",
54
+ "lint": "oxlint src",
55
55
  "test-ci": "pnpm test --ci --runInBand -u",
56
56
  "test-watch": "pnpm test --watchAll"
57
57
  }