npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-long-press-hook

v0.0.3

Published

Unlock the power of long-press interactions with react-press-and-hold! 🚀 This lightweight and highly customizable React hook lets you easily detect and handle long-press events in your applications. Whether you’re looking to enhance your UI with context

Downloads

57

Readme

📱 react-long-press-hook

Unlock the power of long-press interactions with react-long-press-hook! 🚀 This lightweight and highly customizable React hook lets you easily detect and handle long-press events in your applications. Whether you’re looking to enhance your UI with context menus, drag-and-drop functionality, action confirmations, or interactive tutorials, react-long-press-hook has got you covered.

✨ Features

  • 🔧 Customizable: Easily configure thresholds, prevent default behavior, and more.
  • 🪶 Lightweight: Minimal impact on your bundle size.
  • 👐 Accessible: Works with both touch and mouse events.
  • Efficient: No unnecessary re-renders or performance hits.

📦 Installation

npm install react-long-press-hook

or

yarn add react-long-press-hook

📚 Use Cases

📜 Context Menus

Enable context menus on long-press to provide users with additional options or actions.

🖱️ Drag-and-Drop

Implement drag-and-drop functionality where a long-press initiates the dragging action.

✅ Action Confirmations

Use long-press to confirm actions, such as deleting an item, to prevent accidental taps.

🎓 Interactive Tutorials

Create interactive tutorials that require users to long-press on elements to reveal tips or further instructions.

🕹️ Custom Gesture Controls

Develop custom gesture controls for touch interfaces, enhancing user interaction on mobile devices.

🎮 Game Mechanics

Incorporate long-press actions as part of game mechanics, such as charging a power-up or revealing hidden objects.

🚀 Usage

Here is a basic example of how to use the useLongPress hook in your React application:

import React, { useState } from 'react';
import { useLongPress } from 'react-long-press-hook';

const App = () => {
  const [message, setMessage] = useState('');

  const onLongPress = () => {
    setMessage('Long press detected!');
  };

  const onPressHold = () => {
    setMessage('Press and hold in progress...');
  };

  const onPressRelease = () => {
    setMessage('Press released');
  };

  const longPressEvent = useLongPress(onLongPress, {
    onStart: onPressHold,
    onFinish: onPressRelease,
    threshold: 2000, // milliseconds
    preventDefault: true,
    cancelOnMove: true,
  });

  return (
    <div>
      <button {...longPressEvent}>Long Press Me</button>
      <p>{message}</p>
    </div>
  );
};

export default App;

📚 API

useLongPress(callback, options)

Parameters

  • callback (function): The function to call when a long-press is detected.
  • options (object): Configuration options for the hook.
    • onStart (function): Function to call when the press starts.
    • onFinish (function): Function to call when the press ends.
    • threshold (number): Time in milliseconds to detect a long-press. Default is 300.
    • preventDefault (boolean): Whether to prevent the default context menu on long-press. Default is true.
    • cancelOnMove (boolean): Whether to cancel the long-press if the pointer moves. Default is false.
    • stopPropagation (boolean): Whether to stop event propagation. Default is false.

Returns

handlers (object): Event handlers to spread on the target element.

Check Out My Other Packages

Explore more useful packages by visiting my npm profile. Made with ❤️ by Ashish