From 1e32a33d9c47c69ea328e9556b97fee6110dcfaa Mon Sep 17 00:00:00 2001 From: wangxingkang Date: Sun, 28 Aug 2022 23:06:49 +0800 Subject: [PATCH] feat: replace getTargetElement import --- hooks/useClickAway/package.json | 1 + hooks/useClickAway/src/index.ts | 4 +- hooks/useEventListener/package.json | 1 + hooks/useEventListener/src/index.ts | 4 +- hooks/useHover/package.json | 2 +- hooks/useHover/src/index.ts | 3 +- hooks/useInViewport/package.json | 1 + hooks/useInViewport/src/index.ts | 5 ++- hooks/useKeyPress/package.json | 1 + hooks/useKeyPress/src/index.ts | 3 +- hooks/useKeyPress/src/types.ts | 2 +- hooks/useMouse/package.json | 2 +- hooks/useMouse/src/index.ts | 4 +- hooks/useScroll/package.json | 1 + hooks/useScroll/src/index.ts | 3 +- hooks/useScroll/src/types.ts | 2 +- hooks/useScrollLock/package.json | 5 ++- hooks/useScrollLock/src/index.ts | 6 +-- hooks/useSize/package.json | 1 + hooks/useSize/src/index.ts | 5 ++- hooks/useWatermark/package.json | 3 +- hooks/useWatermark/src/index.ts | 5 ++- hooks/utils/package.json | 1 + hooks/utils/src/index.ts | 2 - hooks/utils/src/utils/domTarget.ts | 37 ------------------- .../src/utils/useDeepCompareWithTarget.ts | 2 +- hooks/utils/src/utils/useEffectWithTarget.ts | 4 +- pnpm-lock.yaml | 24 ++++++++++-- 28 files changed, 64 insertions(+), 70 deletions(-) delete mode 100644 hooks/utils/src/utils/domTarget.ts diff --git a/hooks/useClickAway/package.json b/hooks/useClickAway/package.json index 8e947d3c..24582bd5 100644 --- a/hooks/useClickAway/package.json +++ b/hooks/useClickAway/package.json @@ -15,6 +15,7 @@ "react": "^16.8.0 || ^17.0.0 || ^18.0.0" }, "dependencies": { + "@pansy/shared": "^1.2.0", "@pansy/hook-utils": "0.3.0", "@pansy/use-latest": "0.2.4" }, diff --git a/hooks/useClickAway/src/index.ts b/hooks/useClickAway/src/index.ts index 4b10b531..33fbdd1b 100644 --- a/hooks/useClickAway/src/index.ts +++ b/hooks/useClickAway/src/index.ts @@ -1,9 +1,9 @@ import { useLatest } from '@pansy/use-latest'; -import { getTargetElement } from '@pansy/hook-utils'; +import { getTargetElement } from '@pansy/shared/react'; // eslint-disable-next-line @typescript-eslint/no-duplicate-imports import { useEffectWithTarget } from '@pansy/hook-utils'; -import type { BasicTarget } from '@pansy/hook-utils'; +import type { BasicTarget } from '@pansy/shared/react'; /** * 监听目标元素外的点击事件 diff --git a/hooks/useEventListener/package.json b/hooks/useEventListener/package.json index bc2fd5c4..d8210163 100644 --- a/hooks/useEventListener/package.json +++ b/hooks/useEventListener/package.json @@ -15,6 +15,7 @@ "react": "^16.8.0 || ^17.0.0 || ^18.0.0" }, "dependencies": { + "@pansy/shared": "^1.2.0", "@pansy/hook-utils": "0.3.0", "@pansy/use-latest": "0.2.4" }, diff --git a/hooks/useEventListener/src/index.ts b/hooks/useEventListener/src/index.ts index 2925fe1f..78ecaabd 100644 --- a/hooks/useEventListener/src/index.ts +++ b/hooks/useEventListener/src/index.ts @@ -1,10 +1,10 @@ /* eslint-disable no-undef */ import { useLatest } from '@pansy/use-latest'; -import { getTargetElement } from '@pansy/hook-utils'; +import { getTargetElement } from '@pansy/shared/react'; // eslint-disable-next-line @typescript-eslint/no-duplicate-imports import { useEffectWithTarget } from '@pansy/hook-utils'; -import type { BasicTarget } from '@pansy/hook-utils'; +import type { BasicTarget } from '@pansy/shared/react'; type noop = (...p: any) => void; diff --git a/hooks/useHover/package.json b/hooks/useHover/package.json index 6886df35..075c316b 100644 --- a/hooks/useHover/package.json +++ b/hooks/useHover/package.json @@ -15,7 +15,7 @@ "react": "^16.8.0 || ^17.0.0 || ^18.0.0" }, "dependencies": { - "@pansy/hook-utils": "0.3.0", + "@pansy/shared": "^1.2.0", "@pansy/use-boolean": "0.2.5", "@pansy/use-event-listener": "0.2.8" }, diff --git a/hooks/useHover/src/index.ts b/hooks/useHover/src/index.ts index c5b8b8ae..06466920 100644 --- a/hooks/useHover/src/index.ts +++ b/hooks/useHover/src/index.ts @@ -1,6 +1,7 @@ import { useBoolean } from '@pansy/use-boolean'; import { useEventListener } from '@pansy/use-event-listener'; -import type { BasicTarget } from '@pansy/hook-utils'; + +import type { BasicTarget } from '@pansy/shared/react'; export interface Options { onEnter?: () => void; diff --git a/hooks/useInViewport/package.json b/hooks/useInViewport/package.json index a1bf7b5f..4bcb9264 100644 --- a/hooks/useInViewport/package.json +++ b/hooks/useInViewport/package.json @@ -15,6 +15,7 @@ "react": "^16.8.0 || ^17.0.0 || ^18.0.0" }, "dependencies": { + "@pansy/shared": "^1.2.0", "@pansy/hook-utils": "0.3.0", "intersection-observer": "^0.12.0" }, diff --git a/hooks/useInViewport/src/index.ts b/hooks/useInViewport/src/index.ts index 5e8d3654..c6537c08 100644 --- a/hooks/useInViewport/src/index.ts +++ b/hooks/useInViewport/src/index.ts @@ -1,8 +1,9 @@ import 'intersection-observer'; import { useState } from 'react'; -import { getTargetElement, useEffectWithTarget } from '@pansy/hook-utils'; +import { getTargetElement } from '@pansy/shared/react'; +import { useEffectWithTarget } from '@pansy/hook-utils'; -import type { BasicTarget } from '@pansy/hook-utils'; +import type { BasicTarget } from '@pansy/shared/react'; export interface Options { rootMargin?: string; diff --git a/hooks/useKeyPress/package.json b/hooks/useKeyPress/package.json index a75e0737..d3977cf4 100644 --- a/hooks/useKeyPress/package.json +++ b/hooks/useKeyPress/package.json @@ -16,6 +16,7 @@ "react": "^16.8.0 || ^17.0.0 || ^18.0.0" }, "dependencies": { + "@pansy/shared": "^1.2.0", "@pansy/hook-utils": "0.3.0", "@pansy/use-latest": "0.2.4" }, diff --git a/hooks/useKeyPress/src/index.ts b/hooks/useKeyPress/src/index.ts index 5c934289..007fc84a 100644 --- a/hooks/useKeyPress/src/index.ts +++ b/hooks/useKeyPress/src/index.ts @@ -1,5 +1,6 @@ import { useLatest } from '@pansy/use-latest'; -import { useDeepCompareEffectWithTarget, getTargetElement } from '@pansy/hook-utils'; +import { getTargetElement } from '@pansy/shared/react'; +import { useDeepCompareEffectWithTarget } from '@pansy/hook-utils'; import { genKeyFormater } from './utils'; diff --git a/hooks/useKeyPress/src/types.ts b/hooks/useKeyPress/src/types.ts index 9ae84cf9..0faeef8c 100644 --- a/hooks/useKeyPress/src/types.ts +++ b/hooks/useKeyPress/src/types.ts @@ -1,4 +1,4 @@ -import type { BasicTarget } from '@pansy/hook-utils'; +import type { BasicTarget } from '@pansy/shared/react'; export type KeyType = number | string; diff --git a/hooks/useMouse/package.json b/hooks/useMouse/package.json index 132bcda2..6369565d 100644 --- a/hooks/useMouse/package.json +++ b/hooks/useMouse/package.json @@ -16,7 +16,7 @@ "react": "^16.8.0 || ^17.0.0 || ^18.0.0" }, "dependencies": { - "@pansy/hook-utils": "0.3.0", + "@pansy/shared": "^1.2.0", "@pansy/use-event-listener": "0.2.8", "@pansy/use-raf-state": "0.2.5" }, diff --git a/hooks/useMouse/src/index.ts b/hooks/useMouse/src/index.ts index c9f86bf2..fc010374 100644 --- a/hooks/useMouse/src/index.ts +++ b/hooks/useMouse/src/index.ts @@ -1,8 +1,8 @@ import { useRafState } from '@pansy/use-raf-state'; import { useEventListener } from '@pansy/use-event-listener'; -import { getTargetElement } from '@pansy/hook-utils'; +import { getTargetElement } from '@pansy/shared/react'; -import type { BasicTarget } from '@pansy/hook-utils'; +import type { BasicTarget } from '@pansy/shared/react'; import type { CursorState } from './types'; const initState: CursorState = { diff --git a/hooks/useScroll/package.json b/hooks/useScroll/package.json index cc70ce4f..5186d6a7 100644 --- a/hooks/useScroll/package.json +++ b/hooks/useScroll/package.json @@ -16,6 +16,7 @@ }, "dependencies": { "@pansy/hook-utils": "0.3.0", + "@pansy/shared": "^1.2.0", "@pansy/use-latest": "0.2.4", "@pansy/use-raf-state": "0.2.5" }, diff --git a/hooks/useScroll/src/index.ts b/hooks/useScroll/src/index.ts index 3ead4e81..be3cc1f5 100644 --- a/hooks/useScroll/src/index.ts +++ b/hooks/useScroll/src/index.ts @@ -1,6 +1,7 @@ import { useLatest } from '@pansy/use-latest'; import { useRafState } from '@pansy/use-raf-state'; -import { getTargetElement, useEffectWithTarget } from '@pansy/hook-utils'; +import { getTargetElement } from '@pansy/shared/react'; +import { useEffectWithTarget } from '@pansy/hook-utils'; import type { Position, ScrollListenController, Target } from './types'; diff --git a/hooks/useScroll/src/types.ts b/hooks/useScroll/src/types.ts index 735a1706..ae4f3711 100644 --- a/hooks/useScroll/src/types.ts +++ b/hooks/useScroll/src/types.ts @@ -1,4 +1,4 @@ -import type { BasicTarget } from '@pansy/hook-utils'; +import type { BasicTarget } from '@pansy/shared/react'; export interface Position { left: number; diff --git a/hooks/useScrollLock/package.json b/hooks/useScrollLock/package.json index 39fe1df5..d8f4d6c2 100644 --- a/hooks/useScrollLock/package.json +++ b/hooks/useScrollLock/package.json @@ -16,8 +16,9 @@ "react": "^16.8.6 || ^17.0" }, "dependencies": { - "@pansy/hook-utils": "^0.3.0", - "@pansy/shared": "^1.1.0" + "@pansy/use-get-state": "^0.2.5", + "@pansy/use-event-listener": "^0.2.8", + "@pansy/shared": "^1.2.0" }, "publishConfig": { "registry": "https://registry.npmjs.org", diff --git a/hooks/useScrollLock/src/index.ts b/hooks/useScrollLock/src/index.ts index 502b749d..580e877b 100644 --- a/hooks/useScrollLock/src/index.ts +++ b/hooks/useScrollLock/src/index.ts @@ -1,11 +1,11 @@ import { useMemo, useEffect } from 'react'; -import { getTargetElement } from '@pansy/hook-utils'; +import { isIOS } from '@pansy/shared'; +import { getTargetElement } from '@pansy/shared/react'; import { useGetState } from '@pansy/use-get-state'; import { useEventListener } from '@pansy/use-event-listener'; -import { isIOS } from '@pansy/shared'; import type { CSSProperties } from 'react'; -import type { BasicTarget } from '@pansy/hook-utils'; +import type { BasicTarget } from '@pansy/shared/react'; export function useScrollLock( target: BasicTarget, diff --git a/hooks/useSize/package.json b/hooks/useSize/package.json index 1a23f09f..f5979c47 100644 --- a/hooks/useSize/package.json +++ b/hooks/useSize/package.json @@ -16,6 +16,7 @@ }, "dependencies": { "@juggle/resize-observer": "^3.3.1", + "@pansy/shared": "^1.2.0", "@pansy/hook-utils": "0.3.0", "@pansy/use-raf-state": "0.2.5" }, diff --git a/hooks/useSize/src/index.ts b/hooks/useSize/src/index.ts index ed2e758c..3fbd7307 100644 --- a/hooks/useSize/src/index.ts +++ b/hooks/useSize/src/index.ts @@ -1,8 +1,9 @@ import { ResizeObserver } from '@juggle/resize-observer'; import { useRafState } from '@pansy/use-raf-state'; -import { getTargetElement, useIsomorphicLayoutEffectWithTarget } from '@pansy/hook-utils'; +import { getTargetElement } from '@pansy/shared/react'; +import { useIsomorphicLayoutEffectWithTarget } from '@pansy/hook-utils'; -import type { BasicTarget } from '@pansy/hook-utils'; +import type { BasicTarget } from '@pansy/shared/react'; export interface Size { width: number; diff --git a/hooks/useWatermark/package.json b/hooks/useWatermark/package.json index d8ee0c7a..94d37d27 100644 --- a/hooks/useWatermark/package.json +++ b/hooks/useWatermark/package.json @@ -15,10 +15,11 @@ "react": "^16.8.0 || ^17.0.0 || ^18.0.0" }, "dependencies": { + "@pansy/shared": "^1.2.0", "@pansy/hook-utils": "0.3.0", "@pansy/use-memoized-fn": "0.2.5", "@pansy/use-unmount": "0.2.5", - "@pansy/watermark": "^2.0.10" + "@pansy/watermark": "^2.2.1" }, "publishConfig": { "registry": "https://registry.npmjs.org", diff --git a/hooks/useWatermark/src/index.ts b/hooks/useWatermark/src/index.ts index 2ebd1eac..4a434a3f 100644 --- a/hooks/useWatermark/src/index.ts +++ b/hooks/useWatermark/src/index.ts @@ -1,10 +1,11 @@ import { useRef } from 'react'; import { Watermark, WatermarkOptions } from '@pansy/watermark' -import { getTargetElement, useEffectWithTarget } from '@pansy/hook-utils'; +import { getTargetElement } from '@pansy/shared/react'; +import { useEffectWithTarget } from '@pansy/hook-utils'; import { useUnmount } from '@pansy/use-unmount'; import { useMemoizedFn } from '@pansy/use-memoized-fn'; -import type { BasicTarget } from '@pansy/hook-utils'; +import type { BasicTarget } from '@pansy/shared/react'; export type BaseWatermarkOptions = Omit diff --git a/hooks/utils/package.json b/hooks/utils/package.json index dcf40e73..b8414d97 100644 --- a/hooks/utils/package.json +++ b/hooks/utils/package.json @@ -15,6 +15,7 @@ "react": "^16.8.0 || ^17.0.0 || ^18.0.0" }, "dependencies": { + "@pansy/shared": "^1.2.0", "@pansy/use-memoized-fn": "0.2.5", "@pansy/use-unmount": "0.2.5", "@pansy/use-update-effect": "0.2.4", diff --git a/hooks/utils/src/index.ts b/hooks/utils/src/index.ts index fc7c741a..d59300ef 100644 --- a/hooks/utils/src/index.ts +++ b/hooks/utils/src/index.ts @@ -1,7 +1,6 @@ export { createUpdateEffect } from '@pansy/use-update-effect'; export { createUseStorageState } from './utils/createUseStorageState'; export { depsAreSame } from './utils/depsAreSame'; -export { getTargetElement } from './utils/domTarget'; export { isBrowser } from './utils/isBrowser'; export { createEffectWithTarget, @@ -15,6 +14,5 @@ export { isFunction } from './utils/isFunction'; export { createDeepCompareEffect } from './createDeepCompareEffect'; -export type { BasicTarget } from './utils/domTarget'; export type { OptionsWithDefaultValue, Options } from './utils/createUseStorageState'; export type { Noop } from './types'; diff --git a/hooks/utils/src/utils/domTarget.ts b/hooks/utils/src/utils/domTarget.ts deleted file mode 100644 index c270884a..00000000 --- a/hooks/utils/src/utils/domTarget.ts +++ /dev/null @@ -1,37 +0,0 @@ -import type { MutableRefObject } from 'react'; -import { isBrowser } from './isBrowser'; - -type TargetValue = T | undefined | null; - -type TargetType = HTMLElement | Element | Window | Document; - -export type BasicTarget = - | (() => TargetValue) - | TargetValue - | MutableRefObject>; - -/** - * 获取Dom节点 - * @param target - * @param defaultElement - * @returns - */ -export function getTargetElement(target: BasicTarget, defaultElement?: T) { - if (!isBrowser) return undefined; - - if (!target) { - return defaultElement; - } - - let targetElement: TargetValue; - - if (typeof target === 'function') { - targetElement = target(); - } else if ('current' in target) { - targetElement = target.current; - } else { - targetElement = target; - } - - return targetElement; -} diff --git a/hooks/utils/src/utils/useDeepCompareWithTarget.ts b/hooks/utils/src/utils/useDeepCompareWithTarget.ts index ffaae47d..1bf8454e 100644 --- a/hooks/utils/src/utils/useDeepCompareWithTarget.ts +++ b/hooks/utils/src/utils/useDeepCompareWithTarget.ts @@ -3,7 +3,7 @@ import isEqual from 'lodash/isEqual'; import { useEffectWithTarget } from './useEffectWithTarget'; import type { DependencyList, EffectCallback } from 'react'; -import type { BasicTarget } from './domTarget'; +import type { BasicTarget } from '@pansy/shared/react'; const depsEqual = (aDeps: DependencyList, bDeps: DependencyList = []) => { return isEqual(aDeps, bDeps); diff --git a/hooks/utils/src/utils/useEffectWithTarget.ts b/hooks/utils/src/utils/useEffectWithTarget.ts index 657f4501..40a07c77 100644 --- a/hooks/utils/src/utils/useEffectWithTarget.ts +++ b/hooks/utils/src/utils/useEffectWithTarget.ts @@ -1,10 +1,10 @@ import { useEffect, useLayoutEffect, useRef } from 'react'; import { useUnmount } from '@pansy/use-unmount'; import { depsAreSame } from './depsAreSame'; -import { getTargetElement } from './domTarget'; +import { getTargetElement } from '@pansy/shared/react'; import type { DependencyList, EffectCallback } from 'react'; -import type { BasicTarget } from './domTarget'; +import type { BasicTarget } from '@pansy/shared/react'; export const createEffectWithTarget = ( useEffectType: typeof useEffect | typeof useLayoutEffect diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ce238c13..88187562 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -331,9 +331,11 @@ importers: hooks/useEventListener: specifiers: '@pansy/hook-utils': 0.3.0 + '@pansy/shared': ^1.2.0 '@pansy/use-latest': 0.2.4 dependencies: '@pansy/hook-utils': link:../utils + '@pansy/shared': 1.2.0 '@pansy/use-latest': link:../useLatest hooks/useEventTarget: @@ -357,19 +359,23 @@ importers: hooks/useHover: specifiers: '@pansy/hook-utils': 0.3.0 + '@pansy/shared': ^1.2.0 '@pansy/use-boolean': 0.2.5 '@pansy/use-event-listener': 0.2.8 dependencies: '@pansy/hook-utils': link:../utils + '@pansy/shared': 1.2.0 '@pansy/use-boolean': link:../useBoolean '@pansy/use-event-listener': link:../useEventListener hooks/useInViewport: specifiers: '@pansy/hook-utils': 0.3.0 + '@pansy/shared': ^1.2.0 intersection-observer: ^0.12.0 dependencies: '@pansy/hook-utils': link:../utils + '@pansy/shared': 1.2.0 intersection-observer: 0.12.2 hooks/useInfiniteScroll: @@ -384,9 +390,11 @@ importers: hooks/useKeyPress: specifiers: '@pansy/hook-utils': 0.3.0 + '@pansy/shared': ^1.2.0 '@pansy/use-latest': 0.2.4 dependencies: '@pansy/hook-utils': link:../utils + '@pansy/shared': 1.2.0 '@pansy/use-latest': link:../useLatest hooks/useLatest: @@ -421,10 +429,12 @@ importers: hooks/useMouse: specifiers: '@pansy/hook-utils': 0.3.0 + '@pansy/shared': ^1.2.0 '@pansy/use-event-listener': 0.2.8 '@pansy/use-raf-state': 0.2.5 dependencies: '@pansy/hook-utils': link:../utils + '@pansy/shared': 1.2.0 '@pansy/use-event-listener': link:../useEventListener '@pansy/use-raf-state': link:../useRafState @@ -485,11 +495,13 @@ importers: hooks/useScrollLock: specifiers: - '@pansy/hook-utils': ^0.3.0 '@pansy/shared': ^1.2.0 + '@pansy/use-event-listener': ^0.2.8 + '@pansy/use-get-state': ^0.2.5 dependencies: - '@pansy/hook-utils': link:../utils '@pansy/shared': 1.2.0 + '@pansy/use-event-listener': link:../useEventListener + '@pansy/use-get-state': link:../useGetState hooks/useSelections: specifiers: @@ -507,10 +519,12 @@ importers: specifiers: '@juggle/resize-observer': ^3.3.1 '@pansy/hook-utils': 0.3.0 + '@pansy/shared': ^1.2.0 '@pansy/use-raf-state': 0.2.5 dependencies: '@juggle/resize-observer': 3.3.1 '@pansy/hook-utils': link:../utils + '@pansy/shared': 1.2.0 '@pansy/use-raf-state': link:../useRafState hooks/useTimeout: @@ -561,11 +575,13 @@ importers: hooks/useWatermark: specifiers: '@pansy/hook-utils': 0.3.0 + '@pansy/shared': ^1.2.0 '@pansy/use-memoized-fn': 0.2.5 '@pansy/use-unmount': 0.2.5 - '@pansy/watermark': ^2.0.10 + '@pansy/watermark': ^2.2.1 dependencies: '@pansy/hook-utils': link:../utils + '@pansy/shared': 1.2.0 '@pansy/use-memoized-fn': link:../useMemoizedFn '@pansy/use-unmount': link:../useUnmount '@pansy/watermark': 2.2.1 @@ -582,11 +598,13 @@ importers: hooks/utils: specifiers: + '@pansy/shared': ^1.2.0 '@pansy/use-memoized-fn': 0.2.5 '@pansy/use-unmount': 0.2.5 '@pansy/use-update-effect': 0.2.4 lodash: ^4.17.21 dependencies: + '@pansy/shared': 1.2.0 '@pansy/use-memoized-fn': link:../useMemoizedFn '@pansy/use-unmount': link:../useUnmount '@pansy/use-update-effect': link:../useUpdateEffect