diff --git a/src/material/button/button.spec.ts b/src/material/button/button.spec.ts index 7584922bf133..328f1cfca0da 100644 --- a/src/material/button/button.spec.ts +++ b/src/material/button/button.spec.ts @@ -70,6 +70,19 @@ describe('MatButton', () => { expect(buttonDebugElement.nativeElement.classList.contains('custom-class')).toBe(true); }); + it('should be able to focus button with a specific focus origin', () => { + const fixture = TestBed.createComponent(TestApp); + const buttonDebugEl = fixture.debugElement.query(By.css('button')); + const buttonInstance = buttonDebugEl.componentInstance as MatButton; + + expect(buttonDebugEl.nativeElement.classList).not.toContain('cdk-focused'); + + buttonInstance.focus('touch'); + + expect(buttonDebugEl.nativeElement.classList).toContain('cdk-focused'); + expect(buttonDebugEl.nativeElement.classList).toContain('cdk-touch-focused'); + }); + describe('button[mat-fab]', () => { it('should have accent palette by default', () => { const fixture = TestBed.createComponent(TestApp); diff --git a/src/material/button/button.ts b/src/material/button/button.ts index 9dbed00542fb..4f3860c3aa06 100644 --- a/src/material/button/button.ts +++ b/src/material/button/button.ts @@ -119,10 +119,8 @@ export class MatButton extends _MatButtonMixinBase } /** Focuses the button. */ - focus(_origin?: FocusOrigin, options?: FocusOptions): void { - // Note that we aren't using `_origin`, but we need to keep it because some internal consumers - // use `MatButton` in a `FocusKeyManager` and we need it to match `FocusableOption`. - this._getHostElement().focus(options); + focus(origin: FocusOrigin = 'program', options?: FocusOptions): void { + this._focusMonitor.focusVia(this._getHostElement(), origin, options); } _getHostElement() { diff --git a/tools/public_api_guard/material/button.d.ts b/tools/public_api_guard/material/button.d.ts index 196f25850a66..81267ff70d5d 100644 --- a/tools/public_api_guard/material/button.d.ts +++ b/tools/public_api_guard/material/button.d.ts @@ -13,7 +13,7 @@ export declare class MatButton extends _MatButtonMixinBase implements OnDestroy, _getHostElement(): any; _hasHostAttributes(...attributes: string[]): boolean; _isRippleDisabled(): boolean; - focus(_origin?: FocusOrigin, options?: FocusOptions): void; + focus(origin?: FocusOrigin, options?: FocusOptions): void; ngOnDestroy(): void; }