@react-foundry/router 0.1.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (C) 2019-2025 Crown Copyright
4
+ Copyright (C) 2019-2026 Daniel A.C. Martin
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ this software and associated documentation files (the "Software"), to deal in
8
+ the Software without restriction, including without limitation the rights to
9
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10
+ of the Software, and to permit persons to whom the Software is furnished to do
11
+ so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ React Foundry - Router
2
+ ======================
3
+
4
+ An abstraction layer over the routers from react-router and Next.js.
5
+
6
+
7
+ Using this package
8
+ ------------------
9
+
10
+ First install the package into your project:
11
+
12
+ ```shell
13
+ npm install -S @react-foundry/router
14
+ ```
15
+
16
+ Then use it in your code as follows:
17
+
18
+ ```js
19
+ import router from '@react-foundry/router';
20
+
21
+ // WRITEME
22
+
23
+ ```
24
+
25
+
26
+ Working on this package
27
+ -----------------------
28
+
29
+ Before working on this package you must install its dependencies using
30
+ the following command:
31
+
32
+ ```shell
33
+ pnpm install
34
+ ```
35
+
36
+
37
+ ### Building
38
+
39
+ ```shell
40
+ npm run build
41
+ ```
42
+
43
+
44
+ ### Clean-up
45
+
46
+ ```shell
47
+ npm run clean
48
+ ```
@@ -0,0 +1,11 @@
1
+ import type { LinkProps as _LinkProps, Location as _Location, NavigateFunction } from 'react-router';
2
+ import type { Query } from '@react-foundry/uri';
3
+ export type LinkProps = Omit<_LinkProps, 'relative' | 'reloadDocument' | 'state' | 'unstable_viewTransition'>;
4
+ export type Location = _Location & {
5
+ query: Query;
6
+ };
7
+ export type UseLocation = () => Location;
8
+ export type UseNavigate = () => NavigateFunction;
9
+ export type UseParams = () => Record<string, string | undefined>;
10
+ export type { NavigateFunction };
11
+ export type { NavigateOptions, To } from 'react-router';
package/dist/common.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ import type { FC } from 'react';
2
+ import type { LinkProps, UseNavigate, UseParams } from './common';
3
+ import type { UseIsActive } from './is-active';
4
+ import type { UseLocation } from './location';
5
+ export declare const needSuspense = false;
6
+ export declare const useLocation: UseLocation;
7
+ export declare const useIsActive: UseIsActive;
8
+ export declare const useNavigate: UseNavigate;
9
+ export declare const useParams: UseParams;
10
+ export declare const Link: FC<LinkProps>;
11
+ export type { LinkProps };
package/dist/dummy.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Link = exports.useParams = exports.useNavigate = exports.useIsActive = exports.useLocation = exports.needSuspense = void 0;
4
+ const react_1 = require("react");
5
+ const is_active_1 = require("./is-active");
6
+ const location_1 = require("./location");
7
+ exports.needSuspense = false;
8
+ const _useLocation = () => ({
9
+ state: undefined,
10
+ key: '',
11
+ pathname: '',
12
+ search: '',
13
+ hash: ''
14
+ });
15
+ exports.useLocation = (0, location_1.makeUseLocation)(_useLocation);
16
+ exports.useIsActive = (0, is_active_1.makeUseIsActive)(exports.useLocation);
17
+ const useNavigate = () => () => undefined;
18
+ exports.useNavigate = useNavigate;
19
+ const useParams = () => ({});
20
+ exports.useParams = useParams;
21
+ const Link = ({ to, preventScrollReset, replace, ...attrs }) => {
22
+ const href = (typeof to === 'string'
23
+ ? to
24
+ : (to.pathname || '') + (to.search || '') + (to.hash || ''));
25
+ return (0, react_1.createElement)('a', {
26
+ ...attrs,
27
+ href
28
+ });
29
+ };
30
+ exports.Link = Link;
package/dist/dummy.mjs ADDED
@@ -0,0 +1,24 @@
1
+ import { createElement as h } from 'react';
2
+ import { makeUseIsActive } from './is-active';
3
+ import { makeUseLocation } from './location';
4
+ export const needSuspense = false;
5
+ const _useLocation = () => ({
6
+ state: undefined,
7
+ key: '',
8
+ pathname: '',
9
+ search: '',
10
+ hash: ''
11
+ });
12
+ export const useLocation = makeUseLocation(_useLocation);
13
+ export const useIsActive = makeUseIsActive(useLocation);
14
+ export const useNavigate = () => () => undefined;
15
+ export const useParams = () => ({});
16
+ export const Link = ({ to, preventScrollReset, replace, ...attrs }) => {
17
+ const href = (typeof to === 'string'
18
+ ? to
19
+ : (to.pathname || '') + (to.search || '') + (to.hash || ''));
20
+ return h('a', {
21
+ ...attrs,
22
+ href
23
+ });
24
+ };
@@ -0,0 +1,7 @@
1
+ import type { UseIsActive } from './is-active';
2
+ import type { UseLocation } from './location';
3
+ export declare const needSuspense = false;
4
+ export declare const useLocation: UseLocation;
5
+ export declare const useIsActive: UseIsActive;
6
+ export { Link, useNavigate, useParams } from 'react-router';
7
+ export type { LinkProps } from './dummy';
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useParams = exports.useNavigate = exports.Link = exports.useIsActive = exports.useLocation = exports.needSuspense = void 0;
4
+ const react_router_1 = require("react-router");
5
+ const is_active_1 = require("./is-active");
6
+ const location_1 = require("./location");
7
+ exports.needSuspense = false;
8
+ exports.useLocation = (0, location_1.makeUseLocation)(react_router_1.useLocation);
9
+ exports.useIsActive = (0, is_active_1.makeUseIsActive)(exports.useLocation);
10
+ var react_router_2 = require("react-router");
11
+ Object.defineProperty(exports, "Link", { enumerable: true, get: function () { return react_router_2.Link; } });
12
+ Object.defineProperty(exports, "useNavigate", { enumerable: true, get: function () { return react_router_2.useNavigate; } });
13
+ Object.defineProperty(exports, "useParams", { enumerable: true, get: function () { return react_router_2.useParams; } });
package/dist/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ import { useLocation as _useLocation } from 'react-router';
2
+ import { makeUseIsActive } from './is-active';
3
+ import { makeUseLocation } from './location';
4
+ export const needSuspense = false;
5
+ export const useLocation = makeUseLocation(_useLocation);
6
+ export const useIsActive = makeUseIsActive(useLocation);
7
+ export { Link, useNavigate, useParams } from 'react-router';
@@ -0,0 +1,3 @@
1
+ import type { UseLocation } from './common';
2
+ export type UseIsActive = () => (href: string, exact?: boolean) => boolean;
3
+ export declare const makeUseIsActive: (useLocation: UseLocation) => UseIsActive;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeUseIsActive = void 0;
4
+ const uri_1 = require("@react-foundry/uri");
5
+ const includes = (haystack, needle) => {
6
+ const subIncludes = (haystack, needle) => (Array.isArray(needle) ? (needle.length === haystack.length &&
7
+ needle.reduce((acc, cur, idx) => acc && subIncludes(haystack[idx], cur), true)) : (typeof needle === 'object' ? (typeof haystack === 'object' &&
8
+ Object.keys(needle).reduce((acc, cur) => acc && subIncludes(haystack[cur], needle[cur]), true)) : (needle === haystack)));
9
+ return subIncludes(haystack, needle);
10
+ };
11
+ const makeUseIsActive = (useLocation) => () => {
12
+ const location = useLocation();
13
+ const isActive = (href, exact = true) => {
14
+ const target = uri_1.URI.parse(href, location.pathname);
15
+ const dir = (target.pathname.endsWith('/')
16
+ ? target.pathname
17
+ : target.pathname + '/');
18
+ const pathStart = (target.pathname === '' ||
19
+ location.pathname.startsWith(dir));
20
+ const pathMatch = (target.pathname === '' ||
21
+ location.pathname === target.pathname);
22
+ const queryMatch = includes(location.query, target.query);
23
+ const activeExact = !!(pathMatch && queryMatch);
24
+ const active = (exact
25
+ ? activeExact
26
+ : !!(activeExact || (pathStart && queryMatch)));
27
+ return active;
28
+ };
29
+ return isActive;
30
+ };
31
+ exports.makeUseIsActive = makeUseIsActive;
@@ -0,0 +1,27 @@
1
+ import { URI } from '@react-foundry/uri';
2
+ const includes = (haystack, needle) => {
3
+ const subIncludes = (haystack, needle) => (Array.isArray(needle) ? (needle.length === haystack.length &&
4
+ needle.reduce((acc, cur, idx) => acc && subIncludes(haystack[idx], cur), true)) : (typeof needle === 'object' ? (typeof haystack === 'object' &&
5
+ Object.keys(needle).reduce((acc, cur) => acc && subIncludes(haystack[cur], needle[cur]), true)) : (needle === haystack)));
6
+ return subIncludes(haystack, needle);
7
+ };
8
+ export const makeUseIsActive = (useLocation) => () => {
9
+ const location = useLocation();
10
+ const isActive = (href, exact = true) => {
11
+ const target = URI.parse(href, location.pathname);
12
+ const dir = (target.pathname.endsWith('/')
13
+ ? target.pathname
14
+ : target.pathname + '/');
15
+ const pathStart = (target.pathname === '' ||
16
+ location.pathname.startsWith(dir));
17
+ const pathMatch = (target.pathname === '' ||
18
+ location.pathname === target.pathname);
19
+ const queryMatch = includes(location.query, target.query);
20
+ const activeExact = !!(pathMatch && queryMatch);
21
+ const active = (exact
22
+ ? activeExact
23
+ : !!(activeExact || (pathStart && queryMatch)));
24
+ return active;
25
+ };
26
+ return isActive;
27
+ };
@@ -0,0 +1,6 @@
1
+ import type { Location as _Location } from 'react-router';
2
+ import type { Location, UseLocation } from './common';
3
+ export type _UseLocation = () => _Location;
4
+ export declare const enhanceLocation: (location: _Location) => Location;
5
+ export declare const makeUseLocation: (useLocation: _UseLocation) => UseLocation;
6
+ export type { UseLocation, };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeUseLocation = exports.enhanceLocation = void 0;
4
+ const uri_1 = require("@react-foundry/uri");
5
+ const enhanceLocation = (location) => ({
6
+ ...location,
7
+ query: (0, uri_1.qsParse)(location.search)
8
+ });
9
+ exports.enhanceLocation = enhanceLocation;
10
+ const makeUseLocation = (useLocation) => () => ((0, exports.enhanceLocation)(useLocation()));
11
+ exports.makeUseLocation = makeUseLocation;
@@ -0,0 +1,6 @@
1
+ import { qsParse } from '@react-foundry/uri';
2
+ export const enhanceLocation = (location) => ({
3
+ ...location,
4
+ query: qsParse(location.search)
5
+ });
6
+ export const makeUseLocation = (useLocation) => () => (enhanceLocation(useLocation()));
package/dist/next.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { FC } from 'react';
2
+ import type { LinkProps, UseNavigate } from './common';
3
+ import type { UseIsActive } from './is-active';
4
+ import type { UseLocation } from './location';
5
+ export declare const needSuspense = true;
6
+ export declare const useLocation: UseLocation;
7
+ export declare const useIsActive: UseIsActive;
8
+ export declare const useNavigate: UseNavigate;
9
+ export declare const Link: FC<LinkProps>;
10
+ export { useParams } from 'next/navigation';
11
+ export type { LinkProps };
package/dist/next.js ADDED
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useParams = exports.Link = exports.useNavigate = exports.useIsActive = exports.useLocation = exports.needSuspense = void 0;
7
+ const react_1 = require("react");
8
+ const navigation_1 = require("next/navigation");
9
+ const link_1 = __importDefault(require("next/link"));
10
+ const is_active_1 = require("./is-active");
11
+ const location_1 = require("./location");
12
+ exports.needSuspense = true;
13
+ const _useLocation = () => {
14
+ const pathname = (0, navigation_1.usePathname)();
15
+ const searchParams = (0, navigation_1.useSearchParams)();
16
+ const search = (searchParams.size
17
+ ? '?' + searchParams.toString()
18
+ : '');
19
+ const location = {
20
+ state: undefined,
21
+ key: '',
22
+ pathname,
23
+ search,
24
+ hash: ''
25
+ };
26
+ return location;
27
+ };
28
+ exports.useLocation = (0, location_1.makeUseLocation)(_useLocation);
29
+ exports.useIsActive = (0, is_active_1.makeUseIsActive)(exports.useLocation);
30
+ const useNavigate = () => {
31
+ const router = (0, navigation_1.useRouter)();
32
+ const navigate = (to, options = {}) => {
33
+ if (typeof to === 'number') {
34
+ if (to === -1) {
35
+ router.back();
36
+ }
37
+ }
38
+ else {
39
+ const href = (to === 'string'
40
+ ? to
41
+ : to.toString());
42
+ const nextOptions = {
43
+ scroll: !options.preventScrollReset
44
+ };
45
+ if (options.replace) {
46
+ router.replace(href, nextOptions);
47
+ }
48
+ else {
49
+ router.push(href, nextOptions);
50
+ }
51
+ }
52
+ };
53
+ return navigate;
54
+ };
55
+ exports.useNavigate = useNavigate;
56
+ const prefetchMap = new Map([
57
+ [undefined, null],
58
+ ['none', false],
59
+ ['intent', true],
60
+ ['render', true],
61
+ ['viewport', true]
62
+ ]);
63
+ const Link = ({ to, prefetch: _prefetch, preventScrollReset, replace, ...attrs }) => {
64
+ const href = (typeof to === 'string'
65
+ ? to
66
+ : {
67
+ ...to,
68
+ hash: (to.hash !== '#'
69
+ ? to.hash
70
+ : undefined)
71
+ });
72
+ const prefetch = prefetchMap.get(_prefetch);
73
+ return (0, react_1.createElement)(link_1.default, {
74
+ ...attrs,
75
+ href,
76
+ prefetch,
77
+ replace,
78
+ scroll: !preventScrollReset
79
+ });
80
+ };
81
+ exports.Link = Link;
82
+ var navigation_2 = require("next/navigation");
83
+ Object.defineProperty(exports, "useParams", { enumerable: true, get: function () { return navigation_2.useParams; } });
package/dist/next.mjs ADDED
@@ -0,0 +1,74 @@
1
+ import { createElement as h } from 'react';
2
+ import { usePathname, useRouter, useSearchParams } from 'next/navigation';
3
+ import _Link from 'next/link';
4
+ import { makeUseIsActive } from './is-active';
5
+ import { makeUseLocation } from './location';
6
+ export const needSuspense = true;
7
+ const _useLocation = () => {
8
+ const pathname = usePathname();
9
+ const searchParams = useSearchParams();
10
+ const search = (searchParams.size
11
+ ? '?' + searchParams.toString()
12
+ : '');
13
+ const location = {
14
+ state: undefined,
15
+ key: '',
16
+ pathname,
17
+ search,
18
+ hash: ''
19
+ };
20
+ return location;
21
+ };
22
+ export const useLocation = makeUseLocation(_useLocation);
23
+ export const useIsActive = makeUseIsActive(useLocation);
24
+ export const useNavigate = () => {
25
+ const router = useRouter();
26
+ const navigate = (to, options = {}) => {
27
+ if (typeof to === 'number') {
28
+ if (to === -1) {
29
+ router.back();
30
+ }
31
+ }
32
+ else {
33
+ const href = (to === 'string'
34
+ ? to
35
+ : to.toString());
36
+ const nextOptions = {
37
+ scroll: !options.preventScrollReset
38
+ };
39
+ if (options.replace) {
40
+ router.replace(href, nextOptions);
41
+ }
42
+ else {
43
+ router.push(href, nextOptions);
44
+ }
45
+ }
46
+ };
47
+ return navigate;
48
+ };
49
+ const prefetchMap = new Map([
50
+ [undefined, null],
51
+ ['none', false],
52
+ ['intent', true],
53
+ ['render', true],
54
+ ['viewport', true]
55
+ ]);
56
+ export const Link = ({ to, prefetch: _prefetch, preventScrollReset, replace, ...attrs }) => {
57
+ const href = (typeof to === 'string'
58
+ ? to
59
+ : {
60
+ ...to,
61
+ hash: (to.hash !== '#'
62
+ ? to.hash
63
+ : undefined)
64
+ });
65
+ const prefetch = prefetchMap.get(_prefetch);
66
+ return h(_Link, {
67
+ ...attrs,
68
+ href,
69
+ prefetch,
70
+ replace,
71
+ scroll: !preventScrollReset
72
+ });
73
+ };
74
+ export { useParams } from 'next/navigation';
@@ -0,0 +1,7 @@
1
+ import type { UseIsActive } from './is-active';
2
+ import type { UseLocation } from './location';
3
+ export declare const needSuspense = false;
4
+ export declare const useLocation: UseLocation;
5
+ export declare const useIsActive: UseIsActive;
6
+ export { Link, useNavigate, useParams } from '@remix-run/react';
7
+ export type { LinkProps } from './dummy';
package/dist/remix.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useParams = exports.useNavigate = exports.Link = exports.useIsActive = exports.useLocation = exports.needSuspense = void 0;
4
+ const react_1 = require("@remix-run/react");
5
+ const is_active_1 = require("./is-active");
6
+ const location_1 = require("./location");
7
+ exports.needSuspense = false;
8
+ exports.useLocation = (0, location_1.makeUseLocation)(react_1.useLocation);
9
+ exports.useIsActive = (0, is_active_1.makeUseIsActive)(exports.useLocation);
10
+ var react_2 = require("@remix-run/react");
11
+ Object.defineProperty(exports, "Link", { enumerable: true, get: function () { return react_2.Link; } });
12
+ Object.defineProperty(exports, "useNavigate", { enumerable: true, get: function () { return react_2.useNavigate; } });
13
+ Object.defineProperty(exports, "useParams", { enumerable: true, get: function () { return react_2.useParams; } });
package/dist/remix.mjs ADDED
@@ -0,0 +1,7 @@
1
+ import { useLocation as _useLocation } from '@remix-run/react';
2
+ import { makeUseIsActive } from './is-active';
3
+ import { makeUseLocation } from './location';
4
+ export const needSuspense = false;
5
+ export const useLocation = makeUseLocation(_useLocation);
6
+ export const useIsActive = makeUseIsActive(useLocation);
7
+ export { Link, useNavigate, useParams } from '@remix-run/react';
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@react-foundry/router",
3
+ "version": "0.1.0",
4
+ "description": "An abstraction layer over the routers from react-router and Next.js.",
5
+ "main": "dist/index.js",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.mjs",
10
+ "require": "./dist/index.js",
11
+ "default": "./dist/index.mjs"
12
+ },
13
+ "./dummy": {
14
+ "types": "./dist/dummy.d.ts",
15
+ "import": "./dist/dummy.mjs",
16
+ "require": "./dist/dummy.js",
17
+ "default": "./dist/dummy.mjs"
18
+ },
19
+ "./next": {
20
+ "types": "./dist/next.d.ts",
21
+ "import": "./dist/next.mjs",
22
+ "require": "./dist/next.js",
23
+ "default": "./dist/next.mjs"
24
+ },
25
+ "./remix": {
26
+ "types": "./dist/remix.d.ts",
27
+ "import": "./dist/remix.mjs",
28
+ "require": "./dist/remix.js",
29
+ "default": "./dist/remix.mjs"
30
+ }
31
+ },
32
+ "files": [
33
+ "/dist"
34
+ ],
35
+ "author": "Daniel A.C. Martin <npm@daniel-martin.co.uk> (http://daniel-martin.co.uk/)",
36
+ "license": "MIT",
37
+ "engines": {
38
+ "node": ">=12.0.0"
39
+ },
40
+ "dependencies": {
41
+ "@react-foundry/uri": "^0.1.0"
42
+ },
43
+ "peerDependencies": {
44
+ "@remix-run/react": ">2.17",
45
+ "next": ">15.5",
46
+ "react-router": ">7.8"
47
+ },
48
+ "peerDependenciesMeta": {
49
+ "@remix-run/react": {
50
+ "optional": true
51
+ },
52
+ "next": {
53
+ "optional": true
54
+ },
55
+ "react-router": {
56
+ "optional": true
57
+ }
58
+ },
59
+ "devDependencies": {
60
+ "@remix-run/react": "2.17.4",
61
+ "@types/react": "19.2.9",
62
+ "jest": "30.2.0",
63
+ "jest-environment-jsdom": "30.2.0",
64
+ "next": "16.1.4",
65
+ "ts-jest": "29.4.6",
66
+ "typescript": "5.9.3"
67
+ },
68
+ "scripts": {
69
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest",
70
+ "build": "npm run build:esm && npm run build:cjs",
71
+ "build:esm": "tsc -m es2022 && find dist -name '*.js' -exec sh -c 'mv \"$0\" \"${0%.js}.mjs\"' {} \\;",
72
+ "build:cjs": "tsc",
73
+ "clean": "rm -rf dist tsconfig.tsbuildinfo"
74
+ },
75
+ "module": "dist/index.mjs",
76
+ "typings": "dist/index.d.ts"
77
+ }