Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Table): align the sortable header button with its column (#5928)

Ernest42b2fdd7d8a7
diff --git a/.changeset/table-sortable-header-align.md b/.changeset/table-sortable-header-align.md
new file mode 100644
index 0000000000..fbb7cbe27c
--- /dev/null
+++ b/.changeset/table-sortable-header-align.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Table's sortable header button now follows its column's `align`, so an `align: 'end'` or `align: 'center'` column no longer gets a start-hugging header label sitting above right-aligned figures. Sorting wraps the header in a full-width flex button, which the `textAlign` that `align` sets on the cell cannot position; the alignment is now carried onto the button's main axis with a flow-relative `justify-content`, so it keeps mirroring under RTL.
+
+@ernestt
diff --git a/packages/core/src/Table/plugins/sortable/useTableSortable.test.tsx b/packages/core/src/Table/plugins/sortable/useTableSortable.test.tsx
index 0858473863..51a3b2bbac 100644
--- a/packages/core/src/Table/plugins/sortable/useTableSortable.test.tsx
+++ b/packages/core/src/Table/plugins/sortable/useTableSortable.test.tsx
@@ -795,3 +795,75 @@ describe('useTableSortable — i18n', () => {
).toBeInTheDocument();
});
});
+
+// =============================================================================
+// Header Alignment
+// =============================================================================
+
+describe('useTableSortable — header alignment', () => {
+ // A column's `align` lands as `textAlign` on the <th>, which cannot position
+ // the contents of the full-width flex button the plugin renders inside it.
+ // The plugin has to mirror the alignment onto the button's main axis, or a
+ // numeric column gets a left-hugging header over right-aligned figures.
+ const buttonFor = (name: RegExp) => screen.getByRole('button', {name});
+
+ it('gives an end-aligned column a different button style than a default one', () => {
+ render(
+ <SortableTable
+ columns={[
+ {key: 'name', header: 'Name', sortable: true},
+ {key: 'age', header: 'Age', sortable: true, align: 'end'},
+ ]}
+ />,
+ );
+
+ expect(buttonFor(/sort by age/i).className).not.toBe(
+ buttonFor(/sort by name/i).className,
+ );
+ });
+
+ it('gives a center-aligned column a different button style than a default one', () => {
+ render(
+ <SortableTable
+ columns={[
+ {key: 'name', header: 'Name', sortable: true},
+ {key: 'age', header: 'Age', sortable: true, align: 'center'},
+ ]}
+ />,
+ );
+
+ expect(buttonFor(/sort by age/i).className).not.toBe(
+ buttonFor(/sort by name/i).className,
+ );
+ });
+
+ it('distinguishes end from center', () => {
+ render(
+ <SortableTable
+ columns={[
+ {key: 'name', header: 'Name', sortable: true, align: 'end'},
+ {key: 'age', header: 'Age', sortable: true, align: 'center'},
+ ]}
+ />,
+ );
+
+ expect(buttonFor(/sort by name/i).className).not.toBe(
+ buttonFor(/sort by age/i).className,
+ );
+ });
+
+ it('leaves a start-aligned column styled like an unaligned one', () => {
+ render(
+ <SortableTable
+ columns={[
+ {key: 'name', header: 'Name', sortable: true},
+ {key: 'age', header: 'Age', sortable: true, align: 'start'},
+ ]}
+ />,
+ );
+
+ expect(buttonFor(/sort by age/i).className).toBe(
+ buttonFor(/sort by name/i).className,
+ );
+ });
+});
diff --git a/packages/core/src/Table/plugins/sortable/useTableSortable.tsx b/packages/core/src/Table/plugins/sortable/useTableSortable.tsx
index 74bfb47a7a..2f697d0e1f 100644
--- a/packages/core/src/Table/plugins/sortable/useTableSortable.tsx
+++ b/packages/core/src/Table/plugins/sortable/useTableSortable.tsx
@@ -136,6 +136,16 @@ const sortStyles = stylex.create({
textAlign: 'inherit',
borderRadius: radiusVars['--radius-inner'],
},
+ // The button is a full-width flex container, so the `textAlign` a column's
+ // `align` puts on the <th> cannot position its contents. Mirror the column
+ // alignment onto the main axis, or an `align: 'end'` numeric column ends up
+ // with a left-hugging header over right-aligned figures.
+ buttonAlignCenter: {
+ justifyContent: 'center',
+ },
+ buttonAlignEnd: {
+ justifyContent: 'flex-end',
+ },
iconWrapperUnsorted: {
display: 'inline-flex',
opacity: {
@@ -306,7 +316,14 @@ function SortHeaderButton<T extends Record<string, unknown>>({
return (
<button
type="button"
- {...focusOutlineProps.focusVisible(sortStyles.button)}
+ {...focusOutlineProps.focusVisible(
+ sortStyles.button,
+ column.align === 'end'
+ ? sortStyles.buttonAlignEnd
+ : column.align === 'center'
+ ? sortStyles.buttonAlignCenter
+ : null,
+ )}
aria-label={ariaLabel}
onClick={handleClick}>
<span>{children}</span>