@ossy/router-react 3.17.0 → 3.27.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/README.md +5 -2
- package/build/index.js +1 -1
- package/build/types/src/get-href-options.d.ts +5 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -262,9 +262,11 @@ Or pass them on the options object (`search` accepts a string or key/value map):
|
|
|
262
262
|
```tsx
|
|
263
263
|
const router = useRouter()
|
|
264
264
|
<a href={router.getHref({ id: '@collection', hash: 'main', search: { sort: 'desc' } })}>Collection</a>
|
|
265
|
-
<a href={router.getHref({ id: '@storage/home', search: '
|
|
265
|
+
<a href={router.getHref({ id: '@storage/home', search: { location: '/docs/' } })}>Docs folder</a>
|
|
266
266
|
```
|
|
267
267
|
|
|
268
|
+
Search param values are plain decoded strings in app code. `getHref({ search })` / `useSearchParams().set` percent-encode them into the URL, and `useSearchParams()` / `router.searchParams` return them decoded again — so values with spaces, slashes, `&`, or other reserved characters stay intact.
|
|
269
|
+
|
|
268
270
|
#### Switching language (same page)
|
|
269
271
|
Switching the language of the current page is done by navigating to that route.
|
|
270
272
|
To make it easier, if pageId is left out of the getHref call, it will assume you want the current page.
|
|
@@ -332,8 +334,9 @@ function StorageBrowser () {
|
|
|
332
334
|
}
|
|
333
335
|
```
|
|
334
336
|
|
|
335
|
-
- `set(name, value)` — updates the query via `pushState` and re-renders subscribers. Empty values remove the key.
|
|
337
|
+
- `set(name, value)` — updates the query via `pushState` and re-renders subscribers. Pass plain values (encoding is automatic). Empty values remove the key.
|
|
336
338
|
- `delete(name)` — removes a query key.
|
|
339
|
+
- Keys on the returned object (e.g. `searchParams.location`) are decoded.
|
|
337
340
|
- Browser back/forward syncs via `popstate`.
|
|
338
341
|
- SSR: pass `url` on `<Router url={requestUrl} />` so the initial query matches the request; on the client, `window.location.search` is followed after hydration.
|
|
339
342
|
|
package/build/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import n,{createContext as e,useMemo as t,useState as a,useEffect as o,useCallback as i,useContext as r}from"react";import{Router as u,isCatchAllRoutePath as c}from"@ossy/router";var l=function(){return l=Object.assign||function(n){for(var e,t=1,a=arguments.length;t<a;t++)for(var o in e=arguments[t])Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=e[o]);return n},l.apply(this,arguments)};function d(n){if(null!=n){if("string"==typeof n)
|
|
1
|
+
import n,{createContext as e,useMemo as t,useState as a,useEffect as o,useCallback as i,useContext as r}from"react";import{Router as u,isCatchAllRoutePath as c}from"@ossy/router";var l=function(){return l=Object.assign||function(n){for(var e,t=1,a=arguments.length;t<a;t++)for(var o in e=arguments[t])Object.prototype.hasOwnProperty.call(e,o)&&(n[o]=e[o]);return n},l.apply(this,arguments)};function d(n){if(null!=n){if("string"==typeof n){var e=n.replace(/^\?/,"").trim();if(!e)return;return new URLSearchParams(e).toString()||void 0}for(var t=new URLSearchParams,a=0,o=Object.entries(n);a<o.length;a++){var i=o[a],r=i[0],u=i[1];null!=u&&""!==u&&t.set(r,u)}return t.toString()||void 0}}function s(n){if(null!=n)return n.replace(/^#/,"").trim()||void 0}"function"==typeof SuppressedError&&SuppressedError;var f=e({href:"",pages:[],language:void 0,defaultLanguage:void 0,supportedLanguages:[],params:{},navigate:function(){},getHref:function(){return""},back:function(){}}),v=e({set:function(){},delete:function(){},patch:function(){}}),p=function(){return r(v)},h=function(){var n=r(f),e=r(v);return t(function(){return l(l({},n),{searchParams:e})},[n,e])},w=function(e){var r,p,h,w,g=e.url,m=e.pages,y=void 0===m?[]:m,P=e.defaultLanguage,L=void 0===P?void 0:P,S=e.supportedLanguages,b=void 0===S?[]:S,E=e.children;if(g){var R=g.match(/([^?#]*)(\?[^#]*)?(#.*)?/);r=g,p=(null==R?void 0:R[1])||"",h=(null==R?void 0:R[3])||"",w=(null==R?void 0:R[2])||""}else{if("undefined"==typeof window)return n.createElement(n.Fragment,null);r=window.location.href,p=window.location.pathname,h=window.location.hash,w=(null==h?void 0:h.includes("?"))?h.split("?")[1]:window.location.search}var j=b.length>1&&!!L,H=p.split("/")[1]||"",O=b.includes(H)?H:L,U=t(function(){return u.of({pages:y,defaultLanguage:L,supportedLanguages:b})},[y,L,b]),k=t(function(){return U.getParamsFromUrl(r)},[U,r]),F=a(function(){return"undefined"==typeof window?w:window.location.search}),I=F[0],W=F[1];o(function(){if("undefined"!=typeof window){var n=function(){return W(window.location.search)};return window.addEventListener("popstate",n),function(){return window.removeEventListener("popstate",n)}}},[]);var x="undefined"==typeof window&&g?w:I,B=new URLSearchParams(x.replace(/^\?/,"")),N=i(function(n){if("undefined"!=typeof window){var e=new URLSearchParams(window.location.search);n(e);var t=e.toString(),a=t?"".concat(window.location.pathname,"?").concat(t):window.location.pathname;a!=="".concat(window.location.pathname).concat(window.location.search)&&(window.history.pushState({},"",a),W(window.location.search))}},[]),q=i(function(n,e){N(function(t){null==e||""===e?t.delete(n):t.set(n,e)})},[N]),z=i(function(n){N(function(e){e.delete(n)})},[N]),A=i(function(n){n&&"object"==typeof n&&N(function(e){for(var t=0,a=Object.entries(n);t<a.length;t++){var o=a[t],i=o[0],r=o[1];null==r||""===r?e.delete(i):e.set(i,r)}})},[N]),C=U.getPageByUrl(r);C||(j&&"/"===p&&"undefined"!=typeof window&&(window.location.href="/"+L),C=j?y.find(function(n){var e,t=n.path,a=null!==(e=null==t?void 0:t[O])&&void 0!==e?e:null==t?void 0:t[L];return c(a)}):y.find(function(n){return c(n.path)})),C||console.warn("[RouterReact] No active page found for",p);var D=i(function(n){var e;return function(n,e){var t,a,o,i,r,u,c,l=e.activePageId,f=void 0===l?"":l,v=e.params,p=e.language,h=e.activeSearch,w=e.activeHash,g=e.getPathname,m={},y=!1,P=!1,L=!1;if("string"==typeof n)t=n,m=v,a=p;else if((c=n)&&!c.id&&void 0!==c.language)t=f,m=v,a=n.language,y=void 0===(r=n.search),P=void 0===(u=n.hash);else{t=(null==n?void 0:n.id)||f||"",m=(null==n?void 0:n.params)||v,a=(null==n?void 0:n.language)||p,r=null==n?void 0:n.search,u=null==n?void 0:n.hash;var S=f.replace(/^@/,""),b=t.replace(/^@/,""),E=!(null==n?void 0:n.id)||b===S;(null==n?void 0:n.language)&&E&&(y=void 0===r,P=void 0===u)}if(t.includes("#")){var R=t.split("#");if(t=R[0],(o=R[1]).includes("?")){var j=o.split("?");o=j[0],i=j[1],L=!0}}else if(t.includes("?")){var H=t.split("?");t=H[0],i=H[1]}void 0!==r?(i=d(r),L=!1):y?(i=d(h),L=!1):void 0!==i&&(i=d(i)),void 0!==u?o=s(u):P&&(o=s(w));var O=g({id:t.replace("@",""),params:m,language:a});return function(n,e){var t=e.search,a=e.hash,o=e.searchFromHashRoute,i=n;return void 0!==o&&o?(a&&(i="".concat(i,"#").concat(a)),t&&(i="".concat(i,"?").concat(t))):(t&&(i="".concat(i,"?").concat(t)),a&&(i="".concat(i,"#").concat(a))),i}(null!=O?O:"",{search:i,hash:o,searchFromHashRoute:L})}(n,{activePageId:null!==(e=null==C?void 0:C.id)&&void 0!==e?e:"",params:k,language:O,activeSearch:x,activeHash:"undefined"!=typeof window?window.location.hash:h,getPathname:function(n){return U.getPathname(n)}})},[U,O,k,C,x,h]),G=i(function(n){if("undefined"!=typeof window&&n)if("@back"!==n)if("string"!=typeof n||!n.startsWith("/")||n.startsWith("//")){var e=D(n);e&&(window.location.href=e)}else window.location.assign(n);else window.history.back()},[D]),J=i(function(){"undefined"!=typeof window&&window.history.back()},[]),K=t(function(){var n=Object.fromEntries(B.entries());return l(l({},n),{set:q,delete:z,patch:A})},[I,q,z,A]),M=t(function(){return{href:r,pages:y,language:O,defaultLanguage:L,supportedLanguages:b,params:k,navigate:G,getHref:D,back:J}},[r,y,O,L,b,k,G,D,J]);(null==C?void 0:C.redirect)&&G({id:C.redirect,search:x});var Q=t(function(){var n,e;return null!==(n=null==C?void 0:C.element)&&void 0!==n?n:null===(e=null==C?void 0:C.render)||void 0===e?void 0:e.call(C)},[C]);return C?n.createElement(f.Provider,{value:M},n.createElement(v.Provider,{value:K},E||Q)):n.createElement(n.Fragment,null)};export{w as Router,f as RouterContext,v as SearchParamsContext,h as useRouter,p as useSearchParams};
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export type GetHrefSearch = string | Record<string, string>;
|
|
2
|
+
/**
|
|
3
|
+
* Normalize a search value into a query string without a leading `?`.
|
|
4
|
+
* Object maps and query strings are both serialized via `URLSearchParams`,
|
|
5
|
+
* so callers pass plain decoded values and encoding happens only here.
|
|
6
|
+
*/
|
|
2
7
|
export declare function normalizeSearch(search: GetHrefSearch | undefined): string | undefined;
|
|
3
8
|
export declare function normalizeHash(value: string | undefined): string | undefined;
|
|
4
9
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/router-react",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.27.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/ossy-se/packages.git"
|
|
@@ -69,5 +69,5 @@
|
|
|
69
69
|
"/build",
|
|
70
70
|
"README.md"
|
|
71
71
|
],
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "f72042c42723468c010d0d9744ad69257143d0b9"
|
|
73
73
|
}
|