Skip to content

Instantly share code, notes, and snippets.

@eneajaho
Created September 7, 2021 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eneajaho/847e9551e1927aa6658edb7f45a9a06a to your computer and use it in GitHub Desktop.
Save eneajaho/847e9551e1927aa6658edb7f45a9a06a to your computer and use it in GitHub Desktop.
Translucent directive
import { AfterViewInit, Directive, ElementRef, Input, NgModule, Renderer2 } from '@angular/core';
import { transparentize } from 'color2k';
@Directive({ selector: '[translucent]' })
export class TranslucentDirective implements AfterViewInit {
/* nice colors
* red - #f52525
* green - #39BF4D
* blue - #2752FF */
@Input() color?: string;
constructor(private elementRef: ElementRef, private renderer: Renderer2) {}
ngAfterViewInit(): void {
if (this.color) {
this.renderer.setStyle(
this.elementRef.nativeElement, 'background-color', transparentize(this.color, 0.95)
);
this.renderer.setStyle(
this.elementRef.nativeElement, 'color', this.color);
this.renderer.setStyle(
this.elementRef.nativeElement, 'border', `1px solid ${ transparentize(this.color, 0.8) }`
);
}
}
}
@NgModule({
declarations: [ TranslucentDirective ],
exports: [ TranslucentDirective ]
})
export class TranslucentModule { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment