@yamada-ui/use-focus-visible 0.1.0

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 Hirotomo Yamada
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.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @yamada-ui/use-focus-visible
2
+
3
+ > This is an internal utility, not intended for public usage.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ $ pnpm add @yamada-ui/use-focus-visible
9
+ ```
10
+
11
+ or
12
+
13
+ ```sh
14
+ $ yarn add @yamada-ui/use-focus-visible
15
+ ```
16
+
17
+ or
18
+
19
+ ```sh
20
+ $ npm install @yamada-ui/use-focus-visible
21
+ ```
22
+
23
+ ## Contribution
24
+
25
+ Wouldn't you like to contribute? That's amazing! We have prepared a [contribution guide](https://github.com/hirotomoyamada/yamada-ui/blob/main/CONTRIBUTING.md) to assist you.
26
+
27
+ ## Licence
28
+
29
+ This package is licensed under the terms of the
30
+
31
+ [MIT license](https://github.com/hirotomoyamada/yamada-ui/blob/main/LICENSE).
@@ -0,0 +1,9 @@
1
+ type FocusVisibleCallback = (isFocusVisible: boolean) => void;
2
+ declare const trackFocusVisible: (func: FocusVisibleCallback) => () => void;
3
+ declare const useFocusVisible: () => {
4
+ focusVisible: boolean;
5
+ onFocus: () => void;
6
+ onBlur: () => void;
7
+ };
8
+
9
+ export { trackFocusVisible, useFocusVisible };
package/dist/index.js ADDED
@@ -0,0 +1,131 @@
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
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ trackFocusVisible: () => trackFocusVisible,
24
+ useFocusVisible: () => useFocusVisible
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+ var import_utils = require("@yamada-ui/utils");
28
+ var import_react = require("react");
29
+ var hasSetup = false;
30
+ var modality = null;
31
+ var hasEventBeforeFocus = false;
32
+ var hasBlurredWindowRecently = false;
33
+ var handlers = /* @__PURE__ */ new Set();
34
+ var trigger = (modality2, ev) => handlers.forEach((handler) => handler(modality2, ev));
35
+ var onValid = (e) => {
36
+ return !(e.metaKey || !import_utils.isMac && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
37
+ };
38
+ var onKeyboard = (ev) => {
39
+ hasEventBeforeFocus = true;
40
+ if (onValid(ev)) {
41
+ modality = "keyboard";
42
+ trigger("keyboard", ev);
43
+ }
44
+ };
45
+ var onPointer = (ev) => {
46
+ modality = "pointer";
47
+ if (ev.type === "mousedown" || ev.type === "pointerdown") {
48
+ hasEventBeforeFocus = true;
49
+ const target = ev.composedPath ? ev.composedPath()[0] : ev.target;
50
+ if (target.matches(":focus-visible"))
51
+ return;
52
+ trigger("pointer", ev);
53
+ }
54
+ };
55
+ var isVirtualClick = (ev) => {
56
+ if (ev.mozInputSource === 0 && ev.isTrusted)
57
+ return true;
58
+ return ev.detail === 0 && !ev.pointerType;
59
+ };
60
+ var onClick = (ev) => {
61
+ if (!isVirtualClick(ev))
62
+ return;
63
+ hasEventBeforeFocus = true;
64
+ modality = "virtual";
65
+ };
66
+ var onFocus = (ev) => {
67
+ if (ev.target === window || ev.target === document)
68
+ return;
69
+ if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
70
+ modality = "virtual";
71
+ trigger("virtual", ev);
72
+ }
73
+ hasEventBeforeFocus = false;
74
+ hasBlurredWindowRecently = false;
75
+ };
76
+ var onBlur = () => {
77
+ hasEventBeforeFocus = false;
78
+ hasBlurredWindowRecently = true;
79
+ };
80
+ var isFocusVisible = () => modality !== "pointer";
81
+ var setupGlobalFocusEvents = () => {
82
+ if (typeof window === "undefined" || hasSetup)
83
+ return;
84
+ const { focus } = HTMLElement.prototype;
85
+ HTMLElement.prototype.focus = (...args) => {
86
+ hasEventBeforeFocus = true;
87
+ if (void 0)
88
+ focus.apply(void 0, args);
89
+ };
90
+ document.addEventListener("keydown", onKeyboard, true);
91
+ document.addEventListener("keyup", onKeyboard, true);
92
+ document.addEventListener("click", onClick, true);
93
+ window.addEventListener("focus", onFocus, true);
94
+ window.addEventListener("blur", onBlur, false);
95
+ if (typeof PointerEvent !== "undefined") {
96
+ document.addEventListener("pointerdown", onPointer, true);
97
+ document.addEventListener("pointermove", onPointer, true);
98
+ document.addEventListener("pointerup", onPointer, true);
99
+ } else {
100
+ document.addEventListener("mousedown", onPointer, true);
101
+ document.addEventListener("mousemove", onPointer, true);
102
+ document.addEventListener("mouseup", onPointer, true);
103
+ }
104
+ hasSetup = true;
105
+ };
106
+ var trackFocusVisible = (func) => {
107
+ setupGlobalFocusEvents();
108
+ func(isFocusVisible());
109
+ const handler = () => func(isFocusVisible());
110
+ handlers.add(handler);
111
+ return () => {
112
+ handlers.delete(handler);
113
+ };
114
+ };
115
+ var useFocusVisible = () => {
116
+ const [focusVisible, setFocusVisible] = (0, import_react.useState)(false);
117
+ const [focus, setFocus] = (0, import_react.useState)(false);
118
+ (0, import_react.useEffect)(() => {
119
+ return trackFocusVisible(setFocusVisible);
120
+ }, []);
121
+ return {
122
+ focusVisible: focusVisible && focus,
123
+ onFocus: () => setFocus(true),
124
+ onBlur: () => setFocus(false)
125
+ };
126
+ };
127
+ // Annotate the CommonJS export names for ESM import in node:
128
+ 0 && (module.exports = {
129
+ trackFocusVisible,
130
+ useFocusVisible
131
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,105 @@
1
+ // src/index.ts
2
+ import { isMac } from "@yamada-ui/utils";
3
+ import { useEffect, useState } from "react";
4
+ var hasSetup = false;
5
+ var modality = null;
6
+ var hasEventBeforeFocus = false;
7
+ var hasBlurredWindowRecently = false;
8
+ var handlers = /* @__PURE__ */ new Set();
9
+ var trigger = (modality2, ev) => handlers.forEach((handler) => handler(modality2, ev));
10
+ var onValid = (e) => {
11
+ return !(e.metaKey || !isMac && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta");
12
+ };
13
+ var onKeyboard = (ev) => {
14
+ hasEventBeforeFocus = true;
15
+ if (onValid(ev)) {
16
+ modality = "keyboard";
17
+ trigger("keyboard", ev);
18
+ }
19
+ };
20
+ var onPointer = (ev) => {
21
+ modality = "pointer";
22
+ if (ev.type === "mousedown" || ev.type === "pointerdown") {
23
+ hasEventBeforeFocus = true;
24
+ const target = ev.composedPath ? ev.composedPath()[0] : ev.target;
25
+ if (target.matches(":focus-visible"))
26
+ return;
27
+ trigger("pointer", ev);
28
+ }
29
+ };
30
+ var isVirtualClick = (ev) => {
31
+ if (ev.mozInputSource === 0 && ev.isTrusted)
32
+ return true;
33
+ return ev.detail === 0 && !ev.pointerType;
34
+ };
35
+ var onClick = (ev) => {
36
+ if (!isVirtualClick(ev))
37
+ return;
38
+ hasEventBeforeFocus = true;
39
+ modality = "virtual";
40
+ };
41
+ var onFocus = (ev) => {
42
+ if (ev.target === window || ev.target === document)
43
+ return;
44
+ if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {
45
+ modality = "virtual";
46
+ trigger("virtual", ev);
47
+ }
48
+ hasEventBeforeFocus = false;
49
+ hasBlurredWindowRecently = false;
50
+ };
51
+ var onBlur = () => {
52
+ hasEventBeforeFocus = false;
53
+ hasBlurredWindowRecently = true;
54
+ };
55
+ var isFocusVisible = () => modality !== "pointer";
56
+ var setupGlobalFocusEvents = () => {
57
+ if (typeof window === "undefined" || hasSetup)
58
+ return;
59
+ const { focus } = HTMLElement.prototype;
60
+ HTMLElement.prototype.focus = (...args) => {
61
+ hasEventBeforeFocus = true;
62
+ if (void 0)
63
+ focus.apply(void 0, args);
64
+ };
65
+ document.addEventListener("keydown", onKeyboard, true);
66
+ document.addEventListener("keyup", onKeyboard, true);
67
+ document.addEventListener("click", onClick, true);
68
+ window.addEventListener("focus", onFocus, true);
69
+ window.addEventListener("blur", onBlur, false);
70
+ if (typeof PointerEvent !== "undefined") {
71
+ document.addEventListener("pointerdown", onPointer, true);
72
+ document.addEventListener("pointermove", onPointer, true);
73
+ document.addEventListener("pointerup", onPointer, true);
74
+ } else {
75
+ document.addEventListener("mousedown", onPointer, true);
76
+ document.addEventListener("mousemove", onPointer, true);
77
+ document.addEventListener("mouseup", onPointer, true);
78
+ }
79
+ hasSetup = true;
80
+ };
81
+ var trackFocusVisible = (func) => {
82
+ setupGlobalFocusEvents();
83
+ func(isFocusVisible());
84
+ const handler = () => func(isFocusVisible());
85
+ handlers.add(handler);
86
+ return () => {
87
+ handlers.delete(handler);
88
+ };
89
+ };
90
+ var useFocusVisible = () => {
91
+ const [focusVisible, setFocusVisible] = useState(false);
92
+ const [focus, setFocus] = useState(false);
93
+ useEffect(() => {
94
+ return trackFocusVisible(setFocusVisible);
95
+ }, []);
96
+ return {
97
+ focusVisible: focusVisible && focus,
98
+ onFocus: () => setFocus(true),
99
+ onBlur: () => setFocus(false)
100
+ };
101
+ };
102
+ export {
103
+ trackFocusVisible,
104
+ useFocusVisible
105
+ };
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@yamada-ui/use-focus-visible",
3
+ "version": "0.1.0",
4
+ "description": "Yamada UI useFocusVisible custom hook",
5
+ "keywords": [
6
+ "use-focus-visible"
7
+ ],
8
+ "author": "Hirotomo Yamada <hirotomo.yamada@avap.co.jp>",
9
+ "license": "MIT",
10
+ "main": "dist/index.js",
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "sideEffects": false,
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/hirotomoyamada/yamada-ui",
21
+ "directory": "packages/hooks/use-focus-visible"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/hirotomoyamada/yamada-ui/issues"
25
+ },
26
+ "dependencies": {
27
+ "@yamada-ui/utils": "0.1.0"
28
+ },
29
+ "devDependencies": {
30
+ "react": "^18.0.0",
31
+ "clean-package": "2.2.0"
32
+ },
33
+ "peerDependencies": {
34
+ "react": ">=18"
35
+ },
36
+ "clean-package": "../../../clean-package.config.json",
37
+ "tsup": {
38
+ "clean": true,
39
+ "target": "es2019",
40
+ "format": [
41
+ "cjs",
42
+ "esm"
43
+ ]
44
+ },
45
+ "module": "dist/index.mjs",
46
+ "types": "dist/index.d.ts",
47
+ "exports": {
48
+ ".": {
49
+ "types": "./dist/index.d.ts",
50
+ "import": "./dist/index.mjs",
51
+ "require": "./dist/index.js"
52
+ },
53
+ "./package.json": "./package.json"
54
+ },
55
+ "scripts": {
56
+ "dev": "pnpm build:fast -- --watch",
57
+ "build": "tsup src --dts",
58
+ "build:fast": "tsup src",
59
+ "clean": "rimraf dist .turbo",
60
+ "typecheck": "tsc --noEmit"
61
+ }
62
+ }