user-gesture
v1.0.0
Published
Reserve calling function that is needed user gesture(or user activation)
Readme
user-gesture
Reserve calling function that is needed user gesture(or user activation)
install
npm i install user-gesturebuild
npm run buildtest
npm run testUse case and Usage
Some events like mousedown or touchstart are NOT regarded as "user-gesture". If you want to some user-gesture-required action, for exmaple accessing clipboard.
You can use UserGesture.post to reserve to do some user-gesture-required actions like:
document.querySelector("#copy").addEventListener("mousedown", (e)=>{
// accessing to clipboard is not allowed in this context.
console.log("Copying...");
UserGesture.post(async () => {
// accessing to clipboard is allowed in this context.
await navigator.clipboard.writeText("test");
console.log("Copyed!");
});
});Functions
post(action)- Call function
actionat user-gesture context - In default,
actionis called when either of following events is fired ondocument.body- change, click, contextmenu,dblclick, mouseup, pointerup,reset, submit, touchend
actioncan receive an object as argument that contains:postedAt: The timestamp that calledpostin millisecondsevent: The user-gesture event whenactionis called.
- Call function
setHandler(element)- Add an element that processes
actioninpostfunction. - It is mainly used for elements that call
stopPropagationto the event object, that prevent processpostondocument.body
- Add an element that processes
run()- Force process
posted functions - Of course, it should be called user-gesture context.
- Force process
