Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
fix(top-bar-navigation): Highlight top bar actions with keyboard focus.
Browse files Browse the repository at this point in the history
Fixes #692
  • Loading branch information
rebu-dt authored and tomheller committed Mar 24, 2020
1 parent 65ea920 commit f02cff1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ $space: 12px;
);
@include dt-interactive-reset();
@include dt-text-ellipsis();
@include dt-cdkmonitor-focus-style();
box-sizing: border-box;
display: flex;
align-items: center;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ describe('DtTopBarNavigation', () => {

expect(nav.getAttribute('aria-label')).toBe('Main');
});

it('should be highlighted when focused', () => {
const fixture = TestBed.createComponent(TopBarWithAction);
fixture.detectChanges();

const button: HTMLElement = fixture.debugElement.query(By.css('button'))
.nativeElement;
button.focus();

expect(button.classList).toContain('cdk-program-focused');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
* limitations under the License.
*/

import { FocusMonitor } from '@angular/cdk/a11y';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import {
ChangeDetectionStrategy,
Component,
Directive,
Input,
ViewEncapsulation,
ElementRef,
OnDestroy,
} from '@angular/core';

/**
Expand Down Expand Up @@ -83,7 +86,7 @@ export class DtTopBarNavigationItem {
'[class.dt-top-bar-action-has-problem]': 'hasProblem',
},
})
export class DtTopBarAction {
export class DtTopBarAction implements OnDestroy {
/** Indicates if the item has a problem state */
@Input()
get hasProblem(): boolean {
Expand All @@ -94,4 +97,15 @@ export class DtTopBarAction {
}
/** The current state if an item has a problem */
private _hasProblem = false;

constructor(
private _elementRef: ElementRef,
private _focusMonitor: FocusMonitor,
) {
this._focusMonitor.monitor(this._elementRef.nativeElement, true);
}

ngOnDestroy(): void {
this._focusMonitor.stopMonitoring(this._elementRef.nativeElement);
}
}

0 comments on commit f02cff1

Please sign in to comment.