-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(a11y): manager for list keyboard events
- Loading branch information
Showing
4 changed files
with
75 additions
and
50 deletions.
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,64 @@ | ||
import {EventEmitter, Output, QueryList} from '@angular/core'; | ||
import {UP_ARROW, DOWN_ARROW, TAB} from '../core'; | ||
|
||
export interface MdFocusable { | ||
focus(): void; | ||
disabled: boolean; | ||
} | ||
|
||
export class ListKeyManager { | ||
private _focusedItemIndex: number; | ||
|
||
@Output() tabOut: EventEmitter<null> = new EventEmitter(); | ||
|
||
constructor(private _items: QueryList<MdFocusable>) {} | ||
|
||
set focusedItemIndex(value: number) { | ||
this._focusedItemIndex = value; | ||
} | ||
|
||
// TODO(kara): update this when (keydown.downArrow) testability is fixed | ||
onKeydown(event: KeyboardEvent): void { | ||
if (event.keyCode === DOWN_ARROW) { | ||
this._focusNextItem(); | ||
} else if (event.keyCode === UP_ARROW) { | ||
this._focusPreviousItem(); | ||
} else if (event.keyCode === TAB) { | ||
this.tabOut.emit(null); | ||
this._focusedItemIndex = null; | ||
} | ||
} | ||
|
||
private _focusNextItem(): void { | ||
const items = this._items.toArray(); | ||
this._updateFocusedItemIndex(1, items); | ||
items[this._focusedItemIndex].focus(); | ||
} | ||
|
||
private _focusPreviousItem(): void { | ||
const items = this._items.toArray(); | ||
this._updateFocusedItemIndex(-1, items); | ||
items[this._focusedItemIndex].focus(); | ||
} | ||
|
||
/** | ||
* This method sets focus to the correct menu item, given a list of menu items and the delta | ||
* between the currently focused menu item and the new menu item to be focused. It will | ||
* continue to move down the list until it finds an item that is not disabled, and it will wrap | ||
* if it encounters either end of the menu. | ||
* | ||
* @param delta the desired change in focus index | ||
*/ | ||
private _updateFocusedItemIndex(delta: number, items: MdFocusable[]) { | ||
// when focus would leave menu, wrap to beginning or end | ||
this._focusedItemIndex = (this._focusedItemIndex + delta + items.length) | ||
% items.length; | ||
|
||
// skip all disabled menu items recursively until an active one | ||
// is reached or the menu closes for overreaching bounds | ||
while (items[this._focusedItemIndex].disabled) { | ||
this._updateFocusedItemIndex(delta, items); | ||
} | ||
} | ||
|
||
} |
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
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
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