Registry / react-swipeable

react-swipeable

JSON →
library7.0.2jsnpmunverified

React Swipeable is a lightweight React hook, `useSwipeable`, designed to easily add swipe and touch event handling capabilities to any React component. It abstracts away the complexities of touch events, providing a consistent API for detecting various swipe directions (up, down, left, right), tap events, and customizable thresholds for swipe detection via the `delta` prop. The library is currently at version 7.0.2 and is actively maintained by FormidableLabs, with regular updates to support new React versions (e.g., React 19) and introduce new features like `swipeDuration` and `onSwipeStart`. Its key differentiators include its hook-based API, fine-grained control over touch event options, and a focus on performance by leveraging passive event listeners by default, addressing Lighthouse performance issues related to touch events.

npm install react-swipeable
INSTALL
IMPORT
SIG · REACT-SWIPEABLE
R
react-swipeable
javascriptv7.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

useSwipeable
import { useSwipeable } from 'react-swipeable';
const useSwipeable = require('react-swipeable');
Primary hook for adding swipe functionality. The package is ESM-first, so CommonJS `require` is generally not recommended in modern React projects.
SwipeEventData
import { type SwipeEventData } from 'react-swipeable';
import { SwipeEventData } from 'react-swipeable';
Import the type for `SwipeEventData` using `import type` for better tree-shaking and clarity in TypeScript.
SwipeableProps
import { type SwipeableProps } from 'react-swipeable';
Import the type for props if you need to define a component that accepts swipeable configuration externally.

This example demonstrates how to use `useSwipeable` to detect swipe directions and taps, updating the UI accordingly. It includes common configuration options like `preventScrollOnSwipe`, `delta`, and `swipeDuration`.

import React, { useState } from 'react'; import { useSwipeable, SwipeEventData } from 'react-swipeable'; const SwipeableBox = () => { const [swipeDirection, setSwipeDirection] = useState('none'); const [swipeCount, setSwipeCount] = useState(0); const handlers = useSwipeable({ onSwiped: (eventData) => { console.log('User Swiped!', eventData); setSwipeDirection(eventData.dir); setSwipeCount(prev => prev + 1); }, onSwipedLeft: () => console.log('Swiped Left!'), onSwipedRight: () => console.log('Swiped Right!'), onSwipedUp: () => console.log('Swiped Up!'), onSwipedDown: () => console.log('Swiped Down!'), onTap: () => console.log('Tapped!'), onSwipeStart: (eventData) => console.log('Swipe started!', eventData.initial[0]), onSwiping: (eventData: SwipeEventData) => { // console.log('Swiping...', eventData.deltaX, eventData.deltaY); }, preventScrollOnSwipe: true, trackTouch: true, trackMouse: true, delta: 10, // min distance(px) before a swipe starts swipeDuration: 500 // max duration(ms) for a swipe }); return ( <div {...handlers} style={{ touchAction: 'none', width: '300px', height: '200px', backgroundColor: '#f0f0f0', display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', border: '2px dashed gray', fontSize: '1.2em', userSelect: 'none' }} > <p>Swipe or Tap Me!</p> <p>Last Swipe: {swipeDirection}</p> <p>Total Swipes: {swipeCount}</p> </div> ); }; export default SwipeableBox;
Debug
Known issues
breakingVersion 7.0.0 introduced the `swipeDuration` prop, which defaults to `Infinity` for backward compatibility, but developers should be aware that setting it to a finite number will ignore swipes lasting longer than the specified duration. Additionally, the `touchEventOptions` prop was added to provide control over touch event listeners (e.g., `passive` and `capture`).
fix
Review your swipe duration requirements and explicitly set `swipeDuration` if you need to limit the time a swipe can take. Utilize `touchEventOptions` for fine-grained control over event listener behavior, especially if `preventDefaultTouchmoveEvent` is `true`.
affects: >=7.0.0
breakingVersion 6.0.0 changed the default behavior of `addEventListener` to include the passive event listener option, resolving Lighthouse performance issues. This means touch events are passive by default unless `preventDefaultTouchmoveEvent` is set to `true`.
fix
If your application relies on `event.preventDefault()` within touchmove handlers, ensure `preventDefaultTouchmoveEvent` is explicitly set to `true` in your `useSwipeable` configuration. Otherwise, your `preventDefault` calls may be ignored, potentially causing unexpected scroll behavior.
affects: >=6.0.0
gotchaThe `first` property on `SwipeEventData` (introduced in v5.5.0) was initially always `true` due to a bug. This was fixed in v6.0.1, so `first` now correctly indicates only the initial event of a swipe.
fix
If your logic depends on the `first` property to differentiate between the start of a swipe and subsequent `onSwiping` events, ensure you are on version `6.0.1` or higher. Alternatively, use the `onSwipeStart` prop (available since v6.1.0) which is guaranteed to only fire once at the beginning of a swipe.
affects: >=5.5.0 <6.0.1
gotchaThe `delta` prop, which defines the minimum distance (in pixels) for a swipe, can be configured as an object for different values per direction since v6.2.0. Previously, it only accepted a single number.
fix
If you require different swipe thresholds for horizontal and vertical movements, update `delta` to an object like `{ x: 20, y: 10 }`. If you pass a single number, it will apply to all directions.
affects: >=6.2.0
Errors
Common errors & fixes
TypeError: handlers is not a function or is undefined
Attempting to spread the `handlers` object returned by `useSwipeable` onto a non-DOM element or incorrect usage.
fix
Ensure `handlers` is spread directly onto a native HTML element (e.g., `div`, `span`) that can receive event listeners. Example: `<div {...handlers}>...</div>`
TS2322: Type '{ onSwiped: (eventData: SwipeEventData) => void; ... }' is not assignable to type 'IntrinsicAttributes & RefAttributes<HTMLDivElement>'. Property 'onSwiped' does not exist on type 'IntrinsicAttributes & RefAttributes<HTMLDivElement>'.
Trying to pass `useSwipeable` config directly as props to a React component instead of spreading the `handlers` object.
fix
The `useSwipeable` hook returns an object of event handlers and props that must be spread onto the target DOM element. Do not pass the configuration object directly to the element. Correct: `<div {...handlers}>`, Incorrect: `<div onSwiped={...}>`
Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a functional component.
Calling `useSwipeable` outside of a React functional component or a custom hook, or violating React's Rules of Hooks.
fix
Ensure that `useSwipeable` is only called at the top level of a React functional component or another custom hook. Do not call it inside loops, conditions, or nested functions.
Upgrade
Version history
7.0.2latest on npm
Audit
Dependencies
reactrequiredRequired as a peer dependency for all React-based hooks and components.
Agent activity
4 hits · last 30 days
node
4
Resources
react-swipeable — npm install react-swipeable · libregistry