Skip to content

fix(sort): no focus indication for active header #17735

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
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
1 change: 1 addition & 0 deletions src/material/sort/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ng_module(
assets = [":sort-header.css"] + glob(["**/*.html"]),
module_name = "@angular/material/sort",
deps = [
"//src/cdk/a11y",
"//src/cdk/coercion",
"//src/material/core",
"@npm//@angular/animations",
Expand Down
4 changes: 1 addition & 3 deletions src/material/sort/sort-header.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
[class.mat-sort-header-position-before]="arrowPosition == 'before'">
<button class="mat-sort-header-button" type="button"
[attr.disabled]="_isDisabled() || null"
[attr.aria-label]="_intl.sortButtonLabel(id)"
(focus)="_setIndicatorHintVisible(true)"
(blur)="_setIndicatorHintVisible(false)">
[attr.aria-label]="_intl.sortButtonLabel(id)">
<ng-content></ng-content>
</button>

Expand Down
7 changes: 7 additions & 0 deletions src/material/sort/sort-header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ $mat-sort-header-arrow-hint-opacity: 0.38;
font: inherit;
color: currentColor;

// Usually we could rely on the arrow showing up to be focus indication, but if a header is
// active, the arrow will always be shown so the user has no way of telling the difference.
[mat-sort-header].cdk-keyboard-focused &,
[mat-sort-header].cdk-program-focused & {
border-bottom: solid 1px currentColor;
}

// The `outline: 0` from above works on all browsers, however Firefox also
// adds a special `focus-inner` which we have to disable explicitly. See:
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Firefox
Expand Down
22 changes: 21 additions & 1 deletion src/material/sort/sort-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import {
Optional,
ViewEncapsulation,
Inject,
ElementRef,
} from '@angular/core';
import {CanDisable, CanDisableCtor, mixinDisabled} from '@angular/material/core';
import {FocusMonitor} from '@angular/cdk/a11y';
import {merge, Subscription} from 'rxjs';
import {MatSort, MatSortable} from './sort';
import {matSortAnimations} from './sort-animations';
Expand Down Expand Up @@ -138,7 +140,13 @@ export class MatSortHeader extends _MatSortHeaderMixinBase
changeDetectorRef: ChangeDetectorRef,
@Optional() public _sort: MatSort,
@Inject('MAT_SORT_HEADER_COLUMN_DEF') @Optional()
public _columnDef: MatSortHeaderColumnDef) {
public _columnDef: MatSortHeaderColumnDef,
/**
* @deprecated _focusMonitor and _elementRef to become required parameters.
* @breaking-change 10.0.0
*/
private _focusMonitor?: FocusMonitor,
private _elementRef?: ElementRef<HTMLElement>) {
// Note that we use a string token for the `_columnDef`, because the value is provided both by
// `material/table` and `cdk/table` and we can't have the CDK depending on Material,
// and we want to avoid having the sort header depending on the CDK table because
Expand All @@ -163,6 +171,13 @@ export class MatSortHeader extends _MatSortHeaderMixinBase

changeDetectorRef.markForCheck();
});

if (_focusMonitor && _elementRef) {
// We use the focus monitor because we also want to style
// things differently based on the focus origin.
_focusMonitor.monitor(_elementRef, true)
.subscribe(origin => this._setIndicatorHintVisible(!!origin));
}
}

ngOnInit() {
Expand All @@ -179,6 +194,11 @@ export class MatSortHeader extends _MatSortHeaderMixinBase
}

ngOnDestroy() {
// @breaking-change 10.0.0 Remove null check for _focusMonitor and _elementRef.
if (this._focusMonitor && this._elementRef) {
this._focusMonitor.stopMonitoring(this._elementRef);
}

this._sort.deregister(this);
this._rerenderSubscription.unsubscribe();
}
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/material/sort.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export declare class MatSortHeader extends _MatSortHeaderMixinBase implements Ca
disableClear: boolean;
id: string;
start: 'asc' | 'desc';
constructor(_intl: MatSortHeaderIntl, changeDetectorRef: ChangeDetectorRef, _sort: MatSort, _columnDef: MatSortHeaderColumnDef);
constructor(_intl: MatSortHeaderIntl, changeDetectorRef: ChangeDetectorRef, _sort: MatSort, _columnDef: MatSortHeaderColumnDef,
_focusMonitor?: FocusMonitor | undefined, _elementRef?: ElementRef<HTMLElement> | undefined);
_getAriaSortAttribute(): "ascending" | "descending" | null;
_getArrowDirectionState(): string;
_getArrowViewState(): string;
Expand Down