@ossy/router-react 3.9.0 → 3.11.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 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 o,useEffect as i,useCallback as r,useContext as a}from"react";import{Router as u,isCatchAllRoutePath as l}from"@ossy/router";var d=function(){return d=Object.assign||function(n){for(var e,t=1,o=arguments.length;t<o;t++)for(var i in e=arguments[t])Object.prototype.hasOwnProperty.call(e,i)&&(n[i]=e[i]);return n},d.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var c=e({href:"",pages:[],language:void 0,defaultLanguage:void 0,supportedLanguages:[],params:{},navigate:function(){},getHref:function(){return""},back:function(){}}),f=e({set:function(){},delete:function(){}}),s=function(){return a(f)},v=function(){var n=a(c),e=a(f);return t(function(){return d(d({},n),{searchParams:e})},[n,e])},p=function(e){var a,s,v,p,w=e.url,g=e.pages,h=void 0===g?[]:g,m=e.defaultLanguage,y=void 0===m?void 0:m,L=e.supportedLanguages,E=void 0===L?[]:L,P=e.children;if(w){var b=w.match(/([^?#]*)(\?[^#]*)?(#.*)?/);a=w,s=(null==b?void 0:b[1])||"",v=(null==b?void 0:b[3])||"",p=(null==b?void 0:b[2])||""}else{if("undefined"==typeof window)return n.createElement(n.Fragment,null);a=window.location.href,s=window.location.pathname,v=window.location.hash,p=(null==v?void 0:v.includes("?"))?v.split("?")[1]:window.location.search}var S=E.length>1&&!!y,k=s.split("/")[1]||"",R=E.includes(k)?k:y,O=t(function(){return u.of({pages:h,defaultLanguage:y,supportedLanguages:E})},[h,y,E]),U=t(function(){return O.getParamsFromUrl(a)},[O,a]),j=o(function(){return"undefined"==typeof window?p:window.location.search}),F=j[0],H=j[1];i(function(){if("undefined"!=typeof window){var n=function(){return H(window.location.search)};return window.addEventListener("popstate",n),function(){return window.removeEventListener("popstate",n)}}},[]);var W="undefined"==typeof window&&w?p:F,x=new URLSearchParams(W.replace(/^\?/,"")),B=r(function(n){if("undefined"!=typeof window){var e=new URLSearchParams(window.location.search);n(e);var t=e.toString(),o=t?"".concat(window.location.pathname,"?").concat(t):window.location.pathname;window.history.pushState({},"",o),H(window.location.search)}},[]),N=r(function(n,e){B(function(t){null==e||""===e?t.delete(n):t.set(n,e)})},[B]),q=r(function(n){B(function(e){e.delete(n)})},[B]),z=O.getPageByUrl(a);z||(S&&"/"===s&&"undefined"!=typeof window&&(window.location.href="/"+y),z=S?h.find(function(n){var e,t=n.path,o=null!==(e=null==t?void 0:t[R])&&void 0!==e?e:null==t?void 0:t[y];return l(o)}):h.find(function(n){return l(n.path)})),z||console.warn("[RouterReact] No active page found for",s);var A=r(function(n){var e,t,o,i,r,a,u={};if("string"==typeof n?(t=n,u=U,o=R):(a=n).id||void 0===a.language?(t=(null==n?void 0:n.id)||(null==z?void 0:z.id)||"",u=(null==n?void 0:n.params)||U,o=(null==n?void 0:n.language)||R):(t=null!==(e=null==z?void 0:z.id)&&void 0!==e?e:"",u=U,o=n.language),t.includes("#")){var l=t.split("#");if(t=l[0],(i=l[1]).includes("?")){var d=t.split("?");i=d[0],r=d[1]}}else if(t.includes("?")){var c=t.split("?");t=c[0],r=c[1]}var f=O.getPathname({id:t.replace("@",""),params:u,language:o});return i&&(f=f+"#"+i),r&&(f=f+"?"+r),null!=f?f:""},[O,R,U,z]),C=r(function(n){if("undefined"!=typeof window&&n)if("@back"!==n)if("string"!=typeof n||!n.startsWith("/")||n.startsWith("//")){var e=A(n);e&&(window.location.href=e)}else window.location.assign(n);else window.history.back()},[A]),D=r(function(){"undefined"!=typeof window&&window.history.back()},[]),G=t(function(){var n=Object.fromEntries(x.entries());return d(d({},n),{set:N,delete:q})},[F,N,q]),I=t(function(){return{href:a,pages:h,language:R,defaultLanguage:y,supportedLanguages:E,params:U,navigate:C,getHref:A,back:D}},[a,h,R,y,E,U,C,A,D]);(null==z?void 0:z.redirect)&&C({id:z.redirect});var J=t(function(){var n,e;return null!==(n=null==z?void 0:z.element)&&void 0!==n?n:null===(e=null==z?void 0:z.render)||void 0===e?void 0:e.call(z)},[z]);return z?n.createElement(c.Provider,{value:I},n.createElement(f.Provider,{value:G},P||J)):n.createElement(n.Fragment,null)};export{p as Router,c as RouterContext,f as SearchParamsContext,v as useRouter,s as useSearchParams};
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 f(n){if(null!=n)return n.replace(/^#/,"").trim()||void 0}"function"==typeof SuppressedError&&SuppressedError;var s=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)},w=function(){var n=r(s),e=r(v);return t(function(){return l(l({},n),{searchParams:e})},[n,e])},g=function(e){var r,p,w,g,h=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(h){var R=h.match(/([^?#]*)(\?[^#]*)?(#.*)?/);r=h,p=(null==R?void 0:R[1])||"",w=(null==R?void 0:R[3])||"",g=(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,w=window.location.hash,g=(null==w?void 0:w.includes("?"))?w.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?g: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&&h?g: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,s=void 0===l?"":l,v=e.params,p=e.language,w=e.activeSearch,g=e.activeHash,h=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=s,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)||s||"",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=s.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(w),L=!1),void 0!==u?o=f(u):P&&(o=f(g));var O=h({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:w,getPathname:function(n){return k.getPathname(n)}})},[k,O,F,C,x,w]),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});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(s.Provider,{value:M},n.createElement(v.Provider,{value:K},E||Q)):n.createElement(n.Fragment,null)};export{g as Router,s as RouterContext,v as SearchParamsContext,w as useRouter,p as useSearchParams};
@@ -0,0 +1,11 @@
1
+ declare const _default: {
2
+ preset: string;
3
+ testEnvironment: string;
4
+ extensionsToTreatAsEsm: string[];
5
+ roots: string[];
6
+ moduleDirectories: string[];
7
+ moduleNameMapper: {
8
+ '^(\\.{1,2}/.*)\\.js$': string;
9
+ };
10
+ };
11
+ export default _default;
@@ -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.9.0",
3
+ "version": "3.11.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": "f404be69becb27a1fd853a6ff1903554e76e7d17"
72
+ "gitHead": "53ff8456920e09151c9d88df4a6c3ee958d811eb"
69
73
  }