crossroad 2.0.2 → 3.0.0
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/index.min.js +1 -1
- package/package.json +25 -9
- /package/{src/index.d.ts → index.d.ts} +0 -0
package/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import t,{createContext as e,useState as r,useRef as n,useCallback as o,useEffect as i,useContext as s}from"react";var a=e(void 0);const u=t=>t.length>1?t:t[0],c=t=>{if("string"!=typeof t)return t;const e={query:{}},r=new URL(t,"http://localhost:3000/");e.path=(r.pathname.replace(/\/$/,"")||"/").replaceAll("%3C","<").replaceAll("%3E",">"),e.query={};for(const[t]of r.searchParams)e.query[t]=u(r.searchParams.getAll(t));return r.hash&&(e.hash=r.hash.replace(/^#/,"")),e},l=t=>{if("string"==typeof t)return t;const{path:e,query:r={},hash:n}=t||{};let o=e||"/";const i=new URLSearchParams(Object.entries(r).map(([t,e])=>(Array.isArray(e)?e:[e]).map(e=>[t,e])).flat().filter(([t,e])=>e)).toString();return i&&(o+="?"+i),n&&(o+="#"+n),o};var p=()=>"undefined"==typeof window,h=({scrollUp:e,url:s,children:u})=>{const h=s||(p()?"/":window.location.href),[f,y]=r(()=>c(h)),d=n(f),m=o((t,{mode:r="push"}={})=>{if(!history[r+"State"])throw new Error(`Invalid mode "${r}"`);y(e=>{const r="function"==typeof t?t(e):t;return l(e)===l(r)?e:c(r)});const n=d.current,o="function"==typeof t?t(n):t;if(l(n)!==l(o)){const t=c(o);d.current=t,history[r+"State"]({},null,l(t)),e&&window.scrollTo(0,0)}},[]);return i(()=>{d.current=f},[f]),i(()=>{if(p())return;const t=()=>y(c(window.location.href)),e=t=>{const e=t.target,r=(t=>{if(!t)return null;const e=t.getAttribute("href");if(!e)return null;const r=e.trim();return/^[a-zA-Z][a-zA-Z0-9+\-.]*:/.test(r)||null!==t.getAttribute("target")?null:r})(e?.closest("a")??null);if(!r)return!1;t.preventDefault();const[n,o]=r.split("#");n?m(r):o&&(window.location.hash="#"+o)};return window.addEventListener("popstate",t),document.addEventListener("click",e),()=>{window.removeEventListener("popstate",t),document.removeEventListener("click",e)}},[m]),t.createElement(a.Provider,{value:[f,m]},u)};function f(t,e,r={}){let n=JSON.parse(JSON.stringify(c(t))),o=JSON.parse(JSON.stringify(c(e)));if(o.path=o.path.replace(/\/$/,"")||"/",n.path=n.path.replace(/\/$/,"")||"/",n.path.endsWith("*")){n.path=n.path.replace(/\/?\*/,"")||"/";const t=n.path.split("/").filter(Boolean).length;o.path="/"+o.path.slice(1).split("/").slice(0,t).join("/")}if(Object.entries(n.query).length)for(const t in n.query){if(!(t in o.query))return!1;if(n.query[t]&&n.query[t]!==o.query[t])return!1}if(!n.path.includes(":"))return n.path===o.path&&r;if(n.path.split("/").length!==o.path.split("/").length)return!1;const i={},s=n.path.split("/").every((t,e)=>{const n=o.path.split("/")[e];if(t.startsWith(":")){let e=t.slice(1),o="string";e.includes("<")&&([e,o]=e.split("<"),o=o.slice(0,-1));const s=decodeURIComponent(n);return i[e]="number"===o?Number(s):"date"===o?new Date(/^\d+$/.test(s)?Number(s):s):"boolean"===o?"true"===s:s,r}return n===t});return s&&Object.assign(r,i),s&&r}var y=()=>{const t=s(a);if(!t)throw new Error("Wrap your App with <Router>");return t},d=({path:e="*",scrollUp:r,component:n,render:o,children:i})=>{const s=y(),u=f(e,s[0]);if(!u)return null;if(r&&window.scrollTo(0,0),n){const e=n;i=t.createElement(e,u)}else if(o)i=o(u);else if(!i)throw new Error("Route needs prop `component`, `render` or `children`");return t.createElement(a.Provider,{value:[{...s[0],params:u},s[1]]},i)};var m=({redirect:t,children:e})=>{const[r,n]=y(),o=(t=>(Array.isArray(t)?t:[t]).filter(t=>null!=t&&"object"==typeof t&&"props"in t))(e).find(t=>f(t.props.path||"*",r))||null;return i(()=>{if(!t)return;if(o)return;const e="function"==typeof t?t(r):t;n(l(e))},[t,o]),o},w=()=>{const[t,e]=y(),r=o((t,r)=>{e(e=>{const r="function"==typeof t?t(e.path):t;return{...e,path:"string"==typeof r?r:"/"}},r)},[]);return[t.path,r]};const q=t=>l({query:t});function g(t){return t?(t=>{const[e,r]=y(),n=o((e,n)=>{r(r=>{const n=r.query[t],o="function"==typeof e?e(n):e;if(o===n)return r;if(o)return{...r,query:{...r.query,[t]:o}};{const{[t]:e,...n}=r.query;return{...r,query:n}}},n)},[]);return[e.query[t],n]})(t):(()=>{const[t,e]=y(),r=o((t,r)=>{e(e=>{let r="function"==typeof t?t(e.query):t;"string"==typeof r&&(r=c("/?"+r.replace(/^\?/,"")).query);const n=c(q(r)).query;return q(n)===q(e.query)?e:{...e,query:n}},r)},[]);return[t.query,r]})()}var v=()=>{const[t,e]=y(),r=o((t,r)=>{e(e=>{const r="function"==typeof t?t(e.hash):t,n="string"!=typeof r?"":r.replace(/^#/,"");return{...e,hash:n}},r)},[]);return[t.hash,r]};function A(t){const[{params:e}]=s(a);return t?t in e?e[t]:"":e||{}}export{a as Context,d as Route,m as Switch,h as default,v as useHash,A as useParams,w as usePath,g as useQuery,y as useUrl};
|
package/package.json
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "crossroad",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "A React library to handle navigation easily in your WebApp",
|
|
5
5
|
"homepage": "https://crossroad.page/",
|
|
6
6
|
"repository": "https://github.com/franciscop/crossroad.git",
|
|
7
7
|
"bugs": "https://github.com/franciscop/crossroad/issues",
|
|
8
8
|
"funding": "https://www.paypal.me/franciscopresencia/19",
|
|
9
9
|
"author": "Francisco Presencia <public@francisco.io> (https://francisco.io/)",
|
|
10
|
+
"documentation": {
|
|
11
|
+
"home": "assets/home.html",
|
|
12
|
+
"menu": {
|
|
13
|
+
"Documentation": "/documentation",
|
|
14
|
+
"Donate": "https://www.paypal.me/franciscopresencia/19",
|
|
15
|
+
"Author": "https://francisco.io/",
|
|
16
|
+
"Github": "https://github.com/franciscop/crossroad"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
10
19
|
"license": "MIT",
|
|
11
20
|
"scripts": {
|
|
12
21
|
"build": "rollup -c",
|
|
22
|
+
"typecheck": "tsc",
|
|
13
23
|
"size": "echo $(gzip -c index.min.js | wc -c) bytes",
|
|
14
24
|
"start": "jest --watch",
|
|
15
|
-
"test": "jest --coverage
|
|
25
|
+
"test": "jest --coverage"
|
|
16
26
|
},
|
|
17
27
|
"keywords": [
|
|
18
28
|
"react",
|
|
@@ -23,26 +33,28 @@
|
|
|
23
33
|
],
|
|
24
34
|
"main": "index.min.js",
|
|
25
35
|
"type": "module",
|
|
26
|
-
"types": "
|
|
36
|
+
"types": "index.d.ts",
|
|
27
37
|
"files": [
|
|
28
|
-
"
|
|
38
|
+
"index.d.ts"
|
|
29
39
|
],
|
|
30
40
|
"devDependencies": {
|
|
31
41
|
"@babel/core": "^7.15.0",
|
|
32
42
|
"@babel/preset-env": "^7.15.0",
|
|
33
43
|
"@babel/preset-react": "^7.14.5",
|
|
44
|
+
"@babel/preset-typescript": "^7.28.5",
|
|
45
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
46
|
+
"@types/node": "^20.19.39",
|
|
34
47
|
"@types/react": "^18.3.4",
|
|
35
48
|
"babel-loader": "^8.2.2",
|
|
36
49
|
"babel-polyfill": "^6.26.0",
|
|
37
|
-
"check-dts": "^0.8.2",
|
|
38
50
|
"jest": "^29.7.0",
|
|
39
51
|
"jest-environment-jsdom": "^29.7.0",
|
|
40
52
|
"react": "^18.3.1",
|
|
41
53
|
"react-dom": "^18.3.1",
|
|
42
54
|
"react-test": "^0.22.1",
|
|
43
|
-
"rollup": "^
|
|
55
|
+
"rollup": "^4.60.1",
|
|
44
56
|
"rollup-plugin-babel": "^4.4.0",
|
|
45
|
-
"
|
|
57
|
+
"typescript": "^5.0.0"
|
|
46
58
|
},
|
|
47
59
|
"peerDependencies": {
|
|
48
60
|
"react": ">=16.8.0"
|
|
@@ -50,10 +62,14 @@
|
|
|
50
62
|
"babel": {
|
|
51
63
|
"presets": [
|
|
52
64
|
"@babel/preset-env",
|
|
53
|
-
"@babel/preset-react"
|
|
65
|
+
"@babel/preset-react",
|
|
66
|
+
"@babel/preset-typescript"
|
|
54
67
|
]
|
|
55
68
|
},
|
|
56
69
|
"jest": {
|
|
57
|
-
"testEnvironment": "jsdom"
|
|
70
|
+
"testEnvironment": "jsdom",
|
|
71
|
+
"moduleNameMapper": {
|
|
72
|
+
"^(\\.{1,2}/.*)\\.js$": "$1"
|
|
73
|
+
}
|
|
58
74
|
}
|
|
59
75
|
}
|
|
File without changes
|