Created
September 7, 2021 08:03
-
-
Save eneajaho/847e9551e1927aa6658edb7f45a9a06a to your computer and use it in GitHub Desktop.
Translucent directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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