@varlet/use 2.10.0 → 2.10.1-alpha.1682608027166

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/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export * from './useEventListener.js';
2
- export * from './useClickOutside.js';
3
- export * from './useMounted.js';
4
- export * from './useParent.js';
5
- export * from './useChildren.js';
1
+ export * from './useEventListener.js';
2
+ export * from './useClickOutside.js';
3
+ export * from './useMounted.js';
4
+ export * from './useParent.js';
5
+ export * from './useChildren.js';
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
- export * from './useEventListener.js';
2
- export * from './useClickOutside.js';
3
- export * from './useMounted.js';
4
- export * from './useParent.js';
5
- export * from './useChildren.js';
1
+ export * from './useEventListener.js';
2
+ export * from './useClickOutside.js';
3
+ export * from './useMounted.js';
4
+ export * from './useParent.js';
5
+ export * from './useChildren.js';
@@ -1,11 +1,11 @@
1
- import { type ComponentInternalInstance, type ComputedRef } from 'vue';
2
- export interface UseChildrenBaseProvider<C> {
3
- childInstances: ComponentInternalInstance[];
4
- collect(instance: ComponentInternalInstance, childProvider: C): void;
5
- clear(instance: ComponentInternalInstance, childProvider: C): void;
6
- }
7
- export declare function useChildren<P, C>(key: symbol | string): {
8
- length: ComputedRef<number>;
9
- childProviders: C[];
10
- bindChildren: (parentProvider: P) => void;
11
- };
1
+ import { type ComponentInternalInstance, type ComputedRef } from 'vue';
2
+ export interface UseChildrenBaseProvider<C> {
3
+ childInstances: ComponentInternalInstance[];
4
+ collect(instance: ComponentInternalInstance, childProvider: C): void;
5
+ clear(instance: ComponentInternalInstance, childProvider: C): void;
6
+ }
7
+ export declare function useChildren<P, C>(key: symbol | string): {
8
+ length: ComputedRef<number>;
9
+ childProviders: C[];
10
+ bindChildren: (parentProvider: P) => void;
11
+ };
@@ -1,52 +1,52 @@
1
- import { removeItem } from '@varlet/shared';
2
- import { getCurrentInstance, computed, provide, reactive, isVNode, } from 'vue';
3
- function flatVNodes(subTree) {
4
- const vNodes = [];
5
- const flat = (subTree) => {
6
- if (subTree === null || subTree === void 0 ? void 0 : subTree.component) {
7
- flat(subTree === null || subTree === void 0 ? void 0 : subTree.component.subTree);
8
- return;
9
- }
10
- if (Array.isArray(subTree === null || subTree === void 0 ? void 0 : subTree.children)) {
11
- subTree.children.forEach((child) => {
12
- if (isVNode(child)) {
13
- vNodes.push(child);
14
- flat(child);
15
- }
16
- });
17
- }
18
- };
19
- flat(subTree);
20
- return vNodes;
21
- }
22
- export function useChildren(key) {
23
- const parentInstance = getCurrentInstance();
24
- const childInstances = reactive([]);
25
- const childProviders = [];
26
- const length = computed(() => childInstances.length);
27
- const sortInstances = () => {
28
- const vNodes = flatVNodes(parentInstance.subTree);
29
- childInstances.sort((a, b) => {
30
- return vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode);
31
- });
32
- };
33
- const collect = (childInstance, childProvider) => {
34
- childInstances.push(childInstance);
35
- childProviders.push(childProvider);
36
- sortInstances();
37
- };
38
- const clear = (childInstance, childProvider) => {
39
- removeItem(childInstances, childInstance);
40
- removeItem(childProviders, childProvider);
41
- };
42
- const bindChildren = (parentProvider) => {
43
- provide(key, Object.assign({ childInstances,
44
- collect,
45
- clear }, parentProvider));
46
- };
47
- return {
48
- length,
49
- childProviders,
50
- bindChildren,
51
- };
52
- }
1
+ import { removeItem } from '@varlet/shared';
2
+ import { getCurrentInstance, computed, provide, reactive, isVNode, } from 'vue';
3
+ function flatVNodes(subTree) {
4
+ const vNodes = [];
5
+ const flat = (subTree) => {
6
+ if (subTree === null || subTree === void 0 ? void 0 : subTree.component) {
7
+ flat(subTree === null || subTree === void 0 ? void 0 : subTree.component.subTree);
8
+ return;
9
+ }
10
+ if (Array.isArray(subTree === null || subTree === void 0 ? void 0 : subTree.children)) {
11
+ subTree.children.forEach((child) => {
12
+ if (isVNode(child)) {
13
+ vNodes.push(child);
14
+ flat(child);
15
+ }
16
+ });
17
+ }
18
+ };
19
+ flat(subTree);
20
+ return vNodes;
21
+ }
22
+ export function useChildren(key) {
23
+ const parentInstance = getCurrentInstance();
24
+ const childInstances = reactive([]);
25
+ const childProviders = [];
26
+ const length = computed(() => childInstances.length);
27
+ const sortInstances = () => {
28
+ const vNodes = flatVNodes(parentInstance.subTree);
29
+ childInstances.sort((a, b) => {
30
+ return vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode);
31
+ });
32
+ };
33
+ const collect = (childInstance, childProvider) => {
34
+ childInstances.push(childInstance);
35
+ childProviders.push(childProvider);
36
+ sortInstances();
37
+ };
38
+ const clear = (childInstance, childProvider) => {
39
+ removeItem(childInstances, childInstance);
40
+ removeItem(childProviders, childProvider);
41
+ };
42
+ const bindChildren = (parentProvider) => {
43
+ provide(key, Object.assign({ childInstances,
44
+ collect,
45
+ clear }, parentProvider));
46
+ };
47
+ return {
48
+ length,
49
+ childProviders,
50
+ bindChildren,
51
+ };
52
+ }
@@ -1,3 +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;
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;
@@ -1,15 +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
- }
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
+ }
@@ -1,8 +1,8 @@
1
- import { type Ref } from 'vue';
2
- export declare type UseEventListenerTarget = EventTarget | Ref<EventTarget | undefined | null> | (() => EventTarget);
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;
1
+ import { type Ref } from 'vue';
2
+ export declare type UseEventListenerTarget = EventTarget | Ref<EventTarget | undefined | null> | (() => EventTarget);
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;
@@ -1,61 +1,61 @@
1
- import { inBrowser, isFunction } 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 getElement = (target) => {
12
- return isFunction(target) ? target() : unref(target);
13
- };
14
- const add = (target) => {
15
- if (listening || cleaned) {
16
- return;
17
- }
18
- const element = getElement(target);
19
- if (element) {
20
- element.addEventListener(type, listener, {
21
- passive,
22
- capture,
23
- });
24
- listening = true;
25
- }
26
- };
27
- const remove = (target) => {
28
- if (!listening || cleaned) {
29
- return;
30
- }
31
- const element = getElement(target);
32
- if (element) {
33
- element.removeEventListener(type, listener, {
34
- capture,
35
- });
36
- listening = false;
37
- }
38
- };
39
- let watchStopHandle;
40
- if (isRef(target)) {
41
- watchStopHandle = watch(() => target.value, (newValue, oldValue) => {
42
- remove(oldValue);
43
- add(newValue);
44
- });
45
- }
46
- const cleanup = () => {
47
- watchStopHandle === null || watchStopHandle === void 0 ? void 0 : watchStopHandle();
48
- remove(target);
49
- cleaned = true;
50
- };
51
- useMounted(() => {
52
- add(target);
53
- });
54
- onBeforeUnmount(() => {
55
- remove(target);
56
- });
57
- onDeactivated(() => {
58
- remove(target);
59
- });
60
- return cleanup;
61
- }
1
+ import { inBrowser, isFunction } 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 getElement = (target) => {
12
+ return isFunction(target) ? target() : unref(target);
13
+ };
14
+ const add = (target) => {
15
+ if (listening || cleaned) {
16
+ return;
17
+ }
18
+ const element = getElement(target);
19
+ if (element) {
20
+ element.addEventListener(type, listener, {
21
+ passive,
22
+ capture,
23
+ });
24
+ listening = true;
25
+ }
26
+ };
27
+ const remove = (target) => {
28
+ if (!listening || cleaned) {
29
+ return;
30
+ }
31
+ const element = getElement(target);
32
+ if (element) {
33
+ element.removeEventListener(type, listener, {
34
+ capture,
35
+ });
36
+ listening = false;
37
+ }
38
+ };
39
+ let watchStopHandle;
40
+ if (isRef(target)) {
41
+ watchStopHandle = watch(() => target.value, (newValue, oldValue) => {
42
+ remove(oldValue);
43
+ add(newValue);
44
+ });
45
+ }
46
+ const cleanup = () => {
47
+ watchStopHandle === null || watchStopHandle === void 0 ? void 0 : watchStopHandle();
48
+ remove(target);
49
+ cleaned = true;
50
+ };
51
+ useMounted(() => {
52
+ add(target);
53
+ });
54
+ onBeforeUnmount(() => {
55
+ remove(target);
56
+ });
57
+ onDeactivated(() => {
58
+ remove(target);
59
+ });
60
+ return cleanup;
61
+ }
@@ -1 +1 @@
1
- export declare function useMounted(hook: () => void): void;
1
+ export declare function useMounted(hook: () => void): void;
package/lib/useMounted.js CHANGED
@@ -1,16 +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
- }
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
+ }
@@ -1,12 +1,12 @@
1
- import { type ComputedRef } from 'vue';
2
- import { type UseChildrenBaseProvider } from './useChildren.js';
3
- export declare function keyInProvides(key: symbol | string): boolean;
4
- export declare function useParent<P, C>(key: symbol | string): {
5
- index: null;
6
- parentProvider: null;
7
- bindParent: null;
8
- } | {
9
- index: ComputedRef<number>;
10
- parentProvider: Omit<P & UseChildrenBaseProvider<C>, "childInstances" | "collect" | "clear">;
11
- bindParent: (childProvider: C) => void;
12
- };
1
+ import { type ComputedRef } from 'vue';
2
+ import { type UseChildrenBaseProvider } from './useChildren.js';
3
+ export declare function keyInProvides(key: symbol | string): boolean;
4
+ export declare function useParent<P, C>(key: symbol | string): {
5
+ index: null;
6
+ parentProvider: null;
7
+ bindParent: null;
8
+ } | {
9
+ index: ComputedRef<number>;
10
+ parentProvider: Omit<P & UseChildrenBaseProvider<C>, "childInstances" | "collect" | "clear">;
11
+ bindParent: (childProvider: C) => void;
12
+ };
package/lib/useParent.js CHANGED
@@ -1,46 +1,46 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
- import { getCurrentInstance, inject, onMounted, onBeforeUnmount, nextTick, computed, } from 'vue';
13
- export function keyInProvides(key) {
14
- const instance = getCurrentInstance();
15
- return key in instance.provides;
16
- }
17
- export function useParent(key) {
18
- if (!keyInProvides(key)) {
19
- return {
20
- index: null,
21
- parentProvider: null,
22
- bindParent: null,
23
- };
24
- }
25
- const provider = inject(key);
26
- const { childInstances, collect, clear } = provider, parentProvider = __rest(provider, ["childInstances", "collect", "clear"]);
27
- const childInstance = getCurrentInstance();
28
- const index = computed(() => childInstances.indexOf(childInstance));
29
- const bindParent = (childProvider) => {
30
- onMounted(() => {
31
- nextTick().then(() => {
32
- collect(childInstance, childProvider);
33
- });
34
- });
35
- onBeforeUnmount(() => {
36
- nextTick().then(() => {
37
- clear(childInstance, childProvider);
38
- });
39
- });
40
- };
41
- return {
42
- index,
43
- parentProvider,
44
- bindParent,
45
- };
46
- }
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { getCurrentInstance, inject, onMounted, onBeforeUnmount, nextTick, computed, } from 'vue';
13
+ export function keyInProvides(key) {
14
+ const instance = getCurrentInstance();
15
+ return key in instance.provides;
16
+ }
17
+ export function useParent(key) {
18
+ if (!keyInProvides(key)) {
19
+ return {
20
+ index: null,
21
+ parentProvider: null,
22
+ bindParent: null,
23
+ };
24
+ }
25
+ const provider = inject(key);
26
+ const { childInstances, collect, clear } = provider, parentProvider = __rest(provider, ["childInstances", "collect", "clear"]);
27
+ const childInstance = getCurrentInstance();
28
+ const index = computed(() => childInstances.indexOf(childInstance));
29
+ const bindParent = (childProvider) => {
30
+ onMounted(() => {
31
+ nextTick().then(() => {
32
+ collect(childInstance, childProvider);
33
+ });
34
+ });
35
+ onBeforeUnmount(() => {
36
+ nextTick().then(() => {
37
+ clear(childInstance, childProvider);
38
+ });
39
+ });
40
+ };
41
+ return {
42
+ index,
43
+ parentProvider,
44
+ bindParent,
45
+ };
46
+ }
@@ -1,7 +1,7 @@
1
- export interface UseVModelOptions<P, K extends keyof P> {
2
- passive?: boolean;
3
- eventName?: string;
4
- defaultValue?: P[K];
5
- emit?: (event: string, value: P[K]) => void;
6
- }
7
- export declare function useVModel<P extends Record<string, any>, K extends keyof P>(props: P, key: K, options?: UseVModelOptions<P, K>): import("vue").WritableComputedRef<P[K]> | ([P[K]] extends [import("vue").Ref<any>] ? P[K] : import("vue").Ref<import("vue").UnwrapRef<P[K]>>);
1
+ export interface UseVModelOptions<P, K extends keyof P> {
2
+ passive?: boolean;
3
+ eventName?: string;
4
+ defaultValue?: P[K];
5
+ emit?: (event: string, value: P[K]) => void;
6
+ }
7
+ export declare function useVModel<P extends Record<string, any>, K extends keyof P>(props: P, key: K, options?: UseVModelOptions<P, K>): import("vue").WritableComputedRef<P[K]> | ([P[K]] extends [import("vue").Ref<any>] ? P[K] : import("vue").Ref<import("vue").UnwrapRef<P[K]>>);
package/lib/useVModel.js CHANGED
@@ -1,25 +1,25 @@
1
- import { computed, getCurrentInstance, ref, watch } from 'vue';
2
- export function useVModel(props, key, options = {}) {
3
- const vm = getCurrentInstance();
4
- const { passive = true, eventName, defaultValue, emit = vm === null || vm === void 0 ? void 0 : vm.emit } = options;
5
- const event = eventName !== null && eventName !== void 0 ? eventName : `update:${key.toString()}`;
6
- const getValue = () => (props[key] != null ? props[key] : defaultValue);
7
- if (!passive) {
8
- return computed({
9
- get() {
10
- return getValue();
11
- },
12
- set(value) {
13
- emit(event, value);
14
- },
15
- });
16
- }
17
- const proxy = ref(getValue());
18
- watch(() => props[key], () => {
19
- proxy.value = getValue();
20
- });
21
- watch(() => proxy.value, (newValue) => {
22
- emit(event, newValue);
23
- });
24
- return proxy;
25
- }
1
+ import { computed, getCurrentInstance, ref, watch } from 'vue';
2
+ export function useVModel(props, key, options = {}) {
3
+ const vm = getCurrentInstance();
4
+ const { passive = true, eventName, defaultValue, emit = vm === null || vm === void 0 ? void 0 : vm.emit } = options;
5
+ const event = eventName !== null && eventName !== void 0 ? eventName : `update:${key.toString()}`;
6
+ const getValue = () => (props[key] != null ? props[key] : defaultValue);
7
+ if (!passive) {
8
+ return computed({
9
+ get() {
10
+ return getValue();
11
+ },
12
+ set(value) {
13
+ emit(event, value);
14
+ },
15
+ });
16
+ }
17
+ const proxy = ref(getValue());
18
+ watch(() => props[key], () => {
19
+ proxy.value = getValue();
20
+ });
21
+ watch(() => proxy.value, (newValue) => {
22
+ emit(event, newValue);
23
+ });
24
+ return proxy;
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/use",
3
- "version": "2.10.0",
3
+ "version": "2.10.1-alpha.1682608027166",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.js",
@@ -24,7 +24,7 @@
24
24
  "url": "https://github.com/varletjs/varlet/issues"
25
25
  },
26
26
  "dependencies": {
27
- "@varlet/shared": "2.10.0"
27
+ "@varlet/shared": "2.10.1-alpha.1682608027166"
28
28
  },
29
29
  "devDependencies": {
30
30
  "typescript": "^4.4.4",
@@ -1,12 +0,0 @@
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
@@ -1,63 +0,0 @@
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
- }