Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using CSS variable for a CSS hover property in Angular directive?

I have the following directive:

import { Directive, ElementRef, HostListener } from '@angular/core';

@Directive({
  selector: 'd-btn', 
  host: {}
})

export class ButtonDirective {
    constructor(private el: ElementRef){}
    @HostListener('mouseover')
    onMouseOver() {
        this.el.nativeElement.style.transition;
        this.el.nativeElement.style.backgroundColor = "var(theme-color-1)";
    }
    
  
    @HostListener('mouseout')
    onMouseLeave() {
        this.el.nativeElement.style.transition;
        this.el.nativeElement.style.backgroundColor = 'transparent';
    }
}

If instead of "var(theme-color-1)" I write a color it adds the given color when the user hovers over the element. But I would like to give it a variable color, because I am working on different color themes. Any ideas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The solution should be pretty easy and is not an Angular problem. You can solve this easily by CSS:

button[d-btn] {
  transition: all .5s;
  background: transparent; 
}
button[d-btn]:hover {
  transition: all .5s;
  background: var(--theme-color-1); 
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading