Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
Custom directive to replace @mouseenter + @mouseleave event?

I'm having lots of elements on which @mouseenter set a value to true and @mouseleave sets it to false. Basically what I need is a way to set a reactive variable to true if the mouse hovers the element.

I've been trying to figure out how to write such custom directive from the docs but it only mentions how to use .focus() js function on an element. Which js functions would be used for said directive?

Something like:

const vHover = {
    mounted: (el) => {
        el.addEventListener('mouseenter', state.hover=true)
        el.addEventListener('mouseleave', state.hover=false)
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think you could do something like:

app.directive('hover', {
    created(el, binding) {
        const callback = binding.value
        el.onmouseenter = () => callback(true)
        el.onmouseleave = () => callback(false)
    },
    unmounted(el) {
        el.onmouseenter = null
        el.onmouseleave = null
    }
})

Template:

<button v-hover="onHoverChange">Example</button>

Methods:

onHoverChange(isHovered) {
    console.log(isHovered)
}
about 4 years ago · Juan Pablo Isaza Report

0

I believe this is not the intended use of directives. The value of the state cannot be mutated within the directive. You can pass the variable through the binding, but you cannot update it.

binding: an object containing the following properties.

  • value: The value passed to the directive. For example in v-my-directive="1 + 1", the value would be 2.
  • oldValue: The previous value, only available in beforeUpdate and updated. It is available whether or not the value has changed.

so if you do el.addEventListener('mouseenter', binding.hover=true), as you may have noticed, it will not update the state.

However, if we use the internals (PSA: though not recommended since they could potentially change at any time), you could get instance using the vnode, and use the binding.arg to denote which Proxy (state)

so you could get the reactive variable with vnode.el.__vueParentComponent.data[binding.arg]

<script>
export default {
  data(){
    return {
      state: { hover:false }
    }
  },
  directives: {
    hover: {
      mounted(el, binding, vnode) {
        el.addEventListener('mouseenter', () => {
          vnode.el.__vueParentComponent.data[binding.arg].hover = true
        })
        el.addEventListener('mouseleave', () => {
          vnode.el.__vueParentComponent.data[binding.arg].hover = false
        })
      },
    }
  }
}
</script>

<template>
  <h1 v-hover:state="state">HOVER {{ state }}</h1>
</template>

SFC playground link

of course you might want to add the unmounted and even consider adding mouseleave dynamically only when mouseenter fires

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!