@native-router/react 1.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,79 @@
1
+ [![npm](https://img.shields.io/npm/v/@native-router/react.svg)](https://www.npmjs.com/package/@native-router/react)
2
+ [![Build Status](https://github.com/wmzy/@native-router/react/actions/workflows/ci.yml/badge.svg)](https://github.com/wmzy/@native-router/react/actions)
3
+ [![Coverage](https://img.shields.io/codecov/c/github/wmzy/@native-router/react.svg)](https://codecov.io/gh/wmzy/@native-router/react)
4
+ [![install size](https://packagephobia.now.sh/badge?p=@native-router/react)](https://packagephobia.now.sh/result?p=@native-router/react)
5
+
6
+ # Native Router React
7
+
8
+ > A route close to the native experience for react.
9
+
10
+ English | [简体中文](./README-zh_CN.md)
11
+
12
+ ## Features
13
+
14
+ - Asynchronous navigation
15
+ - Cancelable
16
+ - Page data concurrent fetch
17
+ - Link prefetch and preview
18
+ - Most unused features can be tree-shaking
19
+ - SSR support
20
+
21
+ ## Install
22
+
23
+ ```bash
24
+ npm i @native-router/react
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```tsx
30
+ import {View, HistoryRouter as Router} from '@native-router/react';
31
+ import Loading from '@/components/Loading';
32
+ import RouterError from '@/components/RouterError';
33
+ import * as userService from '@/services/user';
34
+
35
+ export default function App() {
36
+ return (
37
+ <Router
38
+ routes={{
39
+ component: () => import('./Layout'),
40
+ children: [
41
+ {
42
+ path: '/',
43
+ component: () => import('./Home')
44
+ },
45
+ {
46
+ path: '/users',
47
+ component: () => import('./UserList'),
48
+ data: userService.fetchList
49
+ },
50
+ {
51
+ path: '/users/:id',
52
+ component: () => import('./UserProfile'),
53
+ data: ({id}) => userService.fetchById(+id)
54
+ },
55
+ {
56
+ path: '/help',
57
+ component: () => import('./Help')
58
+ },
59
+ {
60
+ path: '/about',
61
+ component: () => import('./About')
62
+ }
63
+ ]
64
+ }}
65
+ baseUrl="/demos"
66
+ errorHandler={(e) => <RouterError error={e} />}
67
+ >
68
+ <View />
69
+ <Loading />
70
+ </Router>
71
+ );
72
+ }
73
+
74
+ ```
75
+ See [demos](/demos/) for a complete example.
76
+
77
+ ## Documentation
78
+
79
+ [API](https://wmzy.github.io/@native-router/react/modules.html)
package/dist/index.js ADDED
@@ -0,0 +1,94 @@
1
+ import { u as useRouter } from './server-4edf6acb.js';
2
+ export { g as HashRouter, H as HistoryRouter, M as MemoryRouter, R as Router, V as View, f as createRouter, r as defaultResolveView, e as resolveClientView, b as useData, c as useLoading, d as useMatched, a as useView } from './server-4edf6acb.js';
3
+ import { createHref, navigate, toLocation, commit, resolve } from '@native-router/core';
4
+ import { useRef, useState, useEffect, useMemo, createContext, useContext } from 'react';
5
+ import { jsx } from 'react/jsx-runtime';
6
+ import 'history';
7
+ import '@native-router/core/util';
8
+
9
+ function Link(_ref) {
10
+ let {
11
+ to,
12
+ ...rest
13
+ } = _ref;
14
+ const router = useRouter();
15
+ const lock = useRef(false);
16
+ function handleClick(e) {
17
+ e.preventDefault();
18
+ if (lock.current)
19
+ return;
20
+ lock.current = true;
21
+ navigate(router, to).finally(() => lock.current = false);
22
+ }
23
+ return (
24
+ // eslint-disable-next-line jsx-a11y/anchor-has-content
25
+ /* @__PURE__ */ jsx("a", {
26
+ ...rest,
27
+ href: createHref(router, to),
28
+ onClick: handleClick
29
+ })
30
+ );
31
+ }
32
+
33
+ const Context = /* @__PURE__ */ createContext({
34
+ loading: false
35
+ });
36
+ function usePrefetch() {
37
+ return useContext(Context);
38
+ }
39
+ function PrefetchLink(_ref) {
40
+ let {
41
+ to,
42
+ children,
43
+ ...rest
44
+ } = _ref;
45
+ const router = useRouter();
46
+ const viewPromiseRef = useRef();
47
+ const [loading, setLoading] = useState(false);
48
+ const [error, setError] = useState();
49
+ const [view, setView] = useState();
50
+ const location = toLocation(router, to);
51
+ function prefetchIt() {
52
+ setLoading(true);
53
+ viewPromiseRef.current = resolve(router, location).then((v) => {
54
+ setView(v);
55
+ return v;
56
+ }).catch((e) => {
57
+ setError(e);
58
+ throw e;
59
+ }).finally(() => setLoading(false));
60
+ }
61
+ function handlePrefetch() {
62
+ if (viewPromiseRef.current)
63
+ return;
64
+ prefetchIt();
65
+ }
66
+ function handleClick(e) {
67
+ e.preventDefault();
68
+ if (!viewPromiseRef.current) {
69
+ prefetchIt();
70
+ }
71
+ commit(router, viewPromiseRef.current, location).finally(() => viewPromiseRef.current = void 0);
72
+ }
73
+ useEffect(() => {
74
+ viewPromiseRef.current = void 0;
75
+ }, [to, router]);
76
+ const linkContext = useMemo(() => ({
77
+ loading,
78
+ error,
79
+ view
80
+ }), [loading, error, view]);
81
+ return /* @__PURE__ */ jsx(Context.Provider, {
82
+ value: linkContext,
83
+ children: /* @__PURE__ */ jsx("a", {
84
+ ...rest,
85
+ href: createHref(router, to),
86
+ onMouseEnter: handlePrefetch,
87
+ onClick: handleClick,
88
+ children
89
+ })
90
+ });
91
+ }
92
+
93
+ export { Link, PrefetchLink, usePrefetch, useRouter };
94
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/components/Link.tsx","../src/components/PrefetchLink.tsx"],"sourcesContent":["import {createHref, navigate} from '@native-router/core';\nimport type {LinkProps} from '@@/types';\nimport {useRef, type MouseEvent} from 'react';\nimport {useRouter} from './Router';\n\n/**\n * Link for navigate in app.\n * @param props\n * @group Components\n */\nexport default function Link({to, ...rest}: LinkProps) {\n const router = useRouter();\n const lock = useRef(false);\n\n function handleClick(e: MouseEvent<HTMLAnchorElement>) {\n e.preventDefault();\n\n if (lock.current) return;\n lock.current = true;\n navigate(router, to).finally(() => (lock.current = false));\n }\n return (\n // eslint-disable-next-line jsx-a11y/anchor-has-content\n <a {...rest} href={createHref(router, to)} onClick={handleClick} />\n );\n}\n","import {commit, createHref, resolve, toLocation} from '@native-router/core';\nimport type {LinkProps} from '@@/types';\nimport {\n createContext,\n MouseEvent,\n ReactNode,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState\n} from 'react';\nimport {useRouter} from './Router';\n\ntype PrefetchLinkContext = {loading: boolean; error?: Error; view?: ReactNode};\n\nconst Context = createContext<PrefetchLinkContext>({loading: false});\n\n/**\n * Get the prefetch context. Use for render a preview view.\n * @group Hooks\n */\nexport function usePrefetch() {\n return useContext(Context);\n}\n\n/**\n * Link support hover prefetch.\n * @param props\n * @group Components\n */\nexport default function PrefetchLink({to, children, ...rest}: LinkProps) {\n const router = useRouter();\n const viewPromiseRef = useRef<undefined | Promise<ReactNode>>();\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<Error>();\n const [view, setView] = useState<ReactNode>();\n const location = toLocation(router, to);\n\n function prefetchIt() {\n setLoading(true);\n viewPromiseRef.current = resolve(router, location)\n .then((v) => {\n setView(v);\n return v;\n })\n .catch((e) => {\n setError(e);\n throw e;\n })\n .finally(() => setLoading(false));\n }\n\n function handlePrefetch() {\n if (viewPromiseRef.current) return;\n prefetchIt();\n }\n\n function handleClick(e: MouseEvent<HTMLAnchorElement>) {\n e.preventDefault();\n if (!viewPromiseRef.current) {\n prefetchIt();\n }\n commit(router, viewPromiseRef.current!, location).finally(\n () => (viewPromiseRef.current = undefined)\n );\n }\n\n useEffect(() => {\n viewPromiseRef.current = undefined;\n }, [to, router]);\n\n const linkContext = useMemo(\n () => ({loading, error, view}),\n [loading, error, view]\n );\n\n return (\n <Context.Provider value={linkContext}>\n <a\n {...rest}\n href={createHref(router, to)}\n onMouseEnter={handlePrefetch}\n onClick={handleClick}\n >\n {children}\n </a>\n </Context.Provider>\n );\n}\n"],"names":["Link","_ref","to","rest","router","useRouter","lock","useRef","handleClick","e","preventDefault","current","navigate","finally","href","createHref","onClick","Context","loading","usePrefetch","useContext","PrefetchLink","children","viewPromiseRef","setLoading","useState","error","setError","view","setView","location","toLocation","prefetchIt","resolve","then","v","catch","handlePrefetch","commit","undefined","useEffect","linkContext","useMemo","_jsx","Provider","value","onMouseEnter"],"mappings":";;;;;;;;AAU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OAAAA,GAAU4B,KAClC,CAAA,CAAA,CAAA;AAAA,GACF;AAEAC,EAAAA,SAAAA,CAAU,MAAM;AACdjB,IAAAA,cAAAA,CAAeZ,OAAU4B,GAAAA,KAAAA,CAAAA,CAAAA;AAAAA,GACxB,EAAA,CAACrC,EAAIE,EAAAA,MAAM,CAAC,CAAA,CAAA;AAEf,EAAMqC,MAAAA,WAAAA,GAAcC,QAClB,OAAO;AAAA,IAACxB,OAAAA;AAAAA,IAASQ,KAAAA;AAAAA,IAAOE,IAAAA;AAAAA,GACxB,CAAA,EAAA,CAACV,OAASQ,EAAAA,KAAAA,EAAOE,IAAI,CACvB,CAAA,CAAA;AAEA,EACEe,uBAAAA,GAAAA,CAAC1B,QAAQ2B,QAAQ,EAAA;AAAA,IAACC,KAAOJ,EAAAA,WAAAA;AAAAA,IAAYnB,QAAAA,sBACnC,GAAA,EAAA;AAAA,MAAA,GACMnB,IAAAA;AAAAA,MACJW,IAAAA,EAAMC,UAAWX,CAAAA,MAAAA,EAAQF,EAAE,CAAA;AAAA,MAC3B4C,YAAcT,EAAAA,cAAAA;AAAAA,MACdrB,OAASR,EAAAA,WAAAA;AAAAA,MAAYc,QAAAA;AAAAA,KAGpB,CAAA;AAAA,GACa,CAAA,CAAA;AAEtB;;;;"}
@@ -0,0 +1,231 @@
1
+ import { createMemoryHistory, createBrowserHistory, createHashHistory } from 'history';
2
+ import { useContext, createContext, useMemo, useState, useEffect } from 'react';
3
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
4
+ import { getCurrentView, listen, create, setOptions, toLocation, resolve } from '@native-router/core';
5
+ import { splitProps, uniqId, isString } from '@native-router/core/util';
6
+
7
+ const ViewContext = /* @__PURE__ */ createContext(null);
8
+ function ViewProvider(props) {
9
+ return /* @__PURE__ */ jsx(ViewContext.Provider, {
10
+ ...props
11
+ });
12
+ }
13
+ function useView() {
14
+ return useContext(ViewContext);
15
+ }
16
+ function View() {
17
+ return useView();
18
+ }
19
+ const DataContext = /* @__PURE__ */ createContext([void 0, {}]);
20
+ function useDataContext() {
21
+ return useContext(DataContext);
22
+ }
23
+ function useNamedData() {
24
+ return useDataContext()[1];
25
+ }
26
+ function DataProvider(_ref) {
27
+ let {
28
+ children,
29
+ name,
30
+ data
31
+ } = _ref;
32
+ const namedData = useNamedData();
33
+ const value = useMemo(() => [data, name ? {
34
+ ...namedData,
35
+ [name]: data
36
+ } : namedData], [data, name, namedData]);
37
+ return /* @__PURE__ */ jsx(DataContext.Provider, {
38
+ value,
39
+ children
40
+ });
41
+ }
42
+ const MatchedContext = /* @__PURE__ */ createContext(void 0);
43
+ function useMatched() {
44
+ return useContext(MatchedContext);
45
+ }
46
+ function useData(name) {
47
+ const [data, namedData] = useDataContext();
48
+ return name ? namedData[name] : data;
49
+ }
50
+ const LoadingContext = /* @__PURE__ */ createContext(void 0);
51
+ function useLoading() {
52
+ return useContext(LoadingContext);
53
+ }
54
+
55
+ function resolveView(matched, ctx) {
56
+ return resolveViewBase(matched, ctx, (data, dataCtx) => data?.(dataCtx));
57
+ }
58
+ const viewDataMap = /* @__PURE__ */ new WeakMap();
59
+ function resolveViewServer(matched, ctx) {
60
+ const dataResults = new Array(matched.length);
61
+ return resolveViewBase(matched, ctx, (data, dataCtx) => Promise.resolve(data?.(dataCtx)).then((result) => dataResults[dataCtx.index] = result)).then((view) => {
62
+ viewDataMap.set(view, dataResults);
63
+ return view;
64
+ });
65
+ }
66
+ function createHydrateResolveView(data) {
67
+ return (matched, ctx) => resolveViewBase(matched, ctx, (_, dataCtx) => data[dataCtx.index]);
68
+ }
69
+ function getViewData(view) {
70
+ return viewDataMap.get(view);
71
+ }
72
+ function resolveViewBase(matched, _ref, resolveData) {
73
+ let {
74
+ router,
75
+ location
76
+ } = _ref;
77
+ return Promise.all(matched.map((_ref2, index) => {
78
+ let {
79
+ params,
80
+ route
81
+ } = _ref2;
82
+ const ctx = {
83
+ matched,
84
+ params,
85
+ index,
86
+ router,
87
+ location
88
+ };
89
+ function resolveComponent() {
90
+ if (!route.component)
91
+ return View;
92
+ const r = route.component(ctx);
93
+ return Promise.resolve(r).then((m) => "default" in m ? m.default : m);
94
+ }
95
+ return Promise.all([resolveData(route.data, ctx), resolveComponent()]).then((_ref3) => {
96
+ let [data, C] = _ref3;
97
+ return /* @__PURE__ */ jsx(DataProvider, {
98
+ data,
99
+ name: route.name,
100
+ children: /* @__PURE__ */ jsx(MatchedContext.Provider, {
101
+ value: ctx,
102
+ children: /* @__PURE__ */ jsx(C, {})
103
+ })
104
+ });
105
+ });
106
+ })).then((views) => views.reverse().reduce((acc, view) => /* @__PURE__ */ jsx(ViewProvider, {
107
+ value: acc,
108
+ children: view
109
+ })));
110
+ }
111
+
112
+ const RouterContext = /* @__PURE__ */ createContext(null);
113
+ function Router(_ref) {
114
+ let {
115
+ router,
116
+ children
117
+ } = _ref;
118
+ const [view, setView] = useState(getCurrentView(router));
119
+ useEffect(() => listen(router, setView), [router]);
120
+ return /* @__PURE__ */ jsx(RouterContext.Provider, {
121
+ value: router,
122
+ children: children === "undefined" ? view : /* @__PURE__ */ jsx(ViewProvider, {
123
+ value: view,
124
+ children
125
+ })
126
+ });
127
+ }
128
+ function createRouter(routes, history) {
129
+ let {
130
+ resolveView: resolveView$1 = resolveView,
131
+ ...options
132
+ } = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
133
+ return create(routes, history, resolveView$1, options);
134
+ }
135
+ function useNewRouter(_ref2, createHistory) {
136
+ let {
137
+ routes,
138
+ children,
139
+ ...options
140
+ } = _ref2;
141
+ const [tracked, rest] = splitProps(options, ["baseUrl", "currentView"]);
142
+ const router = useMemo(() => createRouter(routes, createHistory(), tracked), [routes, createHistory, ...Object.keys(tracked), ...Object.values(tracked)]);
143
+ const [loading, setLoading] = useState();
144
+ setOptions(router, {
145
+ ...rest,
146
+ onLoadingChange(status) {
147
+ setLoading(status && {
148
+ key: uniqId(),
149
+ status
150
+ });
151
+ }
152
+ });
153
+ const r = useMemo(() => /* @__PURE__ */ jsx(Router, {
154
+ router,
155
+ children
156
+ }), [router, children]);
157
+ return /* @__PURE__ */ jsx(LoadingContext.Provider, {
158
+ value: loading,
159
+ children: r
160
+ });
161
+ }
162
+ function HistoryRouter(props) {
163
+ return useNewRouter(props, createBrowserHistory);
164
+ }
165
+ function HashRouter(props) {
166
+ return useNewRouter(props, createHashHistory);
167
+ }
168
+ function MemoryRouter(_ref3) {
169
+ let {
170
+ initialEntries,
171
+ initialIndex,
172
+ ...props
173
+ } = _ref3;
174
+ const createHistory = useMemo(() => () => createMemoryHistory({
175
+ initialEntries,
176
+ initialIndex
177
+ }), [initialEntries, initialIndex]);
178
+ return useNewRouter(props, createHistory);
179
+ }
180
+ function useRouter() {
181
+ return useContext(RouterContext);
182
+ }
183
+
184
+ const defaultHydrateKey = "_nativeRouterReactSSRData";
185
+ function resolveServerViewBase(router, location, options) {
186
+ return resolve(router, location).then((view) => {
187
+ const data = getViewData(view);
188
+ return /* @__PURE__ */ jsxs(Fragment, {
189
+ children: [/* @__PURE__ */ jsx(Router, {
190
+ router,
191
+ children: view
192
+ }), /* @__PURE__ */ jsx("script", {
193
+ ...options?.scriptAttributes,
194
+ suppressHydrationWarning: true,
195
+ dangerouslySetInnerHTML: {
196
+ __html: `window.${options?.hydrateKey || defaultHydrateKey} = ${JSON.stringify({
197
+ data,
198
+ location
199
+ })};`
200
+ }
201
+ })]
202
+ });
203
+ });
204
+ }
205
+ function resolveServerView(routes, location) {
206
+ let {
207
+ scriptAttributes,
208
+ hydrateKey,
209
+ ...options
210
+ } = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
211
+ const router = create(routes, createMemoryHistory({
212
+ initialEntries: [location]
213
+ }), resolveViewServer, options);
214
+ return resolveServerViewBase(router, isString(location) ? toLocation(router, location) : location, {
215
+ scriptAttributes,
216
+ hydrateKey
217
+ });
218
+ }
219
+ function resolveClientView(routes, options) {
220
+ const {
221
+ data,
222
+ location
223
+ } = window[options?.hydrateKey || defaultHydrateKey];
224
+ const router = create(routes, createMemoryHistory({
225
+ initialEntries: [location]
226
+ }), createHydrateResolveView(data), options);
227
+ return resolve(router, location);
228
+ }
229
+
230
+ export { HistoryRouter as H, MemoryRouter as M, Router as R, View as V, useView as a, useData as b, useLoading as c, useMatched as d, resolveClientView as e, createRouter as f, HashRouter as g, resolveServerView as h, resolveView as r, useRouter as u };
231
+ //# sourceMappingURL=server-4edf6acb.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-4edf6acb.js","sources":["../src/context.tsx","../src/resolve-view.tsx","../src/components/Router.tsx","../src/ssr.tsx"],"sourcesContent":["import {createContext, ReactNode, useContext, useMemo} from 'react';\nimport type {Context, LoadStatus, Route} from './types';\n\nconst ViewContext = createContext<ReactNode>(null);\n\nexport function ViewProvider(props: {children: ReactNode; value: ReactNode}) {\n return <ViewContext.Provider {...props} />;\n}\n\n/**\n * @group Hooks\n * @see {@link View View Component}\n */\nexport function useView() {\n return useContext(ViewContext);\n}\n\n/**\n * Used for route component to render child route component.\n * It just render the return of {@link useView}\n * @group Components\n */\nexport function View() {\n return useView();\n}\n\nconst DataContext = createContext<[any, Record<string, any>]>([undefined, {}]);\n\nfunction useDataContext() {\n return useContext(DataContext);\n}\n\n/**\n * @group Hooks\n */\nexport function useNamedData() {\n return useDataContext()[1];\n}\n\nexport function DataProvider({\n children,\n name,\n data\n}: {\n children: ReactNode;\n data: any;\n name?: string;\n}) {\n const namedData = useNamedData();\n const value = useMemo(\n () => [data, name ? {...namedData, [name]: data} : namedData] as [any, any],\n [data, name, namedData]\n );\n return <DataContext.Provider value={value}>{children}</DataContext.Provider>;\n}\n\nexport const MatchedContext = createContext<Context<Route> | undefined>(\n undefined\n);\n\n/**\n * @group Hooks\n */\nexport function useMatched() {\n return useContext(MatchedContext)!;\n}\n\n/**\n * @group Hooks\n */\nexport function useData(name?: string) {\n const [data, namedData] = useDataContext();\n return name ? namedData[name] : data;\n}\n\nexport const LoadingContext = createContext<LoadStatus | undefined>(undefined);\n\n/**\n * @group Hooks\n */\nexport function useLoading() {\n return useContext(LoadingContext);\n}\n","import type {ComponentType, ReactElement} from 'react';\nimport type {Context, ResolveViewContext, Route} from '@@/types';\nimport type {Matched} from '@native-router/core';\nimport {DataProvider, MatchedContext, View, ViewProvider} from './context';\n\n/**\n * The default implementation of resolve view\n * @param matched the matched result\n * @param viewContext resolved view context\n * @returns the resolve view\n * @see {@link create router->create}\n */\nexport default function resolveView(\n matched: Matched<Route>[],\n ctx: ResolveViewContext<Route>\n) {\n return resolveViewBase(matched, ctx, (data, dataCtx) => data?.(dataCtx));\n}\n\nconst viewDataMap = new WeakMap<ReactElement, any[]>();\n\nexport function resolveViewServer(\n matched: Matched<Route>[],\n ctx: ResolveViewContext<Route>\n) {\n const dataResults = new Array(matched.length);\n return resolveViewBase(matched, ctx, (data, dataCtx) =>\n Promise.resolve(data?.(dataCtx)).then(\n (result) => (dataResults[dataCtx.index] = result)\n )\n ).then((view) => {\n viewDataMap.set(view, dataResults);\n return view;\n });\n}\n\nexport function createHydrateResolveView(data: any[]) {\n return (matched: Matched<Route>[], ctx: ResolveViewContext<Route>) =>\n resolveViewBase(matched, ctx, (_, dataCtx) => data[dataCtx.index]);\n}\n\nexport function getViewData(view: ReactElement) {\n return viewDataMap.get(view);\n}\n\nfunction resolveViewBase(\n matched: Matched<Route>[],\n {router, location}: ResolveViewContext<Route>,\n resolveData: (\n dataFetcher: ((ctx: Context<Route>) => any) | undefined,\n ctx: Context<Route>\n ) => any\n) {\n return Promise.all(\n matched.map(({params, route}, index) => {\n const ctx = {\n matched: matched!,\n params,\n index,\n router,\n location\n };\n function resolveComponent(): ComponentType | Promise<ComponentType> {\n if (!route.component) return View;\n const r = route.component(ctx);\n return Promise.resolve(r).then((m) => ('default' in m ? m.default : m));\n }\n\n return Promise.all([\n resolveData(route.data, ctx),\n resolveComponent()\n ]).then(([data, C]) => (\n <DataProvider data={data} name={route.name}>\n <MatchedContext.Provider value={ctx}>\n <C />\n </MatchedContext.Provider>\n </DataProvider>\n ));\n })\n ).then((views) =>\n views\n .reverse()\n .reduce((acc, view) => <ViewProvider value={acc}>{view}</ViewProvider>)\n );\n}\n","import {\n ReactNode,\n createContext,\n useContext,\n useEffect,\n useState,\n useMemo\n} from 'react';\nimport {\n History,\n createBrowserHistory,\n createHashHistory,\n createMemoryHistory,\n MemoryHistoryOptions\n} from 'history';\nimport type {\n LoadStatus,\n Route,\n} from '@@/types';\nimport {LoadingContext, ViewProvider} from '@@/context';\nimport {create, getCurrentView, listen, setOptions} from '@native-router/core';\nimport type {\n Options,\n ResolveView,\n RouterInstance\n} from '@native-router/core';\nimport {splitProps, uniqId} from '@native-router/core/util';\nimport defaultResolve from '@@/resolve-view';\n\nconst RouterContext = createContext<RouterInstance<Route, ReactNode> | null>(\n null\n);\n\ntype Props = {\n children: ReactNode;\n routes: Route[] | Route;\n resolveView?: typeof defaultResolve;\n} & Omit<Options<ReactNode>, 'onLoadingChange'>;\n\n/**\n * Base Router Component.\n * @group Components\n */\nexport function Router({\n router,\n children\n}: {\n children: ReactNode;\n router: RouterInstance<Route, ReactNode>;\n}) {\n const [view, setView] = useState<ReactNode>(getCurrentView(router));\n\n useEffect(() => listen(router, setView), [router]);\n return (\n <RouterContext.Provider value={router}>\n {children === 'undefined' ? (\n view\n ) : (\n <ViewProvider value={view}>{children}</ViewProvider>\n )}\n </RouterContext.Provider>\n );\n}\n\nexport function createRouter(\n routes: Route | Route[],\n history: History,\n {\n resolveView = defaultResolve,\n ...options\n }: Options<ReactNode> & {resolveView?: ResolveView<Route, ReactNode>} = {}\n): RouterInstance<Route, ReactNode> {\n return create(routes, history, resolveView, options);\n}\n\nfunction useNewRouter(\n {routes, children, ...options}: Props,\n createHistory: () => History\n) {\n const [tracked, rest] = splitProps(options, ['baseUrl', 'currentView']);\n const router = useMemo(\n () => createRouter(routes, createHistory(), tracked),\n [routes, createHistory, ...Object.keys(tracked), ...Object.values(tracked)]\n );\n\n const [loading, setLoading] = useState<LoadStatus>();\n setOptions(router, {\n ...rest,\n onLoadingChange(status) {\n setLoading(status && {key: uniqId(), status});\n }\n });\n\n const r = useMemo(\n () => <Router router={router}>{children}</Router>,\n [router, children]\n );\n\n return <LoadingContext.Provider value={loading}>{r}</LoadingContext.Provider>;\n}\n\n/**\n * History mode Router Component.\n * @group Components\n */\nexport function HistoryRouter(props: Props) {\n return useNewRouter(props, createBrowserHistory);\n}\n\n/**\n * Hash mode Router Component.\n * @group Components\n */\nexport function HashRouter(props: Props) {\n return useNewRouter(props, createHashHistory);\n}\n\n/**\n * Memory mode Router Component.\n * @group Components\n */\nexport function MemoryRouter({\n initialEntries,\n initialIndex,\n ...props\n}: Props & MemoryHistoryOptions) {\n const createHistory = useMemo(\n () => () => createMemoryHistory({initialEntries, initialIndex}),\n [initialEntries, initialIndex]\n );\n return useNewRouter(props, createHistory);\n}\n\n/**\n * Get Router instance.\n * @group Hooks\n * @returns Router Instance\n */\nexport function useRouter() {\n return useContext(RouterContext)!;\n}\n","import {createMemoryHistory} from 'history';\nimport {ReactElement, ReactNode} from 'react';\nimport {Router} from './components/Router';\nimport {\n createHydrateResolveView,\n getViewData,\n resolveViewServer\n} from './resolve-view';\nimport {create, resolve, toLocation} from '@native-router/core';\nimport type {Location, Options, RouterInstance} from '@native-router/core';\nimport type {Route} from './types';\nimport {isString} from '@native-router/core/util';\n\nconst defaultHydrateKey = '_nativeRouterReactSSRData';\n\nexport function resolveServerViewBase(\n router: RouterInstance<Route, ReactNode>,\n location: Location,\n options?: {\n scriptAttributes?: Record<string, string>;\n hydrateKey?: string;\n }\n) {\n return resolve<Route, ReactNode>(router, location).then((view) => {\n const data = getViewData(view as ReactElement);\n return (\n <>\n <Router router={router}>{view}</Router>\n <script\n {...options?.scriptAttributes}\n suppressHydrationWarning\n // eslint-disable-next-line react/no-danger\n dangerouslySetInnerHTML={{\n __html: `window.${\n options?.hydrateKey || defaultHydrateKey\n } = ${JSON.stringify({data, location})};`\n }}\n />\n </>\n );\n });\n}\n\nexport function resolveServerView(\n routes: Route | Route[],\n location: Location | string,\n {\n scriptAttributes,\n hydrateKey,\n ...options\n }: Options<ReactElement> & {\n scriptAttributes?: Record<string, string>;\n hydrateKey?: string;\n } = {}\n) {\n const router = create(\n routes,\n createMemoryHistory({initialEntries: [location]}),\n resolveViewServer,\n options\n );\n\n return resolveServerViewBase(\n router,\n isString(location) ? toLocation(router, location) : location,\n {\n scriptAttributes,\n hydrateKey\n }\n );\n}\n\nexport function resolveClientView(\n routes: Route | Route[],\n options?: Options<ReactElement> & {\n hydrateKey?: string;\n }\n) {\n const {data, location} = (window as any)[\n options?.hydrateKey || defaultHydrateKey\n ] as {data: any[]; location: Location};\n const router = create(\n routes,\n createMemoryHistory({initialEntries: [location]}),\n createHydrateResolveView(data),\n options\n );\n return resolve(router, location);\n}\n"],"names":["ViewContext","ViewProvider","props","_jsx","Provider","useView","useContext","View","DataContext","undefined","useDataContext","useNamedData","DataProvider","_ref","children","name","data","namedData","value","useMemo","MatchedContext","useMatched","useData","LoadingContext","useLoading","resolveView","matched","ctx","resolveViewBase","dataCtx","viewDataMap","WeakMap","resolveViewServer","dataResults","Array","length","Promise","resolve","then","result","index","view","set","createHydrateResolveView","_","getViewData","get","resolveData","router","location","all","map","_ref2","params","route","resolveComponent","component","r","m","default","_ref3","C","views","reverse","reduce","acc","RouterContext","Router","setView","useState","getCurrentView","useEffect","listen","createRouter","routes","history","defaultResolve","options","arguments","create","useNewRouter","createHistory","tracked","rest","splitProps","Object","keys","values","loading","setLoading","setOptions","onLoadingChange","status","key","uniqId","HistoryRouter","createBrowserHistory","HashRouter","createHashHistory","MemoryRouter","initialEntries","initialIndex","createMemoryHistory","useRouter","defaultHydrateKey","resolveServerViewBase","_Fragment","scriptAttributes","suppressHydrationWarning","dangerouslySetInnerHTML","__html","hydrateKey","JSON","stringify","resolveServerView","isString","toLocation","resolveClientView","window"],"mappings":";;;;;;AAGA,MAAMA,WAAAA,iCAAuC,IAAI,CAAA,CAAA;AAE1C,SAASC,aAAaC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AA,GAAA2C,CAAAA,IAAAA,SAAAA,CAAArE,CAAAA,CAAAA,KAAAA,KAAAqE,CAAAA,GAAAA,SAAAA,CAAG,CAAA,CAAA,GAAA,EAAC,CAAA;AAEL,EAAM9B,MAAAA,MAAAA,GAAS+B,MACbL,CAAAA,MAAAA,EACA4B,mBAAoB,CAAA;AAAA,IAACF,cAAAA,EAAgB,CAACnD,QAAQ,CAAA;AAAA,GAAE,CAChDjB,EAAAA,iBAAAA,EACA6C,OACF,CAAA,CAAA;AAEA,EAAO4B,OAAAA,qBAAAA,CACLzD,QACAmE,QAASlE,CAAAA,QAAQ,IAAImE,UAAWpE,CAAAA,MAAAA,EAAQC,QAAQ,CAAA,GAAIA,QACpD,EAAA;AAAA,IACE0D,gBAAAA;AAAAA,IACAI,UAAAA;AAAAA,GAEJ,CAAA,CAAA;AACF,CAAA;AAEgBM,SAAAA,iBAAAA,CACd3C,QACAG,OAGA,EAAA;AACA,EAAM,MAAA;AAAA,IAAC7D,IAAAA;AAAAA,IAAMiC,QAAAA;AAAAA,GAAaqE,GAAAA,MAAAA,CACxBzC,OAASkC,EAAAA,UAAAA,IAAcP,iBAAiB,CAAA,CAAA;AAE1C,EAAMxD,MAAAA,MAAAA,GAAS+B,MACbL,CAAAA,MAAAA,EACA4B,mBAAoB,CAAA;AAAA,IAACF,cAAAA,EAAgB,CAACnD,QAAQ,CAAA;AAAA,GAAE,CAAA,EAChDN,wBAAyB3B,CAAAA,IAAI,GAC7B6D,OACF,CAAA,CAAA;AACA,EAAOxC,OAAAA,OAAAA,CAAQW,QAAQC,QAAQ,CAAA,CAAA;AACjC;;;;"}
package/dist/server.js ADDED
@@ -0,0 +1,7 @@
1
+ export { h as resolveServerView } from './server-4edf6acb.js';
2
+ import 'history';
3
+ import 'react';
4
+ import 'react/jsx-runtime';
5
+ import '@native-router/core';
6
+ import '@native-router/core/util';
7
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
@@ -0,0 +1,7 @@
1
+ import type { LinkProps } from '../types';
2
+ /**
3
+ * Link for navigate in app.
4
+ * @param props
5
+ * @group Components
6
+ */
7
+ export default function Link({ to, ...rest }: LinkProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,19 @@
1
+ import type { LinkProps } from '../types';
2
+ import { ReactNode } from 'react';
3
+ type PrefetchLinkContext = {
4
+ loading: boolean;
5
+ error?: Error;
6
+ view?: ReactNode;
7
+ };
8
+ /**
9
+ * Get the prefetch context. Use for render a preview view.
10
+ * @group Hooks
11
+ */
12
+ export declare function usePrefetch(): PrefetchLinkContext;
13
+ /**
14
+ * Link support hover prefetch.
15
+ * @param props
16
+ * @group Components
17
+ */
18
+ export default function PrefetchLink({ to, children, ...rest }: LinkProps): import("react/jsx-runtime").JSX.Element;
19
+ export {};
@@ -0,0 +1,43 @@
1
+ import { ReactNode } from 'react';
2
+ import { History, MemoryHistoryOptions } from 'history';
3
+ import type { Route } from '../types';
4
+ import type { Options, ResolveView, RouterInstance } from '@native-router/core';
5
+ import defaultResolve from '../resolve-view';
6
+ type Props = {
7
+ children: ReactNode;
8
+ routes: Route[] | Route;
9
+ resolveView?: typeof defaultResolve;
10
+ } & Omit<Options<ReactNode>, 'onLoadingChange'>;
11
+ /**
12
+ * Base Router Component.
13
+ * @group Components
14
+ */
15
+ export declare function Router({ router, children }: {
16
+ children: ReactNode;
17
+ router: RouterInstance<Route, ReactNode>;
18
+ }): import("react/jsx-runtime").JSX.Element;
19
+ export declare function createRouter(routes: Route | Route[], history: History, { resolveView, ...options }?: Options<ReactNode> & {
20
+ resolveView?: ResolveView<Route, ReactNode>;
21
+ }): RouterInstance<Route, ReactNode>;
22
+ /**
23
+ * History mode Router Component.
24
+ * @group Components
25
+ */
26
+ export declare function HistoryRouter(props: Props): import("react/jsx-runtime").JSX.Element;
27
+ /**
28
+ * Hash mode Router Component.
29
+ * @group Components
30
+ */
31
+ export declare function HashRouter(props: Props): import("react/jsx-runtime").JSX.Element;
32
+ /**
33
+ * Memory mode Router Component.
34
+ * @group Components
35
+ */
36
+ export declare function MemoryRouter({ initialEntries, initialIndex, ...props }: Props & MemoryHistoryOptions): import("react/jsx-runtime").JSX.Element;
37
+ /**
38
+ * Get Router instance.
39
+ * @group Hooks
40
+ * @returns Router Instance
41
+ */
42
+ export declare function useRouter(): RouterInstance<Route, ReactNode>;
43
+ export {};
@@ -0,0 +1,40 @@
1
+ import { ReactNode } from 'react';
2
+ import type { Context, LoadStatus, Route } from './types';
3
+ export declare function ViewProvider(props: {
4
+ children: ReactNode;
5
+ value: ReactNode;
6
+ }): import("react/jsx-runtime").JSX.Element;
7
+ /**
8
+ * @group Hooks
9
+ * @see {@link View View Component}
10
+ */
11
+ export declare function useView(): ReactNode;
12
+ /**
13
+ * Used for route component to render child route component.
14
+ * It just render the return of {@link useView}
15
+ * @group Components
16
+ */
17
+ export declare function View(): ReactNode;
18
+ /**
19
+ * @group Hooks
20
+ */
21
+ export declare function useNamedData(): Record<string, any>;
22
+ export declare function DataProvider({ children, name, data }: {
23
+ children: ReactNode;
24
+ data: any;
25
+ name?: string;
26
+ }): import("react/jsx-runtime").JSX.Element;
27
+ export declare const MatchedContext: import("react").Context<Context<Route> | undefined>;
28
+ /**
29
+ * @group Hooks
30
+ */
31
+ export declare function useMatched(): Context<Route>;
32
+ /**
33
+ * @group Hooks
34
+ */
35
+ export declare function useData(name?: string): any;
36
+ export declare const LoadingContext: import("react").Context<LoadStatus | undefined>;
37
+ /**
38
+ * @group Hooks
39
+ */
40
+ export declare function useLoading(): LoadStatus | undefined;
@@ -0,0 +1,7 @@
1
+ export * from './components/Router';
2
+ export { default as Link } from './components/Link';
3
+ export { default as PrefetchLink, usePrefetch } from './components/PrefetchLink';
4
+ export { useView, View, useData, useLoading, useMatched } from './context';
5
+ export { default as defaultResolveView } from './resolve-view';
6
+ export * from './types';
7
+ export { resolveClientView } from './ssr';
@@ -0,0 +1,14 @@
1
+ import type { ReactElement } from 'react';
2
+ import type { ResolveViewContext, Route } from './types';
3
+ import type { Matched } from '@native-router/core';
4
+ /**
5
+ * The default implementation of resolve view
6
+ * @param matched the matched result
7
+ * @param viewContext resolved view context
8
+ * @returns the resolve view
9
+ * @see {@link create router->create}
10
+ */
11
+ export default function resolveView(matched: Matched<Route>[], ctx: ResolveViewContext<Route>): Promise<import("react/jsx-runtime").JSX.Element>;
12
+ export declare function resolveViewServer(matched: Matched<Route>[], ctx: ResolveViewContext<Route>): Promise<import("react/jsx-runtime").JSX.Element>;
13
+ export declare function createHydrateResolveView(data: any[]): (matched: Matched<Route>[], ctx: ResolveViewContext<Route>) => Promise<import("react/jsx-runtime").JSX.Element>;
14
+ export declare function getViewData(view: ReactElement): any[] | undefined;
@@ -0,0 +1 @@
1
+ export { resolveServerView } from './ssr';
@@ -0,0 +1,14 @@
1
+ import { ReactElement, ReactNode } from 'react';
2
+ import type { Location, Options, RouterInstance } from '@native-router/core';
3
+ import type { Route } from './types';
4
+ export declare function resolveServerViewBase(router: RouterInstance<Route, ReactNode>, location: Location, options?: {
5
+ scriptAttributes?: Record<string, string>;
6
+ hydrateKey?: string;
7
+ }): Promise<import("react/jsx-runtime").JSX.Element>;
8
+ export declare function resolveServerView(routes: Route | Route[], location: Location | string, { scriptAttributes, hydrateKey, ...options }?: Options<ReactElement> & {
9
+ scriptAttributes?: Record<string, string>;
10
+ hydrateKey?: string;
11
+ }): Promise<import("react/jsx-runtime").JSX.Element>;
12
+ export declare function resolveClientView(routes: Route | Route[], options?: Options<ReactElement> & {
13
+ hydrateKey?: string;
14
+ }): Promise<import("react/jsx-runtime").JSX.Element>;
@@ -0,0 +1,28 @@
1
+ import type { AnchorHTMLAttributes, ComponentType, DetailedHTMLProps, ReactNode } from 'react';
2
+ import type { BaseRoute, Matched, Location, RouterInstance } from '@native-router/core';
3
+ export type ResolveViewContext<R extends BaseRoute> = {
4
+ router: RouterInstance<R>;
5
+ location: Location;
6
+ };
7
+ export type Context<T extends BaseRoute> = {
8
+ matched: Matched<T>[];
9
+ index: number;
10
+ router: RouterInstance<BaseRoute>;
11
+ location: Location;
12
+ params: Record<string, string>;
13
+ };
14
+ export type Route = BaseRoute<{
15
+ name?: string;
16
+ data?(ctx: Context<Route>): any | Promise<any>;
17
+ component?(ctx: Context<Route>): ComponentType | Promise<ComponentType | {
18
+ default: ComponentType;
19
+ }>;
20
+ }>;
21
+ export type LoadStatus = {
22
+ key: number;
23
+ status: 'pending' | 'resolved' | 'rejected';
24
+ };
25
+ export type LinkProps = {
26
+ to: string;
27
+ children?: ReactNode;
28
+ } & DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>;
package/package.json ADDED
@@ -0,0 +1,124 @@
1
+ {
2
+ "name": "@native-router/react",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "import": "./dist/index.mjs",
8
+ "types": "./dist/types/index.d.ts"
9
+ },
10
+ "./server": {
11
+ "import": "./dist/server.mjs",
12
+ "types": "./dist/types/server.d.ts"
13
+ }
14
+ },
15
+ "keywords": [
16
+ "react",
17
+ "router",
18
+ "react router",
19
+ "react-router",
20
+ "async",
21
+ "tiny",
22
+ "data-fetching",
23
+ "prefetch",
24
+ "preview"
25
+ ],
26
+ "description": "A route close to the native experience for react.",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "scripts": {
31
+ "start": "vite",
32
+ "start:ssr": "cross-env SSR=true vite",
33
+ "build": "vite build && tsc -p tsconfig.production.json && tsc-alias -p tsconfig.production.json",
34
+ "commit": "lint-staged && git-cz -n",
35
+ "coverage": "c8 report --reporter=text-lcov > ./coverage/coverage.txt",
36
+ "lint": "eslint --fix src test demos *.js --ext .js,.jsx,.ts,.tsx",
37
+ "doc:gen": "typedoc",
38
+ "build:demo": "cross-env BABEL_ENV=development BUILD_DEMO=true vite build",
39
+ "deploy": "npm run doc:gen && npm run build:demo && gh-pages -d dist",
40
+ "test": "cross-env NODE_ENV=test c8 mocha",
41
+ "test:watch": "npm test -- --watch",
42
+ "preversion": "npm run build",
43
+ "postversion": "npm publish",
44
+ "postpublish": "git push --follow-tags && npm run deploy",
45
+ "serve": "vite preview"
46
+ },
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "https://github.com/wmzy/@native-router/react"
50
+ },
51
+ "sideEffects": false,
52
+ "author": "wmzy",
53
+ "license": "MIT",
54
+ "bugs": {
55
+ "url": "https://github.com/wmzy/@native-router/react/issues"
56
+ },
57
+ "homepage": "https://github.com/wmzy/@native-router/react",
58
+ "engines": {
59
+ "node": ">=14"
60
+ },
61
+ "peerDependencies": {
62
+ "react": "^17.0.0 || ^18.0.0",
63
+ "react-dom": "^17.0.0 || ^18.0.0"
64
+ },
65
+ "dependencies": {
66
+ "@native-router/core": "^1.0.3",
67
+ "history": "^5.3.0"
68
+ },
69
+ "devDependencies": {
70
+ "@babel/core": "^7.22.20",
71
+ "@babel/preset-env": "^7.22.20",
72
+ "@babel/preset-react": "^7.22.15",
73
+ "@babel/preset-typescript": "^7.22.15",
74
+ "@linaria/babel-preset": "^5.0.2",
75
+ "@linaria/core": "^5.0.1",
76
+ "@linaria/vite": "^5.0.2",
77
+ "@testing-library/react": "^14.0.0",
78
+ "@types/mocha": "^10.0.1",
79
+ "@types/node": "^20.6.5",
80
+ "@types/react": "^18.2.22",
81
+ "@types/react-dom": "^18.2.7",
82
+ "@types/sinon": "^10.0.16",
83
+ "@typescript-eslint/eslint-plugin": "^6.7.2",
84
+ "@typescript-eslint/parser": "^6.7.2",
85
+ "@vitejs/plugin-react": "^4.1.0",
86
+ "babel-plugin-module-resolver": "^5.0.0",
87
+ "c8": "^8.0.1",
88
+ "commitizen": "^4.3.0",
89
+ "core-js": "^3.32.2",
90
+ "coveralls": "^3.1.1",
91
+ "cross-env": "^7.0.3",
92
+ "eslint": "^8.50.0",
93
+ "eslint-config-airbnb": "^19.0.4",
94
+ "eslint-config-airbnb-typescript": "^17.1.0",
95
+ "eslint-config-prettier": "^9.0.0",
96
+ "eslint-import-resolver-typescript": "^3.6.1",
97
+ "eslint-plugin-compat": "^4.2.0",
98
+ "eslint-plugin-import": "^2.28.1",
99
+ "eslint-plugin-jsx-a11y": "^6.7.1",
100
+ "eslint-plugin-mocha": "^10.1.0",
101
+ "eslint-plugin-prettier": "^5.0.0",
102
+ "eslint-plugin-react": "^7.33.2",
103
+ "eslint-plugin-react-hooks": "^4.6.0",
104
+ "gh-pages": "^6.0.0",
105
+ "global-jsdom": "^9.1.0",
106
+ "husky": "^8.0.3",
107
+ "jsdom": "^22.1.0",
108
+ "lint-staged": "^14.0.1",
109
+ "mocha": "^10.2.0",
110
+ "prettier": "^3.0.3",
111
+ "react": "^18.2.0",
112
+ "react-dom": "^18.2.0",
113
+ "should": "^13.2.3",
114
+ "should-sinon": "0.0.6",
115
+ "sinon": "^16.0.0",
116
+ "terser": "^5.20.0",
117
+ "tsc-alias": "^1.8.8",
118
+ "typedoc": "^0.25.1",
119
+ "typedoc-plugin-mark-react-functional-components": "^0.2.2",
120
+ "typedoc-plugin-missing-exports": "^2.1.0",
121
+ "typescript": "^5.2.2",
122
+ "vite": "^4.4.9"
123
+ }
124
+ }