[Bug]: A sortable TableHeaderCell does not follow the cell alignment, so right aligned columns get left aligned headers
Component
TableHeaderCell
Package version
@fluentui/react-table 9.23.1 (@fluentui/react-components 9.74.10)
React version
19.3.1
Environment
System:
OS: Windows 11 10.0.26300
CPU: (24) x64 AMD Ryzen 9 7900X 12-Core Processor
Memory: 13.83 GB / 31.16 GB
Browsers:
Chrome: 155.0.8059.85
npmPackages:
@fluentui/react-components: 9.74.10 => 9.74.10
@fluentui/react-table: 9.23.1 => 9.23.1
react: 19.3.1 => 19.3.1
react-dom: 19.3.1 => 19.3.1
Current Behavior
A sortable TableHeaderCell does not follow the alignment of the column, so a right aligned numeric column gets a left aligned header while its data cells are right aligned.
The sort control is the button slot, which is a flex container:
button: {
position: 'relative',
width: '100%',
display: 'flex',
flexGrow: 1,
height: '100%',
alignItems: 'center',
gap: tokens.spacingHorizontalXS,
minHeight: '32px',
flex: '1 1 0px',
outlineStyle: 'none',
},
It sets no justifyContent, and resetButton sets textAlign: 'unset'. Because the label lives inside a flex container rather than in normal flow, the cell's textAlign has no effect on it, and the button's own justifyContent is the initial value, so the label sits at the start of the cell no matter how the cell is aligned.
Measured on Chrome 155.0.8059.85, one sortable TableHeaderCell with style={{ justifyContent: 'flex-end', width: '240px' }}, label bounds taken with a Range over the button contents:
| Cell style | Button justify-content |
Label left | Button left | Button right |
|---|---|---|---|---|
justifyContent: 'flex-end' |
normal |
66 | 61 | 484 |
| no justification | normal |
66 | 61 | 484 |
The label stays 5px from the start edge in both cases, so aligning the cell does nothing. The only way to align the header today is to target the private class:
'& .fui-TableHeaderCell__button': { justifyContent: 'flex-end' }
I have that rule in three separate places in one app, which is what made me look at this. Reaching into a slot class name is brittle, since those names are not part of the public API, and it has to be repeated for every aligned column.
Expected Behavior
The header control should follow the cell's own alignment, so consumers align the cell once and the header matches.
Adding justifyContent: 'inherit' to the button style does that, because the button is the cell's child and inherit reads the cell's computed value:
button: {
position: 'relative',
width: '100%',
display: 'flex',
flexGrow: 1,
height: '100%',
alignItems: 'center',
justifyContent: 'inherit',
gap: tokens.spacingHorizontalXS,
},
When the cell sets nothing, the computed value is normal, which behaves as flex-start for a flex container, so the default rendering is unchanged. Measured with that line in place, the label moves to 479 for a cell justified to the end (against a button right edge of 484) and stays at 66 for a cell with no justification.
The consumer then writes justifyContent: 'flex-end' on the header cell and drops the rule that targets .fui-TableHeaderCell__button.
If you would rather not inherit, a documented pattern for aligning a sortable header would also help, since the current options are all private-class overrides.
Reproduction
https://gist.github.com/jakesalvesen/f6fa100b85533612274b3344715dca64 - a Cypress component test with three cases, one per row of the table above
Steps to reproduce
- Render a
Tablewith a sortableTableHeaderCelland give itstyle={{ justifyContent: 'flex-end', width: '240px' }}. - Measure the label with
document.createRange()over the button contents. Its left edge is about 5px from the button's left edge, so it is left aligned. getComputedStyle(button).justifyContentisnormal.- Add
justifyContent: 'inherit'to thebuttonstyle and repeat. The computed value becomesflex-endand the label moves to the end edge. - Repeat with no cell justification. The label stays at the start in both cases, so the change does not affect the default.
Are you reporting an Accessibility issue?
no
Suggested severity
Low - Has or doesn't need a workaround
Products/sites affected
Internal Power Apps code app with data grids and statistics tables that right align numeric columns
Are you willing to submit a PR to fix?
yes
Validations
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] The provided reproduction is a minimal reproducible example of the bug.
Fix PR: #36963. It adds justifyContent: inherit to the header button so it follows the cell justification, and adds Cypress cases for the aligned and default cases.
Hi team, I filed this outside the issue form by mistake, so the usual triage labels (Type: Bug, Needs: Triage, Fluent UI react-components (v9)) weren't applied. Would someone be able to add them when convenient? There's a fix with a regression test in #36963. Thank you!
Hi @tudorpopams, sorry to bother you. When you have a moment, could you take a look at this one too? I filed it without the issue form, so it never got the Needs: Triage label and may have missed the triage queue. The fix is in #36963. Thank you!
Superseded by #37125, which was filed through the issue template and carries the triage labels. Same report, with the package version corrected to @fluentui/react-table 9.19.23 since 9.23.1 is not a published version. Closing this one so there is a single thread. The fix is still in #36963.