Skip to content

feat(material/sort): add showSortIcon option to MatSortHeader and update styles #30912

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

Closed
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
6 changes: 5 additions & 1 deletion goldens/material/sort/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const matSortAnimations: {
export interface MatSortDefaultOptions {
arrowPosition?: SortHeaderArrowPosition;
disableClear?: boolean;
showSortIcon?: boolean;
}

// @public
Expand All @@ -120,21 +121,24 @@ export class MatSortHeader implements MatSortable, OnDestroy, OnInit, AfterViewI
// (undocumented)
static ngAcceptInputType_disabled: unknown;
// (undocumented)
static ngAcceptInputType_showSortIcon: unknown;
// (undocumented)
ngAfterViewInit(): void;
// (undocumented)
ngOnDestroy(): void;
// (undocumented)
ngOnInit(): void;
protected _recentlyCleared: i0.WritableSignal<SortDirection | null>;
_renderArrow(): boolean;
showSortIcon: boolean;
// (undocumented)
_sort: MatSort;
get sortActionDescription(): string;
set sortActionDescription(value: string);
start: SortDirection;
_toggleOnInteraction(): void;
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatSortHeader, "[mat-sort-header]", ["matSortHeader"], { "id": { "alias": "mat-sort-header"; "required": false; }; "arrowPosition": { "alias": "arrowPosition"; "required": false; }; "start": { "alias": "start"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; "sortActionDescription": { "alias": "sortActionDescription"; "required": false; }; "disableClear": { "alias": "disableClear"; "required": false; }; }, {}, never, ["*"], true, never>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatSortHeader, "[mat-sort-header]", ["matSortHeader"], { "id": { "alias": "mat-sort-header"; "required": false; }; "arrowPosition": { "alias": "arrowPosition"; "required": false; }; "start": { "alias": "start"; "required": false; }; "disabled": { "alias": "disabled"; "required": false; }; "sortActionDescription": { "alias": "sortActionDescription"; "required": false; }; "disableClear": { "alias": "disableClear"; "required": false; }; "showSortIcon": { "alias": "showSortIcon"; "required": false; }; }, {}, never, ["*"], true, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatSortHeader, never>;
}
Expand Down
6 changes: 3 additions & 3 deletions src/material/sort/sort-header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

to {
transform: translateY(-25%);
opacity: 0;
opacity: var(--mat-sort-header-opacity);
}
}

Expand All @@ -64,7 +64,7 @@

to {
transform: translateY(25%) rotate(180deg);
opacity: 0;
opacity: var(--mat-sort-header-opacity);
}
}

Expand All @@ -74,7 +74,7 @@
width: 12px;
position: relative;
transition: transform $timing, opacity $timing;
opacity: 0;
opacity: var(--mat-sort-header-opacity);
overflow: visible;

@include token-utils.use-tokens(m2-sort.$prefix, m2-sort.get-token-slots()) {
Expand Down
9 changes: 9 additions & 0 deletions src/material/sort/sort-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ interface MatSortHeaderColumnDef {
'(mouseleave)': '_recentlyCleared.set(null)',
'[attr.aria-sort]': '_getAriaSortAttribute()',
'[class.mat-sort-header-disabled]': '_isDisabled()',
'[style.--mat-sort-header-opacity]': 'showSortIcon ? 0.54 : 0',
},
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
Expand Down Expand Up @@ -151,6 +152,10 @@ export class MatSortHeader implements MatSortable, OnDestroy, OnInit, AfterViewI
@Input({transform: booleanAttribute})
disableClear: boolean;

/** Overrides the show sort icon value of the containing MatSort for this MatSortable. */
@Input({transform: booleanAttribute})
showSortIcon: boolean;

constructor(...args: unknown[]);

constructor() {
Expand All @@ -170,6 +175,10 @@ export class MatSortHeader implements MatSortable, OnDestroy, OnInit, AfterViewI
if (defaultOptions?.arrowPosition) {
this.arrowPosition = defaultOptions?.arrowPosition;
}

if (defaultOptions?.showSortIcon) {
this.showSortIcon = defaultOptions.showSortIcon;
}
}

ngOnInit() {
Expand Down
34 changes: 34 additions & 0 deletions src/material/sort/sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,40 @@ describe('MatSort', () => {
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(true);
});
});

describe('with default showSortIcon', () => {
let fixture: ComponentFixture<MatSortWithoutInputs>;

const getComputedStyleProp = (el: Element | null, prop: string): any =>
el ? getComputedStyle(el).getPropertyValue(prop) : undefined;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatSortModule, MatTableModule, CdkTableModule, MatSortWithoutInputs],
providers: [
{
provide: MAT_SORT_DEFAULT_OPTIONS,
useValue: {
showSortIcon: true,
},
},
],
});
}));

beforeEach(() => {
fixture = TestBed.createComponent(MatSortWithoutInputs);
fixture.detectChanges();
});

it('should show sort icons by default', () => {
const containerA = fixture.nativeElement.querySelector('#defaultA .mat-sort-header-arrow');
const containerB = fixture.nativeElement.querySelector('#defaultB .mat-sort-header-arrow');

expect(getComputedStyleProp(containerA, 'opacity')?.toString()).toBe('0.54');
expect(getComputedStyleProp(containerB, 'opacity')?.toString()).toBe('0.54');
});
});
});

/**
Expand Down
2 changes: 2 additions & 0 deletions src/material/sort/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface MatSortDefaultOptions {
disableClear?: boolean;
/** Position of the arrow that displays when sorted. */
arrowPosition?: SortHeaderArrowPosition;
/** Show sort icons by default. */
showSortIcon?: boolean;
}

/** Injection token to be used to override the default options for `mat-sort`. */
Expand Down
Loading