@v-c/util 1.0.6 → 1.0.8
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/Dom/dynamicCSS.cjs +2 -0
- package/dist/Dom/dynamicCSS.d.ts +1 -1
- package/dist/Dom/dynamicCSS.js +2 -0
- package/dist/Dom/focus.cjs +54 -25
- package/dist/Dom/focus.d.ts +12 -7
- package/dist/Dom/focus.js +53 -22
- package/dist/index.cjs +4 -2
- package/dist/index.d.ts +5 -4
- package/dist/index.js +4 -3
- package/package.json +1 -1
package/dist/Dom/dynamicCSS.cjs
CHANGED
|
@@ -51,6 +51,7 @@ function findExistNode(key, option = {}) {
|
|
|
51
51
|
return findStyles(getContainer(option)).find((node) => node.getAttribute(getMark(option)) === key);
|
|
52
52
|
}
|
|
53
53
|
function removeCSS(key, option = {}) {
|
|
54
|
+
if (!require_canUseDom.default()) return null;
|
|
54
55
|
const existNode = findExistNode(key, option);
|
|
55
56
|
if (existNode) getContainer(option).removeChild(existNode);
|
|
56
57
|
}
|
|
@@ -67,6 +68,7 @@ function clearContainerCache() {
|
|
|
67
68
|
containerCache.clear();
|
|
68
69
|
}
|
|
69
70
|
function updateCSS(css, key, option = {}) {
|
|
71
|
+
if (!require_canUseDom.default()) return null;
|
|
70
72
|
syncRealContainer(getContainer(option), option);
|
|
71
73
|
const existNode = findExistNode(key, option);
|
|
72
74
|
if (existNode) {
|
package/dist/Dom/dynamicCSS.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ interface Options {
|
|
|
15
15
|
mark?: string;
|
|
16
16
|
}
|
|
17
17
|
export declare function injectCSS(css: string, option?: Options): HTMLStyleElement;
|
|
18
|
-
export declare function removeCSS(key: string, option?: Options):
|
|
18
|
+
export declare function removeCSS(key: string, option?: Options): any;
|
|
19
19
|
/**
|
|
20
20
|
* manually clear container cache to avoid global cache in unit testes
|
|
21
21
|
*/
|
package/dist/Dom/dynamicCSS.js
CHANGED
|
@@ -50,6 +50,7 @@ function findExistNode(key, option = {}) {
|
|
|
50
50
|
return findStyles(getContainer(option)).find((node) => node.getAttribute(getMark(option)) === key);
|
|
51
51
|
}
|
|
52
52
|
function removeCSS(key, option = {}) {
|
|
53
|
+
if (!canUseDom()) return null;
|
|
53
54
|
const existNode = findExistNode(key, option);
|
|
54
55
|
if (existNode) getContainer(option).removeChild(existNode);
|
|
55
56
|
}
|
|
@@ -66,6 +67,7 @@ function clearContainerCache() {
|
|
|
66
67
|
containerCache.clear();
|
|
67
68
|
}
|
|
68
69
|
function updateCSS(css, key, option = {}) {
|
|
70
|
+
if (!canUseDom()) return null;
|
|
69
71
|
syncRealContainer(getContainer(option), option);
|
|
70
72
|
const existNode = findExistNode(key, option);
|
|
71
73
|
if (existNode) {
|
package/dist/Dom/focus.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_isVisible = require("./isVisible.cjs");
|
|
3
|
+
let vue = require("vue");
|
|
3
4
|
function focusable(node, includePositive = false) {
|
|
4
5
|
if (require_isVisible.default(node)) {
|
|
5
6
|
const nodeName = node.nodeName.toLowerCase();
|
|
@@ -26,27 +27,6 @@ function getFocusNodeList(node, includePositive = false) {
|
|
|
26
27
|
if (focusable(node, includePositive)) res.unshift(node);
|
|
27
28
|
return res;
|
|
28
29
|
}
|
|
29
|
-
var lastFocusElement = null;
|
|
30
|
-
function saveLastFocusNode() {
|
|
31
|
-
lastFocusElement = document.activeElement;
|
|
32
|
-
}
|
|
33
|
-
function clearLastFocusNode() {
|
|
34
|
-
lastFocusElement = null;
|
|
35
|
-
}
|
|
36
|
-
function backLastFocusNode() {
|
|
37
|
-
if (lastFocusElement) try {
|
|
38
|
-
lastFocusElement.focus();
|
|
39
|
-
} catch (_e) {}
|
|
40
|
-
}
|
|
41
|
-
function limitTabRange(node, e) {
|
|
42
|
-
if (e.keyCode === 9) {
|
|
43
|
-
const tabNodeList = getFocusNodeList(node);
|
|
44
|
-
if (tabNodeList[e.shiftKey ? 0 : tabNodeList.length - 1] === document.activeElement || node === document.activeElement) {
|
|
45
|
-
tabNodeList[e.shiftKey ? tabNodeList.length - 1 : 0].focus();
|
|
46
|
-
e.preventDefault();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
30
|
function triggerFocus(element, option) {
|
|
51
31
|
if (!element) return;
|
|
52
32
|
element.focus(option);
|
|
@@ -64,9 +44,58 @@ function triggerFocus(element, option) {
|
|
|
64
44
|
}
|
|
65
45
|
}
|
|
66
46
|
}
|
|
67
|
-
|
|
68
|
-
|
|
47
|
+
var lastFocusElement = null;
|
|
48
|
+
var focusElements = [];
|
|
49
|
+
function getLastElement() {
|
|
50
|
+
return focusElements[focusElements.length - 1];
|
|
51
|
+
}
|
|
52
|
+
function hasFocus(element) {
|
|
53
|
+
const { activeElement } = document;
|
|
54
|
+
return element === activeElement || element.contains(activeElement);
|
|
55
|
+
}
|
|
56
|
+
function syncFocus() {
|
|
57
|
+
const lastElement = getLastElement();
|
|
58
|
+
const { activeElement } = document;
|
|
59
|
+
if (lastElement && !hasFocus(lastElement)) {
|
|
60
|
+
const focusableList = getFocusNodeList(lastElement);
|
|
61
|
+
(focusableList.includes(lastFocusElement) ? lastFocusElement : focusableList[0])?.focus();
|
|
62
|
+
} else lastFocusElement = activeElement;
|
|
63
|
+
}
|
|
64
|
+
function onWindowKeyDown(e) {
|
|
65
|
+
if (e.key === "Tab") {
|
|
66
|
+
const { activeElement } = document;
|
|
67
|
+
const focusableList = getFocusNodeList(getLastElement());
|
|
68
|
+
const last = focusableList[focusableList.length - 1];
|
|
69
|
+
if (e.shiftKey && activeElement === focusableList[0]) lastFocusElement = last;
|
|
70
|
+
else if (!e.shiftKey && activeElement === last) lastFocusElement = focusableList[0];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function lockFocus(element) {
|
|
74
|
+
if (element) {
|
|
75
|
+
focusElements = focusElements.filter((ele) => ele !== element);
|
|
76
|
+
focusElements.push(element);
|
|
77
|
+
window.addEventListener("focusin", syncFocus);
|
|
78
|
+
window.addEventListener("keydown", onWindowKeyDown, true);
|
|
79
|
+
syncFocus();
|
|
80
|
+
}
|
|
81
|
+
return () => {
|
|
82
|
+
lastFocusElement = null;
|
|
83
|
+
focusElements = focusElements.filter((ele) => ele !== element);
|
|
84
|
+
if (focusElements.length === 0) {
|
|
85
|
+
window.removeEventListener("focusin", syncFocus);
|
|
86
|
+
window.removeEventListener("keydown", onWindowKeyDown, true);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function useLockFocus(lock, getElement) {
|
|
91
|
+
(0, vue.watch)(lock, (_, _o, onCleanup) => {
|
|
92
|
+
if (lock.value) {
|
|
93
|
+
const element = getElement();
|
|
94
|
+
if (element) onCleanup(lockFocus(element));
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
69
98
|
exports.getFocusNodeList = getFocusNodeList;
|
|
70
|
-
exports.
|
|
71
|
-
exports.saveLastFocusNode = saveLastFocusNode;
|
|
99
|
+
exports.lockFocus = lockFocus;
|
|
72
100
|
exports.triggerFocus = triggerFocus;
|
|
101
|
+
exports.useLockFocus = useLockFocus;
|
package/dist/Dom/focus.d.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
1
2
|
export declare function getFocusNodeList(node: HTMLElement, includePositive?: boolean): HTMLElement[];
|
|
2
|
-
/** @deprecated Do not use since this may failed when used in async */
|
|
3
|
-
export declare function saveLastFocusNode(): void;
|
|
4
|
-
/** @deprecated Do not use since this may failed when used in async */
|
|
5
|
-
export declare function clearLastFocusNode(): void;
|
|
6
|
-
/** @deprecated Do not use since this may failed when used in async */
|
|
7
|
-
export declare function backLastFocusNode(): void;
|
|
8
|
-
export declare function limitTabRange(node: HTMLElement, e: KeyboardEvent): void;
|
|
9
3
|
export interface InputFocusOptions extends FocusOptions {
|
|
10
4
|
cursor?: 'start' | 'end' | 'all';
|
|
11
5
|
}
|
|
12
6
|
export declare function triggerFocus(element?: HTMLInputElement | HTMLTextAreaElement, option?: InputFocusOptions): void;
|
|
7
|
+
/**
|
|
8
|
+
* Lock focus in the element.
|
|
9
|
+
* It will force back to the first focusable element when focus leaves the element.
|
|
10
|
+
*/
|
|
11
|
+
export declare function lockFocus(element: HTMLElement): VoidFunction;
|
|
12
|
+
/**
|
|
13
|
+
* Lock focus within an element.
|
|
14
|
+
* When locked, focus will be restricted to focusable elements within the specified element.
|
|
15
|
+
* If multiple elements are locked, only the last locked element will be effective.
|
|
16
|
+
*/
|
|
17
|
+
export declare function useLockFocus(lock: Ref<boolean>, getElement: () => HTMLElement | null): void;
|
package/dist/Dom/focus.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import isVisible_default from "./isVisible.js";
|
|
2
|
+
import { watch } from "vue";
|
|
2
3
|
function focusable(node, includePositive = false) {
|
|
3
4
|
if (isVisible_default(node)) {
|
|
4
5
|
const nodeName = node.nodeName.toLowerCase();
|
|
@@ -25,27 +26,6 @@ function getFocusNodeList(node, includePositive = false) {
|
|
|
25
26
|
if (focusable(node, includePositive)) res.unshift(node);
|
|
26
27
|
return res;
|
|
27
28
|
}
|
|
28
|
-
var lastFocusElement = null;
|
|
29
|
-
function saveLastFocusNode() {
|
|
30
|
-
lastFocusElement = document.activeElement;
|
|
31
|
-
}
|
|
32
|
-
function clearLastFocusNode() {
|
|
33
|
-
lastFocusElement = null;
|
|
34
|
-
}
|
|
35
|
-
function backLastFocusNode() {
|
|
36
|
-
if (lastFocusElement) try {
|
|
37
|
-
lastFocusElement.focus();
|
|
38
|
-
} catch (_e) {}
|
|
39
|
-
}
|
|
40
|
-
function limitTabRange(node, e) {
|
|
41
|
-
if (e.keyCode === 9) {
|
|
42
|
-
const tabNodeList = getFocusNodeList(node);
|
|
43
|
-
if (tabNodeList[e.shiftKey ? 0 : tabNodeList.length - 1] === document.activeElement || node === document.activeElement) {
|
|
44
|
-
tabNodeList[e.shiftKey ? tabNodeList.length - 1 : 0].focus();
|
|
45
|
-
e.preventDefault();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
29
|
function triggerFocus(element, option) {
|
|
50
30
|
if (!element) return;
|
|
51
31
|
element.focus(option);
|
|
@@ -63,4 +43,55 @@ function triggerFocus(element, option) {
|
|
|
63
43
|
}
|
|
64
44
|
}
|
|
65
45
|
}
|
|
66
|
-
|
|
46
|
+
var lastFocusElement = null;
|
|
47
|
+
var focusElements = [];
|
|
48
|
+
function getLastElement() {
|
|
49
|
+
return focusElements[focusElements.length - 1];
|
|
50
|
+
}
|
|
51
|
+
function hasFocus(element) {
|
|
52
|
+
const { activeElement } = document;
|
|
53
|
+
return element === activeElement || element.contains(activeElement);
|
|
54
|
+
}
|
|
55
|
+
function syncFocus() {
|
|
56
|
+
const lastElement = getLastElement();
|
|
57
|
+
const { activeElement } = document;
|
|
58
|
+
if (lastElement && !hasFocus(lastElement)) {
|
|
59
|
+
const focusableList = getFocusNodeList(lastElement);
|
|
60
|
+
(focusableList.includes(lastFocusElement) ? lastFocusElement : focusableList[0])?.focus();
|
|
61
|
+
} else lastFocusElement = activeElement;
|
|
62
|
+
}
|
|
63
|
+
function onWindowKeyDown(e) {
|
|
64
|
+
if (e.key === "Tab") {
|
|
65
|
+
const { activeElement } = document;
|
|
66
|
+
const focusableList = getFocusNodeList(getLastElement());
|
|
67
|
+
const last = focusableList[focusableList.length - 1];
|
|
68
|
+
if (e.shiftKey && activeElement === focusableList[0]) lastFocusElement = last;
|
|
69
|
+
else if (!e.shiftKey && activeElement === last) lastFocusElement = focusableList[0];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function lockFocus(element) {
|
|
73
|
+
if (element) {
|
|
74
|
+
focusElements = focusElements.filter((ele) => ele !== element);
|
|
75
|
+
focusElements.push(element);
|
|
76
|
+
window.addEventListener("focusin", syncFocus);
|
|
77
|
+
window.addEventListener("keydown", onWindowKeyDown, true);
|
|
78
|
+
syncFocus();
|
|
79
|
+
}
|
|
80
|
+
return () => {
|
|
81
|
+
lastFocusElement = null;
|
|
82
|
+
focusElements = focusElements.filter((ele) => ele !== element);
|
|
83
|
+
if (focusElements.length === 0) {
|
|
84
|
+
window.removeEventListener("focusin", syncFocus);
|
|
85
|
+
window.removeEventListener("keydown", onWindowKeyDown, true);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function useLockFocus(lock, getElement) {
|
|
90
|
+
watch(lock, (_, _o, onCleanup) => {
|
|
91
|
+
if (lock.value) {
|
|
92
|
+
const element = getElement();
|
|
93
|
+
if (element) onCleanup(lockFocus(element));
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
export { getFocusNodeList, lockFocus, triggerFocus, useLockFocus };
|
package/dist/index.cjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_KeyCode = require("./KeyCode.cjs");
|
|
3
|
+
const require_raf = require("./raf.cjs");
|
|
3
4
|
const require_omit = require("./omit.cjs");
|
|
4
5
|
const require_RenderComponent = require("./RenderComponent.cjs");
|
|
5
6
|
const require_classnames = require("./classnames.cjs");
|
|
6
|
-
const require_get = require("./utils/get.cjs");
|
|
7
|
-
const require_set = require("./utils/set.cjs");
|
|
8
7
|
const require_useId = require("./hooks/useId.cjs");
|
|
9
8
|
const require_useMergedState = require("./hooks/useMergedState.cjs");
|
|
9
|
+
const require_get = require("./utils/get.cjs");
|
|
10
|
+
const require_set = require("./utils/set.cjs");
|
|
10
11
|
const require_warning = require("./warning.cjs");
|
|
11
12
|
exports.KeyCode = require_KeyCode.default;
|
|
12
13
|
exports.RenderComponent = require_RenderComponent.default;
|
|
@@ -14,6 +15,7 @@ exports.classNames = require_classnames.default;
|
|
|
14
15
|
exports.clsx = require_classnames.clsx;
|
|
15
16
|
exports.get = require_get.default;
|
|
16
17
|
exports.omit = require_omit.default;
|
|
18
|
+
exports.raf = require_raf.default;
|
|
17
19
|
exports.set = require_set.default;
|
|
18
20
|
exports.useId = require_useId.default;
|
|
19
21
|
exports.useMergedState = require_useMergedState.default;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export { default as classNames, clsx } from './classnames';
|
|
2
|
-
export { default as
|
|
2
|
+
export { default as useId } from './hooks/useId';
|
|
3
|
+
export { default as useMergedState } from './hooks/useMergedState';
|
|
3
4
|
export { default as KeyCode } from './KeyCode';
|
|
4
5
|
export { default as omit } from './omit';
|
|
6
|
+
export { default as raf } from './raf';
|
|
7
|
+
export { default as RenderComponent } from './RenderComponent';
|
|
8
|
+
export type { VueNode } from './type';
|
|
5
9
|
export { default as get } from './utils/get';
|
|
6
10
|
export { default as set } from './utils/set';
|
|
7
|
-
export { default as useId } from './hooks/useId';
|
|
8
|
-
export { default as useMergedState } from './hooks/useMergedState';
|
|
9
11
|
export { default as warning } from './warning';
|
|
10
|
-
export type { VueNode } from './type';
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import KeyCode_default from "./KeyCode.js";
|
|
2
|
+
import raf_default from "./raf.js";
|
|
2
3
|
import omit from "./omit.js";
|
|
3
4
|
import RenderComponent_default from "./RenderComponent.js";
|
|
4
5
|
import classNames, { clsx } from "./classnames.js";
|
|
5
|
-
import get from "./utils/get.js";
|
|
6
|
-
import set from "./utils/set.js";
|
|
7
6
|
import useId_default from "./hooks/useId.js";
|
|
8
7
|
import useMergedState from "./hooks/useMergedState.js";
|
|
8
|
+
import get from "./utils/get.js";
|
|
9
|
+
import set from "./utils/set.js";
|
|
9
10
|
import warning_default from "./warning.js";
|
|
10
|
-
export { KeyCode_default as KeyCode, RenderComponent_default as RenderComponent, classNames, clsx, get, omit, set, useId_default as useId, useMergedState, warning_default as warning };
|
|
11
|
+
export { KeyCode_default as KeyCode, RenderComponent_default as RenderComponent, classNames, clsx, get, omit, raf_default as raf, set, useId_default as useId, useMergedState, warning_default as warning };
|