Utility functions for browser and dom events
npm install lemonade-helpers
import { onClick } from "lemonade-helpers";
let offClick;
function init() {
let button = document.querySelector('button');
// add 'click' event listener on button
offClick = onClick(button, () => {
console.log('click');
});
}
function destroy() {
// remove 'click' event listener on button
offClick();
}
Empty function
Return performance.now if available, Date.now otherwise
Attach a blur
listener to element
. Return a function to detach the listener.
Attach a focus
listener to element
. Return a function to detach the listener.
Attach a change
listener to element
. Return a function to detach the listener.
Attach a click
listener to element
. Return a function to detach the listener.
Attach a mousedown
listener to element
. Return a function to detach the listener.
Attach a mousemove
listener to element
. Return a function to detach the listener.
Attach a mouseenter
listener to element
. Return a function to detach the listener.
Attach a touchstart
listener to element
. Return a function to detach the listener.
Attach a touchmove
listener to element
. Return a function to detach the listener.
Attach a touchend
listener to element
. Return a function to detach the listener.
Attach mousedown
and touchstart
listeners to element
. Return a function to detach the listeners.
Attach mousemove
and touchmove
listeners to element
. Return a function to detach the listeners.
Attach mouseup
and touchend
listeners to element
. Return a function to detach the listeners.
Attach a keydown
listener to element
. If only a function is provided, the listener will be attached to document
.
Return a function to detach the listener.
Attach a keypress
listener to element
. If only a function is provided, the listener will be attached to document
.
Return a function to detach the listener.
Attach a keyup
listener to element
. If only a function is provided, the listener will be attached to document
.
Return a function to detach the listener.
Subscribe fn
on requestAnimationFrame
.
Set options.fire
to true
to trigger fn
before waiting for the next frame.
Return a function to cancel the subscription.
Attach a resize
listener to window
if non-existent.
Set options.fire
to true
to trigger fn
before waiting for the next resize.
Subscribe fn
on window resize. Return a function to cancel the subscription.
Attach touchstart
, touchmove
and touchend
to element
and trigger fn
when finger moves more than options.delta on the x
axis (default to 30px). Return a function to detach the listeners.
Attach touchstart
, touchmove
and touchend
to element
and trigger fn
when finger moves more than options.delta on the y
axis (default to 30px). Return a function to detach the listeners.
Trigger fn
when element is mousedown
or touchstart
and when options.outsideElement
is mousemove
or touchmove
Return a function to detach all the listeners created.
element
: the DOM element to attachedmousedown
andtouchstart
eventsfn
: A callback functionoptions.outsideElement
: the DOM element to attachedmousemove
andtouchstart
events. Default todocument
.
Attach keydown
listener to document
and trigger fn
when consecutive keys match the Konami Code.
Return a function to detach the listener.
Avoid event.preventDefault is the event was triggered while CMD
, Alt
, Shift
, Ctrl
was pressed.
Useful to prevent click on links while keeping the default behaviour (opening in a new tab) if a key is pressed simultaneously.
Shorthand for element.style[property] = value
. I found it to improve readability while styling multiple elements at the same time.
Assign multiple property/value pairs at the same time.
styles(element, {
transform: `translate3d(${x}px, ${y}px, 0)',
opacity,
})
MIT License, see LICENSE for details