Skip to content

USWDS - Table: Add sticky headers #5420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
Feb 26, 2024
Merged

USWDS - Table: Add sticky headers #5420

merged 44 commits into from
Feb 26, 2024

Conversation

amyleadem
Copy link
Contributor

@amyleadem amyleadem commented Aug 2, 2023

Summary

New table variant. Created the .usa-table--sticky-header variant to allow sticky positioning on table headers.

Caution

Sticky headers are not compatible with the scrollable and stacked table variants.

Thanks to @etanb for originating this work in PR #5074.

Breaking change

This is not a breaking change.

Related issue

Closes #5077

Related pull requests

Changelog and documentation PR

Preview link

Problem statement

For very long tables, it can be difficult to remember the associated header content. Creating the option to have table headers stick to the top of the page as the user scrolls would help keep tabular data in context.

Solution

This change creates a .usa-table--sticky-header variant that gives the table header sticky positioning.

Note
At this time, sticky headers are not compatible with the scrollable and stacked table variants. For the scrollable variant, this incompatibility is related to a known issue (on W3C CSS repo) with sticky positioning and scrolling overflow. If we want to pursue a sticky header that is compatible with scrollable tables, I recommend we open a separate issue because it will require a decent lift to find a workaround.

New variant

Variant name Description
.usa-table--sticky-header Allows the table header element to stick to the top of the page as the user scrolls. Preferred for long tables with many rows. This variant is not compatible with the scrollable and stacked table variants.

New settings

Setting name Description
$theme-table-background-color Defines the table background color. A value of default uses the body background color.
$theme-table-sticky-top-offset Defines the top offset for sticky headers.

Testing and review

  • Confirm that sticky positioning works with the default, borderless, striped, and sortable table variants.
  • Test on a variety of browsers
  • Confirm that sticky headers are compatible with multiple header rows
  • Confirm that the new settings work as expected
  • Confirm no visual regressions

To test in Storybook:

  1. In a variety of browsers, open each table variant story from the list of preview links
  2. In each story, turn on the sticky header control
  3. Confirm that the table header sticks to the top of the page when you scroll down

    Note
    Sticky headers will not stick in scrollable and stacked variants. If the header is not sticking, confirm that the scrollable control is turned off.

  4. Turn on the control for multiple header rows
  5. Confirm that both header rows stick to the top of the page when you scroll down and confirm that there are no visual irregularities (like table content showing behind the header on scroll)

Copy link
Contributor Author

@amyleadem amyleadem left a comment

Choose a reason for hiding this comment

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

@mejiaj I believe I have addressed all your comments related to USWDS code. I am going to look into the guidance in USWDS-Site - Changelog: add entry and doc updates for sticky table headers to make sure it addresses this comment. Let me know if you need anything else.

Comment on lines 7 to 11
{% if stickyHeader %}
{% set bodyLoopCount = 0..6 %}
{% else %}
{% set bodyLoopCount = 0..0 %}
{% endif %}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in 02034a7

@amyleadem amyleadem requested a review from mejiaj January 18, 2024 17:57
Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

Thanks @amyleadem, two more minor comments. One for improving clarity in settings and another for making the template class easier to update.

@@ -213,6 +214,7 @@ $theme-table-sorted-header-background-color: "accent-cool-light" !default;
$theme-table-sorted-background-color: "accent-cool-lighter" !default;
$theme-table-sorted-stripe-background-color: "blue-cool-10v" !default;
$theme-table-sorted-icon-color: default !default;
$theme-table-sticky-top-offset: -1px !default;
Copy link
Contributor

Choose a reason for hiding this comment

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

Small concern here is this is similar to the current hero setting where it's not very flexible or re-usable.

Not a blocker for this work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the setting is fairly limited, but I am not sure if there is a way around it. I would imagine a lot of projects will need to write some custom style rules for the top offset.

Copy link
Contributor

Choose a reason for hiding this comment

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

@amyleadem does this setting accept units too or only pixels? Do we have any guidance for this?

@amyleadem
Copy link
Contributor Author

@mejiaj I believe I've addressed your concerns. Let me know if you have any questions.

Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

Question on new offset setting. Code looks good, thank you.

@mejiaj mejiaj requested review from thisisdano and removed request for amycole501 and alex-hull February 1, 2024 20:10
Copy link
Contributor

@thisisdano thisisdano left a comment

Choose a reason for hiding this comment

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

NICE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

USWDS - Feature: Ability to have <Table/> headers sticky to the top
6 participants