@renr/parcel-rsc-router 1.0.0-alpha.2 → 1.0.0-alpha.3
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.
- package/README.md +212 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,214 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @renr/parcel-rsc-router
|
|
2
2
|
|
|
3
|
-
A router for
|
|
3
|
+
A client-side router designed for React Server Components (RSC) applications built with Parcel's `react-static` packager.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
⚠️ This package is an alpha / work in progress.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Client-side navigation:** Navigate between pages using the `<Link>` component.
|
|
10
|
+
- **Type safety:** Type-safe route definitions via `@renr/parcel-reporter-rsc-router` (the Link's `to` prop is typed).
|
|
11
|
+
- **Programmatic navigation:** Control navigation programmatically using the `useRouter` hook.
|
|
12
|
+
- **Location access:** Access the current URL and its properties with the `useLocation` hook.
|
|
13
|
+
- **Route data access:** Retrieve data associated with the current route using the `useCurrentRoute` hook.
|
|
14
|
+
- **Automatic scroll restoration:** Remembers and restores scroll positions when navigating back and forth.
|
|
15
|
+
- **Hash scrolling:** Automatically scrolls to elements identified by URL hash.
|
|
16
|
+
- **RSC integration:** Navigations fetch the rsc payload for the target route and update the UI without a full page reload.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
For the initial Parcel static RSC site setup see https://parceljs.org/recipes/rsc/#static-rendering, or check out the example app [here](https://github.com/renrizzolo/parcel-rsc-react/tree/main/apps/my-static-site).
|
|
21
|
+
|
|
22
|
+
Install the package using your preferred package manager:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm add @renr/parcel-rsc-router
|
|
26
|
+
# or
|
|
27
|
+
npm install @renr/parcel-rsc-router
|
|
28
|
+
# or
|
|
29
|
+
yarn add @renr/parcel-rsc-router
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Install the required peer dependency:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pnpm add @renr/parcel-reporter-rsc-router
|
|
36
|
+
# or
|
|
37
|
+
npm install @renr/parcel-reporter-rsc-router
|
|
38
|
+
# or
|
|
39
|
+
yarn add @renr/parcel-reporter-rsc-router
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### 1. Wrapping your Application
|
|
45
|
+
|
|
46
|
+
Wrap your main application component with the `RouterProvider` and pass your routes. This component should be a client component.
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
// src/appProvider.tsx (example)
|
|
50
|
+
"use client";
|
|
51
|
+
import { RouterProvider } from "@renr/parcel-rsc-router";
|
|
52
|
+
import { flatRoutes } from "./routes";
|
|
53
|
+
|
|
54
|
+
export function AppProvider({ children }: { children: React.ReactNode }) {
|
|
55
|
+
return <RouterProvider routes={flatRoutes}>{children}</RouterProvider>;
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 2. Navigation
|
|
60
|
+
|
|
61
|
+
#### Declarative Navigation with `<Link>`
|
|
62
|
+
|
|
63
|
+
Use the `<Link>` component for client-side navigation. It behaves like a standard `<a>` tag but intercepts clicks to use the router's navigation logic.
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
import { Link } from "@renr/parcel-rsc-router";
|
|
67
|
+
|
|
68
|
+
function NavigationMenu() {
|
|
69
|
+
return (
|
|
70
|
+
<nav>
|
|
71
|
+
<Link to="/">Home</Link>
|
|
72
|
+
<Link to="/about">About</Link>
|
|
73
|
+
<Link to="/blog/first-post" className="nav-item">
|
|
74
|
+
First Blog Post
|
|
75
|
+
</Link>
|
|
76
|
+
<Link to="/contact" hash="form-section">
|
|
77
|
+
Contact (scroll to form)
|
|
78
|
+
</Link>
|
|
79
|
+
<Link to="/products" search={{ category: "electronics" }}>
|
|
80
|
+
Electronics
|
|
81
|
+
</Link>
|
|
82
|
+
</nav>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Programmatic Navigation with `useRouter`
|
|
88
|
+
|
|
89
|
+
For more complex navigation scenarios, use the `useRouter` hook to get access to the `navigate` function.
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
import { useRouter } from "@renr/parcel-rsc-router";
|
|
93
|
+
|
|
94
|
+
function LoginForm() {
|
|
95
|
+
const { navigate } = useRouter();
|
|
96
|
+
|
|
97
|
+
const handleSubmit = (event: React.FormEvent) => {
|
|
98
|
+
event.preventDefault();
|
|
99
|
+
// ... form submission logic
|
|
100
|
+
navigate("/dashboard"); // Navigate after successful login
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<form onSubmit={handleSubmit}>
|
|
105
|
+
{/* ... form fields */}
|
|
106
|
+
<button type="submit">Login</button>
|
|
107
|
+
</form>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 4. Accessing Route Information
|
|
113
|
+
|
|
114
|
+
#### `useLocation()`
|
|
115
|
+
|
|
116
|
+
The `useLocation` hook provides access to the browser's `Location` object, giving you details about the current URL.
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
import { useLocation } from "@renr/parcel-rsc-router";
|
|
120
|
+
|
|
121
|
+
function CurrentPathDisplay() {
|
|
122
|
+
const location = useLocation();
|
|
123
|
+
return (
|
|
124
|
+
<div>
|
|
125
|
+
<p>Current Pathname: {location.pathname}</p>
|
|
126
|
+
<p>Current Search Params: {location.search}</p>
|
|
127
|
+
<p>Current Hash: {location.hash}</p>
|
|
128
|
+
</div>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### `useCurrentRoute()`
|
|
134
|
+
|
|
135
|
+
The `useCurrentRoute` hook returns the `RouteData` object for the currently matched route, allowing you to access any custom data associated with it.
|
|
136
|
+
|
|
137
|
+
```tsx
|
|
138
|
+
import { useCurrentRoute } from "@renr/parcel-rsc-router";
|
|
139
|
+
|
|
140
|
+
function PageTitle() {
|
|
141
|
+
const currentRoute = useCurrentRoute();
|
|
142
|
+
const title = currentRoute?.data?.title || "Page Not Found";
|
|
143
|
+
|
|
144
|
+
return <h1>{title}</h1>;
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## API Reference
|
|
149
|
+
|
|
150
|
+
### Components
|
|
151
|
+
|
|
152
|
+
#### `RouterProvider`
|
|
153
|
+
|
|
154
|
+
The main component that provides the routing context to your application.
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
<RouterProvider routes={RouteData[]} children={React.ReactNode} />
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
- `routes`: An array of `RouteData` objects defining your application's routes.
|
|
161
|
+
- `children`: The React nodes to be rendered within the router's context.
|
|
162
|
+
|
|
163
|
+
#### `Link`
|
|
164
|
+
|
|
165
|
+
A component for declarative client-side navigation.
|
|
166
|
+
|
|
167
|
+
```tsx
|
|
168
|
+
<Link
|
|
169
|
+
to={RoutePath}
|
|
170
|
+
hash?: string
|
|
171
|
+
search?: Record<string, string>
|
|
172
|
+
children={React.ReactNode}
|
|
173
|
+
onClick?: (e: React.MouseEvent<HTMLAnchorElement>) => void
|
|
174
|
+
target?: HTMLAttributeAnchorTarget
|
|
175
|
+
{...restProps: React.AnchorHTMLAttributes<HTMLAnchorElement>}
|
|
176
|
+
/>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
- `to`: The target path for the link (e.g., `"/dashboard"`).
|
|
180
|
+
- `hash`: Optional hash segment for the URL (e.g., `"form-section"`).
|
|
181
|
+
- `search`: Optional object of query parameters (e.g., `{ category: "electronics" }`).
|
|
182
|
+
- `children`: The content to render inside the link.
|
|
183
|
+
- `onClick`: Optional click handler.
|
|
184
|
+
- `target`: Standard `target` attribute for `<a>` tags (e.g., `"_blank"`).
|
|
185
|
+
- `{...restProps}`: Any other standard HTML `<a>` tag attributes.
|
|
186
|
+
|
|
187
|
+
### Hooks
|
|
188
|
+
|
|
189
|
+
#### `useRouter()`
|
|
190
|
+
|
|
191
|
+
Returns an object with methods for programmatic navigation.
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
const { navigate } = useRouter();
|
|
195
|
+
navigate(to: string); // Navigates to the specified path.
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### `useLocation()`
|
|
199
|
+
|
|
200
|
+
Returns the browser's `Location` object for the current URL.
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
const location: Location = useLocation();
|
|
204
|
+
// location.pathname, location.search, location.hash, etc.
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### `useCurrentRoute()`
|
|
208
|
+
|
|
209
|
+
Returns the `RouteData` object for the currently matched route.
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
const currentRoute: RouteData | undefined = useCurrentRoute();
|
|
213
|
+
// currentRoute?.path, currentRoute?.data, etc.
|
|
214
|
+
```
|