|
|
|
|
|
import React from 'react' |
|
|
import { Controller } from '@use-gesture/core' |
|
|
import { GenericOptions, GestureKey, InternalHandlers, NativeHandlers } from '@use-gesture/core/types' |
|
|
import { ReactDOMAttributes } from './types' |
|
|
|
|
|
type HookReturnType<Config extends GenericOptions> = Config['target'] extends object |
|
|
? void |
|
|
: (...args: any[]) => ReactDOMAttributes |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useRecognizers<Config extends GenericOptions>( |
|
|
handlers: InternalHandlers, |
|
|
config: Config | {} = {}, |
|
|
gestureKey?: GestureKey, |
|
|
nativeHandlers?: NativeHandlers |
|
|
): HookReturnType<Config> { |
|
|
const ctrl = React.useMemo(() => new Controller(handlers), []) |
|
|
ctrl.applyHandlers(handlers, nativeHandlers) |
|
|
ctrl.applyConfig(config, gestureKey) |
|
|
|
|
|
React.useEffect(ctrl.effect.bind(ctrl)) |
|
|
|
|
|
React.useEffect(() => { |
|
|
return ctrl.clean.bind(ctrl) |
|
|
}, []) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (config.target === undefined) { |
|
|
return ctrl.bind.bind(ctrl) as any |
|
|
} |
|
|
return undefined as any |
|
|
} |
|
|
|