@aminnairi/react-router 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +49 -0
  2. package/index.tsx +30 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -735,6 +735,40 @@ createPage({
735
735
  });
736
736
  ```
737
737
 
738
+ ### useLink
739
+
740
+ Allow you to navigate to another page using a JSX component instead of a callback as for the `useNavigateToPage` hook.
741
+
742
+ The created component is simply a `<a href="...">{children}</a>` under the hood which prevents the default behavior of the navigator which is to create a new HTTP request and to reload the page. The `href` attribute is computed from the page path and its parameters.
743
+
744
+ ```tsx
745
+ import { Fragment } from "react";
746
+ import { createPage, useLink } from "@aminnairi/react-router";
747
+
748
+ const user = createPath({
749
+ path: "/users/:user",
750
+ element: function User({ parameters: { user }}) {
751
+ return (
752
+ <h1>User#{user}</h1>
753
+ );
754
+ }
755
+ });
756
+
757
+ createPage({
758
+ path: "/about",
759
+ element: function About() {
760
+ const Link = useLink(user);
761
+
762
+ return (
763
+ <Fragment>
764
+ <h1>About</h1>
765
+ <Link parameters={{ user: "123" }}>User#123</Link>
766
+ </Fragment>
767
+ );
768
+ }
769
+ });
770
+ ```
771
+
738
772
  ### useSearch
739
773
 
740
774
  Allow you to get one or more search query from the URL.
@@ -976,11 +1010,26 @@ See [`LICENSE`](./LICENSE).
976
1010
 
977
1011
  ### Versions
978
1012
 
1013
+ - [`1.1.0`](#110)
979
1014
  - [`1.0.1`](#101)
980
1015
  - [`1.0.0`](#100)
981
1016
  - [`0.1.1`](#011)
982
1017
  - [`0.1.0`](#010)
983
1018
 
1019
+ ### 1.1.0
1020
+
1021
+ #### Major changes
1022
+
1023
+ None.
1024
+
1025
+ #### Minor changes
1026
+
1027
+ - Added a new `useLink` hook to create components that allow for navigating to another page
1028
+
1029
+ #### Bug & security fixes
1030
+
1031
+ None.
1032
+
984
1033
  ### 1.0.1
985
1034
 
986
1035
  #### Major changes
package/index.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import { useEffect, useState, FunctionComponent, useMemo, Component, PropsWithChildren, createContext, SetStateAction, Dispatch, ReactNode, useContext, useCallback } from "react";
1
+ import { useEffect, useState, FunctionComponent, useMemo, Component, PropsWithChildren, createContext, SetStateAction, Dispatch, ReactNode, useContext, useCallback, memo, MouseEvent } from "react";
2
2
 
3
3
  export type AbsolutePath<Path extends string> =
4
4
  Path extends `${infer Start}:${string}/${infer Rest}`
@@ -187,8 +187,6 @@ export const useNavigateToPage = <Path extends string>(page: Page<Path>) => {
187
187
  return path.replace(`:${parameterName}`, parameterValue);
188
188
  }, initialPath);
189
189
 
190
- console.log({ initialPath, pathWithParameters });
191
-
192
190
  if (replace) {
193
191
  window.history.replaceState(null, pathWithParameters, pathWithParameters);
194
192
  } else {
@@ -216,6 +214,35 @@ export const useHash = () => {
216
214
  return hash;
217
215
  };
218
216
 
217
+ export const useLink = <Path extends string>(page: Page<Path>) => {
218
+ const Link = memo(({ children, parameters }: { children: ReactNode, parameters: Parameters<Path> }) => {
219
+ const { prefix } = useContext(Context);
220
+ const navigateToPage = useNavigateToPage(page);
221
+
222
+ const pathWithParameters = useMemo(() => {
223
+ return Object.entries(parameters).reduce((previousPath, [parameterName, parameterValue]) => {
224
+ return previousPath.replace(`:${parameterName}`, parameterValue);
225
+ }, sanitizePath(`${prefix ?? ""}/${page.path}`));
226
+ }, []);
227
+
228
+ const navigate = useCallback((event: MouseEvent) => {
229
+ event.preventDefault();
230
+ navigateToPage(parameters);
231
+ }, []);
232
+
233
+ return (
234
+ <a
235
+ href={pathWithParameters}
236
+ onClick={navigate}>
237
+ {children}
238
+ </a>
239
+ );
240
+
241
+ });
242
+
243
+ return Link;
244
+ };
245
+
219
246
  export const createRouter = <Path extends string>({ pages, fallback, transition: withViewTransition, issue, prefix }: CreateRouterOptions<Path>) => {
220
247
  const Provider = ({ children }: ProviderProps) => {
221
248
  const [pathname, setPathname] = useState(sanitizePath(window.location.pathname));
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "type": "module",
3
3
  "name": "@aminnairi/react-router",
4
4
  "description": "Type-safe router for the React library",
5
- "version": "1.0.1",
5
+ "version": "1.1.0",
6
6
  "homepage": "https://github.com/aminnairi/react-router#readme",
7
7
  "license": "MIT",
8
8
  "bugs": {