-
Notifications
You must be signed in to change notification settings - Fork 923
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 an option to designate the main content element #5387
Conversation
- Allows users to set the element where the component pulls headers
- Make the name more intuitive
- Will be addressed separately in issue #5392
- Align testing technique with content selector test in #5387
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.
Nice work so far!
Since we use querySelectorAll()
having a duplicate ID or class will generate the nav with their respective headers. Meaning, you could end up with seemingly more content than originally intended. This doesn't seem like a blocker.
Added suggestions below. Can we also include a basic unit test to check for a custom ID & class?
packages/usa-in-page-navigation/src/usa-in-page-navigation.stories.js
Outdated
Show resolved
Hide resolved
to add clarity of purpose - Also Improve JSDoc comments for getSectionHeadings
…age-nav-parent
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.
@mejiaj I have updated the unit tests according to our conversation. I'm hoping the description and naming make sense, but let me know if you see any opportunities for improvement. Thanks!
packages/usa-in-page-navigation/src/test/test-pattern/test-custom-content-selector.twig
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/test/test-pattern/test-custom-content-selector.twig
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/test/test-pattern/test-custom-content-selector.twig
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/test/test-pattern/test-custom-content-selector.twig
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/test/test-pattern/test-custom-content-selector.twig
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/test/test-pattern/test-custom-content-selector.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/in-page-navigation.spec.js
Outdated
Show resolved
Hide resolved
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.
@amyleadem thank you for that tip! Working like a charm now!
This PR looks good to me!
- Including the attribute works as expected
- Links are included or excluded based on the attribute's value
- Works for both ID's and classes
- Works with multiple divs using the same class
- Test is accurate and functions properly
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.
Almost there, just a small change to split this new feature into its own unit test + template.
I also added HTML syntax highlighting to code examples under Solution section for readability.
@mejiaj I have broken the unit tests for this PR into their own files. Will you let me know if you see any opportunities for improvement? |
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 some minor changes, otherwise LGTM.
packages/usa-in-page-navigation/src/test/test-pattern/test-custom-content-selector.twig
Outdated
Show resolved
Hide resolved
packages/usa-in-page-navigation/src/test/in-page-nav-custom-content.spec.js
Outdated
Show resolved
Hide resolved
]; | ||
|
||
tests.forEach(({ name, selector: containerSelector }) => { | ||
describe(`in-page navigation initialized at ${name}`, () => { |
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.
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.
Thanks! Updated in 394a463.
const customRegionLink = listLinks.filter((link) => | ||
link.href.includes("#header-in-content-region") | ||
); | ||
assert.equal(customRegionLink.length === 1, true); |
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.
It might be more reliable to just check the nav link length matches the header in content region length?
For example, it fails if you add more headers.
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.
Instead of updating the test highlighted here, I updated the first unit test that checked for exactly 1 link in the link list in this commit. I might have misunderstood the request, so let me know if wanted me to change this one instead.
|
||
it("does not create a link in the nav list for the header that is outside the custom content region", () => { | ||
const mainRegionLink = listLinks.filter((link) => | ||
link.href.includes("#header-not-in-content-region") |
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
If I delete the header outside of the content region this test still passes. This is expected, but could cause confusion.
FYI - Working on merging in the changes from #5393 |
…age-nav-parent
@mejiaj I resolved all merge conflicts. Can you take a look at the recent changes when you get a chance? |
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.
Summary
Added the optional
data-main-content-selector
attribute to the in-page navigation component. This attribute allows users to designate which element they want the component to pull headers from. If the attribute is not defined, the component will pull headers from the<main>
element.Breaking change
This is not a breaking change.
Related issue
Closes #5050
Related pull requests
Changelog and documentation PR
Preview link
In-page navigation
Problem statement
In its current state, the in-page navigation component is hard-coded to pull headers from the
<main>
element. Users are not able to customize where the component will look for headers.Solution
This PR creates the option to designate which element the in-page nav will pull headers from. It does this by creating the option to include a
data-main-content-selector
attribute on theusa-in-page-nav
element. This attribute can accept both class and id values. If thedata-main-content-selector
attribute is not defined, the component will pull from the<main>
element.Example usage with a class selector:
Example usage with an id selector:
Testing and review
data-main-content-selector
attribute name is intuitivedata-main-content-selector
attribute pulls the expected headingsTo test in Storybook:
Open the Test Custom Content Selector story. Confirm that only this link appears in the in-page navigation:
Set the
customContentSelector
control to false and reload the page. Confirm that both links are shown in the in-page navigation:To test in a project:
data-main-content-selector
to theusa-in-page-nav
element and define it with the id or class from the custom content regionCredits
Thanks to @aduth for the suggestion in this comment on PR #5124