1.2.4 • Published 2 years ago

@odnh/use-swipe v1.2.4

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

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

NameTypeDescriptionRequired
targetHTMLElement or React.MutableRefObject<HTMLElement>target elementtrue
optionObjectOptionsfalse

options

NametypeDescriptionDefault value
fpsnumber-60
useEventbooleanWhether to use the swipe event swipestart - swipemove - swipeend.false
excludeElementHTMLElementElements to exclude the beginning.null

Returns

NameTypeDescription
swipeStateSwipeMoveState or SwipeDoneState
SwipeMoveState
NameTypeDescription
stateSwipeStateEnum.MOVEmove
xnumbermoving position value
ynumbermoving position value
difference{x: 0, y: 0}position difference between the beginning and the end.
duration0time of moved
SwipeDoneState
NameTypeDescription
stateSwipeStateEnum.DONEdone
x0moving position value
y0moving position value
difference{x: number, y: number}position difference between the beginning and the end.
durationnumbertime of moved

Example

live demo

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