@zag-js/focus-visible 0.0.0-dev-20220616113129 → 0.0.0-dev-20220626181040

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.ts CHANGED
@@ -1,3 +1,7 @@
1
+ declare type Modality = "keyboard" | "pointer" | "virtual";
1
2
  declare type FocusVisibleCallback = (isFocusVisible: boolean) => void;
2
3
  export declare function trackFocusVisible(fn: FocusVisibleCallback): () => void;
4
+ export declare function trackInteractionModality(fn: (value: Modality | null) => void): () => void;
5
+ export declare function setInteractionModality(value: Modality): void;
6
+ export declare function getInteractionModality(): Modality;
3
7
  export {};
package/dist/index.js CHANGED
@@ -20,20 +20,24 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- trackFocusVisible: () => trackFocusVisible
23
+ getInteractionModality: () => getInteractionModality,
24
+ setInteractionModality: () => setInteractionModality,
25
+ trackFocusVisible: () => trackFocusVisible,
26
+ trackInteractionModality: () => trackInteractionModality
24
27
  });
25
28
  module.exports = __toCommonJS(src_exports);
26
29
  var hasSetup = false;
27
30
  var modality = null;
28
31
  var hasEventBeforeFocus = false;
32
+ var hasBlurredWindowRecently = false;
29
33
  var handlers = /* @__PURE__ */ new Set();
30
- var isMac = typeof window !== "undefined" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false;
31
- function isValidKey(event) {
32
- return !(event.metaKey || !isMac && event.altKey || event.ctrlKey);
33
- }
34
34
  function trigger(modality2, event) {
35
35
  handlers.forEach((handler) => handler(modality2, event));
36
36
  }
37
+ var isMac = typeof window !== "undefined" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false;
38
+ function isValidKey(e) {
39
+ return !(e.metaKey || !isMac && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
40
+ }
37
41
  function onKeyboardEvent(event) {
38
42
  hasEventBeforeFocus = true;
39
43
  if (isValidKey(event)) {
@@ -51,21 +55,34 @@ function onPointerEvent(event) {
51
55
  trigger("pointer", event);
52
56
  }
53
57
  }
58
+ function isVirtualClick(event) {
59
+ if (event.mozInputSource === 0 && event.isTrusted)
60
+ return true;
61
+ return event.detail === 0 && !event.pointerType;
62
+ }
63
+ function onClickEvent(e) {
64
+ if (isVirtualClick(e)) {
65
+ hasEventBeforeFocus = true;
66
+ modality = "virtual";
67
+ }
68
+ }
54
69
  function onWindowFocus(event) {
55
70
  if (event.target === window || event.target === document) {
56
71
  return;
57
72
  }
58
- if (!hasEventBeforeFocus) {
59
- modality = "keyboard";
60
- trigger("keyboard", event);
73
+ if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
74
+ modality = "virtual";
75
+ trigger("virtual", event);
61
76
  }
62
77
  hasEventBeforeFocus = false;
78
+ hasBlurredWindowRecently = false;
63
79
  }
64
80
  function onWindowBlur() {
65
81
  hasEventBeforeFocus = false;
82
+ hasBlurredWindowRecently = true;
66
83
  }
67
84
  function isFocusVisible() {
68
- return modality === "keyboard";
85
+ return modality !== "pointer";
69
86
  }
70
87
  function setupGlobalFocusEvents() {
71
88
  if (typeof window === "undefined" || hasSetup) {
@@ -78,6 +95,7 @@ function setupGlobalFocusEvents() {
78
95
  };
79
96
  document.addEventListener("keydown", onKeyboardEvent, true);
80
97
  document.addEventListener("keyup", onKeyboardEvent, true);
98
+ document.addEventListener("click", onClickEvent, true);
81
99
  window.addEventListener("focus", onWindowFocus, true);
82
100
  window.addEventListener("blur", onWindowBlur, false);
83
101
  if (typeof PointerEvent !== "undefined") {
@@ -100,3 +118,19 @@ function trackFocusVisible(fn) {
100
118
  handlers.delete(handler);
101
119
  };
102
120
  }
121
+ function trackInteractionModality(fn) {
122
+ setupGlobalFocusEvents();
123
+ fn(modality);
124
+ const handler = () => fn(modality);
125
+ handlers.add(handler);
126
+ return () => {
127
+ handlers.delete(handler);
128
+ };
129
+ }
130
+ function setInteractionModality(value) {
131
+ modality = value;
132
+ trigger(value, null);
133
+ }
134
+ function getInteractionModality() {
135
+ return modality;
136
+ }
package/dist/index.mjs CHANGED
@@ -4,14 +4,15 @@
4
4
  var hasSetup = false;
5
5
  var modality = null;
6
6
  var hasEventBeforeFocus = false;
7
+ var hasBlurredWindowRecently = false;
7
8
  var handlers = /* @__PURE__ */ new Set();
8
- var isMac = typeof window !== "undefined" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false;
9
- function isValidKey(event) {
10
- return !(event.metaKey || !isMac && event.altKey || event.ctrlKey);
11
- }
12
9
  function trigger(modality2, event) {
13
10
  handlers.forEach((handler) => handler(modality2, event));
14
11
  }
12
+ var isMac = typeof window !== "undefined" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false;
13
+ function isValidKey(e) {
14
+ return !(e.metaKey || !isMac && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
15
+ }
15
16
  function onKeyboardEvent(event) {
16
17
  hasEventBeforeFocus = true;
17
18
  if (isValidKey(event)) {
@@ -29,21 +30,34 @@ function onPointerEvent(event) {
29
30
  trigger("pointer", event);
30
31
  }
31
32
  }
33
+ function isVirtualClick(event) {
34
+ if (event.mozInputSource === 0 && event.isTrusted)
35
+ return true;
36
+ return event.detail === 0 && !event.pointerType;
37
+ }
38
+ function onClickEvent(e) {
39
+ if (isVirtualClick(e)) {
40
+ hasEventBeforeFocus = true;
41
+ modality = "virtual";
42
+ }
43
+ }
32
44
  function onWindowFocus(event) {
33
45
  if (event.target === window || event.target === document) {
34
46
  return;
35
47
  }
36
- if (!hasEventBeforeFocus) {
37
- modality = "keyboard";
38
- trigger("keyboard", event);
48
+ if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
49
+ modality = "virtual";
50
+ trigger("virtual", event);
39
51
  }
40
52
  hasEventBeforeFocus = false;
53
+ hasBlurredWindowRecently = false;
41
54
  }
42
55
  function onWindowBlur() {
43
56
  hasEventBeforeFocus = false;
57
+ hasBlurredWindowRecently = true;
44
58
  }
45
59
  function isFocusVisible() {
46
- return modality === "keyboard";
60
+ return modality !== "pointer";
47
61
  }
48
62
  function setupGlobalFocusEvents() {
49
63
  if (typeof window === "undefined" || hasSetup) {
@@ -56,6 +70,7 @@ function setupGlobalFocusEvents() {
56
70
  };
57
71
  document.addEventListener("keydown", onKeyboardEvent, true);
58
72
  document.addEventListener("keyup", onKeyboardEvent, true);
73
+ document.addEventListener("click", onClickEvent, true);
59
74
  window.addEventListener("focus", onWindowFocus, true);
60
75
  window.addEventListener("blur", onWindowBlur, false);
61
76
  if (typeof PointerEvent !== "undefined") {
@@ -78,6 +93,25 @@ function trackFocusVisible(fn) {
78
93
  handlers.delete(handler);
79
94
  };
80
95
  }
96
+ function trackInteractionModality(fn) {
97
+ setupGlobalFocusEvents();
98
+ fn(modality);
99
+ const handler = () => fn(modality);
100
+ handlers.add(handler);
101
+ return () => {
102
+ handlers.delete(handler);
103
+ };
104
+ }
105
+ function setInteractionModality(value) {
106
+ modality = value;
107
+ trigger(value, null);
108
+ }
109
+ function getInteractionModality() {
110
+ return modality;
111
+ }
81
112
  export {
82
- trackFocusVisible
113
+ getInteractionModality,
114
+ setInteractionModality,
115
+ trackFocusVisible,
116
+ trackInteractionModality
83
117
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/focus-visible",
3
- "version": "0.0.0-dev-20220616113129",
3
+ "version": "0.0.0-dev-20220626181040",
4
4
  "description": "Focus visible polyfill utility based on WICG",
5
5
  "keywords": [
6
6
  "js",