Last active
March 4, 2023 15:23
-
-
Save enten/1811369ebd965fa439f12f6c000b8c48 to your computer and use it in GitHub Desktop.
AriaLabelReflectsContentDirective
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, ChangeDetectorRef, Directive, ElementRef, HostBinding, OnDestroy } from '@angular/core'; | |
import { NEVER, Observable, Subject } from 'rxjs'; | |
import { takeUntil } from 'rxjs/operators'; | |
@Directive({ | |
selector: '[ariaLabelReflectsContent]', | |
exportAs: 'ariaLabelReflectsContent', | |
standalone: true, | |
}) | |
export class AriaLabelReflectsContentDirective implements AfterViewInit, OnDestroy { | |
readonly htmlTagsRegexp: RegExp = /<\/?[^>]+(>|$)/g; | |
@HostBinding('attr.aria-label') | |
get attrAriaLabel(): string | null { | |
return this.elementRef?.nativeElement.innerHTML.replace(this.htmlTagsRegexp, '').trim() || null; | |
} | |
private readonly destroyed$ = new Subject<void>(); | |
constructor( | |
private readonly elementRef: ElementRef<HTMLElement>, | |
private readonly changeDetectorRef: ChangeDetectorRef, | |
) {} | |
ngAfterViewInit(): void { | |
createMutationObserver(this.elementRef.nativeElement, { | |
characterData: true, | |
childList: true, | |
subtree: true, | |
}).pipe( | |
takeUntil(this.destroyed$), | |
).subscribe(() => { | |
this.changeDetectorRef.markForCheck(); | |
}); | |
} | |
ngOnDestroy(): void { | |
this.destroyed$.next(); | |
this.destroyed$.complete(); | |
} | |
} | |
export function createMutationObserver( | |
element: HTMLElement, | |
options?: MutationObserverInit, | |
): Observable<MutationRecord[]> { | |
if (typeof MutationObserver === 'undefined') { | |
return NEVER; | |
} | |
return new Observable(subscriber => { | |
const observer = new MutationObserver(mutations => subscriber.next(mutations)); | |
observer.observe(element, options); | |
return () => observer.disconnect(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment