Git Product home page Git Product logo

swiftclick's Introduction

SwiftClick

SwiftClick is a library created to eliminate the 300ms click event delay on touch devices that support orientation change and is designed to be super lightweight.

Teeny-tiny

~1KB minified & gzipped :-)

Usage

You can install SwiftClick using npm:

npm i swiftclick --save

You can install via Bower instead, if that's your thing:

bower install swiftclick

You can also use CDNJS: https://cdnjs.com/libraries/swiftclick

Otherwise, you can grab either the minified, or non-minified source from Github.

Include SwiftClick in your application

If using CommonJS then simply require SwiftClick as per usual:

var SwiftClick = require('swiftclick');

Otherwise, use a script tag:

<script type="application/javascript" src="path/to/swiftclick.min.js"></script>

Setup SwiftClick

Setting up SwiftClick is a very easy process, which mirrors that of FastClick in that instances must be attached to a context element. Touch events from all elements within the context element are automatically captured and converted to click events when necessary, minus the delay.

Start by creating a reference to a new instance of SwiftClick using the 'attach' helper method and attach it to a context element. Attaching to document.body is easiest if you only need a single instance of SwiftClick:

var swiftclick = SwiftClick.attach(document.body);

If necessary, multiple instances of SwiftClick can be created for specific context elements which, although not really necessary in most cases, can sometimes be useful for optimising applications with a large amount of HTML:

var navigationSwiftClick = SwiftClick.attach(someNavElement);
var uiSwiftClick = SwiftClick.attach(someOtherElement);

Default Elements

Once attached, by default SwiftClick will track events originating from the following element types:

  • <a>
  • <div>
  • <span>
  • <button>

Adding non-default element types

If necessary you can make SwiftClick track events originating from additional element types by adding an array of node names. This requires a reference to an instance of SwiftClick:

var swiftclick = SwiftClick.attach(someElement);
swiftclick.addNodeNamesToTrack(['p', 'h1', 'nav']);

Replacing all stored node names to track

var swiftclick = SwiftClick.attach(someElement);
swiftclick.replaceNodeNamesToTrack(['a', 'div', 'h1']);

Doing this will remove all default node names, as well as any that have been added, and replace them with the node names within the array that is passed in, resulting in only the new node names being tracked.

Updating the maximum drift distance

It is possible to set the maximum pixel distance that a touchmove can travel before SwiftClick no longer considers it a click:

var swiftclick = SwiftClick.attach(someElement);
swiftclick.setMaxTouchDrift(10);

The default value is 16 and the minimum value is 4. A value between 10 and 30 is recommended.

Ignoring swift clicks on specific elements

The parsing of CSS class names is disabled by default to improve performance, so in order to use this feature, it must be explicity switched on:

var swiftclick = SwiftClick.attach(document.body);
swiftclick.useCssParser(true);

Adding the swiftclick-ignore class to an element will disable swift clicks on the element and all off its children:

<div class="swiftclick-ignore">
    This element and its children will not get swift clicks.
</div>

Enabling swift clicks on specific elements within an ignored element

Turn on CSS class name parsing:

var swiftclick = SwiftClick.attach(document.body);
swiftclick.useCssParser(true);

Within any element containing the swiftclick-ignore class, swift clicks can be enabled for specific child elements by adding the swiftclick-force class:

<div class="swiftclick-ignore">
    <button>First</button>
    <button class="swiftclick-force">Second</button>
</div>

In this example, the first button will not get swift clicks, but the second button will.

Automatically disabled when not needed

SwiftClick only intercepts events for touch devices that support orientation change, otherwise it just sits there looking pretty.

About the Project

SwiftClick was developed and is currently maintained by Ivan Hayes.

swiftclick's People

Contributors

munkychop avatar prashantpalikhe avatar dennisjamin avatar huhgawz avatar mrmartineau avatar

Stargazers

Jonatas Santos avatar Wellington Barreto avatar  avatar Lipan avatar monkeytao avatar 巫书轶 avatar niceSprite avatar Joshua Boehringer avatar Dan Nagle avatar Wilmerson da Silva avatar Quinn Keaveney avatar  avatar  avatar 大関 金城 秀喜 カシオ avatar 赵金添 avatar barnz avatar Rob Cherny avatar Manuel Deutsch avatar Jonas Taedcke avatar Ingvi Rafn avatar Jarand avatar Sigurður Snær Eiríksson avatar Peter Craig avatar Matt Zimmermann avatar Rio Purnomo avatar Lakatos Tibor avatar Leon Sorokin avatar  avatar Yurii Didkovskyi avatar mougua avatar sdussaut avatar Saiya avatar James Ferguson avatar Urban Sandén avatar  avatar Isaac A. Dettman avatar  avatar Vivek Siruvuri avatar José Fernando Tolentino avatar PiotrD avatar  avatar Todd Dominey avatar Steven Hargrove avatar  avatar Garrett Johnson avatar Zhao Lei avatar janily avatar Tyler Sticka avatar Pablo Heredia avatar hacfi avatar Jonathan Marshall avatar Vamsi Krishna B avatar Alexander Puschilov avatar zspecza avatar Jeremy Mack avatar Simon Collins avatar Adam Brodzinski avatar Ian Murray avatar Brian Fletcher avatar Aldo Lugo avatar Bogdan Teodoru avatar  avatar Kyriakos-Ioannis D. Kyriakou avatar Kenny Smith avatar Yuya Saito avatar Mato Ilic avatar Glavin Wiechert avatar Alexey Tsverov avatar  avatar

Watchers

Rolff avatar Ciaran Park avatar  avatar Ashley Watson-Nolan avatar James Cloos avatar Vivek Siruvuri avatar George Bardis avatar Iqbal Khan avatar  avatar Ashley Sheridan avatar

swiftclick's Issues

Form with a submit button requires double click for submission when SwiftClick is enabled.

The issue is replicated on the following bin

http://jsbin.com/vuwukeqapi/

You can load this bin on iOS safari to see the issue.

Basically what's happening is:

  1. on iOS, if the form has a submit button. Clicking on the submit button on the keyboard fires a click event on the submit button.
  2. No touch events are fired.
  3. SwiftClick's click handler handles the event. And, if the _clickedAlready variable is set to true from any previous clicks, the click handler cancels the event. Therefore, cancelling the form submission on the first click.

Causes issues while handling touch events on an element which has SwiftClick tracked children.

touchEndHandler of SwiftClick is stopping propagation of the event. So, if we are handling touch events on a parent and a SwiftClick handled element is the event target, the touch event will never reach the parent.

https://github.com/munkychop/swiftclick/blob/master/js/libs/swiftclick.js#L117

Example scenario:

We have a drawer element which can be swiped in and out of the viewport. Drawer has links inside which are SwiftClick managed. When we drag the drawer by placing the finger over a link and let the finger go, SwiftClick's touchEndHandler kicks in, stops the propagation and the drawer's touch end handler never gets invoked.

PS stopPropagation() was introduced to prevent ghost clicks on Android devices. But we no longer need SwiftClick for Android.

[Bug] Two events on Android

Configuration:

var swiftclick = SwiftClick.attach(document.body);

It works well on iOS but on Android it doesn't work. The click event fires twice per click. But if I do a "long press" it works !

OS: Android 4.2.2

Programmatic clicks and SwiftClick

We are performing automated tests in the cloud using Perfecto Mobile. But, the tests fail because the programmatic clicks need to performed twice for the desired action to be performed. This is because of the ghost click prevention mechanism of SwiftClick.

  1. First click goes fine. _clickedAlready is set to true
  2. Second click is prevented. Because _clickedAlready is set to true. _clickedAlready is set to false
  3. Third click goes fine.

Now my question is, is this ghost click prevention still necessary? Or can we remove that chunk of code. From my tests on iOS devices, that block of code is redundant. Can you perform some tests on Android devices?

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.