@tanstack/react-router 0.0.1-beta.260 → 0.0.1-beta.262
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/build/cjs/link.js +96 -22
- package/build/cjs/link.js.map +1 -1
- package/build/cjs/router.js +5 -116
- package/build/cjs/router.js.map +1 -1
- package/build/esm/index.js +101 -138
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +355 -355
- package/build/types/router.d.ts +1 -2
- package/build/umd/index.development.js +101 -138
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/link.tsx +126 -19
- package/src/router.ts +5 -142
package/build/esm/index.js
CHANGED
|
@@ -1032,17 +1032,15 @@ function _extends() {
|
|
|
1032
1032
|
return _extends.apply(this, arguments);
|
|
1033
1033
|
}
|
|
1034
1034
|
|
|
1035
|
+
const preloadWarning = 'Error preloading route! ☝️';
|
|
1035
1036
|
function useLinkProps(options) {
|
|
1036
|
-
const
|
|
1037
|
-
buildLink
|
|
1038
|
-
} = useRouter();
|
|
1037
|
+
const router = useRouter();
|
|
1039
1038
|
const matchPathname = useMatch({
|
|
1040
1039
|
strict: false,
|
|
1041
1040
|
select: s => s.pathname
|
|
1042
1041
|
});
|
|
1043
1042
|
const {
|
|
1044
1043
|
// custom props
|
|
1045
|
-
type,
|
|
1046
1044
|
children,
|
|
1047
1045
|
target,
|
|
1048
1046
|
activeProps = () => ({
|
|
@@ -1057,8 +1055,8 @@ function useLinkProps(options) {
|
|
|
1057
1055
|
to,
|
|
1058
1056
|
state,
|
|
1059
1057
|
mask,
|
|
1060
|
-
preload,
|
|
1061
|
-
preloadDelay,
|
|
1058
|
+
preload: userPreload,
|
|
1059
|
+
preloadDelay: userPreloadDelay,
|
|
1062
1060
|
replace,
|
|
1063
1061
|
startTransition,
|
|
1064
1062
|
resetScroll,
|
|
@@ -1072,27 +1070,100 @@ function useLinkProps(options) {
|
|
|
1072
1070
|
onTouchStart,
|
|
1073
1071
|
...rest
|
|
1074
1072
|
} = options;
|
|
1075
|
-
|
|
1073
|
+
|
|
1074
|
+
// If this link simply reloads the current route,
|
|
1075
|
+
// make sure it has a new key so it will trigger a data refresh
|
|
1076
|
+
|
|
1077
|
+
// If this `to` is a valid external URL, return
|
|
1078
|
+
// null for LinkUtils
|
|
1079
|
+
|
|
1080
|
+
const dest = {
|
|
1076
1081
|
from: options.to ? matchPathname : undefined,
|
|
1077
1082
|
...options
|
|
1078
|
-
}
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
+
};
|
|
1084
|
+
let type = 'internal';
|
|
1085
|
+
try {
|
|
1086
|
+
new URL(`${to}`);
|
|
1087
|
+
type = 'external';
|
|
1088
|
+
} catch {}
|
|
1089
|
+
if (type === 'external') {
|
|
1083
1090
|
return {
|
|
1084
|
-
href
|
|
1091
|
+
href: to
|
|
1085
1092
|
};
|
|
1086
1093
|
}
|
|
1087
|
-
const
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1094
|
+
const next = router.buildLocation(dest);
|
|
1095
|
+
const preload = userPreload ?? router.options.defaultPreload;
|
|
1096
|
+
const preloadDelay = userPreloadDelay ?? router.options.defaultPreloadDelay ?? 0;
|
|
1097
|
+
const isActive = useRouterState({
|
|
1098
|
+
select: s => {
|
|
1099
|
+
// Compare path/hash for matches
|
|
1100
|
+
const currentPathSplit = s.location.pathname.split('/');
|
|
1101
|
+
const nextPathSplit = next.pathname.split('/');
|
|
1102
|
+
const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
|
|
1103
|
+
// Combine the matches based on user router.options
|
|
1104
|
+
const pathTest = activeOptions?.exact ? s.location.pathname === next.pathname : pathIsFuzzyEqual;
|
|
1105
|
+
const hashTest = activeOptions?.includeHash ? s.location.hash === next.hash : true;
|
|
1106
|
+
const searchTest = activeOptions?.includeSearch ?? true ? deepEqual(s.location.search, next.search, true) : true;
|
|
1107
|
+
|
|
1108
|
+
// The final "active" test
|
|
1109
|
+
return pathTest && hashTest && searchTest;
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
|
|
1113
|
+
// The click handler
|
|
1114
|
+
const handleClick = e => {
|
|
1115
|
+
if (!disabled && !isCtrlEvent(e) && !e.defaultPrevented && (!target || target === '_self') && e.button === 0) {
|
|
1116
|
+
e.preventDefault();
|
|
1117
|
+
|
|
1118
|
+
// All is well? Navigate!
|
|
1119
|
+
router.commitLocation({
|
|
1120
|
+
...next,
|
|
1121
|
+
replace,
|
|
1122
|
+
resetScroll,
|
|
1123
|
+
startTransition
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
};
|
|
1127
|
+
|
|
1128
|
+
// The click handler
|
|
1129
|
+
const handleFocus = e => {
|
|
1130
|
+
if (preload) {
|
|
1131
|
+
router.preloadRoute(dest).catch(err => {
|
|
1132
|
+
console.warn(err);
|
|
1133
|
+
console.warn(preloadWarning);
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
};
|
|
1137
|
+
const handleTouchStart = e => {
|
|
1138
|
+
if (preload) {
|
|
1139
|
+
router.preloadRoute(dest).catch(err => {
|
|
1140
|
+
console.warn(err);
|
|
1141
|
+
console.warn(preloadWarning);
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
};
|
|
1145
|
+
const handleEnter = e => {
|
|
1146
|
+
const target = e.target || {};
|
|
1147
|
+
if (preload) {
|
|
1148
|
+
if (target.preloadTimeout) {
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
target.preloadTimeout = setTimeout(() => {
|
|
1152
|
+
target.preloadTimeout = null;
|
|
1153
|
+
router.preloadRoute(dest).catch(err => {
|
|
1154
|
+
console.warn(err);
|
|
1155
|
+
console.warn(preloadWarning);
|
|
1156
|
+
});
|
|
1157
|
+
}, preloadDelay);
|
|
1158
|
+
}
|
|
1159
|
+
};
|
|
1160
|
+
const handleLeave = e => {
|
|
1161
|
+
const target = e.target || {};
|
|
1162
|
+
if (target.preloadTimeout) {
|
|
1163
|
+
clearTimeout(target.preloadTimeout);
|
|
1164
|
+
target.preloadTimeout = null;
|
|
1165
|
+
}
|
|
1166
|
+
};
|
|
1096
1167
|
const composeHandlers = handlers => e => {
|
|
1097
1168
|
if (e.persist) e.persist();
|
|
1098
1169
|
handlers.filter(Boolean).forEach(handler => {
|
|
@@ -1140,6 +1211,9 @@ const Link = /*#__PURE__*/React.forwardRef((props, ref) => {
|
|
|
1140
1211
|
}) : props.children
|
|
1141
1212
|
}));
|
|
1142
1213
|
});
|
|
1214
|
+
function isCtrlEvent(e) {
|
|
1215
|
+
return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
|
|
1216
|
+
}
|
|
1143
1217
|
|
|
1144
1218
|
// @ts-nocheck
|
|
1145
1219
|
|
|
@@ -1269,7 +1343,6 @@ function stringifySearchWith(stringify, parser) {
|
|
|
1269
1343
|
//
|
|
1270
1344
|
|
|
1271
1345
|
const componentTypes = ['component', 'errorComponent', 'pendingComponent'];
|
|
1272
|
-
const preloadWarning = 'Error preloading route! ☝️';
|
|
1273
1346
|
class Router {
|
|
1274
1347
|
// Option-independent properties
|
|
1275
1348
|
tempLocationKey = `${Math.round(Math.random() * 10000000)}`;
|
|
@@ -1814,13 +1887,13 @@ class Router {
|
|
|
1814
1887
|
pendingMatches: s.pendingMatches?.map(d => d.id === match.id ? match : d)
|
|
1815
1888
|
}));
|
|
1816
1889
|
};
|
|
1817
|
-
const abortController = new AbortController();
|
|
1818
1890
|
|
|
1819
1891
|
// Check each match middleware to see if the route can be accessed
|
|
1820
1892
|
try {
|
|
1821
1893
|
for (let [index, match] of matches.entries()) {
|
|
1822
1894
|
const parentMatch = matches[index - 1];
|
|
1823
1895
|
const route = this.looseRoutesById[match.routeId];
|
|
1896
|
+
const abortController = new AbortController();
|
|
1824
1897
|
const handleErrorAndRedirect = (err, code) => {
|
|
1825
1898
|
err.routerCode = code;
|
|
1826
1899
|
firstBadMatchIndex = firstBadMatchIndex ?? index;
|
|
@@ -1840,7 +1913,7 @@ class Router {
|
|
|
1840
1913
|
error: err,
|
|
1841
1914
|
status: 'error',
|
|
1842
1915
|
updatedAt: Date.now(),
|
|
1843
|
-
abortController
|
|
1916
|
+
abortController: new AbortController()
|
|
1844
1917
|
};
|
|
1845
1918
|
};
|
|
1846
1919
|
try {
|
|
@@ -1853,7 +1926,7 @@ class Router {
|
|
|
1853
1926
|
const parentContext = parentMatch?.context ?? this.options.context ?? {};
|
|
1854
1927
|
const beforeLoadContext = (await route.options.beforeLoad?.({
|
|
1855
1928
|
search: match.search,
|
|
1856
|
-
abortController
|
|
1929
|
+
abortController,
|
|
1857
1930
|
params: match.params,
|
|
1858
1931
|
preload: !!preload,
|
|
1859
1932
|
context: parentContext,
|
|
@@ -1875,7 +1948,8 @@ class Router {
|
|
|
1875
1948
|
};
|
|
1876
1949
|
matches[index] = match = {
|
|
1877
1950
|
...match,
|
|
1878
|
-
context: replaceEqualDeep(match.context, context)
|
|
1951
|
+
context: replaceEqualDeep(match.context, context),
|
|
1952
|
+
abortController
|
|
1879
1953
|
};
|
|
1880
1954
|
} catch (err) {
|
|
1881
1955
|
handleErrorAndRedirect(err, 'BEFORE_LOAD');
|
|
@@ -2146,114 +2220,6 @@ class Router {
|
|
|
2146
2220
|
});
|
|
2147
2221
|
return matches;
|
|
2148
2222
|
};
|
|
2149
|
-
buildLink = dest => {
|
|
2150
|
-
// If this link simply reloads the current route,
|
|
2151
|
-
// make sure it has a new key so it will trigger a data refresh
|
|
2152
|
-
|
|
2153
|
-
// If this `to` is a valid external URL, return
|
|
2154
|
-
// null for LinkUtils
|
|
2155
|
-
|
|
2156
|
-
const {
|
|
2157
|
-
to,
|
|
2158
|
-
preload: userPreload,
|
|
2159
|
-
preloadDelay: userPreloadDelay,
|
|
2160
|
-
activeOptions,
|
|
2161
|
-
disabled,
|
|
2162
|
-
target,
|
|
2163
|
-
replace,
|
|
2164
|
-
resetScroll,
|
|
2165
|
-
startTransition
|
|
2166
|
-
} = dest;
|
|
2167
|
-
try {
|
|
2168
|
-
new URL(`${to}`);
|
|
2169
|
-
return {
|
|
2170
|
-
type: 'external',
|
|
2171
|
-
href: to
|
|
2172
|
-
};
|
|
2173
|
-
} catch (e) {}
|
|
2174
|
-
const nextOpts = dest;
|
|
2175
|
-
const next = this.buildLocation(nextOpts);
|
|
2176
|
-
const preload = userPreload ?? this.options.defaultPreload;
|
|
2177
|
-
const preloadDelay = userPreloadDelay ?? this.options.defaultPreloadDelay ?? 0;
|
|
2178
|
-
|
|
2179
|
-
// Compare path/hash for matches
|
|
2180
|
-
const currentPathSplit = this.latestLocation.pathname.split('/');
|
|
2181
|
-
const nextPathSplit = next.pathname.split('/');
|
|
2182
|
-
const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
|
|
2183
|
-
// Combine the matches based on user this.options
|
|
2184
|
-
const pathTest = activeOptions?.exact ? this.latestLocation.pathname === next.pathname : pathIsFuzzyEqual;
|
|
2185
|
-
const hashTest = activeOptions?.includeHash ? this.latestLocation.hash === next.hash : true;
|
|
2186
|
-
const searchTest = activeOptions?.includeSearch ?? true ? deepEqual(this.latestLocation.search, next.search, true) : true;
|
|
2187
|
-
|
|
2188
|
-
// The final "active" test
|
|
2189
|
-
const isActive = pathTest && hashTest && searchTest;
|
|
2190
|
-
|
|
2191
|
-
// The click handler
|
|
2192
|
-
const handleClick = e => {
|
|
2193
|
-
if (!disabled && !isCtrlEvent(e) && !e.defaultPrevented && (!target || target === '_self') && e.button === 0) {
|
|
2194
|
-
e.preventDefault();
|
|
2195
|
-
|
|
2196
|
-
// All is well? Navigate!
|
|
2197
|
-
this.commitLocation({
|
|
2198
|
-
...next,
|
|
2199
|
-
replace,
|
|
2200
|
-
resetScroll,
|
|
2201
|
-
startTransition
|
|
2202
|
-
});
|
|
2203
|
-
}
|
|
2204
|
-
};
|
|
2205
|
-
|
|
2206
|
-
// The click handler
|
|
2207
|
-
const handleFocus = e => {
|
|
2208
|
-
if (preload) {
|
|
2209
|
-
this.preloadRoute(nextOpts).catch(err => {
|
|
2210
|
-
console.warn(err);
|
|
2211
|
-
console.warn(preloadWarning);
|
|
2212
|
-
});
|
|
2213
|
-
}
|
|
2214
|
-
};
|
|
2215
|
-
const handleTouchStart = e => {
|
|
2216
|
-
if (preload) {
|
|
2217
|
-
this.preloadRoute(nextOpts).catch(err => {
|
|
2218
|
-
console.warn(err);
|
|
2219
|
-
console.warn(preloadWarning);
|
|
2220
|
-
});
|
|
2221
|
-
}
|
|
2222
|
-
};
|
|
2223
|
-
const handleEnter = e => {
|
|
2224
|
-
const target = e.target || {};
|
|
2225
|
-
if (preload) {
|
|
2226
|
-
if (target.preloadTimeout) {
|
|
2227
|
-
return;
|
|
2228
|
-
}
|
|
2229
|
-
target.preloadTimeout = setTimeout(() => {
|
|
2230
|
-
target.preloadTimeout = null;
|
|
2231
|
-
this.preloadRoute(nextOpts).catch(err => {
|
|
2232
|
-
console.warn(err);
|
|
2233
|
-
console.warn(preloadWarning);
|
|
2234
|
-
});
|
|
2235
|
-
}, preloadDelay);
|
|
2236
|
-
}
|
|
2237
|
-
};
|
|
2238
|
-
const handleLeave = e => {
|
|
2239
|
-
const target = e.target || {};
|
|
2240
|
-
if (target.preloadTimeout) {
|
|
2241
|
-
clearTimeout(target.preloadTimeout);
|
|
2242
|
-
target.preloadTimeout = null;
|
|
2243
|
-
}
|
|
2244
|
-
};
|
|
2245
|
-
return {
|
|
2246
|
-
type: 'internal',
|
|
2247
|
-
next,
|
|
2248
|
-
handleFocus,
|
|
2249
|
-
handleClick,
|
|
2250
|
-
handleEnter,
|
|
2251
|
-
handleLeave,
|
|
2252
|
-
handleTouchStart,
|
|
2253
|
-
isActive,
|
|
2254
|
-
disabled
|
|
2255
|
-
};
|
|
2256
|
-
};
|
|
2257
2223
|
matchRoute = (location, opts) => {
|
|
2258
2224
|
location = {
|
|
2259
2225
|
...location,
|
|
@@ -2359,9 +2325,6 @@ function lazyFn(fn, key) {
|
|
|
2359
2325
|
return imported[key || 'default'](...args);
|
|
2360
2326
|
};
|
|
2361
2327
|
}
|
|
2362
|
-
function isCtrlEvent(e) {
|
|
2363
|
-
return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
|
|
2364
|
-
}
|
|
2365
2328
|
class SearchParamError extends Error {}
|
|
2366
2329
|
class PathParamError extends Error {}
|
|
2367
2330
|
function getInitialRouterState(location) {
|