@deot/vc-hooks 1.0.1

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/README.md ADDED
@@ -0,0 +1 @@
1
+ // TODO
package/dist/index.cjs ADDED
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const vue = require('vue');
6
+
7
+ const useAttrs = (options) => {
8
+ const attrs = vue.useAttrs();
9
+ const { merge = true, exclude = [] } = options || {};
10
+ return vue.computed(() => {
11
+ if (merge && !exclude.length) return attrs;
12
+ const result = Object.entries(attrs).reduce((pre, [key, val]) => {
13
+ if (exclude.includes(key)) return pre;
14
+ if (!merge && /^on([A-Z])/.test(key)) {
15
+ pre.listeners[key] = val;
16
+ } else if (!merge && /(class|style)/.test(key)) {
17
+ pre[key] = val;
18
+ } else {
19
+ pre.attrs[key] = val;
20
+ }
21
+ return pre;
22
+ }, {
23
+ style: void 0,
24
+ class: void 0,
25
+ attrs: {},
26
+ listeners: {}
27
+ });
28
+ return merge ? result.attrs : result;
29
+ });
30
+ };
31
+
32
+ const useScrollbar = (visibleRef) => {
33
+ let original = "";
34
+ let isMounted = false;
35
+ const setScrollBar = (v) => {
36
+ if (!isMounted || original === "hidden") return;
37
+ if (v) {
38
+ document.body.style.overflow = "hidden";
39
+ } else {
40
+ document.body.style.removeProperty("overflow");
41
+ }
42
+ };
43
+ vue.watch(
44
+ () => visibleRef.value,
45
+ (v) => {
46
+ setScrollBar(v);
47
+ },
48
+ { immediate: false }
49
+ );
50
+ vue.onMounted(() => {
51
+ isMounted = true;
52
+ original = document.body.style.overflow;
53
+ visibleRef.value && setScrollBar(true);
54
+ });
55
+ vue.onBeforeUnmount(() => {
56
+ setScrollBar(false);
57
+ });
58
+ };
59
+
60
+ const getInstance = (componentName, privateKey) => {
61
+ const instance = vue.getCurrentInstance();
62
+ const regex = new RegExp(`${componentName}$`);
63
+ let parent = instance.parent;
64
+ /* istanbul ignore next -- @preserve */
65
+ while (parent && !(parent?.type?.name && regex.test(parent.type.name)) && (!privateKey || !parent?.[privateKey] || !parent?.proxy?.[privateKey])) {
66
+ parent = parent.parent || parent?.type?.parent;
67
+ }
68
+ return parent;
69
+ };
70
+
71
+ exports.getInstance = getInstance;
72
+ exports.useAttrs = useAttrs;
73
+ exports.useScrollbar = useScrollbar;
@@ -0,0 +1,25 @@
1
+ import { ComponentInternalInstance } from 'vue';
2
+ import type { ComputedRef } from 'vue';
3
+ import type { Ref } from 'vue';
4
+ import type { StyleValue } from 'vue';
5
+
6
+ declare type Attrs = {
7
+ [key: string]: any;
8
+ style?: StyleValue;
9
+ class?: StyleValue;
10
+ attrs?: Record<string, any>;
11
+ listeners?: Record<string, (...args: any[]) => any>;
12
+ };
13
+
14
+ export declare const getInstance: (componentName: string, privateKey: string) => ComponentInternalInstance | null;
15
+
16
+ declare type Options = {
17
+ merge?: boolean;
18
+ exclude?: string[];
19
+ };
20
+
21
+ export declare const useAttrs: (options?: Options) => ComputedRef<Attrs>;
22
+
23
+ export declare const useScrollbar: (visibleRef: Ref<boolean> | ComputedRef) => void;
24
+
25
+ export { }
@@ -0,0 +1,76 @@
1
+ var VcHooks = (function (exports, vue) {
2
+ 'use strict';
3
+
4
+ const useAttrs = (options) => {
5
+ const attrs = vue.useAttrs();
6
+ const { merge = true, exclude = [] } = options || {};
7
+ return vue.computed(() => {
8
+ if (merge && !exclude.length) return attrs;
9
+ const result = Object.entries(attrs).reduce((pre, [key, val]) => {
10
+ if (exclude.includes(key)) return pre;
11
+ if (!merge && /^on([A-Z])/.test(key)) {
12
+ pre.listeners[key] = val;
13
+ } else if (!merge && /(class|style)/.test(key)) {
14
+ pre[key] = val;
15
+ } else {
16
+ pre.attrs[key] = val;
17
+ }
18
+ return pre;
19
+ }, {
20
+ style: void 0,
21
+ class: void 0,
22
+ attrs: {},
23
+ listeners: {}
24
+ });
25
+ return merge ? result.attrs : result;
26
+ });
27
+ };
28
+
29
+ const useScrollbar = (visibleRef) => {
30
+ let original = "";
31
+ let isMounted = false;
32
+ const setScrollBar = (v) => {
33
+ if (!isMounted || original === "hidden") return;
34
+ if (v) {
35
+ document.body.style.overflow = "hidden";
36
+ } else {
37
+ document.body.style.removeProperty("overflow");
38
+ }
39
+ };
40
+ vue.watch(
41
+ () => visibleRef.value,
42
+ (v) => {
43
+ setScrollBar(v);
44
+ },
45
+ { immediate: false }
46
+ );
47
+ vue.onMounted(() => {
48
+ isMounted = true;
49
+ original = document.body.style.overflow;
50
+ visibleRef.value && setScrollBar(true);
51
+ });
52
+ vue.onBeforeUnmount(() => {
53
+ setScrollBar(false);
54
+ });
55
+ };
56
+
57
+ const getInstance = (componentName, privateKey) => {
58
+ const instance = vue.getCurrentInstance();
59
+ const regex = new RegExp(`${componentName}$`);
60
+ let parent = instance.parent;
61
+ /* istanbul ignore next -- @preserve */
62
+ while (parent && !(parent?.type?.name && regex.test(parent.type.name)) && (!privateKey || !parent?.[privateKey] || !parent?.proxy?.[privateKey])) {
63
+ parent = parent.parent || parent?.type?.parent;
64
+ }
65
+ return parent;
66
+ };
67
+
68
+ exports.getInstance = getInstance;
69
+ exports.useAttrs = useAttrs;
70
+ exports.useScrollbar = useScrollbar;
71
+
72
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
73
+
74
+ return exports;
75
+
76
+ })({}, Vue);
package/dist/index.js ADDED
@@ -0,0 +1,67 @@
1
+ import { useAttrs as useAttrs$1, computed, watch, onMounted, onBeforeUnmount, getCurrentInstance } from 'vue';
2
+
3
+ const useAttrs = (options) => {
4
+ const attrs = useAttrs$1();
5
+ const { merge = true, exclude = [] } = options || {};
6
+ return computed(() => {
7
+ if (merge && !exclude.length) return attrs;
8
+ const result = Object.entries(attrs).reduce((pre, [key, val]) => {
9
+ if (exclude.includes(key)) return pre;
10
+ if (!merge && /^on([A-Z])/.test(key)) {
11
+ pre.listeners[key] = val;
12
+ } else if (!merge && /(class|style)/.test(key)) {
13
+ pre[key] = val;
14
+ } else {
15
+ pre.attrs[key] = val;
16
+ }
17
+ return pre;
18
+ }, {
19
+ style: void 0,
20
+ class: void 0,
21
+ attrs: {},
22
+ listeners: {}
23
+ });
24
+ return merge ? result.attrs : result;
25
+ });
26
+ };
27
+
28
+ const useScrollbar = (visibleRef) => {
29
+ let original = "";
30
+ let isMounted = false;
31
+ const setScrollBar = (v) => {
32
+ if (!isMounted || original === "hidden") return;
33
+ if (v) {
34
+ document.body.style.overflow = "hidden";
35
+ } else {
36
+ document.body.style.removeProperty("overflow");
37
+ }
38
+ };
39
+ watch(
40
+ () => visibleRef.value,
41
+ (v) => {
42
+ setScrollBar(v);
43
+ },
44
+ { immediate: false }
45
+ );
46
+ onMounted(() => {
47
+ isMounted = true;
48
+ original = document.body.style.overflow;
49
+ visibleRef.value && setScrollBar(true);
50
+ });
51
+ onBeforeUnmount(() => {
52
+ setScrollBar(false);
53
+ });
54
+ };
55
+
56
+ const getInstance = (componentName, privateKey) => {
57
+ const instance = getCurrentInstance();
58
+ const regex = new RegExp(`${componentName}$`);
59
+ let parent = instance.parent;
60
+ /* istanbul ignore next -- @preserve */
61
+ while (parent && !(parent?.type?.name && regex.test(parent.type.name)) && (!privateKey || !parent?.[privateKey] || !parent?.proxy?.[privateKey])) {
62
+ parent = parent.parent || parent?.type?.parent;
63
+ }
64
+ return parent;
65
+ };
66
+
67
+ export { getInstance, useAttrs, useScrollbar };
@@ -0,0 +1,77 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vue')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'vue'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.VcHooks = {}, global.Vue));
5
+ })(this, (function (exports, vue) { 'use strict';
6
+
7
+ const useAttrs = (options) => {
8
+ const attrs = vue.useAttrs();
9
+ const { merge = true, exclude = [] } = options || {};
10
+ return vue.computed(() => {
11
+ if (merge && !exclude.length) return attrs;
12
+ const result = Object.entries(attrs).reduce((pre, [key, val]) => {
13
+ if (exclude.includes(key)) return pre;
14
+ if (!merge && /^on([A-Z])/.test(key)) {
15
+ pre.listeners[key] = val;
16
+ } else if (!merge && /(class|style)/.test(key)) {
17
+ pre[key] = val;
18
+ } else {
19
+ pre.attrs[key] = val;
20
+ }
21
+ return pre;
22
+ }, {
23
+ style: void 0,
24
+ class: void 0,
25
+ attrs: {},
26
+ listeners: {}
27
+ });
28
+ return merge ? result.attrs : result;
29
+ });
30
+ };
31
+
32
+ const useScrollbar = (visibleRef) => {
33
+ let original = "";
34
+ let isMounted = false;
35
+ const setScrollBar = (v) => {
36
+ if (!isMounted || original === "hidden") return;
37
+ if (v) {
38
+ document.body.style.overflow = "hidden";
39
+ } else {
40
+ document.body.style.removeProperty("overflow");
41
+ }
42
+ };
43
+ vue.watch(
44
+ () => visibleRef.value,
45
+ (v) => {
46
+ setScrollBar(v);
47
+ },
48
+ { immediate: false }
49
+ );
50
+ vue.onMounted(() => {
51
+ isMounted = true;
52
+ original = document.body.style.overflow;
53
+ visibleRef.value && setScrollBar(true);
54
+ });
55
+ vue.onBeforeUnmount(() => {
56
+ setScrollBar(false);
57
+ });
58
+ };
59
+
60
+ const getInstance = (componentName, privateKey) => {
61
+ const instance = vue.getCurrentInstance();
62
+ const regex = new RegExp(`${componentName}$`);
63
+ let parent = instance.parent;
64
+ /* istanbul ignore next -- @preserve */
65
+ while (parent && !(parent?.type?.name && regex.test(parent.type.name)) && (!privateKey || !parent?.[privateKey] || !parent?.proxy?.[privateKey])) {
66
+ parent = parent.parent || parent?.type?.parent;
67
+ }
68
+ return parent;
69
+ };
70
+
71
+ exports.getInstance = getInstance;
72
+ exports.useAttrs = useAttrs;
73
+ exports.useScrollbar = useScrollbar;
74
+
75
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
76
+
77
+ }));
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@deot/vc-hooks",
3
+ "version": "1.0.1",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/index.cjs",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "license": "MIT",
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "peerDependencies": {
22
+ "vue": "*"
23
+ },
24
+ "peerDependenciesMeta": {
25
+ "vue": {
26
+ "optional": true
27
+ }
28
+ },
29
+ "dependencies": {},
30
+ "devDependencies": {}
31
+ }