Skip to content
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

Merged
merged 40 commits into from
Aug 18, 2023

Conversation

amyleadem
Copy link
Contributor

@amyleadem amyleadem commented Jul 17, 2023

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

Note
This issue was originally worked in #5124.

The edits in this new PR address only the ability to select a custom content region. We will need to discuss if we want to incorporate the other work included in this original PR, namely the changes to the component unit tests.

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 the usa-in-page-nav element. This attribute can accept both class and id values. If the data-main-content-selector attribute is not defined, the component will pull from the <main> element.

Example usage with a class selector:

Note
Using a class selector will allow for the component to pull from multiple content regions on the same page

<aside class="usa-in-page-nav" 
    data-main-content-selector=".main-content">
</aside>
<main>
    <h2>This header will not be included in the component link list</h2>
    <section class="main-content">
      <h2>This header will be included in the component link list</h2>
    </section>
</main>

Example usage with an id selector:

<aside class="usa-in-page-nav" 
    data-main-content-selector="#main-content">
</aside>
<main>
    <h2>This header will not be included in the component link list</h2>
    <section id="main-content">
      <h2>This header will be included in the component link list</h2>
    </section>
</main>

Testing and review

  • Confirm that the data-main-content-selector attribute name is intuitive
  • Confirm that adding a custom value to the data-main-content-selector attribute pulls the expected headings
  • Let me know if this should have a unit test (I might need to pair up for that)

To test in Storybook:

  1. Open the Test Custom Content Selector story. Confirm that only this link appears in the in-page navigation:

    image

  2. Set the customContentSelector control to false and reload the page. Confirm that both links are shown in the in-page navigation:

    image

To test in a project:

  1. Install this branch
  2. Add in-page navigation component code to a test page.
  3. Create a custom main content region that you want the component to pull headers from
  4. Add the data-main-content-selector to the usa-in-page-nav element and define it with the id or class from the custom content region
  5. Confirm that the in-page navigation only pulls headers form the designated content region

Credits

Thanks to @aduth for the suggestion in this comment on PR #5124

- Allows users to set the element where the component pulls headers
@amyleadem amyleadem changed the title USWDS - In-page navigation: Add an option to designate a custom main element USWDS - In-page navigation: Add an option to designate the main content element Jul 17, 2023
amyleadem added a commit that referenced this pull request Jul 19, 2023
- Align testing technique with content selector test in #5387
@amyleadem amyleadem marked this pull request as ready for review July 21, 2023 20:16
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.

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/index.js Outdated Show resolved Hide resolved
packages/usa-in-page-navigation/src/index.js Outdated Show resolved Hide resolved
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 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!

Copy link
Contributor

@mahoneycm mahoneycm left a 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

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.

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.

packages/usa-in-page-navigation/src/test/template.html Outdated Show resolved Hide resolved
@amyleadem
Copy link
Contributor Author

@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?

@amyleadem amyleadem requested a review from mejiaj August 8, 2023 18:12
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.

Just some minor changes, otherwise LGTM.

];

tests.forEach(({ name, selector: containerSelector }) => {
describe(`in-page navigation initialized at ${name}`, () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The describe on line 22 should be updated to match what we're testing.

Currently it's hard to tell what's being tested. Here's the output:

image

You can see at a glance it's hard to tell since they both use the same describe() - in-page navigation initialized at document.body shows twice.

Copy link
Contributor Author

@amyleadem amyleadem Aug 10, 2023

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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")
Copy link
Contributor

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.

@amyleadem
Copy link
Contributor Author

@mejiaj thanks for all the coaching on this. I am leveling up on unit tests - I can feel it! Let me know if you want any other changes.

PS - I also updated the test-pattern twig file to remove the macro like we did on the hidden headers PR (#5393) in 9d319a1.

@amyleadem amyleadem requested a review from mejiaj August 10, 2023 21:39
@amyleadem
Copy link
Contributor Author

FYI - Working on merging in the changes from #5393

@amyleadem
Copy link
Contributor Author

@mejiaj I resolved all merge conflicts. Can you take a look at the recent changes when you get a chance?

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.

LGTM! The unit tests are clear and consistent with current patterns.

Unit test output
image

@mejiaj mejiaj requested a review from thisisdano August 11, 2023 17:45
@thisisdano thisisdano merged commit 4fe8dda into develop Aug 18, 2023
5 checks passed
@thisisdano thisisdano deleted the al-in-page-nav-parent branch August 18, 2023 16:49
@thisisdano thisisdano mentioned this pull request Aug 18, 2023
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 - Enhancement: Allow customization of in-page nav target
4 participants