@destyler/focus-visible 0.0.7-beta.9

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-PRESENT Elone Hoo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,40 @@
1
+ //#region src/focus-visible.d.ts
2
+ type Modality = 'keyboard' | 'pointer' | 'virtual';
3
+ type RootNode = Document | ShadowRoot | Node;
4
+ interface GlobalListenerData {
5
+ focus: VoidFunction;
6
+ }
7
+ declare const listenerMap: Map<Window, GlobalListenerData>;
8
+ declare function getInteractionModality(): Modality | null;
9
+ declare function setInteractionModality(modality: Modality): void;
10
+ interface InteractionModalityChangeDetails {
11
+ /** The modality of the interaction that caused the focus to be visible. */
12
+ modality: Modality | null;
13
+ }
14
+ interface InteractionModalityProps {
15
+ /** The root element to track focus visibility for. */
16
+ root?: RootNode | undefined;
17
+ /** Callback to be called when the interaction modality changes. */
18
+ onChange: (details: InteractionModalityChangeDetails) => void;
19
+ }
20
+ declare function trackInteractionModality(props: InteractionModalityProps): VoidFunction;
21
+ declare function isFocusVisible(): boolean;
22
+ interface FocusVisibleChangeDetails {
23
+ /** Whether keyboard focus is visible globally. */
24
+ isFocusVisible: boolean;
25
+ /** The modality of the interaction that caused the focus to be visible. */
26
+ modality: Modality | null;
27
+ }
28
+ interface FocusVisibleProps {
29
+ /** The root element to track focus visibility for. */
30
+ root?: RootNode | undefined;
31
+ /** Whether the element is a text input. */
32
+ isTextInput?: boolean | undefined;
33
+ /** Whether the element will be auto focused. */
34
+ autoFocus?: boolean | undefined;
35
+ /** Callback to be called when the focus visibility changes. */
36
+ onChange?: ((details: FocusVisibleChangeDetails) => void) | undefined;
37
+ }
38
+ declare function trackFocusVisible(props?: FocusVisibleProps): VoidFunction;
39
+ //#endregion
40
+ export { FocusVisibleChangeDetails, FocusVisibleProps, InteractionModalityChangeDetails, InteractionModalityProps, Modality, getInteractionModality, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
package/dist/index.js ADDED
@@ -0,0 +1,170 @@
1
+ import { getDocument, getEventTarget, getWindow, isMac } from "@destyler/dom";
2
+
3
+ //#region src/focus-visible.ts
4
+ const FOCUS_VISIBLE_INPUT_KEYS = {
5
+ Tab: true,
6
+ Escape: true
7
+ };
8
+ function isVirtualClick(event) {
9
+ if (event.mozInputSource === 0 && event.isTrusted) return true;
10
+ return event.detail === 0 && !event.pointerType;
11
+ }
12
+ function isValidKey(e) {
13
+ return !(e.metaKey || !isMac() && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
14
+ }
15
+ const nonTextInputTypes = new Set([
16
+ "checkbox",
17
+ "radio",
18
+ "range",
19
+ "color",
20
+ "file",
21
+ "image",
22
+ "button",
23
+ "submit",
24
+ "reset"
25
+ ]);
26
+ function isKeyboardFocusEvent(isTextInput, modality, e) {
27
+ const target = e ? getEventTarget(e) : null;
28
+ const win = getWindow(target);
29
+ isTextInput = isTextInput || target instanceof win.HTMLInputElement && !nonTextInputTypes.has(target?.type) || target instanceof win.HTMLTextAreaElement || target instanceof win.HTMLElement && target.isContentEditable;
30
+ return !(isTextInput && modality === "keyboard" && e instanceof win.KeyboardEvent && !Reflect.has(FOCUS_VISIBLE_INPUT_KEYS, e.key));
31
+ }
32
+ let currentModality = null;
33
+ const changeHandlers = /* @__PURE__ */ new Set();
34
+ const listenerMap = /* @__PURE__ */ new Map();
35
+ let hasEventBeforeFocus = false;
36
+ let hasBlurredWindowRecently = false;
37
+ function triggerChangeHandlers(modality, e) {
38
+ for (const handler of changeHandlers) handler(modality, e);
39
+ }
40
+ function handleKeyboardEvent(e) {
41
+ hasEventBeforeFocus = true;
42
+ if (isValidKey(e)) {
43
+ currentModality = "keyboard";
44
+ triggerChangeHandlers("keyboard", e);
45
+ }
46
+ }
47
+ function handlePointerEvent(e) {
48
+ currentModality = "pointer";
49
+ if (e.type === "mousedown" || e.type === "pointerdown") {
50
+ hasEventBeforeFocus = true;
51
+ triggerChangeHandlers("pointer", e);
52
+ }
53
+ }
54
+ function handleClickEvent(e) {
55
+ if (isVirtualClick(e)) {
56
+ hasEventBeforeFocus = true;
57
+ currentModality = "virtual";
58
+ }
59
+ }
60
+ function handleFocusEvent(e) {
61
+ const target = getEventTarget(e);
62
+ if (target === getWindow(target) || target === getDocument(target)) return;
63
+ if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
64
+ currentModality = "virtual";
65
+ triggerChangeHandlers("virtual", e);
66
+ }
67
+ hasEventBeforeFocus = false;
68
+ hasBlurredWindowRecently = false;
69
+ }
70
+ function handleWindowBlur() {
71
+ hasEventBeforeFocus = false;
72
+ hasBlurredWindowRecently = true;
73
+ }
74
+ /**
75
+ * Setup global event listeners to control when keyboard focus style should be visible.
76
+ */
77
+ function setupGlobalFocusEvents(root) {
78
+ if (typeof window === "undefined" || listenerMap.get(getWindow(root))) return;
79
+ const win = getWindow(root);
80
+ const doc = getDocument(root);
81
+ const focus = win.HTMLElement.prototype.focus;
82
+ win.HTMLElement.prototype.focus = function() {
83
+ currentModality = "virtual";
84
+ triggerChangeHandlers("virtual", null);
85
+ hasEventBeforeFocus = true;
86
+ focus.apply(this, arguments);
87
+ };
88
+ doc.addEventListener("keydown", handleKeyboardEvent, true);
89
+ doc.addEventListener("keyup", handleKeyboardEvent, true);
90
+ doc.addEventListener("click", handleClickEvent, true);
91
+ win.addEventListener("focus", handleFocusEvent, true);
92
+ win.addEventListener("blur", handleWindowBlur, false);
93
+ if (typeof win.PointerEvent !== "undefined") {
94
+ doc.addEventListener("pointerdown", handlePointerEvent, true);
95
+ doc.addEventListener("pointermove", handlePointerEvent, true);
96
+ doc.addEventListener("pointerup", handlePointerEvent, true);
97
+ } else {
98
+ doc.addEventListener("mousedown", handlePointerEvent, true);
99
+ doc.addEventListener("mousemove", handlePointerEvent, true);
100
+ doc.addEventListener("mouseup", handlePointerEvent, true);
101
+ }
102
+ win.addEventListener("beforeunload", () => {
103
+ tearDownWindowFocusTracking(root);
104
+ }, { once: true });
105
+ listenerMap.set(win, { focus });
106
+ }
107
+ function tearDownWindowFocusTracking(root, loadListener) {
108
+ const win = getWindow(root);
109
+ const doc = getDocument(root);
110
+ if (loadListener) doc.removeEventListener("DOMContentLoaded", loadListener);
111
+ if (!listenerMap.has(win)) return;
112
+ win.HTMLElement.prototype.focus = listenerMap.get(win).focus;
113
+ doc.removeEventListener("keydown", handleKeyboardEvent, true);
114
+ doc.removeEventListener("keyup", handleKeyboardEvent, true);
115
+ doc.removeEventListener("click", handleClickEvent, true);
116
+ win.removeEventListener("focus", handleFocusEvent, true);
117
+ win.removeEventListener("blur", handleWindowBlur, false);
118
+ if (typeof win.PointerEvent !== "undefined") {
119
+ doc.removeEventListener("pointerdown", handlePointerEvent, true);
120
+ doc.removeEventListener("pointermove", handlePointerEvent, true);
121
+ doc.removeEventListener("pointerup", handlePointerEvent, true);
122
+ } else {
123
+ doc.removeEventListener("mousedown", handlePointerEvent, true);
124
+ doc.removeEventListener("mousemove", handlePointerEvent, true);
125
+ doc.removeEventListener("mouseup", handlePointerEvent, true);
126
+ }
127
+ listenerMap.delete(win);
128
+ }
129
+ function getInteractionModality() {
130
+ return currentModality;
131
+ }
132
+ function setInteractionModality(modality) {
133
+ currentModality = modality;
134
+ triggerChangeHandlers(modality, null);
135
+ }
136
+ function trackInteractionModality(props) {
137
+ const { onChange, root } = props;
138
+ setupGlobalFocusEvents(root);
139
+ onChange({ modality: currentModality });
140
+ const handler = () => onChange({ modality: currentModality });
141
+ changeHandlers.add(handler);
142
+ return () => {
143
+ changeHandlers.delete(handler);
144
+ };
145
+ }
146
+ function isFocusVisible() {
147
+ return currentModality === "keyboard";
148
+ }
149
+ function trackFocusVisible(props = {}) {
150
+ const { isTextInput, autoFocus, onChange, root } = props;
151
+ setupGlobalFocusEvents(root);
152
+ onChange?.({
153
+ isFocusVisible: autoFocus || isFocusVisible(),
154
+ modality: currentModality
155
+ });
156
+ const handler = (modality, e) => {
157
+ if (!isKeyboardFocusEvent(!!isTextInput, modality, e)) return;
158
+ onChange?.({
159
+ isFocusVisible: isFocusVisible(),
160
+ modality
161
+ });
162
+ };
163
+ changeHandlers.add(handler);
164
+ return () => {
165
+ changeHandlers.delete(handler);
166
+ };
167
+ }
168
+
169
+ //#endregion
170
+ export { getInteractionModality, isFocusVisible, listenerMap, setInteractionModality, trackFocusVisible, trackInteractionModality };
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@destyler/focus-visible",
3
+ "type": "module",
4
+ "version": "0.0.7-beta.9",
5
+ "description": "",
6
+ "author": "Elone Hoo <elonehoo@gmail.com>",
7
+ "license": "MIT",
8
+ "exports": {
9
+ ".": "./dist/index.js"
10
+ },
11
+ "main": "dist/index.js",
12
+ "module": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "files": [
15
+ "*.d.ts",
16
+ "dist"
17
+ ],
18
+ "dependencies": {
19
+ "@destyler/dom": "0.0.7-beta.9"
20
+ },
21
+ "scripts": {
22
+ "clear": "rimraf ./dist",
23
+ "clear:node_modules": "rimraf ./node_modules",
24
+ "build": "pnpm run clear && tsdown",
25
+ "watch": "tsdown --watch"
26
+ }
27
+ }