Skip to content

Commit af5b2ea

Browse files
authored
Merge pull request #29 from Semantic-Org/spike/views
Feat: Simplify Integrations Using Views
2 parents a287499 + 782d51f commit af5b2ea

20 files changed

Lines changed: 334 additions & 298 deletions

File tree

‎docs/src/content/examples/back-and-forward/app.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import '@nav-router/plugins/dom';
21
import { router } from 'nav-router';
32

43
import { routes } from './routes.js';

‎docs/src/content/examples/first-router/app.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import '@nav-router/plugins/dom';
21
import { router } from 'nav-router';
32

43
import { routes } from './routes.js';

‎docs/src/content/examples/guards/app.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import '@nav-router/plugins/dom';
21
import { router } from 'nav-router';
32

43
import { routes } from './routes.js';

‎docs/src/content/examples/loading-states/app.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import '@nav-router/plugins/dom';
21
import { router } from 'nav-router';
32

43
import { routes } from './routes.js';

‎docs/src/content/examples/not-found/app.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import '@nav-router/plugins/dom';
21
import { router } from 'nav-router';
32

43
import { routes } from './routes.js';

‎docs/src/content/examples/query-params/app.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import '@nav-router/plugins/dom';
21
import { router, update } from 'nav-router';
32

43
import { routes } from './routes.js';

‎docs/src/lib/plugins.js‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,21 @@ export const PLUGINS = [
5252
category: 'Tooling',
5353
description: 'Lists every route as a page a crawler or a generator can read, and adds `router.sitemap()`.',
5454
},
55-
{
56-
name: 'dom',
57-
short: 'String and element views, no framework',
58-
category: 'Rendering',
59-
description: 'Renders views as markup or elements into the page’s `<slot>`s, with no framework.',
60-
},
6155
];
6256

6357
// the binding a plugin imports under: view-transitions → viewTransitions
6458
export const bindingFor = (name) => name.replace(/-(\w)/g, (_, c) => c.toUpperCase());
6559

66-
// options a plugin needs to do anything, shown in the setup as the router's own: dom renders into a mount
67-
const REQUIRED_OPTIONS = { dom: "mount: '#app'" };
68-
6960
// the setup for a set of plugins, as nav-router's own examples write it: one import per plugin, for
70-
// its effect, dom first, the renderer the others build on, and the plugins' options on the router
61+
// its effect, beside the router, which renders the dom without one
7162
export function sampleFor(names) {
72-
const picked = PLUGINS.map((p) => p.name).filter((n) => names.includes(n))
73-
.sort((a, b) => (b === 'dom') - (a === 'dom'));
63+
const picked = PLUGINS.map((p) => p.name).filter((n) => names.includes(n));
7464
const lines = [];
7565
picked.forEach((n) => lines.push(`import '@nav-router/plugins/${n}';`));
7666
if (!picked.length) { lines.push('// select plugins below to add them here'); }
7767
lines.push("import { router } from 'nav-router';", '');
78-
const options = picked.map((n) => REQUIRED_OPTIONS[n]).filter(Boolean);
7968
lines.push('const app = router({');
80-
options.forEach((option) => lines.push(` ${option},`));
69+
lines.push(" mount: '#app',");
8170
lines.push(' // the routes', '});');
8271
return lines.join('\n');
8372
}

‎examples/flat/client/routes.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { wait } from '@semantic-ui/utils';
22
import { lazy, router } from 'nav-router';
3-
import '@nav-router/plugins/dom';
43

54
import { MEMBERS } from './data.js';
65
import { ErrorPage } from './views/error-page.js';

‎examples/nested/client/router.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { router as defineRouter } from 'nav-router';
2-
import '@nav-router/plugins/dom';
32

43
import { ErrorPage } from './views/error-page.js';
54
import { ForbiddenPage } from './views/forbidden-page.js';

‎integrations/react/index.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { getRouter, Router, View } from 'nav-router';
2+
import { createElement, useSyncExternalStore } from 'react';
3+
import { flushSync } from 'react-dom';
4+
import { createRoot } from 'react-dom/client';
5+
6+
// a react component under a root of its own, in an element that is the view's alone. its props
7+
// are its context, and it renders again when it is updated with a new one
8+
export class ReactView extends View {
9+
static place = 'element';
10+
11+
create({ context, element }) {
12+
this.root = createRoot(element);
13+
this.update(context);
14+
}
15+
16+
// flushed, so the views below find their slots in it at once
17+
update(context) {
18+
flushSync(() => this.root.render(createElement(this.view, context)));
19+
}
20+
21+
destroy() {
22+
this.root?.unmount();
23+
super.destroy();
24+
}
25+
}
26+
27+
export class ReactRouter extends Router {
28+
static View = ReactView;
29+
}
30+
31+
// the router's state, read in a component and kept current: useRouter((router) => router.pending)
32+
export const useRouter = (read) => {
33+
const router = getRouter();
34+
return useSyncExternalStore((listener) => router.subscribe(listener), () => read(router));
35+
};
36+
37+
export * from 'nav-router';
38+
export const reactRouter = (options) => new ReactRouter(options);
39+
export { reactRouter as router };

0 commit comments

Comments
 (0)