/** * @file DOM 元素或 DOM 事件相关的 hooks */ import { useSize as useSizeOriginal, useInViewport as useInViewportOriginal, useEventListener as useEventListenerOriginal, useDocumentVisibility as useDocumentVisibilityOriginal, useKeyPress as useKeyPressOriginal, } from 'ahooks' import { useEffect, useMemo, useRef, TouchEvent } from 'react' import { useSwipeable } from 'react-swipeable' import { noop, throttle } from '../functions/function' /** * 一个用于判断 dom 元素是否在可视范围之内的 hook. * 文档:https://ahooks.js.org/hooks/dom/use-in-viewport/ */ export const useInViewport = useInViewportOriginal /** * 一个用于获取 dom 元素大小的 hook. * 文档: https://ahooks.js.org/hooks/dom/use-size/ */ export const useSize = useSizeOriginal /** * 监听事件 Hook * 文档: https://ahooks.js.org/hooks/dom/use-event-listener */ export const useEventListener = useEventListenerOriginal /** * 检查页面是否可见 * 文档: https://ahooks.js.org/hooks/dom/use-document-visibility */ export const useDocumentVisibility = useDocumentVisibilityOriginal /** * 监听 window.resize 事件, 并可为事件的回调函数设定 throttle 的间隔 * * @param handler - 回调函数 * @param wait - throttle 的间隔, 默认 50ms */ export const useOnResize = (handler: () => void, wait = 50) => { const handle = useMemo(() => throttle(handler, wait), [handler, wait]) useEffect(() => { handle() window.addEventListener('resize', handle) return () => window.removeEventListener('resize', handle) }, [handle]) } /** * 监听键盘事件 Hook * 文档: https://ahooks.js.org/hooks/dom/use-key-press */ export const useKeyPress = useKeyPressOriginal /** * **重新**聚焦页面时调用函数 * 通常的使用场景为: * 当在页面 A 进行查看或编辑时,需要跳转到页面 B 去完成某个 A 的子内容的编辑,同时在编辑 B 完成后,返回 A 时,需要同步 B 处修改的场景。 * 此时使用该 Hook 检测与更新数据 * * @param handler - handler */ export function useDocumentRefocused(handler: Function) { const visible = useDocumentVisibility() const savedHandler = useRef() useEffect(() => { savedHandler.current = handler }, [handler]) const isFirstFocusedRef = useRef(true) useEffect(() => { if (visible === 'visible') { if (isFirstFocusedRef.current) { isFirstFocusedRef.current = false } else { savedHandler.current?.() } } }, [visible]) } /** * 在组件初次挂载 (mount) 时自动聚焦指定元素 * * @returns 返回[应赋给目标元素的 ref, 手动 focus 元素的方法] */ export const useAutofocusRef = () => { const ref = useRef() useEffect(() => { ref.current?.focus?.() }, [ref]) const focus = () => { ref.current?.focus?.() } return [ref, focus] as const } /** * 监听指定元素的左划/右划/上划/下划事件 (主要用于 web 移动端) * * 文档见: https://github.com/FormidableLabs/react-swipeable. * 一般需要根据实际需求调校 delta 参数 (即滑动多少距离生效) * * @example * ```javascript * const handlers = useSwipeable({ * onSwiped: (eventData) => console.log('Swiped!', eventData), * ...config, * }) * return
Swipe here
* ``` */ export const useOnSwipe = useSwipeable /** * 监听指定元素的长按事件 (基于 touch 事件, 只支持移动端) * * @param callback - 回调函数 * @param duration - 要求长按多久? 默认 500ms * @param stopPropagation - 是否阻止事件冒泡 * * @returns 返回应赋给目标元素的 ref * * @example * ```javascript * const ref = useOnLongPressRef(ev => { * console.log('Long pressed', ev) * }) * * return
Long Press Me
* ``` */ export const useOnLongPressRef = ( callback?: (ev: TouchEvent) => void, { duration = 500, stopPropagation = false, }: { duration?: number; stopPropagation?: boolean } = {}, ) => { const ref = useRef(null) useEffect(() => { if (!ref.current || !callback) return noop const el = ref.current let timer: any = null const onTouchStart = (ev: any) => { if (stopPropagation) ev.stopPropagation() timer = setTimeout(() => { timer = null callback(ev) }, duration) } const onTouchMoveOrEnd = () => { clearTimeout(timer) } el.addEventListener('touchstart', onTouchStart) el.addEventListener('touchmove', onTouchMoveOrEnd) el.addEventListener('touchend', onTouchMoveOrEnd) return () => { el?.removeEventListener('touchstart', onTouchStart) el?.removeEventListener('touchmove', onTouchMoveOrEnd) el?.removeEventListener('touchend', onTouchMoveOrEnd) } }, [ref, callback, duration, stopPropagation]) return ref }