@zag-js/focus-visible 0.1.3 → 0.1.5
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 +2 -30
- package/package.json +4 -4
- package/dist/index.mjs +0 -115
package/dist/index.js
CHANGED
|
@@ -1,31 +1,4 @@
|
|
|
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
1
|
// 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);
|
|
29
2
|
var hasSetup = false;
|
|
30
3
|
var modality = null;
|
|
31
4
|
var hasEventBeforeFocus = false;
|
|
@@ -134,10 +107,9 @@ function setInteractionModality(value) {
|
|
|
134
107
|
function getInteractionModality() {
|
|
135
108
|
return modality;
|
|
136
109
|
}
|
|
137
|
-
|
|
138
|
-
0 && (module.exports = {
|
|
110
|
+
export {
|
|
139
111
|
getInteractionModality,
|
|
140
112
|
setInteractionModality,
|
|
141
113
|
trackFocusVisible,
|
|
142
114
|
trackInteractionModality
|
|
143
|
-
}
|
|
115
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
+
"type": "module",
|
|
2
3
|
"name": "@zag-js/focus-visible",
|
|
3
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.5",
|
|
4
5
|
"description": "Focus visible polyfill utility based on WICG",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"js",
|
|
@@ -12,7 +13,6 @@
|
|
|
12
13
|
"homepage": "https://github.com/chakra-ui/zag#readme",
|
|
13
14
|
"license": "MIT",
|
|
14
15
|
"main": "dist/index.js",
|
|
15
|
-
"module": "dist/index.mjs",
|
|
16
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,
|
|
@@ -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",
|
|
30
30
|
"start": "pnpm build --watch",
|
|
31
|
-
"build": "tsup src/index.ts --format=esm
|
|
31
|
+
"build": "tsup src/index.ts --format=esm --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",
|
package/dist/index.mjs
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
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
|
-
};
|