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

feat(Link): add react-router link integration #294

Merged
merged 4 commits into from
Jan 30, 2024

Conversation

billhimmelsbach
Copy link
Contributor

@billhimmelsbach billhimmelsbach commented Jan 30, 2024

Closes: cfpb/sbl-frontend#117

Well this took some experimentation, but we got there. Allows Link to use a isRouterLink prop to convert them to react-router links with minimal hassle.

Changes

  • add react-router-dom as an external dependency in the vite config, since react-router needs its links to share the same instance of the library to work. Works in concert with this change in sbl-frontend. What a pain this was. (Also, I added a maybe missing react-dom from the optimizeDeps.exclude array for good measure)
  • add a isRouterLink prop that switches Link to using react-router Links
  • fixes some Typescript errors
  • adds a story for isRouterLink prop called "Link using React Router Link"

How to test this PR

  1. Does the isRouterLink story "Link using React Router Link" render without errors?

Screenshots

Screenshot 2024-01-29 at 11 15 16 PM

Notes

Copy link

netlify bot commented Jan 30, 2024

Deploy Preview for cfpb-design-system-react ready!

Name Link
🔨 Latest commit 5cbe64d
🔍 Latest deploy log https://app.netlify.com/sites/cfpb-design-system-react/deploys/65b8e87f44b9c20008169bc2
😎 Deploy Preview https://deploy-preview-294--cfpb-design-system-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@billhimmelsbach billhimmelsbach changed the title feat(Link): add react-router link integratiom feat(Link): add react-router link integration Jan 30, 2024
<BrowserRouter>
<p>
<Link href='/#' isRouterLink>
Link using React Router Link
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: after writing out everything below I realized that the fact that it is a CFPB styled Link is important to the name, so you can ignore the rest of this comment 😬


The naming feels a bit awkward. Since it's ultimately returning a RouterLink it would be easier to simply call this React Router Link.

That said, we've been adjusting naming of variants in other components (ex. Instead of "Checkboxes -> Large target area checkbox" using "Checkboxes -> Large target area" to leverage the context that implies that everything under the "Checkboxes" heading is a "checkbox") and the names for Link could probably use some improvement as well. So this is probably fine as-is and can be re-evaluated in the next round of naming reviews.

@billhimmelsbach
Copy link
Contributor Author

Thanks @meissadia! I appreciate the thinking about the naming.

@billhimmelsbach billhimmelsbach merged commit d3888b7 into main Jan 30, 2024
7 of 9 checks passed
@billhimmelsbach billhimmelsbach deleted the 117-react-router-links branch January 30, 2024 18:06
billhimmelsbach added a commit to cfpb/sbl-frontend that referenced this pull request Jan 30, 2024
#201)

This integrates cfpb/design-system-react#294
into the app, adding a little composition to the DSR component to
automatically switch from normal anchor links to `react-router` links
depending on the `href`.

I wanted to make the transition as easy as possible: it's the same props
as the DSR `Link`. As long as you're importing the Link from the app
instead of the `DSR`, you don't have to worry about figuring out which
links should have `isRouterLink` or not.

If we like this approach, I could see us raising this up into the DSR as
a "SBL-specific component" in a later PR.

**Note: don't merge this in until
cfpb/design-system-react#294 is merged and [this
line](https://github.com/cfpb/sbl-frontend/compare/198-integrate-react-router-links?expand=1#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R37)
that temporarily updates the DSR is reverted**

## Changes

- adds new `Link` and `ListLink` components to the App that switch
between `a` and `RouterLink` depending on what `href` is given
- updates all instances of `Link` / `ListLink` across the app to use the
new components
- updates usage of plain `<a>` anchors to use `Link`
- dedupes `react-router-dom` in the vite config that works with this
change in the DSR to allow for `react-router` enabled `Links` to work
within the same instance of `react-router` (what a pain this was)

## How to test this PR

1. Do external and internal links still work?
2. Do internal links (particularly breadcrumbs) seem faster with less
page jank?

## Screenshots

https://github.com/cfpb/sbl-frontend/assets/19983248/a239a808-b200-4244-b910-b6f9141ba244
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.

[Task] [Router] Incorporate React-Router links into the DSR
2 participants