Git Product home page Git Product logo

react-click-n-hold's Introduction

react-click-n-hold

by @sonsoleslp

Long press event for react. Click and hold wrapper component.

Detect when a DOM element has been pressed for the amount of time you specify. http://sonsoleslp.github.io/react-click-n-hold/

react-click-n-hold

Example usage

First install the component

npm install --save react-click-n-hold

Then use the component in your app

import React from 'react';
import ClickNHold from 'react-click-n-hold'; 

export default class Example extends React.Component {
	start(e){
		console.log('START'); 
	} 
    
	end(e, enough){
		console.log('END');
        console.log(enough ? 'Click released after enough time': 'Click released too soon');            
	} 
    
	clickNHold(e){
		console.log('CLICK AND HOLD');  
	} 

	render(){
		return ( 
			<ClickNHold 
				time={2} // Time to keep pressing. Default is 2
				onStart={this.start} // Start callback
				onClickNHold={this.clickNHold} //Timeout callback
				onEnd={this.end} > // Click release callback
					<button>Click and hold</button>
			</ClickNHold>
		); 
	}
}

Styling animation:

  • This component does not provide css for pressing effects
  • However, during press, .cnh_holding is applied to the wrapper, allowing the user to animate the transition using css
  • Below is an example of styling; the one used in the demo
  
@-webkit-keyframes fill { 
  to {
   background-size: 100% 0; 
  }
} 

@keyframes fill { 
  to { 
   background-size: 100% 0;
  }
}

//The wrapper has the .cnh_holding class while the button is being pressed

.cnh_holding button {
  background: -webkit-linear-gradient( white , white) rgb(255,215,235) no-repeat 0 0;
  background: linear-gradient( white , white) rgb(255,215,235) no-repeat 0 0;
  mix-blend-mode: multiply;
  background-size: 100% 100%;
  -webkit-animation: fill 2s forwards;
  animation: fill 2s forwards;
}

Run locally

npm install react-click-n-hold
npm run dev 

Open localhost:8080

react-click-n-hold's People

Contributors

alex-birch avatar f4ndor avatar sonsoleslp avatar williammanco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-click-n-hold's Issues

Add support for keyboard

We want to introduce this lib in our design system but we noticed that the "click and hold" doesn't work when using the keyboard. When using <button> elements events should be fired even while pressing enter or space

ClickNHold Button With State

I have a powerOn powerOff button that uses this library.
If the button state is OFF, when I press to turn on it goes from gray to green color with animation.
But since the button takes
cnh_holding
class on press, I could not achive green to gray animation while pressing ON state button so switch OFF. What is the solution for such a case.

MouseUp / End not Called if ClickNHold Handler Calls Alert

If the ClickNHold handler opens an alert the mouseUp event is never called. This prevents the end call from happening so holding the mouse down on the element again will not trigger the ClickNHold handler.

This was tested on Chrome 71.0.3578.98 on OSX.

no working for table rows

<table> expects <tr> elements as children. Decorating a <tr> element with <ClickNHold> produces some issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.