Convert Figma logo to code with AI

FormidableLabs logoreact-swipeable

React swipe event handler hook

2,029
148
2,029
28

Top Related Projects

A Cross-Browser, Event-based, Element Resize Detection for React

React draggable component

A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list✌️

Quick Overview

React-swipeable is a lightweight, customizable React component for adding swipe gestures to your applications. It provides an easy way to detect and handle swipe events on touch devices and desktops, allowing developers to create interactive and responsive user interfaces.

Pros

  • Simple and intuitive API for implementing swipe gestures
  • Supports both touch and mouse events for cross-device compatibility
  • Highly customizable with various configuration options
  • Lightweight and has minimal dependencies

Cons

  • Limited to React applications only
  • May require additional styling and positioning to work seamlessly with existing components
  • Documentation could be more comprehensive, especially for advanced use cases

Code Examples

Basic usage:

import { useSwipeable } from 'react-swipeable';

function SwipeableComponent() {
  const handlers = useSwipeable({
    onSwiped: (eventData) => console.log("User Swiped!", eventData),
    onSwipedLeft: () => console.log("User Swiped Left!"),
    onSwipedRight: () => console.log("User Swiped Right!"),
  });

  return <div {...handlers}>Swipe me!</div>;
}

Custom swipe distance and velocity:

const handlers = useSwipeable({
  onSwiped: (eventData) => console.log("Swiped!", eventData),
  swipeDuration: 500,
  touchEventOptions: { passive: true },
  trackMouse: true,
  trackTouch: true,
  delta: 100,
  preventDefaultTouchmoveEvent: true,
});

Using with React Router:

import { useSwipeable } from 'react-swipeable';
import { useNavigate } from 'react-router-dom';

function SwipeableNavigation() {
  const navigate = useNavigate();
  const handlers = useSwipeable({
    onSwipedLeft: () => navigate('/next-page'),
    onSwipedRight: () => navigate('/previous-page'),
  });

  return <div {...handlers}>Swipe to navigate</div>;
}

Getting Started

  1. Install the package:

    npm install react-swipeable
    
  2. Import and use in your React component:

    import React from 'react';
    import { useSwipeable } from 'react-swipeable';
    
    function MySwipeableComponent() {
      const handlers = useSwipeable({
        onSwiped: (eventData) => console.log("Swiped!", eventData),
        onSwipedLeft: () => console.log("Swiped left!"),
        onSwipedRight: () => console.log("Swiped right!"),
      });
    
      return (
        <div {...handlers}>
          Swipe me!
        </div>
      );
    }
    
    export default MySwipeableComponent;
    
  3. Customize the swipe behavior by adjusting the options passed to useSwipeable.

Competitor Comparisons

A Cross-Browser, Event-based, Element Resize Detection for React

Pros of react-resize-detector

  • Focuses specifically on detecting element size changes
  • Provides more granular control over resize detection options
  • Supports both width and height change detection

Cons of react-resize-detector

  • Limited to resize detection, lacks swipe gesture functionality
  • May have higher performance overhead for simple resize tracking

Code Comparison

react-resize-detector:

import { useResizeDetector } from 'react-resize-detector';

const Component = () => {
  const { width, height, ref } = useResizeDetector();
  return <div ref={ref}>{`${width}x${height}`}</div>;
};

react-swipeable:

import { useSwipeable } from 'react-swipeable';

const Component = () => {
  const handlers = useSwipeable({
    onSwiped: (eventData) => console.log('Swiped!', eventData),
  });
  return <div {...handlers}>Swipe me!</div>;
};

While react-resize-detector is focused on size changes, react-swipeable provides swipe gesture detection. The code examples show how each library is used in a React component. react-resize-detector uses a ref to track size changes, while react-swipeable applies event handlers to enable swipe detection. Both libraries offer hooks for easy integration, but serve different purposes in React applications.

React draggable component

Pros of react-draggable

  • Provides more comprehensive dragging functionality, including grid snapping and bounds
  • Offers better control over drag behavior with callbacks and customizable drag handles
  • Supports both mouse and touch events for broader device compatibility

Cons of react-draggable

  • Larger bundle size due to more features, which may impact performance
  • Steeper learning curve with more configuration options
  • Less focused on swipe gestures, which may be crucial for certain mobile applications

Code Comparison

react-draggable:

import Draggable from 'react-draggable';

<Draggable
  axis="x"
  handle=".handle"
  defaultPosition={{x: 0, y: 0}}
  grid={[25, 25]}
  scale={1}
>
  <div>
    <div className="handle">Drag from here</div>
    <div>This content will be draggable</div>
  </div>
</Draggable>

react-swipeable:

import { useSwipeable } from 'react-swipeable';

const handlers = useSwipeable({
  onSwiped: (eventData) => console.log("User Swiped!", eventData),
  ...config,
});

return <div {...handlers}>This content is swipeable</div>;

The code examples highlight the different focus of each library: react-draggable emphasizes dragging with various options, while react-swipeable concentrates on swipe gestures with a simpler API.

A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list✌️

Pros of react-sortable-hoc

  • Offers more advanced sorting functionality, including multi-list sorting and virtualized lists
  • Provides better performance for large lists due to its optimized rendering approach
  • Includes built-in animation support for smoother user experience

Cons of react-sortable-hoc

  • Has a steeper learning curve due to its more complex API
  • Requires more setup and configuration compared to react-swipeable
  • May be overkill for simple sorting needs, adding unnecessary overhead

Code Comparison

react-sortable-hoc:

import { SortableContainer, SortableElement } from 'react-sortable-hoc';

const SortableItem = SortableElement(({value}) => <li>{value}</li>);
const SortableList = SortableContainer(({items}) => {
  return (
    <ul>
      {items.map((value, index) => (
        <SortableItem key={`item-${index}`} index={index} value={value} />
      ))}
    </ul>
  );
});

react-swipeable:

import { useSwipeable } from 'react-swipeable';

const SwipeableItem = ({ children }) => {
  const handlers = useSwipeable({
    onSwiped: (eventData) => console.log('Swiped!', eventData),
  });
  return <div {...handlers}>{children}</div>;
};

While react-sortable-hoc focuses on creating sortable lists with advanced features, react-swipeable provides a simpler API for handling swipe gestures. The choice between the two depends on the specific requirements of your project.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React Swipeable — Formidable, We build the modern web

React swipe event handler hook

npm downloads npm version build status gzip size maintenance status

Edit react-swipeable image carousel

Visit the Docs site for information on usage, api, and demos.

License

MIT

Contributing

Please see our contributions guide.

Maintainers

Project Maintenance

Maintenance Status

Active: Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.

NPM DownloadsLast 30 Days