Skip to content

Lightweight React hook for swipe gesture detection with support for left, right, up, and down directions. Built with TypeScript, optimized for touch/mouse interactions, carousel navigation, and mobile interfaces. Zero dependencies, easily customizable threshold, and comprehensive browser support.

License

Notifications You must be signed in to change notification settings

emxemirc/swiping-hooks

Repository files navigation

Swiping Hooks

npm version License: MIT

Demo

⚠️ EARLY DEVELOPMENT WARNING ⚠️

This project is in a very early development stage and is NOT ready for production use. The API is unstable and subject to major changes. Use at your own risk.


useSwipe is a React hook to manage swipe gestures.

Features

  • Lightweight & Customizable
  • TypeScript Support
  • Simple event handlers for mouse/touch

Getting Started

First, install the package using npm:

npm install swiping-hooks

Basic Usage Example

Here's a simple example of how to use the useSwipe hook in your component:

import React, { useState } from "react";
import useSwipe, { TSwipeDir } from "swiping-hooks";

const SwipeableComponent = () => {
  const [swipeDirection, setSwipeDirection] = useState<TSwipeDir | null>(null);

  const { onMouseDown } = useSwipe({
    threshold: 50,
    onSwiped: ({ dir }) => {
      setSwipeDirection(dir);
      console.log(`Swiped: ${dir}`);
    },
  });

  return (
    <div
      style={{ /* ... some basic styles ... */ userSelect: "none" }}
      onMouseDown={handleMouseDown}
      onMouseMove={handleMouseMove}
      onMouseUp={handleMouseUp}
      // Consider adding touch event handlers (see USAGE.md)
    >
      <p>Swipe here!</p>
      {swipeDirection && <p>Last swipe: {swipeDirection}</p>}
    </div>
  );
};

export default SwipeableComponent;

Documentation

Storybook (Live Examples)

To see the hook in action:

npm run storybook

Testing

Unit Tests:

npm test

End-to-End Tests (Playwright):

npx playwright test

License

MIT

About

Lightweight React hook for swipe gesture detection with support for left, right, up, and down directions. Built with TypeScript, optimized for touch/mouse interactions, carousel navigation, and mobile interfaces. Zero dependencies, easily customizable threshold, and comprehensive browser support.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •