Skip to content

Instantly share code, notes, and snippets.

@enten
Last active March 4, 2023 15:23
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 enten/1811369ebd965fa439f12f6c000b8c48 to your computer and use it in GitHub Desktop.
Save enten/1811369ebd965fa439f12f6c000b8c48 to your computer and use it in GitHub Desktop.
AriaLabelReflectsContentDirective
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