@zag-js/focus-visible 2.0.0-next.2 → 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,247 +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 (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;
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;
124
117
  }
125
118
  function handleWindowBlur() {
126
- if (ignoreFocusEvent) return;
127
- hasEventBeforeFocus = false;
128
- pendingLabelControl = null;
129
- hasBlurredWindowRecently = true;
119
+ hasEventBeforeFocus = false;
120
+ pendingLabelControl = null;
121
+ hasBlurredWindowRecently = true;
130
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
+ */
131
161
  function setupGlobalFocusEvents(root) {
132
- if (typeof window === "undefined" || listenerMap.get((0, import_dom_query.getWindow)(root))) {
133
- return;
134
- }
135
- const win = (0, import_dom_query.getWindow)(root);
136
- const doc = (0, import_dom_query.getDocument)(root);
137
- let focus = win.HTMLElement.prototype.focus;
138
- function patchedFocus() {
139
- hasEventBeforeFocus = true;
140
- focus.apply(this, arguments);
141
- }
142
- try {
143
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
144
- configurable: true,
145
- value: patchedFocus
146
- });
147
- } catch {
148
- }
149
- doc.addEventListener("keydown", handleKeyboardEvent, true);
150
- doc.addEventListener("keyup", handleKeyboardEvent, true);
151
- doc.addEventListener("click", handleClickEvent, true);
152
- win.addEventListener("focus", handleFocusEvent, true);
153
- win.addEventListener("blur", handleWindowBlur, false);
154
- if (typeof win.PointerEvent !== "undefined") {
155
- doc.addEventListener("pointerdown", handlePointerEvent, true);
156
- doc.addEventListener("pointermove", handlePointerEvent, true);
157
- doc.addEventListener("pointerup", handlePointerEvent, true);
158
- } else {
159
- doc.addEventListener("mousedown", handlePointerEvent, true);
160
- doc.addEventListener("mousemove", handlePointerEvent, true);
161
- doc.addEventListener("mouseup", handlePointerEvent, true);
162
- }
163
- win.addEventListener(
164
- "beforeunload",
165
- () => {
166
- tearDownWindowFocusTracking(root);
167
- },
168
- { once: true }
169
- );
170
- listenerMap.set(win, { focus });
171
- }
172
- var tearDownWindowFocusTracking = (root, loadListener) => {
173
- const win = (0, import_dom_query.getWindow)(root);
174
- const doc = (0, import_dom_query.getDocument)(root);
175
- if (loadListener) {
176
- doc.removeEventListener("DOMContentLoaded", loadListener);
177
- }
178
- const listenerData = listenerMap.get(win);
179
- if (!listenerData) {
180
- return;
181
- }
182
- try {
183
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
184
- configurable: true,
185
- value: listenerData.focus
186
- });
187
- } catch {
188
- }
189
- doc.removeEventListener("keydown", handleKeyboardEvent, true);
190
- doc.removeEventListener("keyup", handleKeyboardEvent, true);
191
- doc.removeEventListener("click", handleClickEvent, true);
192
- win.removeEventListener("focus", handleFocusEvent, true);
193
- win.removeEventListener("blur", handleWindowBlur, false);
194
- if (typeof win.PointerEvent !== "undefined") {
195
- doc.removeEventListener("pointerdown", handlePointerEvent, true);
196
- doc.removeEventListener("pointermove", handlePointerEvent, true);
197
- doc.removeEventListener("pointerup", handlePointerEvent, true);
198
- } else {
199
- doc.removeEventListener("mousedown", handlePointerEvent, true);
200
- doc.removeEventListener("mousemove", handlePointerEvent, true);
201
- doc.removeEventListener("mouseup", handlePointerEvent, true);
202
- }
203
- 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);
204
213
  };
205
214
  function getInteractionModality() {
206
- return currentModality;
215
+ return currentModality;
207
216
  }
208
217
  function setInteractionModality(modality) {
209
- currentModality = modality;
210
- triggerChangeHandlers(modality, null);
218
+ currentModality = modality;
219
+ triggerChangeHandlers(modality, null);
211
220
  }
212
221
  function trackInteractionModality(props) {
213
- const { onChange, root } = props;
214
- setupGlobalFocusEvents(root);
215
- onChange({ modality: currentModality });
216
- const handler = () => onChange({ modality: currentModality });
217
- changeHandlers.add(handler);
218
- return () => {
219
- changeHandlers.delete(handler);
220
- };
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;
221
234
  }
222
235
  function isFocusVisible() {
223
- return currentModality === "keyboard" || currentModality === "virtual";
236
+ return currentModality === "keyboard" || currentModality === "virtual";
224
237
  }
225
238
  function trackFocusVisible(props = {}) {
226
- const { isTextInput, autoFocus, onChange, root } = props;
227
- setupGlobalFocusEvents(root);
228
- onChange?.({ isFocusVisible: autoFocus || isFocusVisible(), modality: currentModality });
229
- const handler = (modality, e) => {
230
- if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
231
- onChange?.({ isFocusVisible: isFocusVisible(), modality });
232
- };
233
- changeHandlers.add(handler);
234
- return () => {
235
- changeHandlers.delete(handler);
236
- };
237
- }
238
- // Annotate the CommonJS export names for ESM import in node:
239
- 0 && (module.exports = {
240
- getInteractionModality,
241
- ignoreFocusEvent,
242
- isFocusVisible,
243
- listenerMap,
244
- setInteractionModality,
245
- trackFocusVisible,
246
- trackInteractionModality
247
- });
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,216 +1,253 @@
1
+ import { getActiveElement, getDocument, getEventTarget, getWindow, isDocument, isMac, isVirtualClick, isWindow } from "@zag-js/dom-query";
1
2
  // src/index.ts
2
- import { getActiveElement, getDocument, getEventTarget, getWindow, isMac, isVirtualClick } 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
+ */
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 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
40
56
  };
41
57
  function triggerChangeHandlers(modality, e) {
42
- for (let handler of changeHandlers) {
43
- handler(modality, e);
44
- }
58
+ for (let handler of changeHandlers) handler(modality, e);
45
59
  }
46
60
  function handleKeyboardEvent(e) {
47
- pendingLabelControl = null;
48
- hasEventBeforeFocus = true;
49
- if (isValidKey(e)) {
50
- currentModality = "keyboard";
51
- triggerChangeHandlers("keyboard", e);
52
- }
61
+ pendingLabelControl = null;
62
+ hasEventBeforeFocus = true;
63
+ if (isValidKey(e)) {
64
+ currentModality = "keyboard";
65
+ triggerChangeHandlers("keyboard", e);
66
+ }
53
67
  }
54
68
  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
- }
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
+ }
64
80
  }
65
81
  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;
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;
80
96
  }
81
97
  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;
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;
94
113
  }
95
114
  function handleWindowBlur() {
96
- if (ignoreFocusEvent) return;
97
- hasEventBeforeFocus = false;
98
- pendingLabelControl = null;
99
- 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
+ }
100
153
  }
154
+ /**
155
+ * Setup global event listeners to control when keyboard focus style should be visible.
156
+ */
101
157
  function setupGlobalFocusEvents(root) {
102
- if (typeof window === "undefined" || listenerMap.get(getWindow(root))) {
103
- return;
104
- }
105
- const win = getWindow(root);
106
- const doc = getDocument(root);
107
- let focus = win.HTMLElement.prototype.focus;
108
- function patchedFocus() {
109
- hasEventBeforeFocus = true;
110
- focus.apply(this, arguments);
111
- }
112
- try {
113
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
114
- configurable: true,
115
- value: patchedFocus
116
- });
117
- } catch {
118
- }
119
- doc.addEventListener("keydown", handleKeyboardEvent, true);
120
- doc.addEventListener("keyup", handleKeyboardEvent, true);
121
- doc.addEventListener("click", handleClickEvent, true);
122
- win.addEventListener("focus", handleFocusEvent, true);
123
- win.addEventListener("blur", handleWindowBlur, false);
124
- if (typeof win.PointerEvent !== "undefined") {
125
- doc.addEventListener("pointerdown", handlePointerEvent, true);
126
- doc.addEventListener("pointermove", handlePointerEvent, true);
127
- doc.addEventListener("pointerup", handlePointerEvent, true);
128
- } else {
129
- doc.addEventListener("mousedown", handlePointerEvent, true);
130
- doc.addEventListener("mousemove", handlePointerEvent, true);
131
- doc.addEventListener("mouseup", handlePointerEvent, true);
132
- }
133
- win.addEventListener(
134
- "beforeunload",
135
- () => {
136
- tearDownWindowFocusTracking(root);
137
- },
138
- { once: true }
139
- );
140
- listenerMap.set(win, { focus });
141
- }
142
- var tearDownWindowFocusTracking = (root, loadListener) => {
143
- const win = getWindow(root);
144
- const doc = getDocument(root);
145
- if (loadListener) {
146
- doc.removeEventListener("DOMContentLoaded", loadListener);
147
- }
148
- const listenerData = listenerMap.get(win);
149
- if (!listenerData) {
150
- return;
151
- }
152
- try {
153
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
154
- configurable: true,
155
- value: listenerData.focus
156
- });
157
- } catch {
158
- }
159
- doc.removeEventListener("keydown", handleKeyboardEvent, true);
160
- doc.removeEventListener("keyup", handleKeyboardEvent, true);
161
- doc.removeEventListener("click", handleClickEvent, true);
162
- win.removeEventListener("focus", handleFocusEvent, true);
163
- win.removeEventListener("blur", handleWindowBlur, false);
164
- if (typeof win.PointerEvent !== "undefined") {
165
- doc.removeEventListener("pointerdown", handlePointerEvent, true);
166
- doc.removeEventListener("pointermove", handlePointerEvent, true);
167
- doc.removeEventListener("pointerup", handlePointerEvent, true);
168
- } else {
169
- doc.removeEventListener("mousedown", handlePointerEvent, true);
170
- doc.removeEventListener("mousemove", handlePointerEvent, true);
171
- doc.removeEventListener("mouseup", handlePointerEvent, true);
172
- }
173
- 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);
174
209
  };
175
210
  function getInteractionModality() {
176
- return currentModality;
211
+ return currentModality;
177
212
  }
178
213
  function setInteractionModality(modality) {
179
- currentModality = modality;
180
- triggerChangeHandlers(modality, null);
214
+ currentModality = modality;
215
+ triggerChangeHandlers(modality, null);
181
216
  }
182
217
  function trackInteractionModality(props) {
183
- const { onChange, root } = props;
184
- setupGlobalFocusEvents(root);
185
- onChange({ modality: currentModality });
186
- const handler = () => onChange({ modality: currentModality });
187
- changeHandlers.add(handler);
188
- return () => {
189
- changeHandlers.delete(handler);
190
- };
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;
191
230
  }
192
231
  function isFocusVisible() {
193
- return currentModality === "keyboard" || currentModality === "virtual";
232
+ return currentModality === "keyboard" || currentModality === "virtual";
194
233
  }
195
234
  function trackFocusVisible(props = {}) {
196
- const { isTextInput, autoFocus, onChange, root } = props;
197
- setupGlobalFocusEvents(root);
198
- onChange?.({ isFocusVisible: autoFocus || isFocusVisible(), modality: currentModality });
199
- const handler = (modality, e) => {
200
- if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
201
- onChange?.({ isFocusVisible: isFocusVisible(), modality });
202
- };
203
- changeHandlers.add(handler);
204
- return () => {
205
- changeHandlers.delete(handler);
206
- };
207
- }
208
- export {
209
- getInteractionModality,
210
- ignoreFocusEvent,
211
- isFocusVisible,
212
- listenerMap,
213
- setInteractionModality,
214
- trackFocusVisible,
215
- trackInteractionModality
216
- };
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.2",
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.2"
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",