All Projects → minwork → use-long-press

minwork / use-long-press

Licence: MIT license
React hook for detecting click (or tap) and hold event

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to use-long-press

THREE.Interactive
Fast and simple interaction manager for three.js for enabling mouse and touch events on 3D objects
Stars: ✭ 49 (-49.48%)
Mutual labels:  touch-events, mouse-events
Vuetify Swipeout
👆 A swipe out example built with Vue CLI 3 + Vuetify + Swiper.
Stars: ✭ 117 (+20.62%)
Mutual labels:  touch-events
Hover On Touch
A pure Javascript Plugin for an alternative hover function that works on mobile and desktop devices. It triggers a hover css class on »Taphold« and goes to a possible link on »Tap«. It works with all html elements.
Stars: ✭ 256 (+163.92%)
Mutual labels:  touch-events
Zoomlayout
2D zoom and pan behavior for View hierarchies, images, video streams, and much more, written in Kotlin for Android.
Stars: ✭ 688 (+609.28%)
Mutual labels:  touch-events
Detect It
Detect if a device is mouseOnly, touchOnly, or hybrid, and if the primary input is mouse or touch
Stars: ✭ 294 (+203.09%)
Mutual labels:  touch-events
Dom Examples
Code examples that accompany various MDN DOM and Web API documentation pages
Stars: ✭ 984 (+914.43%)
Mutual labels:  touch-events
getting-touchy-presentation
Everything you (n)ever wanted to know about touch and pointer events
Stars: ✭ 42 (-56.7%)
Mutual labels:  touch-events
render-props
㸚 Easy-to-use React state containers which utilize the render props (function as child) pattern
Stars: ✭ 33 (-65.98%)
Mutual labels:  mouse-events
Jkcardlayout
高仿即刻APP探索页卡片布局拖拽、卡片前进、回退和下拉效果,采用RecyclerView和自定义LayoutManager实现
Stars: ✭ 111 (+14.43%)
Mutual labels:  touch-events
Ios13 Simulatetouch
iOS Automation Framework iOS Touch Simulation Library
Stars: ✭ 610 (+528.87%)
Mutual labels:  touch-events
Nestedtouchscrollinglayout
🎱处理子 View,父 View 嵌套滚动,成本比 support v4 NestedScrolling 低,放心食用~
Stars: ✭ 557 (+474.23%)
Mutual labels:  touch-events
Creepyface
A JavaScript library that makes your face follow the pointer. 🤪🖱️👆
Stars: ✭ 412 (+324.74%)
Mutual labels:  touch-events
Slideout
A touch slideout navigation menu for your mobile web apps.
Stars: ✭ 7,981 (+8127.84%)
Mutual labels:  touch-events
Touchdemo
Custom touch handling in Android
Stars: ✭ 273 (+181.44%)
Mutual labels:  touch-events
Keen Slider
The HTML touch slider carousel with the most native feeling
Stars: ✭ 3,097 (+3092.78%)
Mutual labels:  touch-events
touchRobot
js模拟手指触碰点击,手指滑动,下拉刷新
Stars: ✭ 46 (-52.58%)
Mutual labels:  touch-events
Tap
1Kb library for easy unified handling of user interactions such as mouse, touch and pointer events.
Stars: ✭ 541 (+457.73%)
Mutual labels:  touch-events
Swiper
Most modern mobile touch slider with hardware accelerated transitions
Stars: ✭ 29,519 (+30331.96%)
Mutual labels:  touch-events
alloy-finger-vue
alloy-finger vue版集成,移动的适配、阻止冒泡、longtap事件修改
Stars: ✭ 18 (-81.44%)
Mutual labels:  touch-events
Aiforms.effects
AiForms.Effects for Xamarin.Forms
Stars: ✭ 245 (+152.58%)
Mutual labels:  touch-events

React Long Press Hook 👇

React hook for detecting click (or tap) and hold event.

Build Status Codecov npm type definitions npm bundle size npm Github Stars

  • Easy to use
  • Highly customizable options
  • Thoroughly tested

Install

yarn add use-long-press

or

npm install --save use-long-press

Basic Usage

import React from 'react';
import { useLongPress } from 'use-long-press';

const Example = () => {
  const bind = useLongPress(() => {
    console.log('Long pressed!');
  });

  return <button {...bind()}>Press me</button>;
};

Live example

Version 1

Edit useLongPress

Version 2

Edit useLongPress

Advanced usage

Hook first parameter, callback, can be either function or null (if you want to disable the hook).

Additionally, you can supply options object as a second parameter.

As a result hook returns object with various handlers (depending on detect option), which can be spread to some element.

You can supply custom context to the bind function like bind(context) and then access it from callbacks (onStart, onFinish, onCancel, onMove) second argument e.g.: onStart: (event, { context }) => ....

Definition

useLongPress(callback [, options]): handlers

Options

Long press hook can be adjusted using options object, which allow you to fit it to your needs.

Name Type Default Description
threshold number 400 Time user need to hold click or tap before long press callback is triggered
captureEvent boolean false If React MouseEvent (or TouchEvent) should be supplied as first argument to callbacks
detect Enum('mouse' | 'touch' | 'both') 'both' Which event handlers should be returned in bind object. In TS this enum is accessible through LongPressDetectEvents
cancelOnMovement boolean | number false If long press should be cancelled when detected movement while pressing. Use boolean value to turn it on / off or number value to specify move tolerance in pixels.

For more information on how this prop work check JSDoc.
filterEvents (event) => boolean undefined If provided, it gives you the ability to ignore long press detection on specified conditions (for example on right mouse click).

When function returns false, it will prevent ANY callbacks from triggering (including onStart and onCancel) as well as capturing event.
onStart Function undefined Called when element is initially pressed (before starting timer which detects long press).

Can accept mouse or touch event if captureEvents option is set to true.
onFinish Function undefined Called when press is released (after triggering callback).

Can accept mouse or touch event if captureEvents option is set to true.
onCancel Function undefined Called when press is released before threshold time elapses, therefore before long press occurs.

Can accept mouse or touch event if captureEvents option is set to true. You can obtain reason for cancellation from a second callback argument e.g.: onCancel: (event, { reason }) => ...
onMove Function undefined Handler for onTouchMove and onMouseMove props, also allowing to make some operations on event before triggering cancelOnMovement.

Can accept mouse or touch event if captureEvents option is set to true.

Example

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

export default function AdvancedExample() {
  const [enabled, setEnabled] = useState(true);
  const callback = useCallback(event => {
    alert('Long pressed!');
  }, []);
  const bind = useLongPress(enabled ? callback : null, {
    onStart: event => console.log('Press started'),
    onFinish: event => console.log('Long press finished'),
    onCancel: event => console.log('Press cancelled'),
    onMove: event => console.log('Detected mouse or touch movement'),
    filterEvents: event => true, // All events can potentially trigger long press
    threshold: 500,
    captureEvent: true,
    cancelOnMovement: false,
    detect: 'both',
  });

  return (
    <div>
      <button {...bind()}>Press and hold</button>
      <div>
        <label htmlFor="enabled">
          <input type="checkbox" id="enabled" checked={enabled} onChange={() => setEnabled(current => !current)} />
          Hook enabled
        </label>
      </div>
    </div>
  );
}

License

MIT © minwork

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].