Skip to content

fix(Table): Add keyboard support to table sorting#5736

Merged
jmuzina merged 6 commits intomainfrom
table-sort-accessible
Jan 28, 2026
Merged

fix(Table): Add keyboard support to table sorting#5736
jmuzina merged 6 commits intomainfrom
table-sort-accessible

Conversation

@jmuzina
Copy link
Member

@jmuzina jmuzina commented Jan 16, 2026

Done

Makes sortable table headers keyboard-accessible by wrapping the text in button elements. Follows w3c example which recommends wrapping the header text in a button, inside the th.

Fixes https://warthogs.atlassian.net/browse/WD-32943
Fixes #5735

React Components will require an update (updating the table headers markup) to benefit from this fix.

QA

  • Open table example
  • Use the keyboard to sort the table. You can use SPACE or ENTER on a header to sort it.
  • Verify sorting still works, identical to using the mouse.
  • Verify focus ring appears around the header when focused, and not when hovered.
  • Open legacy sortable table example and verify it can still be sorted (with the mouse, not with the keyboard).
  • Use a screen reader like ORCA and verify the table headers are semantically described as table headers with interactive buttons, and they announce when the table is sorted.
  • Use dev tools to add lots of text to a table header so that the text is ellipsized. Verify that text is still ellipsized and sorting still works (though the sorting icon is hidden by the ellipses.... this is a pre-existing issue that should be handled separately). Verify the text doesn't overflow (it is truncated)
  • Review sortable table docs
  • Review 4.39.1 release notes

Design

  • I used the button element for proper interactivity semantics, but this carries with it the default button styling. I made the table sort button inherit the table header label's styling to negate this, but have a look and make sure I didn't miss anything.
  • I handled the focus ring by applying a 2x4x2x4 padding around the button to make the focus ring appear like a link focus ring (since this visually appears like a link, not a button). Then I cancelled out the padding with negative margin to avoid affecting the size of the header itself. This is worth extra attention.

Check if PR is ready for release

If this PR contains Vanilla SCSS or macro code changes, it should contain the following changes to make sure it's ready for the release:

  • PR should have one of the following labels to automatically categorise it in release notes:
    • Feature 🎁, Breaking Change 💣, Bug 🐛, Documentation 📝, Maintenance 🔨.
  • Vanilla version in package.json should be updated relative to the most recent release, following semver convention
    • if existing APIs (CSS classes & macro APIs) are not changed it can be a bugfix release (x.x.X)
    • if existing APIs (CSS classes & macro APIs) are changed/added/removed it should be a minor version (x.X.0)
    • see the wiki for more details
  • Any changes to component class names (new patterns, variants, removed or added features) or macros should be listed on the what's new page.

@webteam-app
Copy link

@jmuzina jmuzina changed the title Table sort accessible fix(Table): Add keyboard support to table sorting Jan 16, 2026
@jmuzina jmuzina added Review: Percy needed This PR needs a review of Percy for visual regressions Review: Design needed Review: UX needed labels Jan 16, 2026
@jmuzina jmuzina marked this pull request as ready for review January 16, 2026 20:43
@juanruitina
Copy link
Contributor

The keyboard navigation looks good to me: there's focus states, chevrons indicate sorting, hitting space works.

There is a bug: I've played a bit with it and sorting seems to be alphabetical; it should be smart enough to detect numbers so in those cases it sorts from higher to lower (instead of "1, 10, 12, 140, 2, 20, 3"). Happy to help test this.

@jmuzina
Copy link
Member Author

jmuzina commented Jan 20, 2026

There is a bug: I've played a bit with it and sorting seems to be alphabetical; it should be smart enough to detect numbers so in those cases it sorts from higher to lower (instead of "1, 10, 12, 140, 2, 20, 3"). Happy to help test this.

@juanruitina I agree - though I believe this warrants a separate issue focusing on the sorting logic itself, rather than the keyboard accessibility. Can you file an issue?

@jmuzina jmuzina added Review: UX +1 Review: A11y +1 Review: Percy +1 and removed Review: A11y needed Review: Percy needed This PR needs a review of Percy for visual regressions labels Jan 20, 2026
@dgtlntv
Copy link
Member

dgtlntv commented Jan 20, 2026

LGTM. I was going to change what the focus state for a table header in terms of sorting is going to look like in the new table that I was designing which you can find here: https://www.figma.com/design/2MYJvamCVgbyWXZRT7IPan/25.10-Data-table---Design-system-contributions?node-id=4351-96714&t=1oohgcxOtOvarVUn-4.
But i think the more link inspired focus state here is probably appropriate for Vanilla as the clickable thing in Vanilla looks more like a link. So I think this is good, thanks for the a11y improvement! :)

Copy link
Member

@bartaz bartaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small comments inline in code, but overall looks good. Thanks!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds keyboard accessibility to sortable table headers by wrapping header text in <button> elements, following W3C guidelines. This enables users to sort tables using Enter and Space keys.

Changes:

  • Wrapped sortable table header text in <button class="p-table__sort-button"> elements
  • Added SCSS styles to make buttons appear as seamless part of headers
  • Created legacy example to maintain backward compatibility
  • Updated documentation to explain the new keyboard-accessible markup
  • Incremented version from 4.39.0 to 4.39.1

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
scss/_patterns_table-sortable.scss Added styles for the new .p-table__sort-button class and adjusted hover/focus states
templates/docs/examples/patterns/tables/_table-sortable.html Updated example to wrap header text in buttons
templates/docs/examples/patterns/tables/table-sortable-legacy.html Added legacy example without keyboard support for backward compatibility
templates/docs/examples/patterns/tables/combined.html Included legacy example in combined examples
templates/docs/base/tables.md Added documentation about keyboard sorting with updated status badge
releases.yml Added 4.39.1 release notes
package.json Bumped version to 4.39.1

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jmuzina jmuzina force-pushed the table-sort-accessible branch from 4b08af8 to 9696046 Compare January 28, 2026 16:33
@jmuzina jmuzina merged commit 11c9b08 into main Jan 28, 2026
12 checks passed
@jmuzina jmuzina deleted the table-sort-accessible branch January 28, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Table sorting is not keyboard accessible

6 participants