-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(a11y): add basic focus-trap directive #1311
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {inject, ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {By} from '@angular/platform-browser'; | ||
import {Component} from '@angular/core'; | ||
import {FocusTrap} from './focus-trap'; | ||
import {InteractivityChecker} from './interactivity-checker'; | ||
|
||
|
||
describe('FocusTrap', () => { | ||
let checker: InteractivityChecker; | ||
let fixture: ComponentFixture<FocusTrapTestApp>; | ||
|
||
describe('with default element', () => { | ||
beforeEach(() => TestBed.configureTestingModule({ | ||
declarations: [FocusTrap, FocusTrapTestApp], | ||
providers: [InteractivityChecker] | ||
})); | ||
|
||
beforeEach(inject([InteractivityChecker], (c: InteractivityChecker) => { | ||
checker = c; | ||
fixture = TestBed.createComponent(FocusTrapTestApp); | ||
})); | ||
|
||
it('wrap focus from end to start', () => { | ||
let focusTrap = fixture.debugElement.query(By.directive(FocusTrap)); | ||
let focusTrapInstance = focusTrap.componentInstance as FocusTrap; | ||
|
||
// Because we can't mimic a real tab press focus change in a unit test, just call the | ||
// focus event handler directly. | ||
focusTrapInstance.wrapFocus(); | ||
|
||
expect(document.activeElement.nodeName.toLowerCase()) | ||
.toBe('input', 'Expected input element to be focused'); | ||
}); | ||
|
||
it('should wrap focus from start to end', () => { | ||
let focusTrap = fixture.debugElement.query(By.directive(FocusTrap)); | ||
let focusTrapInstance = focusTrap.componentInstance as FocusTrap; | ||
|
||
// Because we can't mimic a real tab press focus change in a unit test, just call the | ||
// focus event handler directly. | ||
focusTrapInstance.reverseWrapFocus(); | ||
|
||
expect(document.activeElement.nodeName.toLowerCase()) | ||
.toBe('button', 'Expected button element to be focused'); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
@Component({ | ||
template: ` | ||
<focus-trap> | ||
<input> | ||
<button>SAVE</button> | ||
</focus-trap> | ||
` | ||
}) | ||
class FocusTrapTestApp { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import {Component, ViewEncapsulation, ViewChild, ElementRef} from '@angular/core'; | ||
import {InteractivityChecker} from './interactivity-checker'; | ||
|
||
|
||
/** | ||
* Directive for trapping focus within a region. | ||
* | ||
* NOTE: This directive currently uses a very simple (naive) approach to focus trapping. | ||
* It assumes that the tab order is the same as DOM order, which is not necessarily true. | ||
* Things like tabIndex > 0, flex `order`, and shadow roots can cause to two to misalign. | ||
* This will be replaced with a more intelligent solution before the library is considered stable. | ||
*/ | ||
@Component({ | ||
moduleId: module.id, | ||
selector: 'focus-trap', | ||
// TODO(jelbourn): move this to a separate file. | ||
template: ` | ||
<div tabindex="0" (focus)="reverseWrapFocus()"></div> | ||
<div #trappedContent><ng-content></ng-content></div> | ||
<div tabindex="0" (focus)="wrapFocus()"></div>`, | ||
encapsulation: ViewEncapsulation.None, | ||
}) | ||
export class FocusTrap { | ||
@ViewChild('trappedContent') trappedContent: ElementRef; | ||
|
||
constructor(private _checker: InteractivityChecker) { } | ||
|
||
/** Wrap focus from the end of the trapped region to the beginning. */ | ||
wrapFocus() { | ||
let redirectToElement = this._getFirstTabbableElement(this.trappedContent.nativeElement); | ||
if (redirectToElement) { | ||
redirectToElement.focus(); | ||
} | ||
} | ||
|
||
/** Wrap focus from the beginning of the trapped region to the end. */ | ||
reverseWrapFocus() { | ||
let redirectToElement = this._getLastTabbableElement(this.trappedContent.nativeElement); | ||
if (redirectToElement) { | ||
redirectToElement.focus(); | ||
} | ||
} | ||
|
||
/** Get the first tabbable element from a DOM subtree (inclusive). */ | ||
private _getFirstTabbableElement(root: HTMLElement): HTMLElement { | ||
if (this._checker.isFocusable(root) && this._checker.isTabbable(root)) { | ||
return root; | ||
} | ||
|
||
// Iterate in DOM order. | ||
let childCount = root.children.length; | ||
for (let i = 0; i < childCount; i++) { | ||
let tabbableChild = this._getFirstTabbableElement(root.children[i] as HTMLElement); | ||
if (tabbableChild) { | ||
return tabbableChild; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** Get the last tabbable element from a DOM subtree (inclusive). */ | ||
private _getLastTabbableElement(root: HTMLElement): HTMLElement { | ||
if (this._checker.isFocusable(root) && this._checker.isTabbable(root)) { | ||
return root; | ||
} | ||
|
||
// Iterate in reverse DOM order. | ||
for (let i = root.children.length - 1; i >= 0; i--) { | ||
let tabbableChild = this._getLastTabbableElement(root.children[i] as HTMLElement); | ||
if (tabbableChild) { | ||
return tabbableChild; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to two
->the two