From 1f82bcf5f0a5fd1943b950b4630d41c29505f394 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Sun, 23 Aug 2020 15:06:56 +0800 Subject: [PATCH] feat: add useParent/useChildren --- src/{hooks => api}/use-click-outside.ts | 0 src/{hooks => api}/use-global-event.ts | 2 +- src/{hooks => api}/use-lock-scroll.ts | 0 src/api/use-relation.ts | 29 +++++++++++++++++++++++++ src/{hooks => api}/use-touch.ts | 2 +- 5 files changed, 31 insertions(+), 2 deletions(-) rename src/{hooks => api}/use-click-outside.ts (100%) rename src/{hooks => api}/use-global-event.ts (97%) rename src/{hooks => api}/use-lock-scroll.ts (100%) create mode 100644 src/api/use-relation.ts rename src/{hooks => api}/use-touch.ts (98%) diff --git a/src/hooks/use-click-outside.ts b/src/api/use-click-outside.ts similarity index 100% rename from src/hooks/use-click-outside.ts rename to src/api/use-click-outside.ts diff --git a/src/hooks/use-global-event.ts b/src/api/use-global-event.ts similarity index 97% rename from src/hooks/use-global-event.ts rename to src/api/use-global-event.ts index 319c043bf..8dfb0fff9 100644 --- a/src/hooks/use-global-event.ts +++ b/src/api/use-global-event.ts @@ -5,7 +5,7 @@ import { onMounted, onActivated, onUnmounted, - onDeactivated + onDeactivated, } from 'vue'; export function useGlobalEvent( diff --git a/src/hooks/use-lock-scroll.ts b/src/api/use-lock-scroll.ts similarity index 100% rename from src/hooks/use-lock-scroll.ts rename to src/api/use-lock-scroll.ts diff --git a/src/api/use-relation.ts b/src/api/use-relation.ts new file mode 100644 index 000000000..106819b44 --- /dev/null +++ b/src/api/use-relation.ts @@ -0,0 +1,29 @@ +import { ref, Ref, provide, inject, computed } from 'vue'; + +export type Parent = null | { + children: Ref; +}; + +export function useParent(key: string) { + const children = ref([]); + + provide(key, { children }); + + return { + children, + }; +} + +export function useChildren(key: string, child: unknown) { + const parent = inject(key, null); + const index = computed(() => parent?.children.value.indexOf(child)); + + if (parent) { + parent.children.value.push(child); + } + + return { + index, + parent, + }; +} diff --git a/src/hooks/use-touch.ts b/src/api/use-touch.ts similarity index 98% rename from src/hooks/use-touch.ts rename to src/api/use-touch.ts index 44466fe01..e79d98986 100644 --- a/src/hooks/use-touch.ts +++ b/src/api/use-touch.ts @@ -58,6 +58,6 @@ export function useTouch() { deltaY, offsetX, offsetY, - direction + direction, }; }