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 fewseconds.
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:
- Start a timer once the mousedown event occurs.
- Clear that timer and don’t execute the function once the mouseup event gets fired before the 2secs mark. i.e a full click event.
As long as the timer doesn’t get cleared before it gets to that time we’ve seti.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 3 things, namely:
- A variable to store the timer.
- A start function to start the timer.
- A cancel function to cancel the timer
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.
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.
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);
Putting it all together, we have this:
Wrapping it all in a Vue directive
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, that 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.
Next, we need to add a function that would run the method that will be passed to the longpress directive.
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:
Putting everything together:
Now to reference in our Vue component:
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, function shorthands. The great guys @vuejs has done a great job of explaining it here.
Plug: LogRocket, a DVR for web apps
LogRocket is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.