react-router-dom-v5-compat 6.3.0 → 6.4.0-pre.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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # react-router-dom-v5-compat
2
+
3
+ ## 6.4.0-pre.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
@@ -0,0 +1,259 @@
1
+ /**
2
+ * Just copied over the link click test as a quick smoke test that it's working
3
+ * the same as v6 proper.
4
+ */
5
+ import * as React from "react";
6
+ import * as ReactDOM from "react-dom";
7
+ import { act } from "react-dom/test-utils";
8
+ import { MemoryRouter, Routes, Route, Link } from "../index";
9
+
10
+ function click(anchor: HTMLAnchorElement, eventInit?: MouseEventInit) {
11
+ let event = new MouseEvent("click", {
12
+ view: window,
13
+ bubbles: true,
14
+ cancelable: true,
15
+ ...eventInit,
16
+ });
17
+ anchor.dispatchEvent(event);
18
+ return event;
19
+ }
20
+
21
+ describe("A <Link> click", () => {
22
+ let node: HTMLDivElement;
23
+ beforeEach(() => {
24
+ node = document.createElement("div");
25
+ document.body.appendChild(node);
26
+ });
27
+
28
+ afterEach(() => {
29
+ document.body.removeChild(node);
30
+ node = null!;
31
+ });
32
+
33
+ it("navigates to the new page", () => {
34
+ function Home() {
35
+ return (
36
+ <div>
37
+ <h1>Home</h1>
38
+ <Link to="../about">About</Link>
39
+ </div>
40
+ );
41
+ }
42
+
43
+ act(() => {
44
+ ReactDOM.render(
45
+ <MemoryRouter initialEntries={["/home"]}>
46
+ <Routes>
47
+ <Route path="home" element={<Home />} />
48
+ <Route path="about" element={<h1>About</h1>} />
49
+ </Routes>
50
+ </MemoryRouter>,
51
+ node
52
+ );
53
+ });
54
+
55
+ let anchor = node.querySelector("a");
56
+ expect(anchor).not.toBeNull();
57
+
58
+ let event: MouseEvent;
59
+ act(() => {
60
+ event = click(anchor);
61
+ });
62
+
63
+ expect(event.defaultPrevented).toBe(true);
64
+ let h1 = node.querySelector("h1");
65
+ expect(h1).not.toBeNull();
66
+ expect(h1?.textContent).toEqual("About");
67
+ });
68
+
69
+ describe("when reloadDocument is specified", () => {
70
+ it("does not prevent default", () => {
71
+ function Home() {
72
+ return (
73
+ <div>
74
+ <h1>Home</h1>
75
+ <Link reloadDocument to="../about">
76
+ About
77
+ </Link>
78
+ </div>
79
+ );
80
+ }
81
+
82
+ act(() => {
83
+ ReactDOM.render(
84
+ <MemoryRouter initialEntries={["/home"]}>
85
+ <Routes>
86
+ <Route path="home" element={<Home />} />
87
+ <Route path="about" element={<h1>About</h1>} />
88
+ </Routes>
89
+ </MemoryRouter>,
90
+ node
91
+ );
92
+ });
93
+
94
+ let anchor = node.querySelector("a");
95
+ expect(anchor).not.toBeNull();
96
+
97
+ let event: MouseEvent;
98
+ act(() => {
99
+ event = click(anchor);
100
+ });
101
+
102
+ expect(event.defaultPrevented).toBe(false);
103
+ });
104
+ });
105
+
106
+ describe("when preventDefault is used on the click handler", () => {
107
+ it("stays on the same page", () => {
108
+ function Home() {
109
+ function handleClick(event: React.MouseEvent<HTMLAnchorElement>) {
110
+ event.preventDefault();
111
+ }
112
+
113
+ return (
114
+ <div>
115
+ <h1>Home</h1>
116
+ <Link to="../about" onClick={handleClick}>
117
+ About
118
+ </Link>
119
+ </div>
120
+ );
121
+ }
122
+
123
+ act(() => {
124
+ ReactDOM.render(
125
+ <MemoryRouter initialEntries={["/home"]}>
126
+ <Routes>
127
+ <Route path="home" element={<Home />} />
128
+ <Route path="about" element={<h1>About</h1>} />
129
+ </Routes>
130
+ </MemoryRouter>,
131
+ node
132
+ );
133
+ });
134
+
135
+ let anchor = node.querySelector("a");
136
+ expect(anchor).not.toBeNull();
137
+
138
+ act(() => {
139
+ click(anchor);
140
+ });
141
+
142
+ let h1 = node.querySelector("h1");
143
+ expect(h1).not.toBeNull();
144
+ expect(h1?.textContent).toEqual("Home");
145
+ });
146
+ });
147
+
148
+ describe("with a right click", () => {
149
+ it("stays on the same page", () => {
150
+ function Home() {
151
+ return (
152
+ <div>
153
+ <h1>Home</h1>
154
+ <Link to="../about">About</Link>
155
+ </div>
156
+ );
157
+ }
158
+
159
+ act(() => {
160
+ ReactDOM.render(
161
+ <MemoryRouter initialEntries={["/home"]}>
162
+ <Routes>
163
+ <Route path="home" element={<Home />} />
164
+ <Route path="about" element={<h1>About</h1>} />
165
+ </Routes>
166
+ </MemoryRouter>,
167
+ node
168
+ );
169
+ });
170
+
171
+ let anchor = node.querySelector("a");
172
+ expect(anchor).not.toBeNull();
173
+
174
+ act(() => {
175
+ // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
176
+ let RightMouseButton = 2;
177
+ click(anchor, { button: RightMouseButton });
178
+ });
179
+
180
+ let h1 = node.querySelector("h1");
181
+ expect(h1).not.toBeNull();
182
+ expect(h1?.textContent).toEqual("Home");
183
+ });
184
+ });
185
+
186
+ describe("when the link is supposed to open in a new window", () => {
187
+ it("stays on the same page", () => {
188
+ function Home() {
189
+ return (
190
+ <div>
191
+ <h1>Home</h1>
192
+ <Link to="../about" target="_blank">
193
+ About
194
+ </Link>
195
+ </div>
196
+ );
197
+ }
198
+
199
+ act(() => {
200
+ ReactDOM.render(
201
+ <MemoryRouter initialEntries={["/home"]}>
202
+ <Routes>
203
+ <Route path="home" element={<Home />} />
204
+ <Route path="about" element={<h1>About</h1>} />
205
+ </Routes>
206
+ </MemoryRouter>,
207
+ node
208
+ );
209
+ });
210
+
211
+ let anchor = node.querySelector("a");
212
+ expect(anchor).not.toBeNull();
213
+
214
+ act(() => {
215
+ click(anchor);
216
+ });
217
+
218
+ let h1 = node.querySelector("h1");
219
+ expect(h1).not.toBeNull();
220
+ expect(h1?.textContent).toEqual("Home");
221
+ });
222
+ });
223
+
224
+ describe("when the modifier keys are used", () => {
225
+ it("stays on the same page", () => {
226
+ function Home() {
227
+ return (
228
+ <div>
229
+ <h1>Home</h1>
230
+ <Link to="../about">About</Link>
231
+ </div>
232
+ );
233
+ }
234
+
235
+ act(() => {
236
+ ReactDOM.render(
237
+ <MemoryRouter initialEntries={["/home"]}>
238
+ <Routes>
239
+ <Route path="home" element={<Home />} />
240
+ <Route path="about" element={<h1>About</h1>} />
241
+ </Routes>
242
+ </MemoryRouter>,
243
+ node
244
+ );
245
+ });
246
+
247
+ let anchor = node.querySelector("a");
248
+ expect(anchor).not.toBeNull();
249
+
250
+ act(() => {
251
+ click(anchor, { ctrlKey: true });
252
+ });
253
+
254
+ let h1 = node.querySelector("h1");
255
+ expect(h1).not.toBeNull();
256
+ expect(h1?.textContent).toEqual("Home");
257
+ });
258
+ });
259
+ });
@@ -25,7 +25,7 @@
25
25
  * - TSC needs to generate the types, and it has to derive the output paths from
26
26
  * the import paths. If we have a weird require *outside of this package* to
27
27
  * "../../react-router-dom" it's going to generate types from the common root
28
- * of all module paths (Which makes sense becuase what else would it do? It
28
+ * of all module paths (Which makes sense because what else would it do? It
29
29
  * needs to write the type files next to the source files so that typescript
30
30
  * can resolve the types for tooling in the same location as the modules).
31
31
  * Because tsc isn't as flexible as rollup, we have no control over this
@@ -46,7 +46,69 @@
46
46
  * would break. We could stop doing two bundles in v6 "react-router-dom" and
47
47
  * deprecate the deep require if we wanted to avoid the duplication here.
48
48
  */
49
- export type { Hash, Location, Path, To, MemoryRouterProps, NavigateFunction, NavigateOptions, NavigateProps, Navigator, OutletProps, Params, PathMatch, RouteMatch, RouteObject, RouteProps, PathRouteProps, LayoutRouteProps, IndexRouteProps, RouterProps, Pathname, Search, RoutesProps, } from "./react-router-dom";
50
- export { BrowserRouter, HashRouter, Link, MemoryRouter, NavLink, Navigate, NavigationType, Outlet, Route, Router, Routes, UNSAFE_LocationContext, UNSAFE_NavigationContext, UNSAFE_RouteContext, createPath, createRoutesFromChildren, createSearchParams, generatePath, matchPath, matchRoutes, parsePath, renderMatches, resolvePath, unstable_HistoryRouter, useHref, useInRouterContext, useLinkClickHandler, useLocation, useMatch, useNavigate, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes, useSearchParams, } from "./react-router-dom";
49
+ export type {
50
+ Hash,
51
+ Location,
52
+ Path,
53
+ To,
54
+ MemoryRouterProps,
55
+ NavigateFunction,
56
+ NavigateOptions,
57
+ NavigateProps,
58
+ Navigator,
59
+ OutletProps,
60
+ Params,
61
+ PathMatch,
62
+ RouteMatch,
63
+ RouteObject,
64
+ RouteProps,
65
+ PathRouteProps,
66
+ LayoutRouteProps,
67
+ IndexRouteProps,
68
+ RouterProps,
69
+ Pathname,
70
+ Search,
71
+ RoutesProps,
72
+ } from "./react-router-dom";
73
+ export {
74
+ BrowserRouter,
75
+ HashRouter,
76
+ Link,
77
+ MemoryRouter,
78
+ NavLink,
79
+ Navigate,
80
+ NavigationType,
81
+ Outlet,
82
+ Route,
83
+ Router,
84
+ Routes,
85
+ UNSAFE_LocationContext,
86
+ UNSAFE_NavigationContext,
87
+ UNSAFE_RouteContext,
88
+ createPath,
89
+ createRoutesFromChildren,
90
+ createSearchParams,
91
+ generatePath,
92
+ matchPath,
93
+ matchRoutes,
94
+ parsePath,
95
+ renderMatches,
96
+ resolvePath,
97
+ unstable_HistoryRouter,
98
+ useHref,
99
+ useInRouterContext,
100
+ useLinkClickHandler,
101
+ useLocation,
102
+ useMatch,
103
+ useNavigate,
104
+ useNavigationType,
105
+ useOutlet,
106
+ useOutletContext,
107
+ useParams,
108
+ useResolvedPath,
109
+ useRoutes,
110
+ useSearchParams,
111
+ } from "./react-router-dom";
112
+
51
113
  export type { StaticRouterProps } from "./lib/components";
52
114
  export { CompatRouter, CompatRoute, StaticRouter } from "./lib/components";
@@ -0,0 +1,10 @@
1
+ const babelJest = require("babel-jest");
2
+
3
+ module.exports = babelJest.createTransformer({
4
+ presets: [
5
+ ["@babel/preset-env", { loose: true }],
6
+ "@babel/preset-react",
7
+ "@babel/preset-typescript",
8
+ ],
9
+ plugins: ["babel-plugin-dev-expression"],
10
+ });
package/jest.config.js ADDED
@@ -0,0 +1,9 @@
1
+ module.exports = {
2
+ testMatch: ["**/__tests__/*-test.[jt]s?(x)"],
3
+ transform: {
4
+ "\\.[jt]sx?$": "./jest-transformer.js",
5
+ },
6
+ globals: {
7
+ __DEV__: true,
8
+ },
9
+ };
@@ -0,0 +1,130 @@
1
+ import * as React from "react";
2
+ import type { Location, To } from "history";
3
+ import { Action, createPath, parsePath } from "history";
4
+
5
+ // Get useHistory from react-router-dom v5 (peer dep).
6
+ // @ts-expect-error
7
+ import { useHistory, Route as RouteV5 } from "react-router-dom";
8
+
9
+ // We are a wrapper around react-router-dom v6, so bring it in
10
+ // and bundle it because an app can't have two versions of
11
+ // react-router-dom in its package.json.
12
+ import { Router, Routes, Route } from "../react-router-dom";
13
+
14
+ // v5 isn't in TypeScript, they'll also lose the @types/react-router with this
15
+ // but not worried about that for now.
16
+ export function CompatRoute(props: any) {
17
+ let { path } = props;
18
+ if (!props.exact) path += "/*";
19
+ return (
20
+ <Routes>
21
+ <Route path={path} element={<RouteV5 {...props} />} />
22
+ </Routes>
23
+ );
24
+ }
25
+
26
+ export function CompatRouter({ children }: { children: React.ReactNode }) {
27
+ let history = useHistory();
28
+ let [state, setState] = React.useState(() => ({
29
+ location: history.location,
30
+ action: history.action,
31
+ }));
32
+
33
+ React.useLayoutEffect(() => {
34
+ history.listen((location: Location, action: Action) =>
35
+ setState({ location, action })
36
+ );
37
+ }, [history]);
38
+
39
+ return (
40
+ <Router
41
+ navigationType={state.action}
42
+ location={state.location}
43
+ navigator={history}
44
+ >
45
+ <Routes>
46
+ <Route path="*" element={children} />
47
+ </Routes>
48
+ </Router>
49
+ );
50
+ }
51
+
52
+ export interface StaticRouterProps {
53
+ basename?: string;
54
+ children?: React.ReactNode;
55
+ location: Partial<Location> | string;
56
+ }
57
+
58
+ /**
59
+ * A <Router> that may not navigate to any other location. This is useful
60
+ * on the server where there is no stateful UI.
61
+ */
62
+ export function StaticRouter({
63
+ basename,
64
+ children,
65
+ location: locationProp = "/",
66
+ }: StaticRouterProps) {
67
+ if (typeof locationProp === "string") {
68
+ locationProp = parsePath(locationProp);
69
+ }
70
+
71
+ let action = Action.Pop;
72
+ let location: Location = {
73
+ pathname: locationProp.pathname || "/",
74
+ search: locationProp.search || "",
75
+ hash: locationProp.hash || "",
76
+ state: locationProp.state || null,
77
+ key: locationProp.key || "default",
78
+ };
79
+
80
+ let staticNavigator = {
81
+ createHref(to: To) {
82
+ return typeof to === "string" ? to : createPath(to);
83
+ },
84
+ push(to: To) {
85
+ throw new Error(
86
+ `You cannot use navigator.push() on the server because it is a stateless ` +
87
+ `environment. This error was probably triggered when you did a ` +
88
+ `\`navigate(${JSON.stringify(to)})\` somewhere in your app.`
89
+ );
90
+ },
91
+ replace(to: To) {
92
+ throw new Error(
93
+ `You cannot use navigator.replace() on the server because it is a stateless ` +
94
+ `environment. This error was probably triggered when you did a ` +
95
+ `\`navigate(${JSON.stringify(to)}, { replace: true })\` somewhere ` +
96
+ `in your app.`
97
+ );
98
+ },
99
+ go(delta: number) {
100
+ throw new Error(
101
+ `You cannot use navigator.go() on the server because it is a stateless ` +
102
+ `environment. This error was probably triggered when you did a ` +
103
+ `\`navigate(${delta})\` somewhere in your app.`
104
+ );
105
+ },
106
+ back() {
107
+ throw new Error(
108
+ `You cannot use navigator.back() on the server because it is a stateless ` +
109
+ `environment.`
110
+ );
111
+ },
112
+ forward() {
113
+ throw new Error(
114
+ `You cannot use navigator.forward() on the server because it is a stateless ` +
115
+ `environment.`
116
+ );
117
+ },
118
+ };
119
+
120
+ return (
121
+ <Router
122
+ basename={basename}
123
+ children={children}
124
+ location={location}
125
+ navigationType={action}
126
+ navigator={staticNavigator}
127
+ static={true}
128
+ />
129
+ );
130
+ }
package/node-main.js ADDED
@@ -0,0 +1,7 @@
1
+ /* eslint-env node */
2
+
3
+ if (process.env.NODE_ENV === "production") {
4
+ module.exports = require("./umd/react-router-dom-v5-compat.production.min.js");
5
+ } else {
6
+ module.exports = require("./umd/react-router-dom-v5-compat.development.js");
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-router-dom-v5-compat",
3
- "version": "6.3.0",
3
+ "version": "6.4.0-pre.3",
4
4
  "author": "Remix Software <hello@remix.run>",
5
5
  "description": "Migration path to React Router v6 from v4/5",
6
6
  "repository": {
@@ -15,12 +15,12 @@
15
15
  "unpkg": "./umd/react-router.production.min.js",
16
16
  "dependencies": {
17
17
  "history": "^5.3.0",
18
- "react-router": "6.3.0"
18
+ "react-router": "6.4.0-pre.3"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "react": ">=16.8",
22
22
  "react-dom": ">=16.8",
23
- "react-router-dom": "4 || 5"
23
+ "react-router-dom": "6.4.0-pre.3"
24
24
  },
25
25
  "sideEffects": false,
26
26
  "keywords": [
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "files": ["index.ts"],
3
+ "compilerOptions": {
4
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
5
+ "target": "ES2020",
6
+ "module": "ES2020",
7
+ "moduleResolution": "node",
8
+
9
+ "strict": true,
10
+ "jsx": "react",
11
+
12
+ "declaration": true,
13
+ "emitDeclarationOnly": true,
14
+
15
+ "skipLibCheck": true,
16
+
17
+ "outDir": "../../build/node_modules/react-router-dom-v5-compat",
18
+ "rootDirs": ["."]
19
+ }
20
+ }
package/LICENSE.md DELETED
@@ -1,22 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) React Training 2015-2019
4
- Copyright (c) Remix Software 2020-2021
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining a copy
7
- of this software and associated documentation files (the "Software"), to deal
8
- in the Software without restriction, including without limitation the rights
9
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the Software is
11
- furnished to do so, subject to the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included in all
14
- copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- SOFTWARE.