@ossy/router-react 3.9.0 → 3.14.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 +8 -0
- package/build/index.js +1 -1
- package/build/types/jest.config.d.ts +11 -0
- package/build/types/src/get-href-options.d.ts +13 -0
- package/build/types/src/get-href-options.spec.d.ts +1 -0
- package/build/types/src/get-href.d.ts +34 -0
- package/build/types/src/get-href.spec.d.ts +1 -0
- package/build/types/{router-react.d.ts → src/router-react.d.ts} +4 -9
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -258,9 +258,17 @@ const router = useRouter()
|
|
|
258
258
|
<a href={router.getHref('@collection#main?sort=desc')}>Collection</a>
|
|
259
259
|
```
|
|
260
260
|
|
|
261
|
+
Or pass them on the options object (`search` accepts a string or key/value map):
|
|
262
|
+
```tsx
|
|
263
|
+
const router = useRouter()
|
|
264
|
+
<a href={router.getHref({ id: '@collection', hash: 'main', search: { sort: 'desc' } })}>Collection</a>
|
|
265
|
+
<a href={router.getHref({ id: '@storage/home', search: 'location=/docs/' })}>Docs folder</a>
|
|
266
|
+
```
|
|
267
|
+
|
|
261
268
|
#### Switching language (same page)
|
|
262
269
|
Switching the language of the current page is done by navigating to that route.
|
|
263
270
|
To make it easier, if pageId is left out of the getHref call, it will assume you want the current page.
|
|
271
|
+
Language-only links also keep the active search params and hash, so folder/query state (e.g. storage `?location=`) survives the switch. Passing only `search` or only `hash` still preserves the other.
|
|
264
272
|
So to make a language switcher could look as easy as this:
|
|
265
273
|
```
|
|
266
274
|
const router = useRouter()
|
package/build/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import n,{createContext as e,useMemo as t,useState as
|
|
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)return n.replace(/^\?/,"").trim()||void 0;for(var e=new URLSearchParams,t=0,a=Object.entries(n);t<a.length;t++){var o=a[t],i=o[0],r=o[1];null!=r&&""!==r&&e.set(i,r)}return e.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,b=e.supportedLanguages,S=void 0===b?[]:b,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=S.length>1&&!!L,H=p.split("/")[1]||"",O=S.includes(H)?H:L,k=t(function(){return u.of({pages:y,defaultLanguage:L,supportedLanguages:S})},[y,L,S]),F=t(function(){return k.getParamsFromUrl(r)},[k,r]),U=a(function(){return"undefined"==typeof window?w:window.location.search}),I=U[0],W=U[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=k.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 b=f.replace(/^@/,""),S=t.replace(/^@/,""),E=!(null==n?void 0:n.id)||S===b;(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!==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:F,language:O,activeSearch:x,activeHash:"undefined"!=typeof window?window.location.hash:h,getPathname:function(n){return k.getPathname(n)}})},[k,O,F,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:S,params:F,navigate:G,getHref:D,back:J}},[r,y,O,L,S,F,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};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type GetHrefSearch = string | Record<string, string>;
|
|
2
|
+
export declare function normalizeSearch(search: GetHrefSearch | undefined): string | undefined;
|
|
3
|
+
export declare function normalizeHash(value: string | undefined): string | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* Append search/hash to a pathname.
|
|
6
|
+
* Hash-route pageIds (`@page#hash?q=1`) keep `#hash?search`.
|
|
7
|
+
* Standard query (options / preserved location.search) uses `?search#hash`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function appendSearchAndHash(pathname: string, { search, hash, searchFromHashRoute, }: {
|
|
10
|
+
search?: string;
|
|
11
|
+
hash?: string;
|
|
12
|
+
searchFromHashRoute?: boolean;
|
|
13
|
+
}): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type GetHrefSearch } from './get-href-options.js';
|
|
2
|
+
export interface GetHrefRequest {
|
|
3
|
+
id?: string;
|
|
4
|
+
language?: string;
|
|
5
|
+
params?: Record<string, string>;
|
|
6
|
+
/** Query string (`a=1&b=2` or `?a=1`) or key/value map appended to the href. */
|
|
7
|
+
search?: GetHrefSearch;
|
|
8
|
+
/** Hash fragment (`section` or `#section`) appended to the href. */
|
|
9
|
+
hash?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface GetHrefOfActivePageByLanguageRequest {
|
|
12
|
+
language: string;
|
|
13
|
+
search?: GetHrefSearch;
|
|
14
|
+
hash?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function isGetHrefOfActivePageByLanguageRequest(request: any): request is GetHrefOfActivePageByLanguageRequest;
|
|
17
|
+
export type GetHrefContext = {
|
|
18
|
+
activePageId?: string;
|
|
19
|
+
params: Record<string, string>;
|
|
20
|
+
language: string | undefined;
|
|
21
|
+
activeSearch: string;
|
|
22
|
+
activeHash: string;
|
|
23
|
+
getPathname: (args: {
|
|
24
|
+
id: string;
|
|
25
|
+
params?: Record<string, string>;
|
|
26
|
+
language?: string;
|
|
27
|
+
}) => string | undefined;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Build an href from a getHref request and the active route.
|
|
31
|
+
* Same-page language switches keep search and hash independently: omitting
|
|
32
|
+
* one still preserves the other (e.g. `{ language, hash }` keeps `?location=`).
|
|
33
|
+
*/
|
|
34
|
+
export declare function buildGetHref(getHrefRequest: string | GetHrefRequest | GetHrefOfActivePageByLanguageRequest, { activePageId, params, language, activeSearch, activeHash, getPathname, }: GetHrefContext): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import React, { PropsWithChildren } from 'react';
|
|
2
2
|
import { MultiLanguagePage, RouterOptions, SingleLanguagePage } from '@ossy/router';
|
|
3
|
+
import { type GetHrefOfActivePageByLanguageRequest, type GetHrefRequest } from './get-href.js';
|
|
4
|
+
export type { GetHrefOfActivePageByLanguageRequest, GetHrefRequest };
|
|
3
5
|
export interface RouterSearchParams {
|
|
4
6
|
set: (name: string, value: string) => void;
|
|
5
7
|
delete: (name: string) => void;
|
|
8
|
+
/** Apply several query updates in one history entry. `null` or `''` deletes a key. */
|
|
9
|
+
patch: (updates: Record<string, string | null | undefined>) => void;
|
|
6
10
|
}
|
|
7
11
|
type RouterNavigationContextValue = {
|
|
8
12
|
href: string;
|
|
@@ -64,12 +68,3 @@ export interface MultiLanguageRouterPage extends MultiLanguagePage {
|
|
|
64
68
|
element?: React.ReactNode;
|
|
65
69
|
render?: () => React.ReactNode;
|
|
66
70
|
}
|
|
67
|
-
export interface GetHrefRequest {
|
|
68
|
-
id?: string;
|
|
69
|
-
language?: string;
|
|
70
|
-
params?: Record<string, string>;
|
|
71
|
-
}
|
|
72
|
-
export interface GetHrefOfActivePageByLanguageRequest {
|
|
73
|
-
language: string;
|
|
74
|
-
}
|
|
75
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/router-react",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.14.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/ossy-se/packages.git"
|
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
"author": "Ossy <yourfriends@ossy.se> (https://ossy.se)",
|
|
32
32
|
"scripts": {
|
|
33
33
|
"build": "rollup -c rollup.config.js",
|
|
34
|
-
"test": ""
|
|
34
|
+
"test": "jest --verbose",
|
|
35
|
+
"test:watch": "jest --watch"
|
|
35
36
|
},
|
|
36
37
|
"browserslist": {
|
|
37
38
|
"production": [
|
|
@@ -51,7 +52,10 @@
|
|
|
51
52
|
"@babel/eslint-parser": "^7.15.8",
|
|
52
53
|
"@babel/preset-env": "^7.26.0",
|
|
53
54
|
"@babel/preset-react": "^7.24.1",
|
|
54
|
-
"@babel/preset-typescript": "^7.26.0"
|
|
55
|
+
"@babel/preset-typescript": "^7.26.0",
|
|
56
|
+
"@jest/globals": "^30.2.0",
|
|
57
|
+
"jest": "^30.2.0",
|
|
58
|
+
"ts-jest": "^29.1.1"
|
|
55
59
|
},
|
|
56
60
|
"peerDependencies": {
|
|
57
61
|
"@ossy/router": ">=1.0.0 <4.0.0",
|
|
@@ -65,5 +69,5 @@
|
|
|
65
69
|
"/build",
|
|
66
70
|
"README.md"
|
|
67
71
|
],
|
|
68
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "539cc1b14a57e9f6472a89648939e21821dc204d"
|
|
69
73
|
}
|