@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 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
- focus: VoidFunction;
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
- * When true, the next focus event will be ignored. Used by preventFocus() to avoid
9
- * focus rings when programmatically reverting focus.
10
- */
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;
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
- /** 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
+ /** 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
- /** 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;
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
- focus: VoidFunction;
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
- * When true, the next focus event will be ignored. Used by preventFocus() to avoid
9
- * focus rings when programmatically reverting focus.
10
- */
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;
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
- /** 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
+ /** 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
- /** 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;
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
- "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 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
- for (let handler of changeHandlers) {
73
- handler(modality, e);
74
- }
62
+ for (let handler of changeHandlers) handler(modality, e);
75
63
  }
76
64
  function handleKeyboardEvent(e) {
77
- pendingLabelControl = null;
78
- hasEventBeforeFocus = true;
79
- if (isValidKey(e)) {
80
- currentModality = "keyboard";
81
- triggerChangeHandlers("keyboard", e);
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
- 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
- }
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
- 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;
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
- const target = (0, import_dom_query.getEventTarget)(e);
113
- const isLabelActivationFocus = target === pendingLabelControl;
114
- if (ignoreFocusEvent) return;
115
- if ((0, import_dom_query.isWindow)(target)) {
116
- hasBlurredWindowRecently = true;
117
- return;
118
- }
119
- if ((0, import_dom_query.isDocument)(target) || !e.isTrusted) {
120
- return;
121
- }
122
- if (!hasEventBeforeFocus && !isLabelActivationFocus && !hasBlurredWindowRecently) {
123
- currentModality = "virtual";
124
- triggerChangeHandlers("virtual", e);
125
- }
126
- hasEventBeforeFocus = false;
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
- if (ignoreFocusEvent) return;
132
- hasEventBeforeFocus = false;
133
- pendingLabelControl = null;
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
- if (typeof window === "undefined" || listenerMap.get((0, import_dom_query.getWindow)(root))) {
138
- return;
139
- }
140
- const win = (0, import_dom_query.getWindow)(root);
141
- const doc = (0, import_dom_query.getDocument)(root);
142
- let focus = win.HTMLElement.prototype.focus;
143
- function patchedFocus() {
144
- hasEventBeforeFocus = true;
145
- focus.apply(this, arguments);
146
- }
147
- try {
148
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
149
- configurable: true,
150
- value: patchedFocus
151
- });
152
- } catch {
153
- }
154
- doc.addEventListener("keydown", handleKeyboardEvent, true);
155
- doc.addEventListener("keyup", handleKeyboardEvent, true);
156
- doc.addEventListener("click", handleClickEvent, true);
157
- win.addEventListener("focus", handleFocusEvent, true);
158
- win.addEventListener("blur", handleWindowBlur, false);
159
- if (typeof win.PointerEvent !== "undefined") {
160
- doc.addEventListener("pointerdown", handlePointerEvent, true);
161
- doc.addEventListener("pointermove", handlePointerEvent, true);
162
- doc.addEventListener("pointerup", handlePointerEvent, true);
163
- } else {
164
- doc.addEventListener("mousedown", handlePointerEvent, true);
165
- doc.addEventListener("mousemove", handlePointerEvent, true);
166
- doc.addEventListener("mouseup", handlePointerEvent, true);
167
- }
168
- win.addEventListener(
169
- "beforeunload",
170
- () => {
171
- tearDownWindowFocusTracking(root);
172
- },
173
- { once: true }
174
- );
175
- listenerMap.set(win, { focus });
176
- }
177
- var tearDownWindowFocusTracking = (root, loadListener) => {
178
- const win = (0, import_dom_query.getWindow)(root);
179
- const doc = (0, import_dom_query.getDocument)(root);
180
- if (loadListener) {
181
- doc.removeEventListener("DOMContentLoaded", loadListener);
182
- }
183
- const listenerData = listenerMap.get(win);
184
- if (!listenerData) {
185
- return;
186
- }
187
- try {
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
- return currentModality;
215
+ return currentModality;
212
216
  }
213
217
  function setInteractionModality(modality) {
214
- currentModality = modality;
215
- triggerChangeHandlers(modality, null);
218
+ currentModality = modality;
219
+ triggerChangeHandlers(modality, null);
216
220
  }
217
221
  function trackInteractionModality(props) {
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
- };
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
- return currentModality === "keyboard" || currentModality === "virtual";
236
+ return currentModality === "keyboard" || currentModality === "virtual";
229
237
  }
230
238
  function trackFocusVisible(props = {}) {
231
- const { isTextInput, autoFocus, onChange, root } = props;
232
- setupGlobalFocusEvents(root);
233
- onChange?.({ isFocusVisible: autoFocus || isFocusVisible(), modality: currentModality });
234
- const handler = (modality, e) => {
235
- if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
236
- onChange?.({ isFocusVisible: isFocusVisible(), modality });
237
- };
238
- changeHandlers.add(handler);
239
- return () => {
240
- changeHandlers.delete(handler);
241
- };
242
- }
243
- // Annotate the CommonJS export names for ESM import in node:
244
- 0 && (module.exports = {
245
- getInteractionModality,
246
- ignoreFocusEvent,
247
- isFocusVisible,
248
- listenerMap,
249
- setInteractionModality,
250
- trackFocusVisible,
251
- trackInteractionModality
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
- import {
3
- getActiveElement,
4
- getDocument,
5
- getEventTarget,
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
- return !(e.metaKey || !isMac() && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
14
- }
15
- var nonTextInputTypes = /* @__PURE__ */ new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"]);
16
- var interactiveContentSelector = [
17
- "a[href]",
18
- "audio[controls]",
19
- "button",
20
- "details",
21
- "embed",
22
- "iframe",
23
- "img[controls]",
24
- "img[usemap]",
25
- "input",
26
- "select",
27
- "textarea",
28
- "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]"
29
34
  ].join(",");
30
35
  function isKeyboardFocusEvent(isTextInput, modality, e) {
31
- const eventTarget = e ? getEventTarget(e) : null;
32
- const doc = getDocument(eventTarget);
33
- const win = getWindow(eventTarget);
34
- const activeElement = getActiveElement(doc);
35
- isTextInput = isTextInput || activeElement instanceof win.HTMLInputElement && !nonTextInputTypes.has(activeElement?.type) || activeElement instanceof win.HTMLTextAreaElement || activeElement instanceof win.HTMLElement && activeElement.isContentEditable;
36
- return !(isTextInput && modality === "keyboard" && e instanceof win.KeyboardEvent && !Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key));
37
- }
38
- var currentModality = null;
39
- var changeHandlers = /* @__PURE__ */ new Set();
40
- var listenerMap = /* @__PURE__ */ new Map();
41
- var hasEventBeforeFocus = false;
42
- var pendingLabelControl = null;
43
- var hasBlurredWindowRecently = false;
44
- var lastPointerPosition = null;
45
- var ignoreFocusEvent = false;
46
- var FOCUS_VISIBLE_INPUT_KEYS = {
47
- Tab: true,
48
- 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 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
- for (let handler of changeHandlers) {
52
- handler(modality, e);
53
- }
58
+ for (let handler of changeHandlers) handler(modality, e);
54
59
  }
55
60
  function handleKeyboardEvent(e) {
56
- pendingLabelControl = null;
57
- hasEventBeforeFocus = true;
58
- if (isValidKey(e)) {
59
- currentModality = "keyboard";
60
- triggerChangeHandlers("keyboard", e);
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
- const isMove = e.type === "pointermove" || e.type === "mousemove";
65
- if (isMove && e.isTrusted && lastPointerPosition?.x === e.clientX && lastPointerPosition?.y === e.clientY) return;
66
- lastPointerPosition = { x: e.clientX, y: e.clientY };
67
- currentModality = "pointer";
68
- if (e.type === "mousedown" || e.type === "pointerdown") {
69
- pendingLabelControl = null;
70
- hasEventBeforeFocus = true;
71
- triggerChangeHandlers("pointer", e);
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
- if (isVirtualClick(e)) {
76
- pendingLabelControl = null;
77
- hasEventBeforeFocus = true;
78
- currentModality = "virtual";
79
- return;
80
- }
81
- pendingLabelControl = null;
82
- const target = getEventTarget(e);
83
- const label = target?.closest("label");
84
- const control = label?.control;
85
- if (!label || !control || control.matches(":disabled")) return;
86
- const interactive = target?.closest(interactiveContentSelector);
87
- if (interactive && label.contains(interactive) && interactive !== control) return;
88
- if (control !== getActiveElement(label.ownerDocument)) pendingLabelControl = control;
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
- const target = getEventTarget(e);
92
- const isLabelActivationFocus = target === pendingLabelControl;
93
- if (ignoreFocusEvent) return;
94
- if (isWindow(target)) {
95
- hasBlurredWindowRecently = true;
96
- return;
97
- }
98
- if (isDocument(target) || !e.isTrusted) {
99
- return;
100
- }
101
- if (!hasEventBeforeFocus && !isLabelActivationFocus && !hasBlurredWindowRecently) {
102
- currentModality = "virtual";
103
- triggerChangeHandlers("virtual", e);
104
- }
105
- hasEventBeforeFocus = false;
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
- if (ignoreFocusEvent) return;
111
- hasEventBeforeFocus = false;
112
- pendingLabelControl = null;
113
- hasBlurredWindowRecently = true;
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
- if (typeof window === "undefined" || listenerMap.get(getWindow(root))) {
117
- return;
118
- }
119
- const win = getWindow(root);
120
- const doc = getDocument(root);
121
- let focus = win.HTMLElement.prototype.focus;
122
- function patchedFocus() {
123
- hasEventBeforeFocus = true;
124
- focus.apply(this, arguments);
125
- }
126
- try {
127
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
128
- configurable: true,
129
- value: patchedFocus
130
- });
131
- } catch {
132
- }
133
- doc.addEventListener("keydown", handleKeyboardEvent, true);
134
- doc.addEventListener("keyup", handleKeyboardEvent, true);
135
- doc.addEventListener("click", handleClickEvent, true);
136
- win.addEventListener("focus", handleFocusEvent, true);
137
- win.addEventListener("blur", handleWindowBlur, false);
138
- if (typeof win.PointerEvent !== "undefined") {
139
- doc.addEventListener("pointerdown", handlePointerEvent, true);
140
- doc.addEventListener("pointermove", handlePointerEvent, true);
141
- doc.addEventListener("pointerup", handlePointerEvent, true);
142
- } else {
143
- doc.addEventListener("mousedown", handlePointerEvent, true);
144
- doc.addEventListener("mousemove", handlePointerEvent, true);
145
- doc.addEventListener("mouseup", handlePointerEvent, true);
146
- }
147
- win.addEventListener(
148
- "beforeunload",
149
- () => {
150
- tearDownWindowFocusTracking(root);
151
- },
152
- { once: true }
153
- );
154
- listenerMap.set(win, { focus });
155
- }
156
- var tearDownWindowFocusTracking = (root, loadListener) => {
157
- const win = getWindow(root);
158
- const doc = getDocument(root);
159
- if (loadListener) {
160
- doc.removeEventListener("DOMContentLoaded", loadListener);
161
- }
162
- const listenerData = listenerMap.get(win);
163
- if (!listenerData) {
164
- return;
165
- }
166
- try {
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
- return currentModality;
211
+ return currentModality;
191
212
  }
192
213
  function setInteractionModality(modality) {
193
- currentModality = modality;
194
- triggerChangeHandlers(modality, null);
214
+ currentModality = modality;
215
+ triggerChangeHandlers(modality, null);
195
216
  }
196
217
  function trackInteractionModality(props) {
197
- const { onChange, root } = props;
198
- setupGlobalFocusEvents(root);
199
- onChange({ modality: currentModality });
200
- const handler = () => onChange({ modality: currentModality });
201
- changeHandlers.add(handler);
202
- return () => {
203
- changeHandlers.delete(handler);
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
- return currentModality === "keyboard" || currentModality === "virtual";
232
+ return currentModality === "keyboard" || currentModality === "virtual";
208
233
  }
209
234
  function trackFocusVisible(props = {}) {
210
- const { isTextInput, autoFocus, onChange, root } = props;
211
- setupGlobalFocusEvents(root);
212
- onChange?.({ isFocusVisible: autoFocus || isFocusVisible(), modality: currentModality });
213
- const handler = (modality, e) => {
214
- if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
215
- onChange?.({ isFocusVisible: isFocusVisible(), modality });
216
- };
217
- changeHandlers.add(handler);
218
- return () => {
219
- changeHandlers.delete(handler);
220
- };
221
- }
222
- export {
223
- getInteractionModality,
224
- ignoreFocusEvent,
225
- isFocusVisible,
226
- listenerMap,
227
- setInteractionModality,
228
- trackFocusVisible,
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",
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.3"
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": "tsup",
52
+ "build": "tsdown",
53
53
  "test": "vitest",
54
54
  "lint": "oxlint src",
55
55
  "test-ci": "pnpm test --ci --runInBand -u",