@v-c/util 1.0.14 → 1.0.15
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/focus.d.ts +4 -2
- package/dist/Dom/focus.js +29 -3
- package/dist/getScrollBarSize.d.ts +5 -3
- package/dist/getScrollBarSize.js +46 -42
- package/dist/hooks/useEvent.d.ts +2 -1
- package/dist/hooks/useEvent.js +4 -3
- package/dist/hooks/useId.d.ts +2 -2
- package/dist/hooks/useId.js +1 -1
- package/dist/isMobile.d.ts +2 -2
- package/dist/isMobile.js +2 -1
- package/package.json +1 -1
package/dist/Dom/focus.d.ts
CHANGED
|
@@ -7,11 +7,13 @@ export declare function triggerFocus(element?: HTMLInputElement | HTMLTextAreaEl
|
|
|
7
7
|
/**
|
|
8
8
|
* Lock focus in the element.
|
|
9
9
|
* It will force back to the first focusable element when focus leaves the element.
|
|
10
|
+
* @param id - A stable ID for this lock instance
|
|
10
11
|
*/
|
|
11
|
-
export declare function lockFocus(element: HTMLElement): VoidFunction;
|
|
12
|
+
export declare function lockFocus(element: HTMLElement, id: string): VoidFunction;
|
|
12
13
|
/**
|
|
13
14
|
* Lock focus within an element.
|
|
14
15
|
* When locked, focus will be restricted to focusable elements within the specified element.
|
|
15
16
|
* If multiple elements are locked, only the last locked element will be effective.
|
|
17
|
+
* @returns A function to mark an element as ignored, which will temporarily allow focus on that element even if it's outside the locked area.
|
|
16
18
|
*/
|
|
17
|
-
export declare function useLockFocus(lock: Ref<boolean>, getElement: () => HTMLElement | null): void;
|
|
19
|
+
export declare function useLockFocus(lock: Ref<boolean>, getElement: () => HTMLElement | null): [ignoreElement: (ele: HTMLElement) => void];
|
package/dist/Dom/focus.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import useId_default from "../hooks/useId.js";
|
|
1
2
|
import { getDOM } from "../dist/Dom/findDOMNode.js";
|
|
2
3
|
import isVisible_default from "./isVisible.js";
|
|
3
4
|
import { watch } from "vue";
|
|
@@ -46,9 +47,25 @@ function triggerFocus(element, option) {
|
|
|
46
47
|
}
|
|
47
48
|
var lastFocusElement = null;
|
|
48
49
|
var focusElements = [];
|
|
50
|
+
var idToElementMap = /* @__PURE__ */ new Map();
|
|
51
|
+
var ignoredElementMap = /* @__PURE__ */ new Map();
|
|
49
52
|
function getLastElement() {
|
|
50
53
|
return focusElements[focusElements.length - 1];
|
|
51
54
|
}
|
|
55
|
+
function isIgnoredElement(element) {
|
|
56
|
+
const lastElement = getLastElement();
|
|
57
|
+
if (element && lastElement) {
|
|
58
|
+
let lockId;
|
|
59
|
+
for (const [id, ele] of idToElementMap.entries()) if (ele === lastElement) {
|
|
60
|
+
lockId = id;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
if (!lockId) return false;
|
|
64
|
+
const ignoredEle = ignoredElementMap.get(lockId);
|
|
65
|
+
return !!ignoredEle && (ignoredEle === element || ignoredEle.contains(element));
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
52
69
|
function hasFocus(element) {
|
|
53
70
|
const { activeElement } = document;
|
|
54
71
|
return element === activeElement || element.contains(activeElement);
|
|
@@ -56,9 +73,10 @@ function hasFocus(element) {
|
|
|
56
73
|
function syncFocus() {
|
|
57
74
|
const lastElement = getLastElement();
|
|
58
75
|
const { activeElement } = document;
|
|
76
|
+
if (isIgnoredElement(activeElement)) return;
|
|
59
77
|
if (lastElement && !hasFocus(lastElement)) {
|
|
60
78
|
const focusableList = getFocusNodeList(lastElement);
|
|
61
|
-
(focusableList.includes(lastFocusElement) ? lastFocusElement : focusableList[0])?.focus();
|
|
79
|
+
(focusableList.includes(lastFocusElement) ? lastFocusElement : focusableList[0])?.focus({ preventScroll: true });
|
|
62
80
|
} else lastFocusElement = activeElement;
|
|
63
81
|
}
|
|
64
82
|
function onWindowKeyDown(e) {
|
|
@@ -70,8 +88,9 @@ function onWindowKeyDown(e) {
|
|
|
70
88
|
else if (!e.shiftKey && activeElement === last) lastFocusElement = focusableList[0];
|
|
71
89
|
}
|
|
72
90
|
}
|
|
73
|
-
function lockFocus(element) {
|
|
91
|
+
function lockFocus(element, id) {
|
|
74
92
|
if (element) {
|
|
93
|
+
idToElementMap.set(id, element);
|
|
75
94
|
focusElements = focusElements.filter((ele) => ele !== element);
|
|
76
95
|
focusElements.push(element);
|
|
77
96
|
window.addEventListener("focusin", syncFocus);
|
|
@@ -81,6 +100,8 @@ function lockFocus(element) {
|
|
|
81
100
|
return () => {
|
|
82
101
|
lastFocusElement = null;
|
|
83
102
|
focusElements = focusElements.filter((ele) => ele !== element);
|
|
103
|
+
idToElementMap.delete(id);
|
|
104
|
+
ignoredElementMap.delete(id);
|
|
84
105
|
if (focusElements.length === 0) {
|
|
85
106
|
window.removeEventListener("focusin", syncFocus);
|
|
86
107
|
window.removeEventListener("keydown", onWindowKeyDown, true);
|
|
@@ -88,12 +109,17 @@ function lockFocus(element) {
|
|
|
88
109
|
};
|
|
89
110
|
}
|
|
90
111
|
function useLockFocus(lock, getElement) {
|
|
112
|
+
const id = useId_default();
|
|
91
113
|
watch([lock, () => getElement()], ([nextLock, element], _o, onCleanup) => {
|
|
92
114
|
element = getDOM(element);
|
|
93
|
-
if (nextLock && element) onCleanup(lockFocus(element));
|
|
115
|
+
if (nextLock && element) onCleanup(lockFocus(element, id));
|
|
94
116
|
}, {
|
|
95
117
|
flush: "post",
|
|
96
118
|
immediate: true
|
|
97
119
|
});
|
|
120
|
+
const ignoreElement = (ele) => {
|
|
121
|
+
if (ele) ignoredElementMap.set(id, ele);
|
|
122
|
+
};
|
|
123
|
+
return [ignoreElement];
|
|
98
124
|
}
|
|
99
125
|
export { getFocusNodeList, lockFocus, triggerFocus, useLockFocus };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function getTargetScrollBarSize(target: HTMLElement): {
|
|
1
|
+
interface ScrollBarSize {
|
|
3
2
|
width: number;
|
|
4
3
|
height: number;
|
|
5
|
-
}
|
|
4
|
+
}
|
|
5
|
+
export default function getScrollBarSize(fresh?: boolean): number;
|
|
6
|
+
export declare function getTargetScrollBarSize(target: HTMLElement): ScrollBarSize;
|
|
7
|
+
export {};
|
package/dist/getScrollBarSize.js
CHANGED
|
@@ -1,53 +1,57 @@
|
|
|
1
|
+
import { removeCSS, updateCSS } from "./Dom/dynamicCSS.js";
|
|
1
2
|
var cached;
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
3
|
+
function measureScrollbarSize(ele) {
|
|
4
|
+
const randomId = `vc-scrollbar-measure-${Math.random().toString(36).substring(7)}`;
|
|
5
|
+
const measureEle = document.createElement("div");
|
|
6
|
+
measureEle.id = randomId;
|
|
7
|
+
const measureStyle = measureEle.style;
|
|
8
|
+
measureStyle.position = "absolute";
|
|
9
|
+
measureStyle.left = "0";
|
|
10
|
+
measureStyle.top = "0";
|
|
11
|
+
measureStyle.width = "100px";
|
|
12
|
+
measureStyle.height = "100px";
|
|
13
|
+
measureStyle.overflow = "scroll";
|
|
14
|
+
let fallbackWidth;
|
|
15
|
+
let fallbackHeight;
|
|
16
|
+
if (ele) {
|
|
17
|
+
const targetStyle = getComputedStyle(ele);
|
|
18
|
+
measureStyle.scrollbarColor = targetStyle.scrollbarColor;
|
|
19
|
+
measureStyle.scrollbarWidth = targetStyle.scrollbarWidth;
|
|
20
|
+
const webkitScrollbarStyle = getComputedStyle(ele, "::-webkit-scrollbar");
|
|
21
|
+
const width = parseInt(webkitScrollbarStyle.width, 10);
|
|
22
|
+
const height = parseInt(webkitScrollbarStyle.height, 10);
|
|
23
|
+
try {
|
|
24
|
+
updateCSS(`
|
|
25
|
+
#${randomId}::-webkit-scrollbar {
|
|
26
|
+
${width ? `width: ${webkitScrollbarStyle.width};` : ""}
|
|
27
|
+
${height ? `height: ${webkitScrollbarStyle.height};` : ""}
|
|
28
|
+
}`, randomId);
|
|
29
|
+
} catch (e) {
|
|
30
|
+
console.error(e);
|
|
31
|
+
fallbackWidth = width;
|
|
32
|
+
fallbackHeight = height;
|
|
33
|
+
}
|
|
26
34
|
}
|
|
27
|
-
|
|
35
|
+
document.body.appendChild(measureEle);
|
|
36
|
+
const scrollWidth = ele && fallbackWidth && !Number.isNaN(fallbackWidth) ? fallbackWidth : measureEle.offsetWidth - measureEle.clientWidth;
|
|
37
|
+
const scrollHeight = ele && fallbackHeight && !Number.isNaN(fallbackHeight) ? fallbackHeight : measureEle.offsetHeight - measureEle.clientHeight;
|
|
38
|
+
document.body.removeChild(measureEle);
|
|
39
|
+
removeCSS(randomId);
|
|
40
|
+
return {
|
|
41
|
+
width: scrollWidth,
|
|
42
|
+
height: scrollHeight
|
|
43
|
+
};
|
|
28
44
|
}
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return
|
|
45
|
+
function getScrollBarSize(fresh) {
|
|
46
|
+
if (typeof document === "undefined") return 0;
|
|
47
|
+
if (fresh || cached === void 0) cached = measureScrollbarSize();
|
|
48
|
+
return cached.width;
|
|
33
49
|
}
|
|
34
50
|
function getTargetScrollBarSize(target) {
|
|
35
51
|
if (typeof document === "undefined" || !target || !(target instanceof Element)) return {
|
|
36
52
|
width: 0,
|
|
37
53
|
height: 0
|
|
38
54
|
};
|
|
39
|
-
|
|
40
|
-
width: 0,
|
|
41
|
-
height: 0
|
|
42
|
-
};
|
|
43
|
-
let width = "0px";
|
|
44
|
-
let height = "0px";
|
|
45
|
-
try {
|
|
46
|
-
({width, height} = getComputedStyle(target, "::-webkit-scrollbar"));
|
|
47
|
-
} catch {}
|
|
48
|
-
return {
|
|
49
|
-
width: ensureSize(width),
|
|
50
|
-
height: ensureSize(height)
|
|
51
|
-
};
|
|
55
|
+
return measureScrollbarSize(target);
|
|
52
56
|
}
|
|
53
57
|
export { getScrollBarSize as default, getTargetScrollBarSize };
|
package/dist/hooks/useEvent.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
declare const useEvent: <T extends Function>(callback: T) => T;
|
|
2
|
+
export default useEvent;
|
package/dist/hooks/useEvent.js
CHANGED
package/dist/hooks/useId.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export default function (id?: string): string;
|
|
|
3
3
|
* Generate a valid HTML id from prefix and key.
|
|
4
4
|
* Sanitizes the key by replacing invalid characters with hyphens.
|
|
5
5
|
* @param prefix - The prefix for the id
|
|
6
|
-
* @param key - The key
|
|
6
|
+
* @param key - The element key, may contain spaces or invalid characters
|
|
7
7
|
* @returns A valid HTML id string
|
|
8
8
|
*/
|
|
9
|
-
export declare function getId(prefix: string, key: string): string;
|
|
9
|
+
export declare function getId(prefix: string, key: string | number): string;
|
package/dist/hooks/useId.js
CHANGED
|
@@ -10,6 +10,6 @@ function useId_default(id) {
|
|
|
10
10
|
return vueId;
|
|
11
11
|
}
|
|
12
12
|
function getId(prefix, key) {
|
|
13
|
-
return `${prefix}-${key.replace(/[^a-zA-Z0-9_.:-]/g, "-")}`;
|
|
13
|
+
return `${prefix}-${String(key).replace(/[^a-zA-Z0-9_.:-]/g, "-")}`;
|
|
14
14
|
}
|
|
15
15
|
export { useId_default as default, getId };
|
package/dist/isMobile.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const
|
|
2
|
-
export default
|
|
1
|
+
declare const getIsMobile: () => boolean;
|
|
2
|
+
export default getIsMobile;
|
package/dist/isMobile.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
var
|
|
1
|
+
var getIsMobile = () => {
|
|
2
2
|
if (typeof navigator === "undefined" || typeof window === "undefined") return false;
|
|
3
3
|
const agent = navigator.userAgent || navigator.vendor || window.opera;
|
|
4
4
|
return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(agent) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-([mpt])|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c([\- _agpst])|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac([ \-/])|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/([klu])|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t([\- ov])|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[23]|n30(0|2)|n50([025])|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan([adt])|pdxg|pg(13|-([1-8c]))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c([\-01])|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(agent?.substr(0, 4));
|
|
5
5
|
};
|
|
6
|
+
var isMobile_default = getIsMobile;
|
|
6
7
|
export { isMobile_default as default };
|