angular / angular/components

bug(mat-text-column): Column-name based CSS ignored unless view encapsulation is disabled

Open
#24,893 3 comments 2 reactions 0 assignees View on GitHub
area: material/table P4
Dominant language
TypeScript
Stars
25k
Forks
6.8k
Avg merge
1d 8h
Merged PRs (30d)
91

Description

### Is this a regression?

- [ ] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

May not technically be a bug, and more of an undocumented quirk.

There are two aspects to mat-table column definitions that lead to an unexpected conclusion:

- For a column named `foo`, you can define a `.mat-column-foo` style which will get applied to it.
- A `mat-text-column` named `foo` is shorthand for an explicit matColumnDef with sensible defaults for a text-only field.

However, if you define said style, and try to apply it to a mat-text-column, it won't match.

I supppose that technically, the affected th/td elements are no longer generated by the parent view, but by mat-text-node itself - and thus, those are outside the scope of view-encapsulated styles defined there.

The fix may not be trivial, but it might still be worth documenting as a gotcha.

### Reproduction

See StackBlitz [here](https://stackblitz.com/edit/components-issue-e4h1lq?file=src/app/example-component.ts). By making the component non-encapsulated, the style applies to both columns. Otherwise, it only matches the explicitly defined one, not the text-column.

### Expected Behavior

Styles affect matColumnDefs and mat-text-columns the same way.

### Actual Behavior

...they don't!

### Environment

Angular CLI: 13.3.5
Node: 14.17.6
Package Manager: yarn 1.22.15
OS: win32 x64

Angular: 13.3.6

Contributor guide

Open the contributing guide

Research direction

Start with the linked StackBlitz reproduction and inspect src/app/example-component.ts, comparing the explicitly defined matColumnDef with the mat-text-column under view encapsulation. Confirm the styling mismatch and determine whether the intended outcome is a component fix or documentation of the behavior; done means the two column types receive equivalent styles or the limitation is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.