|
| 1 | +import {Component, ViewEncapsulation, ViewChild, ElementRef} from '@angular/core'; |
| 2 | +import {InteractivityChecker} from './interactivity-checker'; |
| 3 | + |
| 4 | + |
| 5 | +/** |
| 6 | + * Directive for trapping focus within a region. |
| 7 | + * |
| 8 | + * NOTE: This directive currently uses a very simple (naive) approach to focus trapping. |
| 9 | + * It assumes that the tab order is the same as DOM order, which is not necessarily true. |
| 10 | + * Things like tabIndex > 0, flex `order`, and shadow roots can cause to two to misalign. |
| 11 | + * This will be replaced with a more intelligent solution before the library is considered stable. |
| 12 | + */ |
| 13 | +@Component({ |
| 14 | + moduleId: module.id, |
| 15 | + selector: 'focus-trap', |
| 16 | + // TODO(jelbourn): move this to a separate file. |
| 17 | + template: ` |
| 18 | + <div tabindex="0" (focus)="reverseWrapFocus()"></div> |
| 19 | + <div #trappedContent><ng-content></ng-content></div> |
| 20 | + <div tabindex="0" (focus)="wrapFocus()"></div>`, |
| 21 | + encapsulation: ViewEncapsulation.None, |
| 22 | +}) |
| 23 | +export class FocusTrap { |
| 24 | + @ViewChild('trappedContent') trappedContent: ElementRef; |
| 25 | + |
| 26 | + constructor(private _checker: InteractivityChecker) { } |
| 27 | + |
| 28 | + /** Wrap focus from the end of the trapped region to the beginning. */ |
| 29 | + wrapFocus() { |
| 30 | + let redirectToElement = this._getFirstTabbableElement(this.trappedContent.nativeElement); |
| 31 | + if (redirectToElement) { |
| 32 | + redirectToElement.focus(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** Wrap focus from the beginning of the trapped region to the end. */ |
| 37 | + reverseWrapFocus() { |
| 38 | + let redirectToElement = this._getLastTabbableElement(this.trappedContent.nativeElement); |
| 39 | + if (redirectToElement) { |
| 40 | + redirectToElement.focus(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** Get the first tabbable element from a DOM subtree (inclusive). */ |
| 45 | + private _getFirstTabbableElement(root: HTMLElement): HTMLElement { |
| 46 | + if (this._checker.isFocusable(root) && this._checker.isTabbable(root)) { |
| 47 | + return root; |
| 48 | + } |
| 49 | + |
| 50 | + // Iterate in DOM order. |
| 51 | + let childCount = root.children.length; |
| 52 | + for (let i = 0; i < childCount; i++) { |
| 53 | + let tabbableChild = this._getFirstTabbableElement(root.children[i] as HTMLElement); |
| 54 | + if (tabbableChild) { |
| 55 | + return tabbableChild; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + /** Get the last tabbable element from a DOM subtree (inclusive). */ |
| 63 | + private _getLastTabbableElement(root: HTMLElement): HTMLElement { |
| 64 | + if (this._checker.isFocusable(root) && this._checker.isTabbable(root)) { |
| 65 | + return root; |
| 66 | + } |
| 67 | + |
| 68 | + // Iterate in reverse DOM order. |
| 69 | + for (let i = root.children.length - 1; i >= 0; i--) { |
| 70 | + let tabbableChild = this._getLastTabbableElement(root.children[i] as HTMLElement); |
| 71 | + if (tabbableChild) { |
| 72 | + return tabbableChild; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return null; |
| 77 | + } |
| 78 | +} |
0 commit comments