generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Link): add a sbl-frontend Link that uses react-router when needed (
#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
- Loading branch information
1 parent
a704ad3
commit d0a7142
Showing
28 changed files
with
253 additions
and
147 deletions.
There are no files selected for viewing
Binary file renamed
BIN
+943 KB
...tem-react-https-2ee151c291-b6b47c84d5.zip → ...tem-react-https-a4b466e9f0-695d77f250.zip
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// this component could potentially be lifted with others into the DSR in a later refactor | ||
// see: https://github.com/cfpb/sbl-frontend/issues/200 | ||
|
||
import type { LinkProperties as DesignSystemReactLinkProperties } from 'design-system-react'; | ||
import { | ||
Link as DesignSystemReactLink, | ||
ListLink as DesignSystemReactListLink, | ||
} from 'design-system-react'; | ||
|
||
const getIsLinkExternal = (url: string | undefined): boolean => { | ||
if (url === undefined) { | ||
return false; | ||
} | ||
return url.startsWith('http'); | ||
}; | ||
|
||
const getIsRouterUsageInferred = ( | ||
href: string | undefined, | ||
isRouterLink: boolean | undefined, | ||
): boolean => isRouterLink === undefined && !getIsLinkExternal(href); | ||
|
||
const getIsRouterLink = ( | ||
href: string | undefined, | ||
isRouterLink: boolean | undefined, | ||
): boolean => { | ||
const isRouterUsageInferred = getIsRouterUsageInferred(href, isRouterLink); | ||
if (isRouterUsageInferred) { | ||
return true; | ||
} | ||
if (isRouterLink === undefined) { | ||
return false; | ||
} | ||
return isRouterLink; | ||
}; | ||
|
||
interface LinkProperties extends DesignSystemReactLinkProperties { | ||
// design system react's Link component correctly allows undefined values without defaultProps | ||
/* eslint-disable react/require-default-props */ | ||
href?: string | undefined; | ||
isRouterLink?: boolean | undefined; | ||
/* eslint-enable react/require-default-props */ | ||
} | ||
|
||
export function Link({ | ||
children, | ||
href, | ||
isRouterLink, | ||
...others | ||
}: LinkProperties): JSX.Element { | ||
return ( | ||
<DesignSystemReactLink | ||
href={href} | ||
isRouterLink={getIsRouterLink(href, isRouterLink)} | ||
{...others} | ||
> | ||
{children} | ||
</DesignSystemReactLink> | ||
); | ||
} | ||
|
||
export function ListLink({ | ||
href, | ||
isRouterLink, | ||
children, | ||
...others | ||
}: LinkProperties): JSX.Element { | ||
return ( | ||
<DesignSystemReactListLink | ||
href={href} | ||
isRouterLink={getIsRouterLink(href, isRouterLink)} | ||
{...others} | ||
> | ||
{children} | ||
</DesignSystemReactListLink> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useEffect } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
export default function ScrollToTop(): null { | ||
const { pathname } = useLocation(); | ||
|
||
useEffect(() => { | ||
window.scrollTo(0, 0); | ||
}, [pathname]); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/pages/ProfileForm/Step1Form/AssociatedFinancialInstitutions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters