Skip to content

Commit

Permalink
feat: 添加 react-use-gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
cncolder committed Apr 24, 2020
1 parent 1ba0274 commit 046b079
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/react-use-gesture/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from 'react-use-gesture'

export * from './useGestureConfig'
43 changes: 43 additions & 0 deletions src/react-use-gesture/useGestureConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useState } from 'react'
import { uuid } from '../utils'

export interface GestureConfigOptions {
/** 容器元素 ID, 默认为随机生成的 uuid. */
id?: string
ref?: any
}

/**
* useGestureConfig hook. 获取 `react-use-gesture` 配置, 需要把返回的第二个参数赋值给包裹元素.
*
* @example
* ```jsx
* const [gestureConfig, containerProps] = useGestureConfig()
* const bind = useDrag(() => {}, gestureConfig)
* return (
* <View {...containerProps}>{
* <View {...bind()} />
* }</View>
* )
* ```
*/
export function useGestureConfig(options = {} as GestureConfigOptions) {
const { id = uuid(), ref = () => {} } = options

const [container, setContainer] = useState<HTMLElement>()

const gestureConfig = {
window: container,
}

const containerProps = {
id,
ref: (container: HTMLElement) => {
if (!container) return
setContainer(container)
ref(container)
},
}

return [gestureConfig, containerProps] as [typeof gestureConfig, typeof containerProps]
}

0 comments on commit 046b079

Please sign in to comment.