60 lines
6.4 KiB
JavaScript
60 lines
6.4 KiB
JavaScript
import { DomHandler } from 'primeng/dom';
|
|
import { CommonModule } from '@angular/common';
|
|
import { Directive, ElementRef, HostListener, Input, NgModule, inject, booleanAttribute } from '@angular/core';
|
|
import * as i0 from "@angular/core";
|
|
/**
|
|
* Focus Trap keeps focus within a certain DOM element while tabbing.
|
|
* @group Components
|
|
*/
|
|
export class FocusTrap {
|
|
/**
|
|
* When set as true, focus wouldn't be managed.
|
|
* @group Props
|
|
*/
|
|
pFocusTrapDisabled = false;
|
|
host = inject(ElementRef);
|
|
onkeydown(e) {
|
|
if (this.pFocusTrapDisabled !== true) {
|
|
e.preventDefault();
|
|
const focusableElement = DomHandler.getNextFocusableElement(this.host.nativeElement, e.shiftKey);
|
|
if (focusableElement) {
|
|
focusableElement.focus();
|
|
focusableElement.select?.();
|
|
}
|
|
}
|
|
}
|
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: FocusTrap, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "16.1.0", version: "17.3.7", type: FocusTrap, selector: "[pFocusTrap]", inputs: { pFocusTrapDisabled: ["pFocusTrapDisabled", "pFocusTrapDisabled", booleanAttribute] }, host: { listeners: { "keydown.tab": "onkeydown($event)", "keydown.shift.tab": "onkeydown($event)" }, classAttribute: "p-element" }, ngImport: i0 });
|
|
}
|
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: FocusTrap, decorators: [{
|
|
type: Directive,
|
|
args: [{
|
|
selector: '[pFocusTrap]',
|
|
host: {
|
|
class: 'p-element'
|
|
}
|
|
}]
|
|
}], propDecorators: { pFocusTrapDisabled: [{
|
|
type: Input,
|
|
args: [{ transform: booleanAttribute }]
|
|
}], onkeydown: [{
|
|
type: HostListener,
|
|
args: ['keydown.tab', ['$event']]
|
|
}, {
|
|
type: HostListener,
|
|
args: ['keydown.shift.tab', ['$event']]
|
|
}] } });
|
|
export class FocusTrapModule {
|
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: FocusTrapModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.7", ngImport: i0, type: FocusTrapModule, declarations: [FocusTrap], imports: [CommonModule], exports: [FocusTrap] });
|
|
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: FocusTrapModule, imports: [CommonModule] });
|
|
}
|
|
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.7", ngImport: i0, type: FocusTrapModule, decorators: [{
|
|
type: NgModule,
|
|
args: [{
|
|
imports: [CommonModule],
|
|
exports: [FocusTrap],
|
|
declarations: [FocusTrap]
|
|
}]
|
|
}] });
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZm9jdXN0cmFwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2FwcC9jb21wb25lbnRzL2ZvY3VzdHJhcC9mb2N1c3RyYXAudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUV6QyxPQUFPLEVBQUUsWUFBWSxFQUFFLE1BQU0saUJBQWlCLENBQUM7QUFDL0MsT0FBTyxFQUFFLFNBQVMsRUFBRSxVQUFVLEVBQUUsWUFBWSxFQUFFLEtBQUssRUFBRSxRQUFRLEVBQUUsTUFBTSxFQUFFLGdCQUFnQixFQUFFLE1BQU0sZUFBZSxDQUFDOztBQUMvRzs7O0dBR0c7QUFPSCxNQUFNLE9BQU8sU0FBUztJQUNsQjs7O09BR0c7SUFDcUMsa0JBQWtCLEd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