-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from 'react-use-gesture' | ||
|
||
export * from './useGestureConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |