1.2.4 • Published 2 years ago
@odnh/use-swipe v1.2.4
useSwipe
React Custom Hook useSwipe
Install
yarn add @odnh/use-swipe
# or
npm install @odnh/use-swipe
Use
useSwipe(target: Target, options: Options) => SwipeMoveState | SwipeDoneState;
Parameters
Name | Type | Description | Required |
---|---|---|---|
target | HTMLElement or React.MutableRefObject<HTMLElement> | target element | true |
option | Object | Options | false |
options
Name | type | Description | Default value |
---|---|---|---|
fps | number | - | 60 |
useEvent | boolean | Whether to use the swipe event swipestart - swipemove - swipeend . | false |
excludeElement | HTMLElement | Elements to exclude the beginning. | null |
Returns
Name | Type | Description |
---|---|---|
swipeState | SwipeMoveState or SwipeDoneState |
SwipeMoveState
Name | Type | Description |
---|---|---|
state | SwipeStateEnum.MOVE | move |
x | number | moving position value |
y | number | moving position value |
difference | {x: 0, y: 0} | position difference between the beginning and the end. |
duration | 0 | time of moved |
SwipeDoneState
Name | Type | Description |
---|---|---|
state | SwipeStateEnum.DONE | done |
x | 0 | moving position value |
y | 0 | moving position value |
difference | {x: number, y: number} | position difference between the beginning and the end. |
duration | number | time of moved |
Example
import React from 'react';
import useSwipe, { SwipeStateEnum } from '@odnh/use-swipe';
const DEFAULT_STYLE = {
width: '100px',
height: '100px',
border: '1px solid #ddd',
transform: 'translate(0, 0)',
transition: 'transform 300ms'
};
const App = () => {
const elementRef = React.useRef(null);
const swipe = useSwipe(elementRef);
const swipeStyle = React.useMemo(() => {
if(state === SwipeStateEnum.DONE) {
return DEFAULT_STYLE;
} else {
const { x, y } = swipe
return {
...DEFAULT_STYLE,
transform: `translate(${x}px, ${y}px)`,
transition: 'transform 100ms'
}
}
}, [swipe]);
return (
<div ref={elementRef} style={{width: '100vw', height: '100vh'}}>
<div
style={swipeStyle}
>
swipe
</div>
</div>
)
}
1.2.4
2 years ago
1.2.3
2 years ago
1.2.0
2 years ago
1.2.2
2 years ago
1.2.1
2 years ago
1.1.1
3 years ago
1.1.0
3 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago
0.1.3-1
3 years ago
0.1.2-1
3 years ago
0.1.2
3 years ago
0.1.1
3 years ago
0.1.3
3 years ago
0.1.0
3 years ago
0.0.17
3 years ago
0.0.16
3 years ago
0.0.14
3 years ago
0.0.15
3 years ago
0.0.10
3 years ago
0.0.11
3 years ago
0.0.12
3 years ago
0.0.13
3 years ago
0.0.9
3 years ago
0.0.8
3 years ago
0.0.7
3 years ago
0.0.3-2
3 years ago
0.0.5
3 years ago
0.0.6
3 years ago
0.0.3-1
3 years ago
0.0.3
3 years ago
0.0.2
3 years ago
0.0.1
3 years ago