MCPcopy Create free account
hub / github.com/angular/components / FocusTrapDemo

Class FocusTrapDemo

src/dev-app/focus-trap/focus-trap-demo.ts:47–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45 imports: [A11yModule, MatButtonModule, MatCardModule, MatToolbarModule, FocusTrapShadowDomDemo],
46})
47export class FocusTrapDemo implements AfterViewInit {
48 dialog = inject(MatDialog);
49
50 @ViewChild('newElements')
51 private _newElements!: ElementRef<HTMLElement>;
52
53 @ViewChildren(CdkTrapFocus)
54 private _focusTraps!: QueryList<CdkTrapFocus>;
55
56 _supportsShadowDom = _supportsShadowDom();
57
58 readonly cdr = inject(ChangeDetectorRef);
59
60 ngAfterViewInit() {
61 // We want all the traps to be disabled by default, but doing so while using the value in
62 // the view will result in "changed after checked" errors so we defer it to the next tick.
63 setTimeout(() => {
64 this._focusTraps.forEach(trap => (trap.enabled = false));
65 this.cdr.markForCheck();
66 });
67 }
68
69 toggleFocus(instance: CdkTrapFocus) {
70 instance.enabled = !instance.enabled;
71 if (instance.enabled) {
72 instance.focusTrap.focusInitialElementWhenReady();
73 }
74 }
75
76 addNewElement() {
77 const textarea = document.createElement('textarea');
78 textarea.setAttribute('placeholder', 'I am a new element!');
79 this._newElements.nativeElement.appendChild(textarea);
80 }
81
82 openDialog() {
83 this.dialog.open(FocusTrapDialogDemo);
84 }
85}
86
87let dialogCount = 0;
88

Callers

nothing calls this directly

Calls 1

_supportsShadowDomFunction · 0.90

Tested by

no test coverage detected