Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rfc: useSwipe #2336

Open
SnowingFox opened this issue Oct 3, 2023 · 0 comments · May be fixed by #2337
Open

rfc: useSwipe #2336

SnowingFox opened this issue Oct 3, 2023 · 0 comments · May be fixed by #2337
Labels

Comments

@SnowingFox
Copy link

SnowingFox commented Oct 3, 2023

介绍

一个检测手势的hook

用法

const App =  () => {
  const swipeElRef = useRef()
  const { isSwipeSuccess } = useSwipe(swipeElRef, { direction: 'left' })

  useEffect(() => {
    if (isSwipeSuccess) {
        fetchNextDayData()
    }
  }, [isSwipeSuccess])

  return <div ref={swipeElRef}>
     <Component />
  <div/>
}

类型定义

export type UseSwipeDirection = 'up' | 'down' | 'left' | 'right'

export interface UseSwipeOptions {
  /**
   * 检测滑动的方向
   * 如果不传direction的话就是默认用户自己实现swipe的direction
   * 如果传了direction我们hook内部就自动帮用户检测
   */
  direction?: UseSwipeDirection

  /**
   * Register events as passive
   *
   * @default true
   */
  passive?: boolean

  /**
   * @default 50
   */
  threshold?: number

  /**
   * Callback on swipe start
   */
  onSwipeStart?: (e: TouchEvent) => void

  /**
   * Callback on swipe moves
   */
  onSwipe?: (e: TouchEvent) => void

  /**
   * Callback on swipe ends
   */
  onSwipeEnd?: (e: TouchEvent, direction: UseSwipeDirection) => void
}

export interface UseSwipeReturn {
  /**
   * 是否在滑动
   */
  isSwiping: boolean
  
  /**
   * 停止检测滑动
   */
  stop: () => void

  /**
   * 滑动的 x 轴距离
   */
  lengthX: number

  /**
   * 滑动的 y 轴距离
   */
  lengthY: number

  /**
   * 滑动是否符合预期direction,boolean值在options.direction有的时候再返回,否则是null
   */
  isSwipeSuccess: boolean | null
}


function useSwipe(elRef: Ref<HTMLElement>, options: UseSwipeOptions = {}): UseSwipeReturn {}
@SnowingFox SnowingFox linked a pull request Oct 4, 2023 that will close this issue
18 tasks
@crazylxr crazylxr added the v4 label Oct 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants