4.0.3 • Published 5 years ago

clicked v4.0.3

Weekly downloads
56
License
MIT
Repository
github
Last release
5 years ago

clicked

Configurable vanilla handler for clicks, double-clicks, and/or long-clicks that works for both mouse and touch

changes in version 4

  • ported code to typescript
  • added options.mouse to select which mouse buttons are active (if any)
  • added options.touch to select whether touch is active and how many touch points are allowed
  • removed rollup and compiled using tsc
  • improved demo

usage

import { clicked } from 'clicked'

or

const clicked = require('clicked').clicked

demo

https://davidfig.github.io/clicked/

rationale

I wanted to create a vanilla javascript way to handle mouse and touch clicks, double-clicks, and long-clicks.

example

import { clicked } from 'clicked'

function handleClick()
{
   console.log('I was clicked.')
}
const div = document.getElementById('clickme')
const c = clicked(div, handleClick, { threshold: 15 })

// change callback
c.callback = () => console.log('different clicker')

// destroy
c.destroy()

// using built-in querySelector
clicked('#clickme', handleClick2)

// support for all types of clicks
function handleAllClicks(e) {
    switch (e.type)
    {
        case 'clicked': ...
        case 'double-clicked': ...
        case 'long-clicked': ...
    }
    // view UIEvent that caused callback
    console.log(e.event)
}
clicked('#clickme', handleAllClicks, { doubleClick: true, longClick: true })

API

clicked(element, callback, options) : Clicked

creates Clicked object for element

nametypedefaultdescription
elementHTMLElement or stringelement or querySelector entry (e.g., #id-name or .class-name)
callbackClickedCallbackcallback called after clicked
optionsobjectoptional options
options.clickedbooleantruedispatch event for clicked
options.thresholdnumber10threshold of movement to cancel all events
options.mouseboolean or 'left' or 'right' 'middle' or 'left-right' or 'left-middle' or 'right-middle'truewhether to listen for mouse events; can also be used to set which mouse buttons are active
options.touchboolean or number1whether to listen for touch events; can also be used to set the number of touch points to accept
options.doubleClickbooleanfalsedispatch events for double click
options.doubleClickTime]number500wait time in millseconds for double click
options.longClick]booleanfalseenable watcher for long click
options.longClickTime]boolean500wait time for long click
options.clickDownbooleandispatch event for click down (ie, after touchstart or mousedown callback will be called with type 'click-down')
options.capturebooleanfalseevents will be dispatched to this registered listener before being dispatched to any EventTarget beneath it in the DOM tree

Clicked

returned by clicked(...)

Clicked.destroy()

removes event listeners on element

Clicked.callback (function): ClickedCallback

nametypedescription
eventMouseEvent or TouchEventmouse or touch event that triggered callback
type'clicked' or 'double-clicked' or 'long-clicked' or 'click-down'type of click

Clicked.cancel()

cancel any outstanding events

demo

yarn demo

Open browser to https://localhost:1234/

license

MIT License
(c) 2020 YOPEY YOPEY LLC by David Figatner

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.2.3

5 years ago

3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.0

5 years ago

3.0.0

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

7 years ago

1.3.0

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago