Skip to content

Commit

Permalink
Preload link
Browse files Browse the repository at this point in the history
  • Loading branch information
ogarich89 committed Oct 12, 2024
1 parent 2541ccb commit 1f94c2d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@prisma/client": "^5.18.0",
"@swc/core": "^1.7.11",
"@swc/jest": "^0.2.36",
"@swc/plugin-loadable-components": "^1.0.11",
"@swc/plugin-loadable-components": "^2.0.2",
"@testing-library/dom": "^10.4.0",
"@testing-library/react": "^16.0.0",
"@types/classnames": "^2.2.9",
Expand Down
2 changes: 1 addition & 1 deletion src/components/molecules/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Link } from 'react-router-dom';
import { Link } from 'src/components/molecules/Link/Link';

import style from './Card.scss';

Expand Down
6 changes: 3 additions & 3 deletions src/components/molecules/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cx from 'classnames';
import { useTranslation } from 'react-i18next';
import { NavLink, Link } from 'react-router-dom';
import { Link } from 'src/components/molecules/Link/Link';
import { TOGGLE_MODAL, emitter } from 'src/libs/emitter';
import { session } from 'src/libs/session';

Expand Down Expand Up @@ -37,14 +37,14 @@ export const Header: FunctionComponent = () => {
<nav>
<ul>
<li>
<NavLink
<Link
to="/products"
className={({ isActive }) =>
isActive ? style.active : undefined
}
>
{t('products')}
</NavLink>
</Link>
</li>
<li>
<span
Expand Down
32 changes: 32 additions & 0 deletions src/components/molecules/Link/Link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NavLink, matchPath } from 'react-router-dom';
import routes from 'src/routes';

import type { FunctionComponent, MouseEventHandler } from 'react';
import type { NavLinkProps } from 'react-router-dom';

export const Link: FunctionComponent<NavLinkProps> = ({
style,
to,
className,
children,
}) => {
const mouseOverHandler: MouseEventHandler = () => {
routes.forEach(({ children }) => {
children?.forEach(({ path, component }) => {
if (matchPath(path, to as string) && path !== '*') {
component.preload();
}
});
});
};
return (
<NavLink
style={style}
to={to}
className={className}
onMouseOver={mouseOverHandler}
>
{children}
</NavLink>
);
};
2 changes: 1 addition & 1 deletion src/components/organisms/Product/Product.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cx from 'classnames';
import { Link } from 'react-router-dom';
import { Card } from 'src/components/molecules/Card/Card';
import { Link } from 'src/components/molecules/Link/Link';

import style from './Product.scss';

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1404,10 +1404,10 @@
"@swc/counter" "^0.1.3"
jsonc-parser "^3.2.0"

"@swc/plugin-loadable-components@^1.0.11":
version "1.0.12"
resolved "https://registry.yarnpkg.com/@swc/plugin-loadable-components/-/plugin-loadable-components-1.0.12.tgz#35677396a81b999027f85f1f9f69988e4ff2b7c7"
integrity sha512-aZrPM64FndkqJJwETDbIOavnKsD+02NdjrWZqoFHvhyB/xq2v/807kEDrQCKFezgGxUDnz2odkEWcV3HhskDbw==
"@swc/plugin-loadable-components@^2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@swc/plugin-loadable-components/-/plugin-loadable-components-2.0.2.tgz#b568d63cf234b0ed9d209497a4944e177fe51ad2"
integrity sha512-3QcThRp6Eh00GPQnmBSDwSHUlDyN54/WyWukj62OQfRdbvrEJmrl5WGRJ3l0Ij67X3//Ilf1LzTWIkW5On+qLg==
dependencies:
"@swc/counter" "^0.1.3"

Expand Down

0 comments on commit 1f94c2d

Please sign in to comment.