How can I programmatically trigger ripple effect on Angular MatListItem?

后端 未结 2 1459
囚心锁ツ
囚心锁ツ 2021-01-21 14:21

In Angular material you have the lovely ripple effect on a lot of the components. When you click on a mat-list-item the ripple effect happens.

Now with buttons and many

2条回答
  •  [愿得一人]
    2021-01-21 14:50

    As of recent versions of Material, there is now a Ripples module (docs here) that allow for the customization and programmatic triggering of ripples in a component, such as mat-list-item.

    Programmatic triggering involves the following: First, we make our list and attach the matRipple directive. In my example, I'm using a button click to trigger the ripple, so an event handler has been added as well:

    
      Item 1
      Item 2
      Item 3
    
    
    

    Within the component, we grab the matRipple using ViewChild, allowing us to call launch() on the ripple to trigger the actual ripple effect:

    @ViewChild(MatRipple) ripple: MatRipple;
    
    triggerRipple() {
      this.ripple.launch({centered: true});
    }
    

    Here's a stackblitz showing this sample in action; click the button to cause a ripple in the mat-list. Naturally, you can move the matRipple directive to any component if you'd like the ripple effect applied to something else. Take a look at the docs for more configuration options available.

提交回复
热议问题