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
USWDS - In-page navigation: Add custom headings attribute #5444
Conversation
…age-nav-header-select
data-title-text="On this page" | ||
data-title-heading-level="h4" | ||
data-scroll-offset="0" | ||
data-root-margin="0px 0px 0px 0px" | ||
data-threshold="1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed these attributes so that the component uses defaults. The aim was to improve clarity in testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loving this change! Great work 👍
- Attribute name is intuitive and follows convention
- Correct headings are included
- Links are styled appropriately
- Test various heading levels in sandbox
-
h2 h3 h4
-
h2 h4
-
h4 h2
-
h3
-
- Confirmed highest level heading is respected despite order in data attribute
- JS matches USWDS standard
- Automated tests pass
Following |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works well and the story controls were helpful! I was able to test:
- All available settings
- Changing order of headings
data-heading-selector="h6 h5 h1 h2 h3 h4"
- Defaults work as expected
Added some suggestions to improve clarity. Also, can you add the release note to the PR description?
packages/usa-in-page-navigation/src/styles/_usa-in-page-navigation.scss
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/test/test-patterns/test-custom-header-selector.twig
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/test/test-patterns/test-hidden-headers.twig
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/usa-in-page-navigation.stories.js
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/usa-in-page-navigation.stories.js
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/test/test-patterns/test-custom-header-selector.twig
Outdated
Show resolved
Hide resolved
@@ -216,9 +248,11 @@ const createInPageNav = (inPageNavEl) => { | |||
const anchorTag = document.createElement("a"); | |||
const textContentOfLink = el.textContent; | |||
const tag = el.tagName.toLowerCase(); | |||
const topHeadingLevel = getTopLevelHeading(sectionHeadings); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noting this runs when there's only a single heading passed. This seems okay for the small amount of headers available.
- Rename Error control - Remove empty control since it is the same as "default" - Change control from inline-radio to select
- This adds clarity to the code
…age-nav-header-select
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
Standardized the visual presentation of the expectations/instructions in this test to match the custom header test.
@mejiaj I believe I have addressed all of your questions/comments. Please let me know if you need anything else. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good.
Just sound small outstanding items:
- Note on BEM I missed on first round (sorry)
- Please add the release note in the PR description with the bold text + description.
- Old question in previous review.
- Question on sanitization in JS.
packages/usa-in-page-navigation/src/styles/_usa-in-page-navigation.scss
Outdated
Show resolved
Hide resolved
@mejiaj Thanks for catching these. Here are my responses to your items:
Let me know if you see anything else. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, minor note on nonexistent headers.
Testing items
- Single header values like
h1
- Several headers like
h1, h2, h3
-
Specifying headers that don't appearex: h6
. Empty nav is generated, see comment below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dismissing previous review because issue with empty nav is captured in #5610.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to approve this, but I think there's a modest usability issue here since there's no distinction between the "top" heading level, and all the "lower" heading levels. But that could be a issue for another day
Summary
Added the ability to customize which headings will be pulled into the in-page navigation link list.
Use the new
data-heading-elements
attribute to designate the heading levels that should be included in the component.The headings should be listed with a space in between. Here is a sample implementation:
If this attribute is not added to the component or has an empty value, it will default to pulling h2 and h3 headers.
Breaking change
This is not a breaking change.
Related issue
Closes #5030
Related pull requests
Changelog PR
This work originated in #5034.
Preview link
In-page navigation - custom header selector test
Problem statement
The in-page navigation component is hard-coded to pull all h2 and h3 headers from the designated main content region. However, there are valid use cases where teams would want to control which headings will be included in their component.
Solution
Creating
data-heading-elements
attribute will allow users select which header levels they want to include in the component link list. This attribute can accept multiple values (between h1-h6), each separated by a space. If the attribute is not added or is left blank, the component will pull the default h2 and h3 headers.Testing and review
data-heading-elements
attribute is presentTo test in Storybook:
To test in a project:
data-heading-elements
to any mix of headings and confirm that the link list pulls the correct headings.