@zag-js/focus-visible 1.33.1 → 1.34.1

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
@@ -4,6 +4,11 @@ interface GlobalListenerData {
4
4
  focus: VoidFunction;
5
5
  }
6
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;
7
12
  declare function getInteractionModality(): Modality | null;
8
13
  declare function setInteractionModality(modality: Modality): void;
9
14
  interface InteractionModalityChangeDetails {
@@ -36,4 +41,4 @@ interface FocusVisibleProps {
36
41
  }
37
42
  declare function trackFocusVisible(props?: FocusVisibleProps): VoidFunction;
38
43
 
39
- export { type FocusVisibleChangeDetails, type FocusVisibleProps, type InteractionModalityChangeDetails, type InteractionModalityProps, type Modality, getInteractionModality, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
44
+ export { type FocusVisibleChangeDetails, type FocusVisibleProps, type InteractionModalityChangeDetails, type InteractionModalityProps, type Modality, getInteractionModality, ignoreFocusEvent, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
package/dist/index.d.ts CHANGED
@@ -4,6 +4,11 @@ interface GlobalListenerData {
4
4
  focus: VoidFunction;
5
5
  }
6
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;
7
12
  declare function getInteractionModality(): Modality | null;
8
13
  declare function setInteractionModality(modality: Modality): void;
9
14
  interface InteractionModalityChangeDetails {
@@ -36,4 +41,4 @@ interface FocusVisibleProps {
36
41
  }
37
42
  declare function trackFocusVisible(props?: FocusVisibleProps): VoidFunction;
38
43
 
39
- export { type FocusVisibleChangeDetails, type FocusVisibleProps, type InteractionModalityChangeDetails, type InteractionModalityProps, type Modality, getInteractionModality, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
44
+ export { type FocusVisibleChangeDetails, type FocusVisibleProps, type InteractionModalityChangeDetails, type InteractionModalityProps, type Modality, getInteractionModality, ignoreFocusEvent, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
package/dist/index.js CHANGED
@@ -8,9 +8,11 @@ function isValidKey(e) {
8
8
  }
9
9
  var nonTextInputTypes = /* @__PURE__ */ new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"]);
10
10
  function isKeyboardFocusEvent(isTextInput, modality, e) {
11
- const target = e ? domQuery.getEventTarget(e) : null;
12
- const win = domQuery.getWindow(target);
13
- isTextInput = isTextInput || target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target?.type) || target instanceof win.HTMLTextAreaElement || target instanceof win.HTMLElement && target.isContentEditable;
11
+ const eventTarget = e ? domQuery.getEventTarget(e) : null;
12
+ const doc = domQuery.getDocument(eventTarget);
13
+ const win = domQuery.getWindow(eventTarget);
14
+ const activeElement = domQuery.getActiveElement(doc);
15
+ isTextInput = isTextInput || activeElement instanceof win.HTMLInputElement && !nonTextInputTypes.has(activeElement?.type) || activeElement instanceof win.HTMLTextAreaElement || activeElement instanceof win.HTMLElement && activeElement.isContentEditable;
14
16
  return !(isTextInput && modality === "keyboard" && e instanceof win.KeyboardEvent && !Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key));
15
17
  }
16
18
  var currentModality = null;
@@ -18,6 +20,7 @@ var changeHandlers = /* @__PURE__ */ new Set();
18
20
  var listenerMap = /* @__PURE__ */ new Map();
19
21
  var hasEventBeforeFocus = false;
20
22
  var hasBlurredWindowRecently = false;
23
+ var ignoreFocusEvent = false;
21
24
  var FOCUS_VISIBLE_INPUT_KEYS = {
22
25
  Tab: true,
23
26
  Escape: true
@@ -49,7 +52,7 @@ function handleClickEvent(e) {
49
52
  }
50
53
  function handleFocusEvent(e) {
51
54
  const target = domQuery.getEventTarget(e);
52
- if (target === domQuery.getWindow(target) || target === domQuery.getDocument(target)) {
55
+ if (target === domQuery.getWindow(target) || target === domQuery.getDocument(target) || ignoreFocusEvent || !e.isTrusted) {
53
56
  return;
54
57
  }
55
58
  if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
@@ -71,8 +74,6 @@ function setupGlobalFocusEvents(root) {
71
74
  const doc = domQuery.getDocument(root);
72
75
  let focus = win.HTMLElement.prototype.focus;
73
76
  function patchedFocus() {
74
- currentModality = "virtual";
75
- triggerChangeHandlers("virtual", null);
76
77
  hasEventBeforeFocus = true;
77
78
  focus.apply(this, arguments);
78
79
  }
@@ -154,7 +155,7 @@ function trackInteractionModality(props) {
154
155
  };
155
156
  }
156
157
  function isFocusVisible() {
157
- return currentModality === "keyboard";
158
+ return currentModality === "keyboard" || currentModality === "virtual";
158
159
  }
159
160
  function trackFocusVisible(props = {}) {
160
161
  const { isTextInput, autoFocus, onChange, root } = props;
@@ -171,6 +172,7 @@ function trackFocusVisible(props = {}) {
171
172
  }
172
173
 
173
174
  exports.getInteractionModality = getInteractionModality;
175
+ exports.ignoreFocusEvent = ignoreFocusEvent;
174
176
  exports.isFocusVisible = isFocusVisible;
175
177
  exports.listenerMap = listenerMap;
176
178
  exports.setInteractionModality = setInteractionModality;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { getWindow, getDocument, getEventTarget, isVirtualClick, isMac } from '@zag-js/dom-query';
1
+ import { getWindow, getDocument, getEventTarget, getActiveElement, isVirtualClick, isMac } from '@zag-js/dom-query';
2
2
 
3
3
  // src/index.ts
4
4
  function isValidKey(e) {
@@ -6,9 +6,11 @@ function isValidKey(e) {
6
6
  }
7
7
  var nonTextInputTypes = /* @__PURE__ */ new Set(["checkbox", "radio", "range", "color", "file", "image", "button", "submit", "reset"]);
8
8
  function isKeyboardFocusEvent(isTextInput, modality, e) {
9
- const target = e ? getEventTarget(e) : null;
10
- const win = getWindow(target);
11
- isTextInput = isTextInput || target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target?.type) || target instanceof win.HTMLTextAreaElement || target instanceof win.HTMLElement && target.isContentEditable;
9
+ const eventTarget = e ? getEventTarget(e) : null;
10
+ const doc = getDocument(eventTarget);
11
+ const win = getWindow(eventTarget);
12
+ const activeElement = getActiveElement(doc);
13
+ isTextInput = isTextInput || activeElement instanceof win.HTMLInputElement && !nonTextInputTypes.has(activeElement?.type) || activeElement instanceof win.HTMLTextAreaElement || activeElement instanceof win.HTMLElement && activeElement.isContentEditable;
12
14
  return !(isTextInput && modality === "keyboard" && e instanceof win.KeyboardEvent && !Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key));
13
15
  }
14
16
  var currentModality = null;
@@ -16,6 +18,7 @@ var changeHandlers = /* @__PURE__ */ new Set();
16
18
  var listenerMap = /* @__PURE__ */ new Map();
17
19
  var hasEventBeforeFocus = false;
18
20
  var hasBlurredWindowRecently = false;
21
+ var ignoreFocusEvent = false;
19
22
  var FOCUS_VISIBLE_INPUT_KEYS = {
20
23
  Tab: true,
21
24
  Escape: true
@@ -47,7 +50,7 @@ function handleClickEvent(e) {
47
50
  }
48
51
  function handleFocusEvent(e) {
49
52
  const target = getEventTarget(e);
50
- if (target === getWindow(target) || target === getDocument(target)) {
53
+ if (target === getWindow(target) || target === getDocument(target) || ignoreFocusEvent || !e.isTrusted) {
51
54
  return;
52
55
  }
53
56
  if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
@@ -69,8 +72,6 @@ function setupGlobalFocusEvents(root) {
69
72
  const doc = getDocument(root);
70
73
  let focus = win.HTMLElement.prototype.focus;
71
74
  function patchedFocus() {
72
- currentModality = "virtual";
73
- triggerChangeHandlers("virtual", null);
74
75
  hasEventBeforeFocus = true;
75
76
  focus.apply(this, arguments);
76
77
  }
@@ -152,7 +153,7 @@ function trackInteractionModality(props) {
152
153
  };
153
154
  }
154
155
  function isFocusVisible() {
155
- return currentModality === "keyboard";
156
+ return currentModality === "keyboard" || currentModality === "virtual";
156
157
  }
157
158
  function trackFocusVisible(props = {}) {
158
159
  const { isTextInput, autoFocus, onChange, root } = props;
@@ -168,4 +169,4 @@ function trackFocusVisible(props = {}) {
168
169
  };
169
170
  }
170
171
 
171
- export { getInteractionModality, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
172
+ export { getInteractionModality, ignoreFocusEvent, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/focus-visible",
3
- "version": "1.33.1",
3
+ "version": "1.34.1",
4
4
  "description": "Focus visible polyfill utility based on WICG",
5
5
  "keywords": [
6
6
  "js",
@@ -25,7 +25,7 @@
25
25
  "clean-package": "../../../clean-package.config.json",
26
26
  "main": "dist/index.js",
27
27
  "dependencies": {
28
- "@zag-js/dom-query": "1.33.1"
28
+ "@zag-js/dom-query": "1.34.1"
29
29
  },
30
30
  "devDependencies": {
31
31
  "clean-package": "2.2.0"