@v-c/util 1.0.13 → 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.
@@ -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
- export default function getScrollBarSize(fresh?: boolean): number;
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 {};
@@ -1,53 +1,57 @@
1
+ import { removeCSS, updateCSS } from "./Dom/dynamicCSS.js";
1
2
  var cached;
2
- function getScrollBarSize(fresh) {
3
- if (typeof document === "undefined") return 0;
4
- if (fresh || cached === void 0) {
5
- const inner = document.createElement("div");
6
- inner.style.width = "100%";
7
- inner.style.height = "200px";
8
- const outer = document.createElement("div");
9
- const outerStyle = outer.style;
10
- outerStyle.position = "absolute";
11
- outerStyle.top = "0";
12
- outerStyle.left = "0";
13
- outerStyle.pointerEvents = "none";
14
- outerStyle.visibility = "hidden";
15
- outerStyle.width = "200px";
16
- outerStyle.height = "150px";
17
- outerStyle.overflow = "hidden";
18
- outer.appendChild(inner);
19
- document.body.appendChild(outer);
20
- const widthContained = inner.offsetWidth;
21
- outer.style.overflow = "scroll";
22
- let widthScroll = inner.offsetWidth;
23
- if (widthContained === widthScroll) widthScroll = outer.clientWidth;
24
- document.body.removeChild(outer);
25
- cached = widthContained - widthScroll;
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
- return cached;
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 ensureSize(str) {
30
- const match = str.match(/^(.*)px$/);
31
- const value = Number(match?.[1]);
32
- return Number.isNaN(value) ? getScrollBarSize() : value;
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
- if (typeof navigator !== "undefined" && /jsdom/i.test(navigator.userAgent)) return {
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 };
@@ -1 +1,2 @@
1
- export default function useEvent<T extends Function>(callback: T): T;
1
+ declare const useEvent: <T extends Function>(callback: T) => T;
2
+ export default useEvent;
@@ -1,4 +1,5 @@
1
- function useEvent(callback) {
1
+ var useEvent = (callback) => {
2
2
  return callback;
3
- }
4
- export { useEvent as default };
3
+ };
4
+ var useEvent_default = useEvent;
5
+ export { useEvent_default as default };
@@ -1 +1,9 @@
1
1
  export default function (id?: string): string;
2
+ /**
3
+ * Generate a valid HTML id from prefix and key.
4
+ * Sanitizes the key by replacing invalid characters with hyphens.
5
+ * @param prefix - The prefix for the id
6
+ * @param key - The element key, may contain spaces or invalid characters
7
+ * @returns A valid HTML id string
8
+ */
9
+ export declare function getId(prefix: string, key: string | number): string;
@@ -9,4 +9,7 @@ function useId_default(id) {
9
9
  if (process.env.NODE_ENV === "test") return "test-id";
10
10
  return vueId;
11
11
  }
12
- export { useId_default as default };
12
+ function getId(prefix, key) {
13
+ return `${prefix}-${String(key).replace(/[^a-zA-Z0-9_.:-]/g, "-")}`;
14
+ }
15
+ export { useId_default as default, getId };
@@ -1,2 +1,2 @@
1
- declare const _default: () => boolean;
2
- export default _default;
1
+ declare const getIsMobile: () => boolean;
2
+ export default getIsMobile;
package/dist/isMobile.js CHANGED
@@ -1,6 +1,7 @@
1
- var isMobile_default = () => {
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 };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@v-c/util",
3
3
  "type": "module",
4
- "version": "1.0.13",
4
+ "version": "1.0.15",
5
5
  "description": "Vue3 components utils",
6
6
  "publishConfig": {
7
7
  "access": "public"