@shuvi/router-react 0.0.1-rc.9 → 1.0.0-rc.10
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/esm/Link.d.ts +35 -0
- package/esm/Link.js +91 -0
- package/esm/MemoryRouter.d.ts +22 -0
- package/esm/MemoryRouter.js +37 -0
- package/esm/Router.d.ts +19 -0
- package/esm/Router.js +39 -0
- package/esm/RouterView.d.ts +5 -0
- package/esm/RouterView.js +40 -0
- package/esm/constants.d.ts +1 -0
- package/esm/constants.js +1 -0
- package/esm/contexts.d.ts +14 -0
- package/esm/contexts.js +20 -0
- package/esm/hooks.d.ts +54 -0
- package/esm/hooks.js +116 -0
- package/esm/index.d.ts +9 -0
- package/esm/index.js +9 -0
- package/esm/types.d.ts +47 -0
- package/esm/types.js +1 -0
- package/esm/utils.d.ts +9 -0
- package/esm/utils.js +42 -0
- package/lib/Link.d.ts +25 -1
- package/lib/Link.js +45 -28
- package/lib/MemoryRouter.d.ts +3 -3
- package/lib/MemoryRouter.js +20 -22
- package/lib/Router.d.ts +2 -2
- package/lib/Router.js +16 -28
- package/lib/RouterView.d.ts +1 -1
- package/lib/RouterView.js +22 -21
- package/lib/constants.js +1 -0
- package/lib/contexts.d.ts +11 -3
- package/lib/contexts.js +7 -9
- package/lib/hooks.d.ts +15 -2
- package/lib/hooks.js +34 -30
- package/lib/index.d.ts +4 -2
- package/lib/index.js +25 -9
- package/lib/types.d.ts +6 -12
- package/lib/utils.d.ts +0 -2
- package/lib/utils.js +5 -13
- package/package.json +21 -13
package/lib/Link.d.ts
CHANGED
|
@@ -1,11 +1,35 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { State, PathRecord } from '@shuvi/router';
|
|
3
3
|
/**
|
|
4
|
-
* The public API for rendering a history-aware
|
|
4
|
+
* The public API for rendering a history-aware `<a>`.
|
|
5
|
+
* ```ts
|
|
6
|
+
* // jump to `/about`
|
|
7
|
+
* <Link to="/about">About</Link>
|
|
8
|
+
* // jump with query
|
|
9
|
+
* <Link to="/about?sort=name">About</Link>
|
|
10
|
+
* // with some state
|
|
11
|
+
* <Link to="/about" state={{fromDashboard: true}}>About</Link>
|
|
12
|
+
* // props `to` could be a object
|
|
13
|
+
* <Link to={{
|
|
14
|
+
* pathname: "/about",
|
|
15
|
+
* search: "?sort=name",
|
|
16
|
+
* hash: "#the-hash",
|
|
17
|
+
* }}>About</Link>
|
|
18
|
+
* // props target '_self' | '_blank', default is '_self'
|
|
19
|
+
* <Link to="/about" target="_self">About</Link>
|
|
20
|
+
* // overrides default redirect mode by `replace`
|
|
21
|
+
* <Link to="/about" replace>About</Link>
|
|
22
|
+
* // if `onClick` function, run it first
|
|
23
|
+
* <Link to="/about" onClick={fn}>About</Link>
|
|
24
|
+
* // other props will be delivered to `<a>`
|
|
25
|
+
* <Link to="/about" a='a' b='b'>About</Link> => <{...rest} a>
|
|
26
|
+
* ```
|
|
5
27
|
*/
|
|
6
28
|
export declare const Link: React.ForwardRefExoticComponent<LinkProps & React.RefAttributes<HTMLAnchorElement>>;
|
|
7
29
|
export interface LinkProps extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
|
|
8
30
|
replace?: boolean;
|
|
9
31
|
state?: State;
|
|
10
32
|
to: PathRecord;
|
|
33
|
+
prefetch?: boolean;
|
|
34
|
+
onMouseEnter?: (e: any) => void;
|
|
11
35
|
}
|
package/lib/Link.js
CHANGED
|
@@ -10,19 +10,10 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
10
10
|
}
|
|
11
11
|
return t;
|
|
12
12
|
};
|
|
13
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
14
|
-
if (mod && mod.__esModule) return mod;
|
|
15
|
-
var result = {};
|
|
16
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
17
|
-
result["default"] = mod;
|
|
18
|
-
return result;
|
|
19
|
-
};
|
|
20
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
21
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
22
|
-
};
|
|
23
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
|
|
25
|
-
const
|
|
14
|
+
exports.Link = void 0;
|
|
15
|
+
const React = require("react");
|
|
16
|
+
const PropTypes = require("prop-types");
|
|
26
17
|
const _1 = require(".");
|
|
27
18
|
const router_1 = require("@shuvi/router");
|
|
28
19
|
const constants_1 = require("./constants");
|
|
@@ -31,14 +22,36 @@ function isModifiedEvent(event) {
|
|
|
31
22
|
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
|
|
32
23
|
}
|
|
33
24
|
/**
|
|
34
|
-
* The public API for rendering a history-aware
|
|
25
|
+
* The public API for rendering a history-aware `<a>`.
|
|
26
|
+
* ```ts
|
|
27
|
+
* // jump to `/about`
|
|
28
|
+
* <Link to="/about">About</Link>
|
|
29
|
+
* // jump with query
|
|
30
|
+
* <Link to="/about?sort=name">About</Link>
|
|
31
|
+
* // with some state
|
|
32
|
+
* <Link to="/about" state={{fromDashboard: true}}>About</Link>
|
|
33
|
+
* // props `to` could be a object
|
|
34
|
+
* <Link to={{
|
|
35
|
+
* pathname: "/about",
|
|
36
|
+
* search: "?sort=name",
|
|
37
|
+
* hash: "#the-hash",
|
|
38
|
+
* }}>About</Link>
|
|
39
|
+
* // props target '_self' | '_blank', default is '_self'
|
|
40
|
+
* <Link to="/about" target="_self">About</Link>
|
|
41
|
+
* // overrides default redirect mode by `replace`
|
|
42
|
+
* <Link to="/about" replace>About</Link>
|
|
43
|
+
* // if `onClick` function, run it first
|
|
44
|
+
* <Link to="/about" onClick={fn}>About</Link>
|
|
45
|
+
* // other props will be delivered to `<a>`
|
|
46
|
+
* <Link to="/about" a='a' b='b'>About</Link> => <{...rest} a>
|
|
47
|
+
* ```
|
|
35
48
|
*/
|
|
36
49
|
exports.Link = React.forwardRef(function LinkWithRef(_a, ref) {
|
|
37
50
|
var { onClick, replace: replaceProp = false, state, target, to } = _a, rest = __rest(_a, ["onClick", "replace", "state", "target", "to"]);
|
|
38
|
-
let href = _1.useHref(to);
|
|
39
|
-
let navigate = _1.useNavigate();
|
|
40
|
-
const location = hooks_1.useCurrentRoute();
|
|
41
|
-
let path = _1.useResolvedPath(to);
|
|
51
|
+
let href = (0, _1.useHref)(to);
|
|
52
|
+
let navigate = (0, _1.useNavigate)();
|
|
53
|
+
const location = (0, hooks_1.useCurrentRoute)();
|
|
54
|
+
let path = (0, _1.useResolvedPath)(to);
|
|
42
55
|
function handleClick(event) {
|
|
43
56
|
if (onClick)
|
|
44
57
|
onClick(event);
|
|
@@ -50,7 +63,8 @@ exports.Link = React.forwardRef(function LinkWithRef(_a, ref) {
|
|
|
50
63
|
event.preventDefault();
|
|
51
64
|
// If the URL hasn't changed, a regular <a> will do a replace instead of
|
|
52
65
|
// a push, so do the same here.
|
|
53
|
-
let replace = !!replaceProp ||
|
|
66
|
+
let replace = !!replaceProp ||
|
|
67
|
+
(location && (0, router_1.pathToString)(location)) === (0, router_1.pathToString)(path);
|
|
54
68
|
navigate(to, { replace, state });
|
|
55
69
|
}
|
|
56
70
|
}
|
|
@@ -61,16 +75,19 @@ exports.Link = React.forwardRef(function LinkWithRef(_a, ref) {
|
|
|
61
75
|
if (constants_1.__DEV__) {
|
|
62
76
|
exports.Link.displayName = 'Link';
|
|
63
77
|
exports.Link.propTypes = {
|
|
64
|
-
onClick:
|
|
65
|
-
replace:
|
|
66
|
-
state:
|
|
67
|
-
target:
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
78
|
+
onClick: PropTypes.func,
|
|
79
|
+
replace: PropTypes.bool,
|
|
80
|
+
state: PropTypes.object,
|
|
81
|
+
target: PropTypes.string,
|
|
82
|
+
prefetch: PropTypes.bool,
|
|
83
|
+
onMouseEnter: PropTypes.func,
|
|
84
|
+
// @ts-ignore proptypes's bug?
|
|
85
|
+
to: PropTypes.oneOfType([
|
|
86
|
+
PropTypes.string,
|
|
87
|
+
PropTypes.shape({
|
|
88
|
+
pathname: PropTypes.string,
|
|
89
|
+
search: PropTypes.string,
|
|
90
|
+
hash: PropTypes.string
|
|
74
91
|
})
|
|
75
92
|
]).isRequired
|
|
76
93
|
};
|
package/lib/MemoryRouter.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import PropTypes from 'prop-types';
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import * as PropTypes from 'prop-types';
|
|
3
3
|
import { IMemoryRouterProps } from './types';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* a <Router> that stores all entries in memory.
|
|
6
6
|
*/
|
|
7
7
|
export declare function MemoryRouter({ basename, children, routes, initialEntries, initialIndex }: IMemoryRouterProps): React.ReactElement;
|
|
8
8
|
export declare namespace MemoryRouter {
|
package/lib/MemoryRouter.js
CHANGED
|
@@ -1,43 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
const
|
|
3
|
+
exports.MemoryRouter = void 0;
|
|
4
|
+
const React = require("react");
|
|
5
|
+
const PropTypes = require("prop-types");
|
|
8
6
|
const router_1 = require("@shuvi/router");
|
|
9
7
|
const Router_1 = require("./Router");
|
|
10
8
|
const constants_1 = require("./constants");
|
|
11
9
|
/**
|
|
12
|
-
*
|
|
10
|
+
* a <Router> that stores all entries in memory.
|
|
13
11
|
*/
|
|
14
12
|
function MemoryRouter({ basename, children, routes, initialEntries, initialIndex }) {
|
|
15
|
-
let routerRef =
|
|
13
|
+
let routerRef = React.useRef();
|
|
16
14
|
if (routerRef.current == null) {
|
|
17
|
-
routerRef.current = router_1.createRouter({
|
|
15
|
+
routerRef.current = (0, router_1.createRouter)({
|
|
18
16
|
basename,
|
|
19
17
|
routes: routes || [],
|
|
20
|
-
history: router_1.createMemoryHistory({ initialEntries, initialIndex })
|
|
21
|
-
});
|
|
18
|
+
history: (0, router_1.createMemoryHistory)({ initialEntries, initialIndex })
|
|
19
|
+
}).init();
|
|
22
20
|
}
|
|
23
|
-
return
|
|
21
|
+
return React.createElement(Router_1.Router, { children: children, router: routerRef.current });
|
|
24
22
|
}
|
|
25
23
|
exports.MemoryRouter = MemoryRouter;
|
|
26
24
|
if (constants_1.__DEV__) {
|
|
27
25
|
MemoryRouter.displayName = 'MemoryRouter';
|
|
28
26
|
MemoryRouter.propTypes = {
|
|
29
|
-
children:
|
|
30
|
-
routes:
|
|
31
|
-
initialEntries:
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
pathname:
|
|
35
|
-
search:
|
|
36
|
-
hash:
|
|
37
|
-
state:
|
|
38
|
-
key:
|
|
27
|
+
children: PropTypes.node,
|
|
28
|
+
routes: PropTypes.arrayOf(PropTypes.object),
|
|
29
|
+
initialEntries: PropTypes.arrayOf(PropTypes.oneOfType([
|
|
30
|
+
PropTypes.string,
|
|
31
|
+
PropTypes.shape({
|
|
32
|
+
pathname: PropTypes.string,
|
|
33
|
+
search: PropTypes.string,
|
|
34
|
+
hash: PropTypes.string,
|
|
35
|
+
state: PropTypes.object,
|
|
36
|
+
key: PropTypes.string
|
|
39
37
|
})
|
|
40
38
|
])),
|
|
41
|
-
initialIndex:
|
|
39
|
+
initialIndex: PropTypes.number
|
|
42
40
|
};
|
|
43
41
|
}
|
package/lib/Router.d.ts
CHANGED
package/lib/Router.js
CHANGED
|
@@ -1,21 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
|
-
if (mod && mod.__esModule) return mod;
|
|
4
|
-
var result = {};
|
|
5
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
6
|
-
result["default"] = mod;
|
|
7
|
-
return result;
|
|
8
|
-
};
|
|
9
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
-
};
|
|
12
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
|
|
14
|
-
const
|
|
3
|
+
exports.Router = void 0;
|
|
4
|
+
const React = require("react");
|
|
5
|
+
const PropTypes = require("prop-types");
|
|
6
|
+
const invariant_1 = require("@shuvi/utils/lib/invariant");
|
|
7
|
+
const shim_1 = require("use-sync-external-store/shim");
|
|
15
8
|
const contexts_1 = require("./contexts");
|
|
16
9
|
const hooks_1 = require("./hooks");
|
|
17
10
|
const constants_1 = require("./constants");
|
|
18
|
-
const utils_1 = require("./utils");
|
|
19
11
|
/**
|
|
20
12
|
* Provides location context for the rest of the app.
|
|
21
13
|
*
|
|
@@ -24,32 +16,28 @@ const utils_1 = require("./utils");
|
|
|
24
16
|
* in web browsers or a <StaticRouter> for server rendering.
|
|
25
17
|
*/
|
|
26
18
|
function Router({ children = null, static: staticProp = false, router }) {
|
|
27
|
-
|
|
19
|
+
(0, invariant_1.default)(!(0, hooks_1.useInRouterContext)(), `You cannot render a <Router> inside another <Router>.` +
|
|
28
20
|
` You never need more than one.`);
|
|
29
|
-
const contextVal =
|
|
21
|
+
const contextVal = React.useMemo(() => {
|
|
30
22
|
return {
|
|
31
23
|
static: staticProp,
|
|
32
24
|
router: router
|
|
33
25
|
};
|
|
34
26
|
}, [staticProp, router]);
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
utils_1.useIsomorphicEffect(() => router.listen(() => {
|
|
39
|
-
if (unmount.current) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
forceupdate();
|
|
27
|
+
const { subscribe, getSnapshot } = React.useMemo(() => ({
|
|
28
|
+
subscribe: (fn) => router.listen(fn),
|
|
29
|
+
getSnapshot: () => router.current
|
|
43
30
|
}), [router]);
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
const current = (0, shim_1.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
|
|
32
|
+
return (React.createElement(contexts_1.RouterContext.Provider, { value: contextVal },
|
|
33
|
+
React.createElement(contexts_1.RouteContext.Provider, { children: children, value: current })));
|
|
46
34
|
}
|
|
47
35
|
exports.Router = Router;
|
|
48
36
|
if (constants_1.__DEV__) {
|
|
49
37
|
Router.displayName = 'Router';
|
|
50
38
|
Router.propTypes = {
|
|
51
|
-
children:
|
|
52
|
-
router:
|
|
53
|
-
static:
|
|
39
|
+
children: PropTypes.node,
|
|
40
|
+
router: PropTypes.object,
|
|
41
|
+
static: PropTypes.bool
|
|
54
42
|
};
|
|
55
43
|
}
|
package/lib/RouterView.d.ts
CHANGED
package/lib/RouterView.js
CHANGED
|
@@ -1,41 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
const
|
|
3
|
+
exports.RouterView = void 0;
|
|
4
|
+
const React = require("react");
|
|
5
|
+
const router_1 = require("@shuvi/router");
|
|
8
6
|
const hooks_1 = require("./hooks");
|
|
9
7
|
const constants_1 = require("./constants");
|
|
10
8
|
const contexts_1 = require("./contexts");
|
|
11
|
-
const
|
|
12
|
-
const defaultElement =
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
const defaultElement = React.createElement(RouterView, null);
|
|
11
|
+
function MatchedRoute({ match, depth, parentPathname, parentParams }) {
|
|
12
|
+
const { route, params, pathname } = match;
|
|
13
|
+
const element = React.useMemo(() => route.component
|
|
14
|
+
? React.createElement(route.component, route.props)
|
|
15
|
+
: defaultElement, [route.component, route.props, defaultElement]);
|
|
16
|
+
return (React.createElement(contexts_1.MatchedRouteContext.Provider, { children: element, value: {
|
|
17
|
+
depth: depth + 1,
|
|
18
|
+
params: (0, utils_1.readOnly)(Object.assign(Object.assign({}, parentParams), params)),
|
|
19
|
+
pathname: (0, router_1.joinPaths)([parentPathname, pathname]),
|
|
20
|
+
route
|
|
21
|
+
} }));
|
|
22
|
+
}
|
|
13
23
|
function RouterView() {
|
|
14
|
-
let { depth, pathname: parentPathname, params: parentParams } =
|
|
15
|
-
const { matches } = hooks_1.useCurrentRoute();
|
|
16
|
-
if (!matches) {
|
|
24
|
+
let { depth, pathname: parentPathname, params: parentParams } = React.useContext(contexts_1.MatchedRouteContext);
|
|
25
|
+
const { matches } = (0, hooks_1.useCurrentRoute)();
|
|
26
|
+
if (!matches.length) {
|
|
17
27
|
return null;
|
|
18
28
|
}
|
|
19
29
|
// Otherwise render an element.
|
|
20
30
|
const matched = matches[depth];
|
|
21
31
|
if (!matched) {
|
|
22
32
|
if (constants_1.__DEV__) {
|
|
23
|
-
|
|
33
|
+
(0, utils_1.warningOnce)(parentPathname, false, `Use <RouterView/> under path "${parentPathname}", but it has no children routes.` +
|
|
24
34
|
`\n\n` +
|
|
25
35
|
`Please remove the <RouterView/>.`);
|
|
26
36
|
}
|
|
27
37
|
return null;
|
|
28
38
|
}
|
|
29
|
-
|
|
30
|
-
const element = route.component
|
|
31
|
-
? react_1.default.createElement(route.component, route.props)
|
|
32
|
-
: defaultElement;
|
|
33
|
-
return (react_1.default.createElement(contexts_1.MactedRouteContext.Provider, { children: element, value: {
|
|
34
|
-
depth: depth + 1,
|
|
35
|
-
params: utils_2.readOnly(Object.assign(Object.assign({}, parentParams), params)),
|
|
36
|
-
pathname: utils_1.joinPaths([parentPathname, pathname]),
|
|
37
|
-
route
|
|
38
|
-
} }));
|
|
39
|
+
return (React.createElement(MatchedRoute, { match: matched, depth: depth, parentPathname: parentPathname, parentParams: parentParams }));
|
|
39
40
|
}
|
|
40
41
|
exports.RouterView = RouterView;
|
|
41
42
|
if (constants_1.__DEV__) {
|
package/lib/constants.js
CHANGED
package/lib/contexts.d.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import * as React from 'react';
|
|
2
2
|
import { IRoute } from '@shuvi/router';
|
|
3
3
|
import { IRouterContextObject, IRouteContextObject } from './types';
|
|
4
4
|
export declare const RouterContext: React.Context<IRouterContextObject>;
|
|
5
|
-
export declare const RouteContext: React.Context<IRoute<
|
|
6
|
-
|
|
5
|
+
export declare const RouteContext: React.Context<IRoute<{
|
|
6
|
+
caseSensitive?: boolean | undefined;
|
|
7
|
+
children?: any[] | undefined;
|
|
8
|
+
component?: any;
|
|
9
|
+
redirect?: string | undefined;
|
|
10
|
+
props?: import("@shuvi/router").IRouteComponentProps | undefined;
|
|
11
|
+
path: string;
|
|
12
|
+
filepath?: string | undefined;
|
|
13
|
+
}>>;
|
|
14
|
+
export declare const MatchedRouteContext: React.Context<IRouteContextObject<{}>>;
|
package/lib/contexts.js
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
3
|
+
exports.MatchedRouteContext = exports.RouteContext = exports.RouterContext = void 0;
|
|
4
|
+
const React = require("react");
|
|
7
5
|
const utils_1 = require("./utils");
|
|
8
6
|
const constants_1 = require("./constants");
|
|
9
|
-
exports.RouterContext =
|
|
7
|
+
exports.RouterContext = React.createContext(null);
|
|
10
8
|
if (constants_1.__DEV__) {
|
|
11
9
|
exports.RouterContext.displayName = 'Router';
|
|
12
10
|
}
|
|
13
|
-
exports.RouteContext =
|
|
11
|
+
exports.RouteContext = React.createContext(null);
|
|
14
12
|
if (constants_1.__DEV__) {
|
|
15
13
|
exports.RouterContext.displayName = 'Route';
|
|
16
14
|
}
|
|
17
|
-
exports.
|
|
15
|
+
exports.MatchedRouteContext = React.createContext({
|
|
18
16
|
depth: 0,
|
|
19
|
-
params: utils_1.readOnly({}),
|
|
17
|
+
params: (0, utils_1.readOnly)({}),
|
|
20
18
|
pathname: '',
|
|
21
19
|
route: null
|
|
22
20
|
});
|
|
23
21
|
if (constants_1.__DEV__) {
|
|
24
|
-
exports.
|
|
22
|
+
exports.MatchedRouteContext.displayName = 'MatchedRoute';
|
|
25
23
|
}
|
package/lib/hooks.d.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { IParams, IPathMatch, Blocker, Path, PathRecord, IRouter, IPathPattern } from '@shuvi/router';
|
|
2
|
-
import { INavigateFunction } from './types';
|
|
3
|
-
|
|
2
|
+
import { INavigateFunction, IRouteContextObject } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* is just point `router.current` object
|
|
5
|
+
*/
|
|
6
|
+
export declare function useCurrentRoute(): import("@shuvi/router").IRoute<{
|
|
7
|
+
caseSensitive?: boolean | undefined;
|
|
8
|
+
children?: any[] | undefined;
|
|
9
|
+
component?: any;
|
|
10
|
+
redirect?: string | undefined;
|
|
11
|
+
props?: import("@shuvi/router").IRouteComponentProps | undefined;
|
|
12
|
+
path: string;
|
|
13
|
+
filepath?: string | undefined;
|
|
14
|
+
}>;
|
|
4
15
|
/**
|
|
5
16
|
* Blocks all navigation attempts. This is useful for preventing the page from
|
|
6
17
|
* changing until some condition is met, like saving form data.
|
|
@@ -37,5 +48,7 @@ export declare function useParams(): IParams;
|
|
|
37
48
|
export declare function useResolvedPath(to: PathRecord): Path;
|
|
38
49
|
/**
|
|
39
50
|
* Returns the current router object
|
|
51
|
+
* two parts, one is router behavior(browser history mode, hash history mode), another is router content
|
|
40
52
|
*/
|
|
41
53
|
export declare function useRouter(): IRouter;
|
|
54
|
+
export declare function useMatchedRoute<ExtendedTypes = {}>(): IRouteContextObject<ExtendedTypes>;
|
package/lib/hooks.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
|
-
if (mod && mod.__esModule) return mod;
|
|
4
|
-
var result = {};
|
|
5
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
6
|
-
result["default"] = mod;
|
|
7
|
-
return result;
|
|
8
|
-
};
|
|
9
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
|
|
3
|
+
exports.useMatchedRoute = exports.useRouter = exports.useResolvedPath = exports.useParams = exports.useNavigate = exports.useMatch = exports.useInRouterContext = exports.useHref = exports.useBlocker = exports.useCurrentRoute = void 0;
|
|
4
|
+
const React = require("react");
|
|
5
|
+
const react_1 = require("react");
|
|
11
6
|
const router_1 = require("@shuvi/router");
|
|
7
|
+
const invariant_1 = require("@shuvi/utils/lib/invariant");
|
|
12
8
|
const contexts_1 = require("./contexts");
|
|
13
9
|
const utils_1 = require("./utils");
|
|
10
|
+
/**
|
|
11
|
+
* is just point `router.current` object
|
|
12
|
+
*/
|
|
14
13
|
function useCurrentRoute() {
|
|
15
|
-
return react_1.useContext(contexts_1.RouteContext);
|
|
14
|
+
return (0, react_1.useContext)(contexts_1.RouteContext);
|
|
16
15
|
}
|
|
17
16
|
exports.useCurrentRoute = useCurrentRoute;
|
|
18
17
|
/**
|
|
@@ -20,9 +19,9 @@ exports.useCurrentRoute = useCurrentRoute;
|
|
|
20
19
|
* changing until some condition is met, like saving form data.
|
|
21
20
|
*/
|
|
22
21
|
function useBlocker(blocker, when = true) {
|
|
23
|
-
|
|
24
|
-
const { router } = react_1.useContext(contexts_1.RouterContext);
|
|
25
|
-
|
|
22
|
+
(0, invariant_1.default)(useInRouterContext(), `useBlocker() may be used only in the context of a <Router> component.`);
|
|
23
|
+
const { router } = (0, react_1.useContext)(contexts_1.RouterContext);
|
|
24
|
+
React.useEffect(() => {
|
|
26
25
|
if (!when)
|
|
27
26
|
return;
|
|
28
27
|
let unblock = router.block((tx) => {
|
|
@@ -44,8 +43,8 @@ exports.useBlocker = useBlocker;
|
|
|
44
43
|
* custom links that are also accessible and preserve right-click behavior.
|
|
45
44
|
*/
|
|
46
45
|
function useHref(to) {
|
|
47
|
-
|
|
48
|
-
const { router } = react_1.useContext(contexts_1.RouterContext);
|
|
46
|
+
(0, invariant_1.default)(useInRouterContext(), `useHref() may be used only in the context of a <Router> component.`);
|
|
47
|
+
const { router } = (0, react_1.useContext)(contexts_1.RouterContext);
|
|
49
48
|
const path = useResolvedPath(to);
|
|
50
49
|
return router.resolve(path).href;
|
|
51
50
|
}
|
|
@@ -54,7 +53,7 @@ exports.useHref = useHref;
|
|
|
54
53
|
* Returns true if this component is a descendant of a <Router>.
|
|
55
54
|
*/
|
|
56
55
|
function useInRouterContext() {
|
|
57
|
-
return react_1.useContext(contexts_1.RouterContext) != null;
|
|
56
|
+
return (0, react_1.useContext)(contexts_1.RouterContext) != null;
|
|
58
57
|
}
|
|
59
58
|
exports.useInRouterContext = useInRouterContext;
|
|
60
59
|
/**
|
|
@@ -63,9 +62,9 @@ exports.useInRouterContext = useInRouterContext;
|
|
|
63
62
|
* <NavLink>.
|
|
64
63
|
*/
|
|
65
64
|
function useMatch(pattern) {
|
|
66
|
-
|
|
65
|
+
(0, invariant_1.default)(useInRouterContext(), `useMatch() may be used only in the context of a <Router> component.`);
|
|
67
66
|
const { pathname } = useCurrentRoute();
|
|
68
|
-
return router_1.
|
|
67
|
+
return (0, router_1.matchPathname)(pattern, pathname);
|
|
69
68
|
}
|
|
70
69
|
exports.useMatch = useMatch;
|
|
71
70
|
/**
|
|
@@ -73,14 +72,14 @@ exports.useMatch = useMatch;
|
|
|
73
72
|
* may also be used by other elements to change the location.
|
|
74
73
|
*/
|
|
75
74
|
function useNavigate() {
|
|
76
|
-
|
|
77
|
-
const { router } = react_1.useContext(contexts_1.RouterContext);
|
|
78
|
-
const { pathname } = react_1.useContext(contexts_1.
|
|
79
|
-
const activeRef =
|
|
80
|
-
|
|
75
|
+
(0, invariant_1.default)(useInRouterContext(), `useNavigate() may be used only in the context of a <Router> component.`);
|
|
76
|
+
const { router } = (0, react_1.useContext)(contexts_1.RouterContext);
|
|
77
|
+
const { pathname } = (0, react_1.useContext)(contexts_1.MatchedRouteContext);
|
|
78
|
+
const activeRef = React.useRef(false);
|
|
79
|
+
React.useEffect(() => {
|
|
81
80
|
activeRef.current = true;
|
|
82
81
|
});
|
|
83
|
-
let navigate =
|
|
82
|
+
let navigate = React.useCallback((to, options = {}) => {
|
|
84
83
|
if (activeRef.current) {
|
|
85
84
|
if (typeof to === 'number') {
|
|
86
85
|
router.go(to);
|
|
@@ -91,7 +90,7 @@ function useNavigate() {
|
|
|
91
90
|
}
|
|
92
91
|
}
|
|
93
92
|
else {
|
|
94
|
-
utils_1.warning(false, `You should call navigate() in a useEffect, not when ` +
|
|
93
|
+
(0, utils_1.warning)(false, `You should call navigate() in a useEffect, not when ` +
|
|
95
94
|
`your component is first rendered.`);
|
|
96
95
|
}
|
|
97
96
|
}, [router, pathname]);
|
|
@@ -103,23 +102,28 @@ exports.useNavigate = useNavigate;
|
|
|
103
102
|
* URL that were matched by the route path.
|
|
104
103
|
*/
|
|
105
104
|
function useParams() {
|
|
106
|
-
return react_1.useContext(contexts_1.
|
|
105
|
+
return (0, react_1.useContext)(contexts_1.MatchedRouteContext).params;
|
|
107
106
|
}
|
|
108
107
|
exports.useParams = useParams;
|
|
109
108
|
/**
|
|
110
109
|
* Resolves the pathname of the given `to` value against the current location.
|
|
111
110
|
*/
|
|
112
111
|
function useResolvedPath(to) {
|
|
113
|
-
const { router } = react_1.useContext(contexts_1.RouterContext);
|
|
114
|
-
const { pathname } = react_1.useContext(contexts_1.
|
|
115
|
-
return
|
|
112
|
+
const { router } = (0, react_1.useContext)(contexts_1.RouterContext);
|
|
113
|
+
const { pathname } = (0, react_1.useContext)(contexts_1.MatchedRouteContext);
|
|
114
|
+
return React.useMemo(() => router.resolve(to, pathname).path, [to, pathname]);
|
|
116
115
|
}
|
|
117
116
|
exports.useResolvedPath = useResolvedPath;
|
|
118
117
|
/**
|
|
119
118
|
* Returns the current router object
|
|
119
|
+
* two parts, one is router behavior(browser history mode, hash history mode), another is router content
|
|
120
120
|
*/
|
|
121
121
|
function useRouter() {
|
|
122
|
-
|
|
123
|
-
return react_1.useContext(contexts_1.RouterContext).router;
|
|
122
|
+
(0, invariant_1.default)(useInRouterContext(), `useRouter() may be used only in the context of a <Router> component.`);
|
|
123
|
+
return (0, react_1.useContext)(contexts_1.RouterContext).router;
|
|
124
124
|
}
|
|
125
125
|
exports.useRouter = useRouter;
|
|
126
|
+
function useMatchedRoute() {
|
|
127
|
+
return (0, react_1.useContext)(contexts_1.MatchedRouteContext);
|
|
128
|
+
}
|
|
129
|
+
exports.useMatchedRoute = useMatchedRoute;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
export { generatePath } from './utils';
|
|
1
|
+
export { generatePath, useIsomorphicEffect } from './utils';
|
|
2
2
|
export { MemoryRouter } from './MemoryRouter';
|
|
3
3
|
export { RouterView } from './RouterView';
|
|
4
4
|
export { Router } from './Router';
|
|
5
|
-
export { Link } from './Link';
|
|
5
|
+
export { Link, LinkProps } from './Link';
|
|
6
6
|
export * from './hooks';
|
|
7
|
+
export * from './contexts';
|
|
7
8
|
export * from './types';
|
|
9
|
+
export * from '@shuvi/router';
|