@varlet/use 2.8.6-alpha.1678026101378

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/LICENCE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 varlet
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/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './useEventListener.js';
2
+ export * from './useClickOutside.js';
3
+ export * from './useMounted';
package/lib/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './useEventListener.js';
2
+ export * from './useClickOutside.js';
3
+ export * from './useMounted';
@@ -0,0 +1,3 @@
1
+ import { type Ref } from 'vue';
2
+ export declare type UseClickOutsideTarget = Element | Ref<Element | undefined | null>;
3
+ export declare function useClickOutside(target: UseClickOutsideTarget, type: string, listener: EventListener): void;
@@ -0,0 +1,15 @@
1
+ import { useEventListener } from './useEventListener.js';
2
+ import { inBrowser } from '@varlet/shared';
3
+ import { unref } from 'vue';
4
+ export function useClickOutside(target, type, listener) {
5
+ if (!inBrowser()) {
6
+ return;
7
+ }
8
+ const handler = (event) => {
9
+ const element = unref(target);
10
+ if (element && !element.contains(event.target)) {
11
+ listener(event);
12
+ }
13
+ };
14
+ useEventListener(document, type, handler);
15
+ }
@@ -0,0 +1,12 @@
1
+ import { type Ref } from 'vue'
2
+ export declare type UseEventListenerTarget = EventTarget | undefined | Ref<EventTarget | undefined>
3
+ export interface UseEventListenerOptions {
4
+ capture?: boolean
5
+ passive?: boolean
6
+ }
7
+ export declare function useEventListener<T extends keyof DocumentEventMap>(
8
+ target: UseEventListenerTarget,
9
+ type: T,
10
+ listener: (event: DocumentEventMap[T]) => void,
11
+ options?: UseEventListenerOptions
12
+ ): () => void
@@ -0,0 +1,63 @@
1
+ import { inBrowser } from '@varlet/shared'
2
+ import { isRef, onActivated, onDeactivated, onMounted, onUnmounted, unref, watch } from 'vue'
3
+ export function useEventListener(target, type, listener, options = {}) {
4
+ if (!inBrowser()) {
5
+ return
6
+ }
7
+ const { passive = false, capture = false } = options
8
+ let listening = false
9
+ let cleaned = false
10
+ const add = (target) => {
11
+ if (listening || cleaned) {
12
+ return
13
+ }
14
+ const element = unref(target)
15
+ if (element) {
16
+ element.addEventListener(type, listener, {
17
+ passive,
18
+ capture,
19
+ })
20
+ listening = true
21
+ }
22
+ }
23
+ const remove = (target) => {
24
+ if (!listening || cleaned) {
25
+ return
26
+ }
27
+ const element = unref(target)
28
+ if (element) {
29
+ element.removeEventListener(type, listener, {
30
+ capture,
31
+ })
32
+ listening = false
33
+ }
34
+ }
35
+ let watchStopHandle
36
+ if (isRef(target)) {
37
+ watchStopHandle = watch(
38
+ () => target.value,
39
+ (newValue, oldValue) => {
40
+ remove(oldValue)
41
+ add(newValue)
42
+ }
43
+ )
44
+ }
45
+ const cleanup = () => {
46
+ watchStopHandle === null || watchStopHandle === void 0 ? void 0 : watchStopHandle()
47
+ remove(target)
48
+ cleaned = true
49
+ }
50
+ onMounted(() => {
51
+ add(target)
52
+ })
53
+ onActivated(() => {
54
+ add(target)
55
+ })
56
+ onUnmounted(() => {
57
+ remove(target)
58
+ })
59
+ onDeactivated(() => {
60
+ remove(target)
61
+ })
62
+ return cleanup
63
+ }
@@ -0,0 +1,8 @@
1
+ import { type Ref } from 'vue';
2
+ export declare type UseEventListenerTarget = EventTarget | Ref<EventTarget | undefined | null>;
3
+ export interface UseEventListenerOptions {
4
+ capture?: boolean;
5
+ passive?: boolean;
6
+ }
7
+ export declare function useEventListener<T extends keyof DocumentEventMap>(target: UseEventListenerTarget, type: T, listener: (event: DocumentEventMap[T]) => void, options?: UseEventListenerOptions): () => void;
8
+ export declare function useEventListener(target: UseEventListenerTarget, type: string, listener: EventListener, options?: UseEventListenerOptions): () => void;
@@ -0,0 +1,58 @@
1
+ import { inBrowser } from '@varlet/shared';
2
+ import { isRef, onDeactivated, onBeforeUnmount, unref, watch } from 'vue';
3
+ import { useMounted } from './useMounted.js';
4
+ export function useEventListener(target, type, listener, options = {}) {
5
+ if (!inBrowser()) {
6
+ return;
7
+ }
8
+ const { passive = false, capture = false } = options;
9
+ let listening = false;
10
+ let cleaned = false;
11
+ const add = (target) => {
12
+ if (listening || cleaned) {
13
+ return;
14
+ }
15
+ const element = unref(target);
16
+ if (element) {
17
+ element.addEventListener(type, listener, {
18
+ passive,
19
+ capture,
20
+ });
21
+ listening = true;
22
+ }
23
+ };
24
+ const remove = (target) => {
25
+ if (!listening || cleaned) {
26
+ return;
27
+ }
28
+ const element = unref(target);
29
+ if (element) {
30
+ element.removeEventListener(type, listener, {
31
+ capture,
32
+ });
33
+ listening = false;
34
+ }
35
+ };
36
+ let watchStopHandle;
37
+ if (isRef(target)) {
38
+ watchStopHandle = watch(() => target.value, (newValue, oldValue) => {
39
+ remove(oldValue);
40
+ add(newValue);
41
+ });
42
+ }
43
+ const cleanup = () => {
44
+ watchStopHandle === null || watchStopHandle === void 0 ? void 0 : watchStopHandle();
45
+ remove(target);
46
+ cleaned = true;
47
+ };
48
+ useMounted(() => {
49
+ add(target);
50
+ });
51
+ onBeforeUnmount(() => {
52
+ remove(target);
53
+ });
54
+ onDeactivated(() => {
55
+ remove(target);
56
+ });
57
+ return cleanup;
58
+ }
@@ -0,0 +1 @@
1
+ export declare function useMounted(hook: () => void): void;
@@ -0,0 +1,16 @@
1
+ import { onMounted, nextTick, onActivated } from 'vue';
2
+ export function useMounted(hook) {
3
+ let isMounted = false;
4
+ onMounted(() => {
5
+ hook();
6
+ nextTick(() => {
7
+ isMounted = true;
8
+ });
9
+ });
10
+ onActivated(() => {
11
+ if (!isMounted) {
12
+ return;
13
+ }
14
+ hook();
15
+ });
16
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@varlet/use",
3
+ "version": "2.8.6-alpha.1678026101378",
4
+ "type": "module",
5
+ "main": "lib/index.js",
6
+ "module": "lib/index.js",
7
+ "description": "composable utils of varlet",
8
+ "keywords": [
9
+ "composable",
10
+ "utils",
11
+ "varlet"
12
+ ],
13
+ "author": "haoziqaq <357229046@qq.com>",
14
+ "license": "MIT",
15
+ "files": [
16
+ "lib",
17
+ "tsconfig.json"
18
+ ],
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/varletjs/varlet.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/varletjs/varlet/issues"
25
+ },
26
+ "dependencies": {
27
+ "@varlet/shared": "2.8.6-alpha.1678026101378"
28
+ },
29
+ "devDependencies": {
30
+ "typescript": "^4.4.4",
31
+ "vue": "3.2.25"
32
+ },
33
+ "peerDependencies": {
34
+ "vue": "3.2.25"
35
+ },
36
+ "scripts": {
37
+ "dev": "tsc --watch",
38
+ "build": "tsc"
39
+ }
40
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./lib"
5
+ }
6
+ }