Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically trigger ripple effect on a Vuetify.js component?

I have a v-btn with ripple enabled. Is there a way to programmatically trigger the ripple effect? Is this possible if I use v-ripple directive on another component of my own that wraps the button?

I need to draw the users attention to the button in an unobtrusive way.

like image 844
David Tinker Avatar asked Oct 15 '22 13:10

David Tinker


1 Answers

I figured it out. You have to dispatch a "mousedown" event with clientX and clientY filled in to trigger the ripple and then a "mouseup" to get rid of it.

I have a utility method in CompUtil.js:

export default {

  ripple: function($el) {
    let ev = new Event("mousedown")
    let offset = $el.getBoundingClientRect()
    ev.clientX = offset.left + offset.width/2
    ev.clientY = offset.top + offset.height/2
    $el.dispatchEvent(ev)

    setTimeout(function() {
      $el.dispatchEvent(new Event("mouseup"))
    }, 300)
  }
}

Then I can do:

<v-btn ref="help" ...>

CompUtil.ripple(this.$refs.help.$el)
like image 117
David Tinker Avatar answered Oct 20 '22 05:10

David Tinker