@tinkoff/router 0.1.70 → 0.1.73
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/lib/components/react/provider.d.ts +1 -0
- package/lib/index.browser.js +18 -13
- package/lib/index.es.js +18 -13
- package/lib/index.js +28 -24
- package/package.json +2 -2
package/lib/index.browser.js
CHANGED
|
@@ -7,8 +7,9 @@ import noop from '@tinkoff/utils/function/noop';
|
|
|
7
7
|
import each from '@tinkoff/utils/array/each';
|
|
8
8
|
import find from '@tinkoff/utils/array/find';
|
|
9
9
|
import findIndex from '@tinkoff/utils/array/findIndex';
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
10
|
+
import { jsx } from 'react/jsx-runtime';
|
|
11
|
+
import { createContext, useState, useContext, useCallback, isValidElement, cloneElement } from 'react';
|
|
12
|
+
import { useIsomorphicLayoutEffect, useShallowEqual } from '@tinkoff/react-hooks';
|
|
12
13
|
|
|
13
14
|
const PARAMETER_DELIMITER = ':';
|
|
14
15
|
const WILDCARD_REGEXP = /\*/;
|
|
@@ -764,13 +765,19 @@ class ClientRouter extends AbstractRouter {
|
|
|
764
765
|
return super.resolveRoute(...options);
|
|
765
766
|
}
|
|
766
767
|
async notfound(navigation) {
|
|
768
|
+
var _a, _b;
|
|
767
769
|
await super.notfound(navigation);
|
|
768
770
|
// in case we didn't find any matched route just force hard page navigation
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
771
|
+
const prevUrl = (_b = (_a = navigation.fromUrl) === null || _a === void 0 ? void 0 : _a.href) !== null && _b !== void 0 ? _b : window.location.href;
|
|
772
|
+
const nextUrl = navigation.url.href;
|
|
773
|
+
// prevent redirect cycle on the same page
|
|
774
|
+
if (prevUrl !== nextUrl) {
|
|
775
|
+
if (navigation.replace) {
|
|
776
|
+
window.location.replace(nextUrl);
|
|
777
|
+
}
|
|
778
|
+
else {
|
|
779
|
+
window.location.assign(nextUrl);
|
|
780
|
+
}
|
|
774
781
|
}
|
|
775
782
|
// prevent routing from any continues navigation returning promise which will be not resolved
|
|
776
783
|
return new Promise(() => { });
|
|
@@ -1104,19 +1111,17 @@ const RouterContext = createContext(null);
|
|
|
1104
1111
|
const RouteContext = createContext(null);
|
|
1105
1112
|
const UrlContext = createContext(null);
|
|
1106
1113
|
|
|
1107
|
-
const Provider = ({ router, children
|
|
1114
|
+
const Provider = ({ router, children }) => {
|
|
1108
1115
|
const [state, setState] = useState({
|
|
1109
1116
|
route: router.getCurrentRoute(),
|
|
1110
1117
|
url: router.getCurrentUrl(),
|
|
1111
1118
|
});
|
|
1112
|
-
|
|
1119
|
+
useIsomorphicLayoutEffect(() => {
|
|
1113
1120
|
return router.registerSyncHook('change', ({ to, url }) => {
|
|
1114
1121
|
setState({ route: to, url });
|
|
1115
1122
|
});
|
|
1116
1123
|
}, [router]);
|
|
1117
|
-
return (
|
|
1118
|
-
React.createElement(RouteContext.Provider, { value: state.route },
|
|
1119
|
-
React.createElement(UrlContext.Provider, { value: state.url }, children))));
|
|
1124
|
+
return (jsx(RouterContext.Provider, Object.assign({ value: router }, { children: jsx(RouteContext.Provider, Object.assign({ value: state.route }, { children: jsx(UrlContext.Provider, Object.assign({ value: state.url }, { children: children }), void 0) }), void 0) }), void 0));
|
|
1120
1125
|
};
|
|
1121
1126
|
Provider.displayName = 'Provider';
|
|
1122
1127
|
|
|
@@ -1168,7 +1173,7 @@ function Link(props) {
|
|
|
1168
1173
|
}
|
|
1169
1174
|
return (
|
|
1170
1175
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
1171
|
-
|
|
1176
|
+
jsx("a", Object.assign({}, otherProps, extraProps, { children: children }), void 0));
|
|
1172
1177
|
}
|
|
1173
1178
|
Link.displayName = 'Link';
|
|
1174
1179
|
|
package/lib/index.es.js
CHANGED
|
@@ -7,8 +7,9 @@ import noop from '@tinkoff/utils/function/noop';
|
|
|
7
7
|
import each from '@tinkoff/utils/array/each';
|
|
8
8
|
import find from '@tinkoff/utils/array/find';
|
|
9
9
|
import findIndex from '@tinkoff/utils/array/findIndex';
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
10
|
+
import { jsx } from 'react/jsx-runtime';
|
|
11
|
+
import { createContext, useState, useContext, useCallback, isValidElement, cloneElement } from 'react';
|
|
12
|
+
import { useIsomorphicLayoutEffect, useShallowEqual } from '@tinkoff/react-hooks';
|
|
12
13
|
|
|
13
14
|
const PARAMETER_DELIMITER = ':';
|
|
14
15
|
const WILDCARD_REGEXP = /\*/;
|
|
@@ -994,13 +995,19 @@ class ClientRouter extends AbstractRouter {
|
|
|
994
995
|
return super.resolveRoute(...options);
|
|
995
996
|
}
|
|
996
997
|
async notfound(navigation) {
|
|
998
|
+
var _a, _b;
|
|
997
999
|
await super.notfound(navigation);
|
|
998
1000
|
// in case we didn't find any matched route just force hard page navigation
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1001
|
+
const prevUrl = (_b = (_a = navigation.fromUrl) === null || _a === void 0 ? void 0 : _a.href) !== null && _b !== void 0 ? _b : window.location.href;
|
|
1002
|
+
const nextUrl = navigation.url.href;
|
|
1003
|
+
// prevent redirect cycle on the same page
|
|
1004
|
+
if (prevUrl !== nextUrl) {
|
|
1005
|
+
if (navigation.replace) {
|
|
1006
|
+
window.location.replace(nextUrl);
|
|
1007
|
+
}
|
|
1008
|
+
else {
|
|
1009
|
+
window.location.assign(nextUrl);
|
|
1010
|
+
}
|
|
1004
1011
|
}
|
|
1005
1012
|
// prevent routing from any continues navigation returning promise which will be not resolved
|
|
1006
1013
|
return new Promise(() => { });
|
|
@@ -1032,19 +1039,17 @@ const RouterContext = createContext(null);
|
|
|
1032
1039
|
const RouteContext = createContext(null);
|
|
1033
1040
|
const UrlContext = createContext(null);
|
|
1034
1041
|
|
|
1035
|
-
const Provider = ({ router, children
|
|
1042
|
+
const Provider = ({ router, children }) => {
|
|
1036
1043
|
const [state, setState] = useState({
|
|
1037
1044
|
route: router.getCurrentRoute(),
|
|
1038
1045
|
url: router.getCurrentUrl(),
|
|
1039
1046
|
});
|
|
1040
|
-
|
|
1047
|
+
useIsomorphicLayoutEffect(() => {
|
|
1041
1048
|
return router.registerSyncHook('change', ({ to, url }) => {
|
|
1042
1049
|
setState({ route: to, url });
|
|
1043
1050
|
});
|
|
1044
1051
|
}, [router]);
|
|
1045
|
-
return (
|
|
1046
|
-
React.createElement(RouteContext.Provider, { value: state.route },
|
|
1047
|
-
React.createElement(UrlContext.Provider, { value: state.url }, children))));
|
|
1052
|
+
return (jsx(RouterContext.Provider, Object.assign({ value: router }, { children: jsx(RouteContext.Provider, Object.assign({ value: state.route }, { children: jsx(UrlContext.Provider, Object.assign({ value: state.url }, { children: children }), void 0) }), void 0) }), void 0));
|
|
1048
1053
|
};
|
|
1049
1054
|
Provider.displayName = 'Provider';
|
|
1050
1055
|
|
|
@@ -1096,7 +1101,7 @@ function Link(props) {
|
|
|
1096
1101
|
}
|
|
1097
1102
|
return (
|
|
1098
1103
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
1099
|
-
|
|
1104
|
+
jsx("a", Object.assign({}, otherProps, extraProps, { children: children }), void 0));
|
|
1100
1105
|
}
|
|
1101
1106
|
Link.displayName = 'Link';
|
|
1102
1107
|
|
package/lib/index.js
CHANGED
|
@@ -11,7 +11,8 @@ var noop = require('@tinkoff/utils/function/noop');
|
|
|
11
11
|
var each = require('@tinkoff/utils/array/each');
|
|
12
12
|
var find = require('@tinkoff/utils/array/find');
|
|
13
13
|
var findIndex = require('@tinkoff/utils/array/findIndex');
|
|
14
|
-
var
|
|
14
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
15
|
+
var react = require('react');
|
|
15
16
|
var reactHooks = require('@tinkoff/react-hooks');
|
|
16
17
|
|
|
17
18
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
@@ -24,7 +25,6 @@ var noop__default = /*#__PURE__*/_interopDefaultLegacy(noop);
|
|
|
24
25
|
var each__default = /*#__PURE__*/_interopDefaultLegacy(each);
|
|
25
26
|
var find__default = /*#__PURE__*/_interopDefaultLegacy(find);
|
|
26
27
|
var findIndex__default = /*#__PURE__*/_interopDefaultLegacy(findIndex);
|
|
27
|
-
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
28
28
|
|
|
29
29
|
const PARAMETER_DELIMITER = ':';
|
|
30
30
|
const WILDCARD_REGEXP = /\*/;
|
|
@@ -1010,13 +1010,19 @@ class ClientRouter extends AbstractRouter {
|
|
|
1010
1010
|
return super.resolveRoute(...options);
|
|
1011
1011
|
}
|
|
1012
1012
|
async notfound(navigation) {
|
|
1013
|
+
var _a, _b;
|
|
1013
1014
|
await super.notfound(navigation);
|
|
1014
1015
|
// in case we didn't find any matched route just force hard page navigation
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1016
|
+
const prevUrl = (_b = (_a = navigation.fromUrl) === null || _a === void 0 ? void 0 : _a.href) !== null && _b !== void 0 ? _b : window.location.href;
|
|
1017
|
+
const nextUrl = navigation.url.href;
|
|
1018
|
+
// prevent redirect cycle on the same page
|
|
1019
|
+
if (prevUrl !== nextUrl) {
|
|
1020
|
+
if (navigation.replace) {
|
|
1021
|
+
window.location.replace(nextUrl);
|
|
1022
|
+
}
|
|
1023
|
+
else {
|
|
1024
|
+
window.location.assign(nextUrl);
|
|
1025
|
+
}
|
|
1020
1026
|
}
|
|
1021
1027
|
// prevent routing from any continues navigation returning promise which will be not resolved
|
|
1022
1028
|
return new Promise(() => { });
|
|
@@ -1044,36 +1050,34 @@ class NoSpaRouter extends ClientRouter {
|
|
|
1044
1050
|
async runHooks() { }
|
|
1045
1051
|
}
|
|
1046
1052
|
|
|
1047
|
-
const RouterContext =
|
|
1048
|
-
const RouteContext =
|
|
1049
|
-
const UrlContext =
|
|
1053
|
+
const RouterContext = react.createContext(null);
|
|
1054
|
+
const RouteContext = react.createContext(null);
|
|
1055
|
+
const UrlContext = react.createContext(null);
|
|
1050
1056
|
|
|
1051
|
-
const Provider = ({ router, children
|
|
1052
|
-
const [state, setState] =
|
|
1057
|
+
const Provider = ({ router, children }) => {
|
|
1058
|
+
const [state, setState] = react.useState({
|
|
1053
1059
|
route: router.getCurrentRoute(),
|
|
1054
1060
|
url: router.getCurrentUrl(),
|
|
1055
1061
|
});
|
|
1056
|
-
|
|
1062
|
+
reactHooks.useIsomorphicLayoutEffect(() => {
|
|
1057
1063
|
return router.registerSyncHook('change', ({ to, url }) => {
|
|
1058
1064
|
setState({ route: to, url });
|
|
1059
1065
|
});
|
|
1060
1066
|
}, [router]);
|
|
1061
|
-
return (
|
|
1062
|
-
React__default["default"].createElement(RouteContext.Provider, { value: state.route },
|
|
1063
|
-
React__default["default"].createElement(UrlContext.Provider, { value: state.url }, children))));
|
|
1067
|
+
return (jsxRuntime.jsx(RouterContext.Provider, Object.assign({ value: router }, { children: jsxRuntime.jsx(RouteContext.Provider, Object.assign({ value: state.route }, { children: jsxRuntime.jsx(UrlContext.Provider, Object.assign({ value: state.url }, { children: children }), void 0) }), void 0) }), void 0));
|
|
1064
1068
|
};
|
|
1065
1069
|
Provider.displayName = 'Provider';
|
|
1066
1070
|
|
|
1067
1071
|
const useRouter = () => {
|
|
1068
|
-
return
|
|
1072
|
+
return react.useContext(RouterContext);
|
|
1069
1073
|
};
|
|
1070
1074
|
|
|
1071
1075
|
const useRoute = () => {
|
|
1072
|
-
return
|
|
1076
|
+
return react.useContext(RouteContext);
|
|
1073
1077
|
};
|
|
1074
1078
|
|
|
1075
1079
|
const useUrl = () => {
|
|
1076
|
-
return
|
|
1080
|
+
return react.useContext(UrlContext);
|
|
1077
1081
|
};
|
|
1078
1082
|
|
|
1079
1083
|
const convertToNavigateOptions = (options) => {
|
|
@@ -1082,7 +1086,7 @@ const convertToNavigateOptions = (options) => {
|
|
|
1082
1086
|
const useNavigate = (rootOptions) => {
|
|
1083
1087
|
const router = useRouter();
|
|
1084
1088
|
const rootOpts = reactHooks.useShallowEqual(convertToNavigateOptions(rootOptions));
|
|
1085
|
-
return
|
|
1089
|
+
return react.useCallback((specificOptions) => {
|
|
1086
1090
|
const opts = rootOpts !== null && rootOpts !== void 0 ? rootOpts : convertToNavigateOptions(specificOptions);
|
|
1087
1091
|
return router.navigate(opts);
|
|
1088
1092
|
}, [rootOpts, router]);
|
|
@@ -1091,7 +1095,7 @@ const useNavigate = (rootOptions) => {
|
|
|
1091
1095
|
function Link(props) {
|
|
1092
1096
|
const { children, onClick, url, query, replace, target, navigateOptions, ...otherProps } = props;
|
|
1093
1097
|
const navigate = useNavigate({ url, query, replace, ...navigateOptions });
|
|
1094
|
-
const handleClick =
|
|
1098
|
+
const handleClick = react.useCallback((event) => {
|
|
1095
1099
|
// ignores the navigation when clicked using right mouse button or
|
|
1096
1100
|
// by holding a special modifier key: ctrl, command, win, alt, shift
|
|
1097
1101
|
if (target ||
|
|
@@ -1107,12 +1111,12 @@ function Link(props) {
|
|
|
1107
1111
|
onClick && onClick(event);
|
|
1108
1112
|
}, [navigate, target, onClick]);
|
|
1109
1113
|
const extraProps = { href: url, onClick: handleClick, target };
|
|
1110
|
-
if (
|
|
1111
|
-
return
|
|
1114
|
+
if (react.isValidElement(children)) {
|
|
1115
|
+
return react.cloneElement(children, extraProps);
|
|
1112
1116
|
}
|
|
1113
1117
|
return (
|
|
1114
1118
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
1115
|
-
|
|
1119
|
+
jsxRuntime.jsx("a", Object.assign({}, otherProps, extraProps, { children: children }), void 0));
|
|
1116
1120
|
}
|
|
1117
1121
|
Link.displayName = 'Link';
|
|
1118
1122
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinkoff/router",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.73",
|
|
4
4
|
"description": "router",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"build-for-publish": "true"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@tinkoff/react-hooks": "0.0.
|
|
25
|
+
"@tinkoff/react-hooks": "0.0.24",
|
|
26
26
|
"@tinkoff/url": "0.7.37",
|
|
27
27
|
"@tinkoff/utils": "^2.1.2"
|
|
28
28
|
},
|