[Bug]: A sortable TableHeaderCell does not follow the cell alignment, so right aligned columns get left aligned headers
Component
Table
Package version
9.19.23
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.19.23 => 9.19.23
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 its 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',
},
justifyContent is absent from that slot on master, 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 stays at 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.
Supersedes #36961, which was filed outside this template so it never received triage labels. That report also listed @fluentui/react-table 9.23.1, which is not a published version. The current release is 9.19.24 and the button slot above is unchanged.
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
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.
Thanks for the detailed report and proposed fix in #36963. TableHeaderCell exposes a public button slot, so you can align the sort control without targeting a class name: <TableHeaderCell sortable button={{ style: { justifyContent: 'flex-end' } }}>Amount</TableHeaderCell>. The remaining API/design question is whether the button should automatically inherit justification from the root; the table owners should confirm that behavior before changing the default.
You are right, and thank you for the correction. I checked TableHeaderCell.types.ts and button is a public slot (NonNullable<Slot<ARIAButtonSlotProps>>), and useTableHeaderCell composes it with slot.always(props.button, ...), so consumer props merge through. button={{ style: { justifyContent: 'flex-end' } }} works today.
That makes two statements in my report wrong. I wrote that the private class was "the only way to align the header today" and that slot class names "are not part of the public API". The second is true of the class name itself but not of the slot, and the first is simply incorrect.
What survives the correction is narrower, and it is the design question you named. The slot approach still has to be written per header cell, and the cell's own justifyContent still does not reach the label, so a table that right aligns its numeric columns sets both the cell and the slot on each one. The open question is whether the button should inherit justification from the root so a consumer aligns the cell once.
That is the table owners' call, and I will go with whatever they decide. #36963 changes the default for every sortable header, so if the answer is no, the PR should close rather than change. If it would help the decision, I can rework it to keep the default and document the slot pattern instead, which is strictly additive.
Thanks for the report and for following up on the button slot. Since aligning the header today works through button={{ style: { justifyContent: 'flex-end' } }}, the open question is whether sortable headers should follow the cell's alignment by default, which changes behavior for every sortable header. I've marked this for discussion with the Table owners. They'll decide between changing the default in #36963 and documenting the slot pattern.
cc @microsoft/cxe-prg