Have you ever wanted to execute a function in your Vue application just by holding a button down for a few seconds?
Have you ever wanted to create a button on your application that helps to clear out either a single input by pressing once (or a whole input holding a button down)?
You have? Good. Me too. And you’re in the right place.
This article will explain how to both execute functions and remove inputs by a press (or hold) of a button.
First, I will explain how to achieve this in VanillaJS. Then, create a Vue directive for it.
Buckle up. I’m about to blow your mind.
To achieve a long press, a user needs to press and hold the button for a few seconds.
To replicate this in code, we need to listen to when the mouse “click” button is pressed, start a timer for however long we want the user to hold down the button before executing the function, and execute the function after the time set has passed.
Pretty straightforward! However, we need to know when the user is holding down that button.
When a user clicks a button, two other events gets fired before the click event: mousedown
and mouseup
.
The mousedown
event gets called when the user presses the mouse button, while the mouseup
event gets called when the user releases that button.
All we need to do is:
mousedown
event occursmouseup
event gets fired before the 2secs mark, i.e., a full click eventAs long as the timer doesn’t get cleared before it gets to that time we’ve set — i.e., the mouseup
event doesn’t get fired — we can say that user hasn’t released the button. Therefore, it’s considered a long press and we can then proceed to execute said function.
Let’s dive into the code and get this done.
Firstly, we have to define three things, namely:
This variable basically holds the value of the setTimeout
so we can cancel this when a mouseup
event occurs.
We are setting the variable to null
just so we can check the variable to know if there’s an active timer currently on before going ahead to cancel it.
This function consists of a setTimeout
which, basically, is a method in Javascript that allows us to execute a function after a particular duration stated in the function.
Remember, in the process of creating a click event, two events gets fired. But what we need to start the timer is the mousedown
event. Therefore, we do not need to start the timer if it’s a click event.
This function basically does what the name says, to cancel the setTimeout
that was created when the start function got called.
To cancel the setTimeout
, we would be using the clearTimeout
method in JavaScript, which basically clears a timer set with the setTimeout()
method.
Before using the clearTimeout
we first need to check if the pressTimer
variable is set to null. If it’s not set to null that means there’s an active timer. So, we need to clear the timer and, you guessed it, set the pressTimer
variable to null
.
This function would be called once the mouseup
event is fired.
What’s left is to add event listeners to the button you want to add the long press effect on.
addEventListener("mousedown", start); addEventListener("click", cancel);
When creating a Vue directive, Vue allows us to define a directive globally or locally to a component, but in this article we would be going the global route.
Let’s build the directive that accomplishes this.
Firstly we have to declare the name of the custom directive.
This basically registers a global custom directive named v-longpress
.
Next, we add the bind
hook function with some arguments, which allows us to reference the element the directive is bound to, fetch the value that is passed to the directive, and identify the component the directive is used in.
Vue.directive('longpress', { bind: function (el, binding, vNode) { } }
Next, we make add our long-press JavaScript code in the bind function.
Vue.directive('longpress', { bind: function (el, binding, vNode) { // Define variable let pressTimer = null // Define funtion handlers // Create timeout ( run function after 1s ) let start = (e) => { if (e.type === 'click' && e.button !== 0) { return; } if (pressTimer === null) { pressTimer = setTimeout(() => { // Execute something !!! }, 1000) } } // Cancel Timeout let cancel = (e) => { // Check if timer has a value or not if (pressTimer !== null) { clearTimeout(pressTimer) pressTimer = null } } // Add Event listeners el.addEventListener("mousedown", start); // Cancel timeouts if this events happen el.addEventListener("click", cancel); el.addEventListener("mouseout", cancel); } })
Next, we need to add a function that would run the method that will be passed to the longpress
directive.
Vue.directive('longpress', { bind: function (el, binding, vNode) { // Define variable let pressTimer = null // Define funtion handlers // Create timeout ( run function after 1s ) let start = (e) => { if (e.type === 'click' && e.button !== 0) { return; } if (pressTimer === null) { pressTimer = setTimeout(() => { // Execute function handler() }, 1000) } } // Cancel Timeout let cancel = (e) => { // Check if timer has a value or not if (pressTimer !== null) { clearTimeout(pressTimer) pressTimer = null } } // Run Function const handler = (e) => { // Execute method that is passed to the directive binding.value(e) } // Add Event listeners el.addEventListener("mousedown", start); // Cancel timeouts if this events happen el.addEventListener("click", cancel); el.addEventListener("mouseout", cancel); } })
Now we can use the directive in our Vue application which will work fine until a user adds a value that isn’t a function in the value of the directive. So we have to prevent this by warning the user once this happens.
To warn the user, we add the following to the bind function:
// Make sure expression provided is a function if (typeof binding.value !== 'function') { // Fetch name of component const compName = vNode.context.name // pass warning to console let warn = `[longpress:] provided expression '${binding.expression}' is not a function, but has to be` if (compName) { warn += `Found in component '${compName}' ` } console.warn(warn) }
Lastly, it would be great for this directive to also work on touch devices. So we add event listeners for touchstart
, touchend
, touchcancel
.
Putting everything together:
Vue.directive('longpress', { bind: function (el, binding, vNode) { // Make sure expression provided is a function if (typeof binding.value !== 'function') { // Fetch name of component const compName = vNode.context.name // pass warning to console let warn = `[longpress:] provided expression '${binding.expression}' is not a function, but has to be` if (compName) { warn += `Found in component '${compName}' ` } console.warn(warn) } // Define variable let pressTimer = null // Define funtion handlers // Create timeout ( run function after 1s ) let start = (e) => { if (e.type === 'click' && e.button !== 0) { return; } if (pressTimer === null) { pressTimer = setTimeout(() => { // Run function handler() }, 1000) } } // Cancel Timeout let cancel = (e) => { // Check if timer has a value or not if (pressTimer !== null) { clearTimeout(pressTimer) pressTimer = null } } // Run Function const handler = (e) => { binding.value(e) } // Add Event listeners el.addEventListener("mousedown", start); el.addEventListener("touchstart", start); // Cancel timeouts if this events happen el.addEventListener("click", cancel); el.addEventListener("mouseout", cancel); el.addEventListener("touchend", cancel); el.addEventListener("touchcancel", cancel); } })
Now to reference in our Vue component:
<template> <div> <button v-longpress="incrementPlusTen" @click="incrementPlusOne">{{value}}</button> </div> </template> <script> export default { data() { return { value: 10 } }, methods: { // Increment value plus one incrementPlusOne() { this.value++ }, // increment value plus 10 incrementPlusTen() { this.value += 10 } } } </script>
If you would love to know more about custom directives, the hook functions that are available, the arguments you can pass into this hook functions, and function shorthands, the great guys @vuejs have done a great job of explaining it here.
Cheers !!!
Debugging Vue.js applications can be difficult, especially when there are dozens, if not hundreds of mutations during a user session. If you’re interested in monitoring and tracking Vue mutations for all of your users in production, try LogRocket.
LogRocket is like a DVR for web and mobile apps, recording literally everything that happens in your Vue apps, including network requests, JavaScript errors, performance problems, and much more. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred.
The LogRocket Vuex plugin logs Vuex mutations to the LogRocket console, giving you context around what led to an error and what state the application was in when an issue occurred.
Modernize how you debug your Vue apps — start monitoring for free.
Hey there, want to help make our blog better?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowLearn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.
Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.
Handle frontend data discrepancies with eventual consistency using WebSockets, Docker Compose, and practical code examples.
Efficient initializing is crucial to smooth-running websites. One way to optimize that process is through lazy initialization in Rust 1.80.
5 Replies to "Building a long-press directive in Vue"
Really cool. Thanks
But click event will be triggered alongside longpress. Is there any approach how to prevent it?
Thanks a lot
Thanks for this well written post. I have a long-press button (a push-to-talk application that keeps the mic on only while the button is being held down) and it’s working fine, but one of the beta testers kept moving the mouse while holding it down so the mouse-up event often didn’t fire on the button itself but on a different component. Is that common and is there a workaround that could call a method on the longpress component even if the mouse-up occurs elsewhere on the page? (The component maintains its state so if it’s not in the “recording” state, that would be a no-op.)
Thank you for the post. When I handle the longpress event, it is getting triggered continuously as soon as I launch the app. Any idea what I am missing?