@zag-js/focus-visible 0.1.5 → 0.1.6
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.js +30 -2
- package/dist/index.mjs +115 -0
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,4 +1,31 @@
|
|
|
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
|
+
|
|
1
20
|
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
getInteractionModality: () => getInteractionModality,
|
|
24
|
+
setInteractionModality: () => setInteractionModality,
|
|
25
|
+
trackFocusVisible: () => trackFocusVisible,
|
|
26
|
+
trackInteractionModality: () => trackInteractionModality
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
2
29
|
var hasSetup = false;
|
|
3
30
|
var modality = null;
|
|
4
31
|
var hasEventBeforeFocus = false;
|
|
@@ -107,9 +134,10 @@ function setInteractionModality(value) {
|
|
|
107
134
|
function getInteractionModality() {
|
|
108
135
|
return modality;
|
|
109
136
|
}
|
|
110
|
-
export
|
|
137
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
138
|
+
0 && (module.exports = {
|
|
111
139
|
getInteractionModality,
|
|
112
140
|
setInteractionModality,
|
|
113
141
|
trackFocusVisible,
|
|
114
142
|
trackInteractionModality
|
|
115
|
-
};
|
|
143
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var hasSetup = false;
|
|
3
|
+
var modality = null;
|
|
4
|
+
var hasEventBeforeFocus = false;
|
|
5
|
+
var hasBlurredWindowRecently = false;
|
|
6
|
+
var handlers = /* @__PURE__ */ new Set();
|
|
7
|
+
function trigger(modality2, event) {
|
|
8
|
+
handlers.forEach((handler) => handler(modality2, event));
|
|
9
|
+
}
|
|
10
|
+
var isMac = typeof window !== "undefined" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false;
|
|
11
|
+
function isValidKey(e) {
|
|
12
|
+
return !(e.metaKey || !isMac && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
|
|
13
|
+
}
|
|
14
|
+
function onKeyboardEvent(event) {
|
|
15
|
+
hasEventBeforeFocus = true;
|
|
16
|
+
if (isValidKey(event)) {
|
|
17
|
+
modality = "keyboard";
|
|
18
|
+
trigger("keyboard", event);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function onPointerEvent(event) {
|
|
22
|
+
modality = "pointer";
|
|
23
|
+
if (event.type === "mousedown" || event.type === "pointerdown") {
|
|
24
|
+
hasEventBeforeFocus = true;
|
|
25
|
+
const target = event.composedPath ? event.composedPath()[0] : event.target;
|
|
26
|
+
if (target.matches(":focus-visible"))
|
|
27
|
+
return;
|
|
28
|
+
trigger("pointer", event);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function isVirtualClick(event) {
|
|
32
|
+
if (event.mozInputSource === 0 && event.isTrusted)
|
|
33
|
+
return true;
|
|
34
|
+
return event.detail === 0 && !event.pointerType;
|
|
35
|
+
}
|
|
36
|
+
function onClickEvent(e) {
|
|
37
|
+
if (isVirtualClick(e)) {
|
|
38
|
+
hasEventBeforeFocus = true;
|
|
39
|
+
modality = "virtual";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function onWindowFocus(event) {
|
|
43
|
+
if (event.target === window || event.target === document) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
|
|
47
|
+
modality = "virtual";
|
|
48
|
+
trigger("virtual", event);
|
|
49
|
+
}
|
|
50
|
+
hasEventBeforeFocus = false;
|
|
51
|
+
hasBlurredWindowRecently = false;
|
|
52
|
+
}
|
|
53
|
+
function onWindowBlur() {
|
|
54
|
+
hasEventBeforeFocus = false;
|
|
55
|
+
hasBlurredWindowRecently = true;
|
|
56
|
+
}
|
|
57
|
+
function isFocusVisible() {
|
|
58
|
+
return modality !== "pointer";
|
|
59
|
+
}
|
|
60
|
+
function setupGlobalFocusEvents() {
|
|
61
|
+
if (typeof window === "undefined" || hasSetup) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const { focus } = HTMLElement.prototype;
|
|
65
|
+
HTMLElement.prototype.focus = function focusElement(...args) {
|
|
66
|
+
hasEventBeforeFocus = true;
|
|
67
|
+
focus.apply(this, args);
|
|
68
|
+
};
|
|
69
|
+
document.addEventListener("keydown", onKeyboardEvent, true);
|
|
70
|
+
document.addEventListener("keyup", onKeyboardEvent, true);
|
|
71
|
+
document.addEventListener("click", onClickEvent, true);
|
|
72
|
+
window.addEventListener("focus", onWindowFocus, true);
|
|
73
|
+
window.addEventListener("blur", onWindowBlur, false);
|
|
74
|
+
if (typeof PointerEvent !== "undefined") {
|
|
75
|
+
document.addEventListener("pointerdown", onPointerEvent, true);
|
|
76
|
+
document.addEventListener("pointermove", onPointerEvent, true);
|
|
77
|
+
document.addEventListener("pointerup", onPointerEvent, true);
|
|
78
|
+
} else {
|
|
79
|
+
document.addEventListener("mousedown", onPointerEvent, true);
|
|
80
|
+
document.addEventListener("mousemove", onPointerEvent, true);
|
|
81
|
+
document.addEventListener("mouseup", onPointerEvent, true);
|
|
82
|
+
}
|
|
83
|
+
hasSetup = true;
|
|
84
|
+
}
|
|
85
|
+
function trackFocusVisible(fn) {
|
|
86
|
+
setupGlobalFocusEvents();
|
|
87
|
+
fn(isFocusVisible());
|
|
88
|
+
const handler = () => fn(isFocusVisible());
|
|
89
|
+
handlers.add(handler);
|
|
90
|
+
return () => {
|
|
91
|
+
handlers.delete(handler);
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function trackInteractionModality(fn) {
|
|
95
|
+
setupGlobalFocusEvents();
|
|
96
|
+
fn(modality);
|
|
97
|
+
const handler = () => fn(modality);
|
|
98
|
+
handlers.add(handler);
|
|
99
|
+
return () => {
|
|
100
|
+
handlers.delete(handler);
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function setInteractionModality(value) {
|
|
104
|
+
modality = value;
|
|
105
|
+
trigger(value, null);
|
|
106
|
+
}
|
|
107
|
+
function getInteractionModality() {
|
|
108
|
+
return modality;
|
|
109
|
+
}
|
|
110
|
+
export {
|
|
111
|
+
getInteractionModality,
|
|
112
|
+
setInteractionModality,
|
|
113
|
+
trackFocusVisible,
|
|
114
|
+
trackInteractionModality
|
|
115
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"type": "module",
|
|
3
2
|
"name": "@zag-js/focus-visible",
|
|
4
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
5
4
|
"description": "Focus visible polyfill utility based on WICG",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
6
8
|
"keywords": [
|
|
7
9
|
"js",
|
|
8
10
|
"utils",
|
|
@@ -12,8 +14,6 @@
|
|
|
12
14
|
"author": "Segun Adebayo <sage@adebayosegun.com>",
|
|
13
15
|
"homepage": "https://github.com/chakra-ui/zag#readme",
|
|
14
16
|
"license": "MIT",
|
|
15
|
-
"main": "dist/index.js",
|
|
16
|
-
"types": "dist/index.d.ts",
|
|
17
17
|
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/focus-visible",
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"files": [
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
|
-
"build-fast": "tsup src/index.ts --format=esm",
|
|
29
|
+
"build-fast": "tsup src/index.ts --format=esm,cjs",
|
|
30
30
|
"start": "pnpm build --watch",
|
|
31
|
-
"build": "tsup src/index.ts --format=esm --dts",
|
|
31
|
+
"build": "tsup src/index.ts --format=esm,cjs --dts",
|
|
32
32
|
"test": "jest --config ../../../jest.config.js --rootDir tests",
|
|
33
33
|
"lint": "eslint src --ext .ts,.tsx",
|
|
34
34
|
"test-ci": "pnpm test --ci --runInBand -u",
|