From 6ac679355ddfe045e2aca9bb469d5b7e82811371 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 1 Apr 2019 22:36:07 -0400 Subject: [PATCH] a11y title --- .../directives/a11y-title.directive.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/angular/directives/a11y-title.directive.ts diff --git a/src/angular/directives/a11y-title.directive.ts b/src/angular/directives/a11y-title.directive.ts new file mode 100644 index 0000000000..e8d5766935 --- /dev/null +++ b/src/angular/directives/a11y-title.directive.ts @@ -0,0 +1,28 @@ +import { + Directive, + ElementRef, + Input, + Renderer2, +} from '@angular/core'; + +@Directive({ + selector: '[appA11yTitle]', +}) +export class A11yTitleDirective { + @Input() set appA11yTitle(title: string) { + this.title = title; + } + + private title: string; + + constructor(private el: ElementRef, private renderer: Renderer2) { } + + ngOnInit() { + if (!this.el.nativeElement.hasAttribute('title')) { + this.renderer.setAttribute(this.el.nativeElement, 'title', this.title); + } + if (!this.el.nativeElement.hasAttribute('aria-label')) { + this.renderer.setAttribute(this.el.nativeElement, 'aria-label', this.title); + } + } +}