UNPKG

@ng-doc/ui-kit

Version:

<!-- PROJECT LOGO --> <br /> <div align="center"> <a href="https://github.com/ng-doc/ng-doc"> <img src="https://ng-doc.com/assets/images/ng-doc.svg?raw=true" alt="Logo" height="150px"> </a>

54 lines (50 loc) 2.8 kB
import * as i0 from '@angular/core'; import { inject, ElementRef, NgZone, ChangeDetectorRef, EventEmitter, HostBinding, Output, Directive } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { FOCUS_EVENT, BLUR_EVENT } from '@ng-doc/ui-kit/constants'; import { toElement } from '@ng-doc/ui-kit/helpers'; import { ngDocZoneOptimize } from '@ng-doc/ui-kit/observables'; import { merge, fromEvent } from 'rxjs'; import { debounceTime, distinctUntilChanged } from 'rxjs/operators'; class NgDocFocusCatcherDirective { constructor() { this.elementRef = inject(ElementRef); this.ngZone = inject(NgZone); this.changeDetectorRef = inject(ChangeDetectorRef); this.focusEvent = new EventEmitter(); this.blurEvent = new EventEmitter(); this.focused = false; NgDocFocusCatcherDirective.observeFocus(toElement(this.elementRef)) .pipe(ngDocZoneOptimize(this.ngZone), takeUntilDestroyed()) .subscribe((event) => { this.focused = event.type === FOCUS_EVENT; this.focused ? this.focusEvent.emit(event) : this.blurEvent.emit(event); this.changeDetectorRef.markForCheck(); }); } static observeFocus(element) { return merge(fromEvent(element, FOCUS_EVENT), fromEvent(element, BLUR_EVENT)).pipe(debounceTime(0), distinctUntilChanged((a, b) => a.type === b.type)); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.3", ngImport: i0, type: NgDocFocusCatcherDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.0.3", type: NgDocFocusCatcherDirective, isStandalone: true, selector: "[ngDocFocusCatcher]", outputs: { focusEvent: "focusEvent", blurEvent: "blurEvent" }, host: { properties: { "attr.data-ng-doc-focused": "this.focused" } }, exportAs: ["ngDocFocusCatcher"], ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.3", ngImport: i0, type: NgDocFocusCatcherDirective, decorators: [{ type: Directive, args: [{ selector: '[ngDocFocusCatcher]', exportAs: 'ngDocFocusCatcher', standalone: true, }] }], ctorParameters: () => [], propDecorators: { focusEvent: [{ type: Output }], blurEvent: [{ type: Output }], focused: [{ type: HostBinding, args: ['attr.data-ng-doc-focused'] }] } }); /** * Generated bundle index. Do not edit. */ export { NgDocFocusCatcherDirective }; //# sourceMappingURL=ng-doc-ui-kit-directives-focus-catcher.mjs.map