Skip to content

refactor(focus-monitor): accept ElementRef in focusVia #12978

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

Merged
merged 1 commit into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/cdk/a11y/focus-monitor/focus-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,28 @@ export class FocusMonitor implements OnDestroy {
* @param origin Focus origin.
* @param options Options that can be used to configure the focus behavior.
*/
focusVia(element: HTMLElement, origin: FocusOrigin, options?: FocusOptions): void {
focusVia(element: HTMLElement, origin: FocusOrigin, options?: FocusOptions): void;

/**
* Focuses the element via the specified focus origin.
* @param element Element to focus.
* @param origin Focus origin.
* @param options Options that can be used to configure the focus behavior.
*/
focusVia(element: ElementRef<HTMLElement>, origin: FocusOrigin, options?: FocusOptions): void;

focusVia(element: HTMLElement | ElementRef<HTMLElement>,
origin: FocusOrigin,
options?: FocusOptions): void {

const nativeElement = this._getNativeElement(element);

this._setOriginForCurrentEventQueue(origin);

// `focus` isn't available on the server
if (typeof element.focus === 'function') {
if (typeof nativeElement.focus === 'function') {
// Cast the element to `any`, because the TS typings don't have the `options` parameter yet.
(element as any).focus(options);
(nativeElement as any).focus(options);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc

/** Focuses the checkbox. */
focus(): void {
this._focusMonitor.focusVia(this._inputElement.nativeElement, 'keyboard');
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
}

_onInteractionEvent(event: Event) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/expansion/expansion-panel-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
// Avoids focus being lost if the panel contained the focused element and was closed.
panel.closed
.pipe(filter(() => panel._containsFocus()))
.subscribe(() => _focusMonitor.focusVia(_element.nativeElement, 'program'));
.subscribe(() => _focusMonitor.focusVia(_element, 'program'));

_focusMonitor.monitor(_element).subscribe(origin => {
if (origin && panel.accordion) {
Expand Down Expand Up @@ -158,7 +158,7 @@ export class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
* @docs-private
*/
focus(origin: FocusOrigin = 'program') {
this._focusMonitor.focusVia(this._element.nativeElement, origin);
this._focusMonitor.focusVia(this._element, origin);
}

ngOnDestroy() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
*/
focus(origin: FocusOrigin = 'program') {
if (this._focusMonitor) {
this._focusMonitor.focusVia(this._element.nativeElement, origin);
this._focusMonitor.focusVia(this._element, origin);
} else {
this._element.nativeElement.focus();
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/radio/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ export class MatRadioButton extends _MatRadioButtonMixinBase

/** Focuses the radio button. */
focus(): void {
this._focusMonitor.focusVia(this._inputElement.nativeElement, 'keyboard');
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/slide-toggle/slide-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro

/** Focuses the slide-toggle. */
focus(): void {
this._focusMonitor.focusVia(this._inputElement.nativeElement, 'keyboard');
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
}

/** Toggles the checked state of the slide-toggle. */
Expand Down
2 changes: 1 addition & 1 deletion src/universal-app/kitchen-sink/kitchen-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class KitchenSink {
focusMonitor: FocusMonitor,
elementRef: ElementRef<HTMLElement>,
bottomSheet: MatBottomSheet) {
focusMonitor.focusVia(elementRef.nativeElement, 'program');
focusMonitor.focusVia(elementRef, 'program');
snackBar.open('Hello there');
dialog.open(TestEntryComponent);
bottomSheet.open(TestEntryComponent);
Expand Down