-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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(sidenav): close via escape key and restore focus to trigger element #1990
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e455bdd
feat(sidenav): close via escape key and restore focus to trigger element
crisbeto 404721d
fix: test failures in IE and blur element if there's no focusable tri…
crisbeto f5baaa2
fix: use the keycode instead of (keydown.escape)
crisbeto 8603889
fix: use the renderer for focusing and blurring and fix a typo
crisbeto 77520d8
fix a faulty merge
crisbeto 3b44ae1
Fix a linter warning.
crisbeto 6765217
Stop the propagation of the keydown event.
crisbeto ca43aee
Pointless commit to resolve git issue.
crisbeto 139801e
Revert pointless commit.
crisbeto 9e53fca
Fix conflict between the new functionality and the focus trapping.
crisbeto f498f42
Move the focus trapping behavior to the onOpen listener for improved …
crisbeto 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,18 @@ import { | |
ViewChild | ||
} from '@angular/core'; | ||
import {CommonModule} from '@angular/common'; | ||
import {Dir, coerceBooleanProperty, DefaultStyleCompatibilityModeModule} from '../core'; | ||
import {Dir, MdError, coerceBooleanProperty, DefaultStyleCompatibilityModeModule} from '../core'; | ||
import {A11yModule, A11Y_PROVIDERS} from '../core/a11y/index'; | ||
import {FocusTrap} from '../core/a11y/focus-trap'; | ||
import {ESCAPE} from '../core/keyboard/keycodes'; | ||
|
||
|
||
/** Exception thrown when two MdSidenav are matching the same side. */ | ||
export class MdDuplicatedSidenavError extends MdError { | ||
constructor(align: string) { | ||
super(`A sidenav was already declared for 'align="${align}"'`); | ||
} | ||
} | ||
|
||
|
||
/** Sidenav toggle promise result. */ | ||
|
@@ -40,6 +49,7 @@ export class MdSidenavToggleResult { | |
template: '<focus-trap [disabled]="isFocusTrapDisabled"><ng-content></ng-content></focus-trap>', | ||
host: { | ||
'(transitionend)': '_onTransitionEnd($event)', | ||
'(keydown)': 'handleKeydown($event)', | ||
// must prevent the browser from aligning text based on value | ||
'[attr.align]': 'null', | ||
'[class.md-sidenav-closed]': '_isClosed', | ||
|
@@ -51,6 +61,7 @@ export class MdSidenavToggleResult { | |
'[class.md-sidenav-push]': '_modePush', | ||
'[class.md-sidenav-side]': '_modeSide', | ||
'[class.md-sidenav-invalid]': '!valid', | ||
'tabIndex': '-1' | ||
}, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
encapsulation: ViewEncapsulation.None, | ||
|
@@ -128,7 +139,25 @@ export class MdSidenav implements AfterContentInit { | |
* @param _elementRef The DOM element reference. Used for transition and width calculation. | ||
* If not available we do not hook on transitions. | ||
*/ | ||
constructor(private _elementRef: ElementRef) {} | ||
constructor(private _elementRef: ElementRef, private _renderer: Renderer) { | ||
this.onOpen.subscribe(() => { | ||
this._elementFocusedBeforeSidenavWasOpened = document.activeElement as HTMLElement; | ||
|
||
if (!this.isFocusTrapDisabled) { | ||
this._focusTrap.focusFirstTabbableElementWhenReady(); | ||
} | ||
}); | ||
|
||
this.onClose.subscribe(() => { | ||
if (this._elementFocusedBeforeSidenavWasOpened instanceof HTMLElement) { | ||
this._renderer.invokeElementMethod(this._elementFocusedBeforeSidenavWasOpened, 'focus'); | ||
} else { | ||
this._renderer.invokeElementMethod(this._elementRef.nativeElement, 'blur'); | ||
} | ||
|
||
this._elementFocusedBeforeSidenavWasOpened = null; | ||
}); | ||
} | ||
|
||
ngAfterContentInit() { | ||
// This can happen when the sidenav is set to opened in the template and the transition | ||
|
@@ -188,10 +217,6 @@ export class MdSidenav implements AfterContentInit { | |
this.onCloseStart.emit(); | ||
} | ||
|
||
if (!this.isFocusTrapDisabled) { | ||
this._focusTrap.focusFirstTabbableElementWhenReady(); | ||
} | ||
|
||
if (this._toggleAnimationPromise) { | ||
this._resolveToggleAnimationPromise(false); | ||
} | ||
|
@@ -202,6 +227,16 @@ export class MdSidenav implements AfterContentInit { | |
return this._toggleAnimationPromise; | ||
} | ||
|
||
/** | ||
* Handles the keyboard events. | ||
*/ | ||
handleKeydown(event: KeyboardEvent) { | ||
if (event.keyCode === ESCAPE) { | ||
this.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably want to stop propagation (if we have a dialog with a sidenav, esc should close the sidenav but not the dialog) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
event.stopPropagation(); | ||
} | ||
} | ||
|
||
/** | ||
* When transition has finished, set the internal state for classes and emit the proper event. | ||
* The event passed is actually of type TransitionEvent, but that type is not available in | ||
|
@@ -255,6 +290,8 @@ export class MdSidenav implements AfterContentInit { | |
} | ||
return 0; | ||
} | ||
|
||
private _elementFocusedBeforeSidenavWasOpened: HTMLElement = null; | ||
} | ||
|
||
/** | ||
|
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.
what about universal?
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.
I don't think Angular has a way of getting this, at the moment. There are other components that use
document.activeElement
as well.AFAIK when we start supporting Universal, we can check if the renderer is a browser with this and take the appropriate action:
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.
for things such as
you can use renderer
Core already have different renderer implementations for browser and server envs.
But
Renderer
doesn't have methods to retrieve dom elements and afaik the recommended approach is to use DomAdapter.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.
Hmm good point about the renderer methods, I was under the impression that I used it already, but I probably forgot to switch it over after I was done testing. I'll move those. I'll have to look into using the adapter for querying.