@ryanfw/universal-route 6.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/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Universal Route
2
+
3
+ Universal Route is a lightweight, type-safe routing library for React.
4
+
5
+ A Router keeps your UI in sync with the URL. Universal Route is designed to be small and focused, helping small-to-medium React projects stay "frameworkless" by avoiding the overhead and complexity of larger full-stack frameworks.
6
+
7
+ ### Features
8
+
9
+ - **TypeScript**: Written in TypeScript for type safety and great developer experience.
10
+ - **Lightweight**: Covers specific use cases without bloat.
11
+ - **Tested**: High test coverage for reliability.
12
+ - **Performance**: Optimized for speed with performance measurements.
13
+ - **Demo**: Includes a demo to get you started quickly.
14
+
15
+ ### How it Works
16
+
17
+ - Associate [URL patterns](https://github.com/pillarjs/path-to-regexp) with a top-level React component.
18
+ - Make an XHR request on each history transition, switching the displayed component after success.
19
+ - Start / stop progress page loading indicators automatically.
20
+
21
+ ### Defining Routes
22
+
23
+ routes.ts
24
+
25
+ ```ts
26
+ // NPM dependencies
27
+ import React from "react";
28
+ import { helper } from "@ryanfw/universal-route";
29
+
30
+ // React component dependencies
31
+ import Home from "./components/Home";
32
+ import About from "./components/About";
33
+ import Staff from "./components/Staff";
34
+
35
+ // associate paths to components
36
+ const routes = {
37
+ "/": Home,
38
+ "/about": About,
39
+ "/staff/:name": Staff,
40
+ };
41
+
42
+ // export the routes after preparing them
43
+ export default helper.prepare(routes);
44
+ ```
@@ -0,0 +1,88 @@
1
+ import React, { ComponentType, AnchorHTMLAttributes, Context } from 'react';
2
+ import { BrowserHistory, MemoryHistory } from 'history';
3
+
4
+ interface RouteDefinition {
5
+ path: string;
6
+ Component?: ComponentType<any>;
7
+ element?: ComponentType<any>;
8
+ render?: ComponentType<any>;
9
+ reducerKey?: string;
10
+ }
11
+ type RouteMapValue = ComponentType<any> | [ComponentType<any>] | [ComponentType<any>, string] | {
12
+ Component?: ComponentType<any>;
13
+ element?: ComponentType<any>;
14
+ render?: ComponentType<any>;
15
+ reducerKey?: string;
16
+ };
17
+ type RouteMap = Record<string, RouteMapValue>;
18
+ type RoutesInput = RouteDefinition[] | RouteMap;
19
+ interface PreparedRoute {
20
+ path: string;
21
+ Component: ComponentType<any>;
22
+ reducerKey?: string;
23
+ matcher: (pathname: string) => {
24
+ params: Record<string, string>;
25
+ } | null;
26
+ }
27
+ interface RouteMatchResult {
28
+ Component: ComponentType<any>;
29
+ params: Record<string, string>;
30
+ reducerKey?: string;
31
+ }
32
+ declare const _default: {
33
+ prepare: (routes?: RoutesInput) => PreparedRoute[];
34
+ match: (routes: RoutesInput | PreparedRoute[], pathname: string) => RouteMatchResult;
35
+ };
36
+
37
+ type LinkTo = string | {
38
+ pathname: string;
39
+ search?: string;
40
+ hash?: string;
41
+ };
42
+ interface NavigateOptions {
43
+ replace?: boolean;
44
+ state?: unknown;
45
+ }
46
+ interface LinkProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "href"> {
47
+ to: LinkTo;
48
+ replace?: boolean;
49
+ state?: unknown;
50
+ }
51
+ interface StoreContextValue {
52
+ state: Record<string, any>;
53
+ dispatch: ((action: Record<string, unknown>) => void) | false;
54
+ pageRefresher?: unknown;
55
+ }
56
+ declare const navigate: (to: LinkTo, options?: NavigateOptions) => void;
57
+ declare const Link: React.FC<LinkProps>;
58
+ declare const createRouter: (routes: RoutesInput, storeContext?: Context<StoreContextValue>) => React.FC<Record<string, any>>;
59
+
60
+ declare const appHistory: BrowserHistory | null;
61
+ declare const makeMemoryHistory: (initialEntries?: string[]) => MemoryHistory;
62
+
63
+ interface ProgressAPI {
64
+ start(): void;
65
+ done(): void;
66
+ }
67
+ interface HandleHistoryChangeOptions {
68
+ history?: BrowserHistory | null;
69
+ fetchImpl?: ((url: string, init?: RequestInit) => Promise<Response>) | null;
70
+ setTitle?: (title: string) => void;
71
+ progress?: ProgressAPI;
72
+ }
73
+ declare function handleHistoryChange(dispatch: (action: {
74
+ type: string;
75
+ data: Record<string, unknown>;
76
+ }) => void, { history, fetchImpl, setTitle, progress, }?: HandleHistoryChangeOptions): () => void;
77
+
78
+ interface ScrollPosition {
79
+ x: number;
80
+ y: number;
81
+ }
82
+ declare const getScrollPosition: () => ScrollPosition;
83
+ declare const setScrollToSessionStorage: () => void;
84
+ declare const setScrollForKey: (key: string, pos?: ScrollPosition) => void;
85
+ declare function getScrollFromSessionStorage(key: "*"): Record<string, ScrollPosition>;
86
+ declare function getScrollFromSessionStorage(key: string): ScrollPosition | null;
87
+
88
+ export { Link, type LinkProps, type LinkTo, type NavigateOptions, type PreparedRoute, type RouteDefinition, type RouteMap, type RouteMatchResult, type RoutesInput, type ScrollPosition, appHistory, createRouter, getScrollFromSessionStorage, getScrollPosition, handleHistoryChange, makeMemoryHistory, navigate, _default as routesHelper, setScrollForKey, setScrollToSessionStorage };
@@ -0,0 +1,88 @@
1
+ import React, { ComponentType, AnchorHTMLAttributes, Context } from 'react';
2
+ import { BrowserHistory, MemoryHistory } from 'history';
3
+
4
+ interface RouteDefinition {
5
+ path: string;
6
+ Component?: ComponentType<any>;
7
+ element?: ComponentType<any>;
8
+ render?: ComponentType<any>;
9
+ reducerKey?: string;
10
+ }
11
+ type RouteMapValue = ComponentType<any> | [ComponentType<any>] | [ComponentType<any>, string] | {
12
+ Component?: ComponentType<any>;
13
+ element?: ComponentType<any>;
14
+ render?: ComponentType<any>;
15
+ reducerKey?: string;
16
+ };
17
+ type RouteMap = Record<string, RouteMapValue>;
18
+ type RoutesInput = RouteDefinition[] | RouteMap;
19
+ interface PreparedRoute {
20
+ path: string;
21
+ Component: ComponentType<any>;
22
+ reducerKey?: string;
23
+ matcher: (pathname: string) => {
24
+ params: Record<string, string>;
25
+ } | null;
26
+ }
27
+ interface RouteMatchResult {
28
+ Component: ComponentType<any>;
29
+ params: Record<string, string>;
30
+ reducerKey?: string;
31
+ }
32
+ declare const _default: {
33
+ prepare: (routes?: RoutesInput) => PreparedRoute[];
34
+ match: (routes: RoutesInput | PreparedRoute[], pathname: string) => RouteMatchResult;
35
+ };
36
+
37
+ type LinkTo = string | {
38
+ pathname: string;
39
+ search?: string;
40
+ hash?: string;
41
+ };
42
+ interface NavigateOptions {
43
+ replace?: boolean;
44
+ state?: unknown;
45
+ }
46
+ interface LinkProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "href"> {
47
+ to: LinkTo;
48
+ replace?: boolean;
49
+ state?: unknown;
50
+ }
51
+ interface StoreContextValue {
52
+ state: Record<string, any>;
53
+ dispatch: ((action: Record<string, unknown>) => void) | false;
54
+ pageRefresher?: unknown;
55
+ }
56
+ declare const navigate: (to: LinkTo, options?: NavigateOptions) => void;
57
+ declare const Link: React.FC<LinkProps>;
58
+ declare const createRouter: (routes: RoutesInput, storeContext?: Context<StoreContextValue>) => React.FC<Record<string, any>>;
59
+
60
+ declare const appHistory: BrowserHistory | null;
61
+ declare const makeMemoryHistory: (initialEntries?: string[]) => MemoryHistory;
62
+
63
+ interface ProgressAPI {
64
+ start(): void;
65
+ done(): void;
66
+ }
67
+ interface HandleHistoryChangeOptions {
68
+ history?: BrowserHistory | null;
69
+ fetchImpl?: ((url: string, init?: RequestInit) => Promise<Response>) | null;
70
+ setTitle?: (title: string) => void;
71
+ progress?: ProgressAPI;
72
+ }
73
+ declare function handleHistoryChange(dispatch: (action: {
74
+ type: string;
75
+ data: Record<string, unknown>;
76
+ }) => void, { history, fetchImpl, setTitle, progress, }?: HandleHistoryChangeOptions): () => void;
77
+
78
+ interface ScrollPosition {
79
+ x: number;
80
+ y: number;
81
+ }
82
+ declare const getScrollPosition: () => ScrollPosition;
83
+ declare const setScrollToSessionStorage: () => void;
84
+ declare const setScrollForKey: (key: string, pos?: ScrollPosition) => void;
85
+ declare function getScrollFromSessionStorage(key: "*"): Record<string, ScrollPosition>;
86
+ declare function getScrollFromSessionStorage(key: string): ScrollPosition | null;
87
+
88
+ export { Link, type LinkProps, type LinkTo, type NavigateOptions, type PreparedRoute, type RouteDefinition, type RouteMap, type RouteMatchResult, type RoutesInput, type ScrollPosition, appHistory, createRouter, getScrollFromSessionStorage, getScrollPosition, handleHistoryChange, makeMemoryHistory, navigate, _default as routesHelper, setScrollForKey, setScrollToSessionStorage };