netjes 0.0.2 → 0.0.3
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 +11 -11
- package/cli/agents.mjs +25 -2
- package/cli/commands.mjs +56 -6
- package/cli/help.mjs +7 -2
- package/cli/info.mjs +7 -2
- package/cli/scaffold.mjs +31 -4
- package/dist/actions.d.ts +7 -2
- package/dist/actions.d.ts.map +1 -1
- package/dist/actions.js +9 -3
- package/dist/actions.js.map +1 -1
- package/dist/client-entry.d.ts +1 -0
- package/dist/client-entry.d.ts.map +1 -1
- package/dist/client-entry.js +19 -2
- package/dist/client-entry.js.map +1 -1
- package/dist/deferred.d.ts +11 -0
- package/dist/deferred.d.ts.map +1 -1
- package/dist/deferred.js +15 -1
- package/dist/deferred.js.map +1 -1
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +10 -1
- package/dist/document.js.map +1 -1
- package/dist/errors.d.ts +7 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +11 -0
- package/dist/errors.js.map +1 -0
- package/dist/form.d.ts +5 -5
- package/dist/form.d.ts.map +1 -1
- package/dist/form.js +5 -2
- package/dist/form.js.map +1 -1
- package/dist/href.d.ts +24 -3
- package/dist/href.d.ts.map +1 -1
- package/dist/href.js +117 -7
- package/dist/href.js.map +1 -1
- package/dist/image.d.ts +3 -3
- package/dist/image.d.ts.map +1 -1
- package/dist/image.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/link.d.ts +15 -7
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +38 -7
- package/dist/link.js.map +1 -1
- package/dist/metadata.d.ts +8 -2
- package/dist/metadata.d.ts.map +1 -1
- package/dist/metadata.js +14 -5
- package/dist/metadata.js.map +1 -1
- package/dist/modules.d.ts +15 -1
- package/dist/modules.d.ts.map +1 -1
- package/dist/modules.js.map +1 -1
- package/dist/navigation-focus.d.ts +38 -0
- package/dist/navigation-focus.d.ts.map +1 -0
- package/dist/navigation-focus.js +71 -0
- package/dist/navigation-focus.js.map +1 -0
- package/dist/query.d.ts +5 -2
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +11 -8
- package/dist/query.js.map +1 -1
- package/dist/render-document.d.ts +60 -0
- package/dist/render-document.d.ts.map +1 -0
- package/dist/render-document.js +330 -0
- package/dist/render-document.js.map +1 -0
- package/dist/route-announcer.d.ts +18 -0
- package/dist/route-announcer.d.ts.map +1 -0
- package/dist/route-announcer.js +58 -0
- package/dist/route-announcer.js.map +1 -0
- package/dist/route-tree.d.ts +6 -2
- package/dist/route-tree.d.ts.map +1 -1
- package/dist/route-tree.js +17 -7
- package/dist/route-tree.js.map +1 -1
- package/dist/router.d.ts +28 -5
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +71 -26
- package/dist/router.js.map +1 -1
- package/dist/script.d.ts +28 -5
- package/dist/script.d.ts.map +1 -1
- package/dist/script.js +192 -21
- package/dist/script.js.map +1 -1
- package/dist/server-render.d.ts.map +1 -1
- package/dist/server-render.js +4 -1
- package/dist/server-render.js.map +1 -1
- package/dist/testing-vitest.d.ts +8 -0
- package/dist/testing-vitest.d.ts.map +1 -0
- package/dist/testing-vitest.js +26 -0
- package/dist/testing-vitest.js.map +1 -0
- package/dist/testing.d.ts +11 -1
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +17 -3
- package/dist/testing.js.map +1 -1
- package/dist/zod-jitless.d.ts +2 -0
- package/dist/zod-jitless.d.ts.map +1 -0
- package/dist/zod-jitless.js +4 -0
- package/dist/zod-jitless.js.map +1 -0
- package/package.json +1 -1
- package/sidecar/server.mjs +73 -6
- package/skills/netjes/SKILL.md +13 -6
- package/skills/netjes/references/caching-and-streaming.md +37 -4
- package/skills/netjes/references/data-and-actions.md +9 -5
- package/skills/netjes/references/deployment.md +7 -3
- package/skills/netjes/references/routing.md +18 -6
- package/skills/netjes/references/testing.md +23 -7
- package/src/actions.tsx +9 -3
- package/src/client-entry.tsx +20 -2
- package/src/deferred.ts +14 -1
- package/src/document.tsx +22 -3
- package/src/errors.ts +12 -0
- package/src/form.tsx +10 -7
- package/src/href.ts +123 -8
- package/src/image.tsx +3 -3
- package/src/index.ts +2 -1
- package/src/link.tsx +47 -13
- package/src/metadata.tsx +19 -6
- package/src/modules.ts +15 -2
- package/src/navigation-focus.ts +75 -0
- package/src/query.tsx +14 -8
- package/src/render-document.tsx +386 -0
- package/src/route-announcer.tsx +69 -0
- package/src/route-tree.tsx +17 -6
- package/src/router.tsx +116 -31
- package/src/script.tsx +214 -23
- package/src/server-render.tsx +4 -1
- package/src/testing-vitest.ts +37 -0
- package/src/testing.tsx +31 -5
- package/src/virtual.d.ts +2 -0
- package/src/zod-jitless.ts +15 -0
- package/template/.dockerignore +3 -0
- package/template/Dockerfile +11 -1
- package/template/README.md +11 -7
- package/template/_AGENTS.md +36 -8
- package/template/__NAME__.csproj +2 -0
- package/template/app/document.test.ts +29 -0
- package/template/app/error.tsx +3 -2
- package/template/app/layout.tsx +10 -1
- package/template/docker-compose.override.yml +4 -3
- package/template/docker-compose.prod.yml +2 -1
- package/template/e2e/home.spec.ts +32 -0
- package/template/generated/netjes.ts +43 -14
- package/template/package.json +4 -1
- package/template/playwright.config.ts +36 -0
- package/template/tests/__NAME__.Tests/AppFactory.cs +31 -0
- package/template/tests/__NAME__.Tests/HomePageTests.cs +55 -0
- package/template/tests/__NAME__.Tests/__NAME__.Tests.csproj +30 -0
- package/template/tsconfig.app.json +20 -0
- package/template/tsconfig.json +2 -17
- package/template/tsconfig.node.json +8 -0
- package/vite/plugin.d.ts +9 -1
- package/vite/plugin.mjs +17 -5
package/src/deferred.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Deferred loader values (Page.Defer in C#): placeholders in the payload that become promises the page reads with
|
|
2
2
|
// React's use() inside a Suspense boundary.
|
|
3
|
+
import { withheldError } from './errors.ts';
|
|
3
4
|
import type { PagePayload, SegmentRef } from './payload.ts';
|
|
4
5
|
|
|
5
6
|
/** A promise that records its outcome the way React's `use()` reads it, so a settled one renders without suspending. */
|
|
@@ -94,8 +95,9 @@ export function deferredRegistry() {
|
|
|
94
95
|
resolve(id: string, value: unknown) {
|
|
95
96
|
entry(id).resolve(value);
|
|
96
97
|
},
|
|
98
|
+
/** Fails a value with the server's message; an empty one is a message the server withheld (no error details). */
|
|
97
99
|
reject(id: string, message: string) {
|
|
98
|
-
entry(id).reject(new Error(message));
|
|
100
|
+
entry(id).reject(message === '' ? withheldError() : new Error(message));
|
|
99
101
|
},
|
|
100
102
|
/** Rejects every value still pending, e.g. when the stream that was to deliver them ended. */
|
|
101
103
|
rejectPending(message: (id: string) => string) {
|
|
@@ -115,6 +117,17 @@ export function deferredRegistry() {
|
|
|
115
117
|
export const DEFERRED_BOOTSTRAP =
|
|
116
118
|
"self.__netjes_d=self.__netjes_d||function(i,v,e){self.__netjes_v[i]=[v,e]};Object.prototype.hasOwnProperty.call(self,'__netjes_v')||(self.__netjes_v={})";
|
|
117
119
|
|
|
120
|
+
/**
|
|
121
|
+
* The `script-src` sources that allow Netjes's fixed inline scripts by their hashes, for a policy without a nonce: each
|
|
122
|
+
* `'sha256-…'` in its quotes, separated by spaces, as in `` `script-src 'self' ${CSP_SCRIPT_HASHES}` ``. A fixed script
|
|
123
|
+
* has the same text in every document. In this version there is one, the bootstrap that collects streamed `Page.Defer`
|
|
124
|
+
* values, and a Netjes version that changes it or adds another changes the value. The scripts that deliver those
|
|
125
|
+
* values, and React's scripts that reveal Suspense boundaries, differ from render to render, so no hash covers them.
|
|
126
|
+
* React's script for form actions that are functions is left out too, since its text follows the React version. The
|
|
127
|
+
* same value as `NetjesContentSecurityPolicy.ScriptHashes` in C#.
|
|
128
|
+
*/
|
|
129
|
+
export const CSP_SCRIPT_HASHES: string = "'sha256-FSjVV+FzmXP8KKrLKLdvst/5N/bEWSkde01XzpGKRgs='";
|
|
130
|
+
|
|
118
131
|
const UNSAFE = /[<>&\u2028\u2029]/g;
|
|
119
132
|
|
|
120
133
|
/** JSON that can sit inside a <script> element. */
|
package/src/document.tsx
CHANGED
|
@@ -15,8 +15,17 @@ type DocumentProps = { info: DocumentInfo; dataJson: string; children: ReactNode
|
|
|
15
15
|
*/
|
|
16
16
|
export function Document({ info, dataJson, children }: DocumentProps) {
|
|
17
17
|
const { nonce } = info;
|
|
18
|
+
// Netjes owns <html> and <body>: the app's layout renders inside the body, so its markup cannot give either one an
|
|
19
|
+
// attribute. A script that runs before hydration can (a theme, a browser extension, a translator), and in development
|
|
20
|
+
// React logs a mismatch for each attribute on an element that its props do not have. suppressHydrationWarning ends
|
|
21
|
+
// that check for the element's own attributes. It also makes React accept any text that is the element's own child,
|
|
22
|
+
// in production too: text that a layout or a page renders outside any element or Suspense boundary sits directly in
|
|
23
|
+
// the body, and a mismatch there is neither reported nor repaired. The elements in the body are checked as before,
|
|
24
|
+
// their attributes and their text. React does not write the prop into the markup, and production does not check
|
|
25
|
+
// attributes at all. The check is off for `lang` too: the server and the client read it from the same payload, so it
|
|
26
|
+
// differs only when a script changed it, or when Netjes has a bug.
|
|
18
27
|
return (
|
|
19
|
-
<html lang={info.lang}>
|
|
28
|
+
<html lang={info.lang} suppressHydrationWarning>
|
|
20
29
|
<head>
|
|
21
30
|
<meta charSet="utf-8" />
|
|
22
31
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
@@ -28,8 +37,18 @@ export function Document({ info, dataJson, children }: DocumentProps) {
|
|
|
28
37
|
{info.assets.stylesheets.map((href) => (
|
|
29
38
|
<link key={href} rel="stylesheet" href={href} nonce={nonce} />
|
|
30
39
|
))}
|
|
40
|
+
{/* In development, the CSS the page imports, so that it is styled before its scripts run. Vite's client owns
|
|
41
|
+
each element's text once it runs: the CSS module with that id sets it as it loads, before hydration, and
|
|
42
|
+
again after every edit. The sidecar inlines the build that module carries, but a file edited between the
|
|
43
|
+
render and the import differs, so React leaves the text unchecked. */}
|
|
31
44
|
{info.devStyles?.map((style) => (
|
|
32
|
-
<style
|
|
45
|
+
<style
|
|
46
|
+
key={style.id}
|
|
47
|
+
data-vite-dev-id={style.id}
|
|
48
|
+
nonce={nonce}
|
|
49
|
+
suppressHydrationWarning
|
|
50
|
+
dangerouslySetInnerHTML={{ __html: style.css }}
|
|
51
|
+
/>
|
|
33
52
|
))}
|
|
34
53
|
{info.assets.modulePreloads.map((href) => (
|
|
35
54
|
<link key={href} rel="modulepreload" href={href} nonce={nonce} />
|
|
@@ -38,7 +57,7 @@ export function Document({ info, dataJson, children }: DocumentProps) {
|
|
|
38
57
|
<script key={src} type="module" src={src} async nonce={nonce} />
|
|
39
58
|
))}
|
|
40
59
|
</head>
|
|
41
|
-
<body>
|
|
60
|
+
<body suppressHydrationWarning>
|
|
42
61
|
{/* Already escaped for HTML by the server; the client reads it back verbatim to hydrate. */}
|
|
43
62
|
<script id={DATA_ELEMENT_ID} type="application/json" nonce={nonce} dangerouslySetInnerHTML={{ __html: dataJson }} />
|
|
44
63
|
{children}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The mark on errors whose message Netjes withheld: `withheld` is true when the message is Netjes's stand-in for one it
|
|
2
|
+
// does not show (error details are off, or the thrown value had none), so `!error.withheld` means it is worth showing.
|
|
3
|
+
|
|
4
|
+
/** Netjes's stand-in for a message it does not show, marked as one. */
|
|
5
|
+
export function withheldError(): Error & { withheld: true } {
|
|
6
|
+
return Object.assign(new Error('Something went wrong'), { withheld: true as const });
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Whether `error` is marked as one whose message was withheld. */
|
|
10
|
+
export function isWithheld(error: unknown): boolean {
|
|
11
|
+
return error instanceof Error && (error as { withheld?: unknown }).withheld === true;
|
|
12
|
+
}
|
package/src/form.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ComponentPropsWithRef, SubmitEvent } from 'react';
|
|
2
2
|
import { RETURN_FIELD, actionMetaOf } from './actions.tsx';
|
|
3
3
|
import { encodeForm } from './form-data.ts';
|
|
4
4
|
import type { Href } from './href.ts';
|
|
@@ -7,8 +7,8 @@ import { useBasePath, useInternalRouter, usePagePayload, withBasePath } from './
|
|
|
7
7
|
/** A React form action, e.g. the `formAction` from `useAction(serverAction('name'))`. */
|
|
8
8
|
export type FormAction = (formData: FormData) => void | Promise<void>;
|
|
9
9
|
|
|
10
|
-
/** The props of `<Form>`: a form's own, with client navigation and server action support. */
|
|
11
|
-
export type FormProps = Omit<
|
|
10
|
+
/** The props of `<Form>`: a form's own and its ref, with client navigation and server action support. */
|
|
11
|
+
export type FormProps = Omit<ComponentPropsWithRef<'form'>, 'method' | 'action'> & {
|
|
12
12
|
method?: 'get' | 'post';
|
|
13
13
|
/**
|
|
14
14
|
* Where to submit. Default: the current URL, which is where `MapPageAction` handlers for this page listen. Or a
|
|
@@ -74,6 +74,9 @@ export function Form({ method = 'get', action, replace = false, reloadDocument =
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
function handleSubmit(event: SubmitEvent<HTMLFormElement>) {
|
|
77
|
+
// Read before the app's own onSubmit, which runs as the form is submitted: where it moves the focus, the router
|
|
78
|
+
// leaves it, as after a link's onClick.
|
|
79
|
+
const focusedAtStart = document.activeElement;
|
|
77
80
|
onSubmit?.(event);
|
|
78
81
|
if (event.defaultPrevented || reloadDocument) return;
|
|
79
82
|
const form = event.currentTarget;
|
|
@@ -88,7 +91,7 @@ export function Form({ method = 'get', action, replace = false, reloadDocument =
|
|
|
88
91
|
const data = formDataOf(form, submitter);
|
|
89
92
|
const submitMethod = (submitter?.getAttribute('formmethod') ?? method).toLowerCase();
|
|
90
93
|
if (submitMethod === 'post') {
|
|
91
|
-
void router.submit(url, encodeForm(data, form.enctype === 'multipart/form-data'));
|
|
94
|
+
void router.submit(url, encodeForm(data, form.enctype === 'multipart/form-data'), { focusedAtStart });
|
|
92
95
|
return;
|
|
93
96
|
}
|
|
94
97
|
const query = new URLSearchParams();
|
|
@@ -97,7 +100,7 @@ export function Form({ method = 'get', action, replace = false, reloadDocument =
|
|
|
97
100
|
url.hash = '';
|
|
98
101
|
// Built from the form's own action, which is not one of the app's typed routes.
|
|
99
102
|
const href = url.href as Href;
|
|
100
|
-
void (replace ? router.replace(href) : router.push(href));
|
|
103
|
+
void (replace ? router.replace(href, { focusedAtStart }) : router.push(href, { focusedAtStart }));
|
|
101
104
|
}
|
|
102
105
|
|
|
103
106
|
return (
|
|
@@ -108,8 +111,8 @@ export function Form({ method = 'get', action, replace = false, reloadDocument =
|
|
|
108
111
|
);
|
|
109
112
|
}
|
|
110
113
|
|
|
111
|
-
/** The props of `<SubmitButton>`: a button's own, except `type`. */
|
|
112
|
-
export type SubmitButtonProps = Omit<
|
|
114
|
+
/** The props of `<SubmitButton>`: a button's own and its ref, except `type`. */
|
|
115
|
+
export type SubmitButtonProps = Omit<ComponentPropsWithRef<'button'>, 'type'>;
|
|
113
116
|
|
|
114
117
|
/**
|
|
115
118
|
* A submit button whose `formAction` is rendered under the base path, so it posts to the right place with and
|
package/src/href.ts
CHANGED
|
@@ -26,9 +26,13 @@ function matches(pattern: readonly string[], path: readonly string[]): boolean {
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Whether a URL is one of the app's pages (by route pattern), a link within the page (`?…`, `#…`), or another
|
|
29
|
-
* site. The generated `isHref()` and `Href` schema call this to check URLs that come from data.
|
|
29
|
+
* site. The generated `isHref()` and `Href` schema call this to check URLs that come from data. A URL with an ASCII
|
|
30
|
+
* control character, or with a space at either end, is none of these.
|
|
30
31
|
*/
|
|
31
32
|
export function isRouteHref(value: string, patterns: readonly string[]): boolean {
|
|
33
|
+
// Browsers drop tabs and newlines anywhere in a URL, and control characters and spaces at either end: `/\t/host`
|
|
34
|
+
// would be `//host`. A C# Href refuses the same.
|
|
35
|
+
if (/[\u0000-\u001F\u007F]/.test(value) || value.startsWith(' ') || value.endsWith(' ')) return false;
|
|
32
36
|
if (/^(https?:|mailto:|tel:)/i.test(value) || value.startsWith('?') || value.startsWith('#')) return true;
|
|
33
37
|
// "//host" and "/\host" are other hosts as far as browsers are concerned.
|
|
34
38
|
if (!value.startsWith('/') || value.startsWith('//') || value.startsWith('/\\')) return false;
|
|
@@ -36,14 +40,39 @@ export function isRouteHref(value: string, patterns: readonly string[]): boolean
|
|
|
36
40
|
return patterns.some((pattern) => matches(pattern.split('/').filter(Boolean), path));
|
|
37
41
|
}
|
|
38
42
|
|
|
43
|
+
/**
|
|
44
|
+
* A segment that would change the URL's path when a browser or `fetch` resolves it: `.` and `..` are dot segments,
|
|
45
|
+
* which move up the path, and an empty one doubles a slash, which right after the leading one names another host.
|
|
46
|
+
*/
|
|
47
|
+
function changesThePath(segment: string): boolean {
|
|
48
|
+
return segment === '' || segment === '.' || segment === '..';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const changesThePathMessage = "a '.', '..' or empty segment would change the URL's path";
|
|
52
|
+
|
|
39
53
|
/**
|
|
40
54
|
* Builds the URL for a route pattern written in file-name syntax (`/blog/[slug]`, `/docs/[...path]`,
|
|
41
|
-
* `/shop/[[...filters]]`), encoding every segment value.
|
|
55
|
+
* `/shop/[[...filters]]`), encoding every segment value. A value, or a catch-all entry, that is `.`, `..` or empty
|
|
56
|
+
* throws, since it would change the path the URL names. `options.hash` adds a fragment after the query: the id of an
|
|
57
|
+
* element in the page, with one leading `#` dropped and the rest encoded; with nothing left, no fragment is added. The
|
|
58
|
+
* generated `route()` helper calls this with typed params.
|
|
42
59
|
*/
|
|
43
|
-
export function buildHref(
|
|
60
|
+
export function buildHref(
|
|
61
|
+
pattern: string,
|
|
62
|
+
params: Record<string, unknown>,
|
|
63
|
+
search?: SearchParamsInput,
|
|
64
|
+
options?: { hash?: string | undefined },
|
|
65
|
+
): string {
|
|
44
66
|
const missing = (name: string): never => {
|
|
45
67
|
throw new Error(`netjes: route '${pattern}' needs the parameter '${name}'`);
|
|
46
68
|
};
|
|
69
|
+
const encode = (name: string, value: unknown): string => {
|
|
70
|
+
const text = String(value);
|
|
71
|
+
if (changesThePath(text)) {
|
|
72
|
+
throw new Error(`netjes: route '${pattern}' cannot take '${text}' for the parameter '${name}': ${changesThePathMessage}`);
|
|
73
|
+
}
|
|
74
|
+
return encodeURIComponent(text);
|
|
75
|
+
};
|
|
47
76
|
const segments = pattern
|
|
48
77
|
.split('/')
|
|
49
78
|
.filter(Boolean)
|
|
@@ -51,29 +80,115 @@ export function buildHref(pattern: string, params: Record<string, unknown>, sear
|
|
|
51
80
|
const optional = /^\[\[\.\.\.(\w+)\]\]$/.exec(segment)?.[1];
|
|
52
81
|
if (optional) {
|
|
53
82
|
const values = params[optional];
|
|
54
|
-
return Array.isArray(values) ? values.map((v) =>
|
|
83
|
+
return Array.isArray(values) ? values.map((v) => encode(optional, v)) : [];
|
|
55
84
|
}
|
|
56
85
|
const catchAll = /^\[\.\.\.(\w+)\]$/.exec(segment)?.[1];
|
|
57
86
|
if (catchAll) {
|
|
58
87
|
const values = params[catchAll];
|
|
59
88
|
if (!Array.isArray(values) || values.length === 0) return missing(catchAll);
|
|
60
|
-
return values.map((v) =>
|
|
89
|
+
return values.map((v) => encode(catchAll, v));
|
|
61
90
|
}
|
|
62
91
|
const single = /^\[(\w+)\]$/.exec(segment)?.[1];
|
|
63
92
|
if (single) {
|
|
64
93
|
const value = params[single];
|
|
65
94
|
if (value === undefined || value === null || value === '') return missing(single);
|
|
66
|
-
return [
|
|
95
|
+
return [encode(single, value)];
|
|
67
96
|
}
|
|
68
97
|
return [segment];
|
|
69
98
|
});
|
|
70
99
|
|
|
71
100
|
const query = new URLSearchParams();
|
|
101
|
+
appendSearch(query, search);
|
|
102
|
+
const queryString = query.toString();
|
|
103
|
+
const fragment = (options?.hash ?? '').replace(/^#/, '');
|
|
104
|
+
return `/${segments.join('/')}${queryString ? `?${queryString}` : ''}${fragment ? `#${encodeURIComponent(fragment)}` : ''}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Appends each value of `search`, a list repeating its key; `null` and `undefined` leave the key out. */
|
|
108
|
+
function appendSearch(query: URLSearchParams, search: Readonly<Record<string, unknown>> | undefined): void {
|
|
72
109
|
for (const [key, value] of Object.entries(search ?? {})) {
|
|
73
|
-
for (const item of Array.isArray(value) ? value : [value]) {
|
|
110
|
+
for (const item of Array.isArray(value) ? (value as readonly unknown[]) : [value]) {
|
|
74
111
|
if (item !== undefined && item !== null) query.append(key, String(item));
|
|
75
112
|
}
|
|
76
113
|
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** A missing route value: `undefined`, `null` or `''`. */
|
|
117
|
+
function absent(value: unknown): boolean {
|
|
118
|
+
return value === undefined || value === null || value === '';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Builds the URL of one of the app's endpoints from its route template, as the generated file writes it: the
|
|
123
|
+
* literals as they are (with `%`, `{`, `}`, `#` and `\` encoded), `{name}`, `{name?}`, `{name=default}` (the default
|
|
124
|
+
* encoded), `{*name}` and `{**name}`, without constraints. Each route value is encoded; a `{**name}` value keeps its
|
|
125
|
+
* slashes, and a `{*name}` value has them encoded. A value that would make a segment `.` or `..`, or a `{**name}` value
|
|
126
|
+
* with such a piece or with an empty one before its end (`/x`, `a//b`), throws `netjes: endpoint '<endpoint>' cannot
|
|
127
|
+
* take '<value>' for the route value '<name>'`, since it would change the path the URL names. A missing, `null` or
|
|
128
|
+
* empty optional value drops its segment, or its
|
|
129
|
+
* part of a segment with the `.` before it (`{name}.{ext?}`); a missing value with a default is filled in, unless it
|
|
130
|
+
* is a whole segment at the end, which is dropped; a missing, `null` or empty required value, or an optional one whose
|
|
131
|
+
* segment is followed by one that stays, throws `netjes: endpoint '<endpoint>' needs the route value '<name>'`. The
|
|
132
|
+
* values the template does not name are the query string, followed by `search`. The result is root-relative and has no base path: pass it through
|
|
133
|
+
* `useHref` for a `fetch` or an `<a href>`. The generated `endpointUrl()` calls this with typed values.
|
|
134
|
+
*/
|
|
135
|
+
export function buildEndpointUrl(
|
|
136
|
+
template: string,
|
|
137
|
+
values: Readonly<Record<string, unknown>>,
|
|
138
|
+
search?: SearchParamsInput,
|
|
139
|
+
endpoint?: string,
|
|
140
|
+
): string {
|
|
141
|
+
const needs = (name: string) => new Error(`netjes: endpoint '${endpoint ?? template}' needs the route value '${name}'`);
|
|
142
|
+
const cannotTake = (name: string, value: unknown) =>
|
|
143
|
+
new Error(`netjes: endpoint '${endpoint ?? template}' cannot take '${String(value)}' for the route value '${name}': ${changesThePathMessage}`);
|
|
144
|
+
const named = new Set<string>();
|
|
145
|
+
// Each segment's text, and the name of the value it is missing, when the segment can go for want of it.
|
|
146
|
+
const segments: { text: string; missing: string | undefined }[] = [];
|
|
147
|
+
for (const segment of template.split('/').filter(Boolean)) {
|
|
148
|
+
let text = '';
|
|
149
|
+
let missing: string | undefined;
|
|
150
|
+
// The value that made the segment's text, which must not leave it a dot segment.
|
|
151
|
+
let filled: string | undefined;
|
|
152
|
+
for (const part of segment.split(/(\{[^}]*\})/).filter(Boolean)) {
|
|
153
|
+
const parameter = /^\{(\*\*?)?([^=?]+?)(\?|=([^]*))?\}$/.exec(part);
|
|
154
|
+
if (!parameter) {
|
|
155
|
+
text += part;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const [, star, name = '', marker, fallback] = parameter;
|
|
160
|
+
named.add(name);
|
|
161
|
+
const value = values[name];
|
|
162
|
+
if (!absent(value) && star === '**') {
|
|
163
|
+
// Each piece is a segment of its own; an empty last one is a trailing slash.
|
|
164
|
+
const pieces = String(value).split('/');
|
|
165
|
+
if (pieces.some((piece, i) => (piece === '' ? i < pieces.length - 1 : changesThePath(piece)))) throw cannotTake(name, value);
|
|
166
|
+
text += pieces.map((piece) => encodeURIComponent(piece)).join('/');
|
|
167
|
+
} else if (!absent(value)) {
|
|
168
|
+
text += encodeURIComponent(String(value));
|
|
169
|
+
filled = name;
|
|
170
|
+
} else if (fallback !== undefined) {
|
|
171
|
+
text += fallback;
|
|
172
|
+
if (part === segment) missing = name;
|
|
173
|
+
} else if (marker === '?' || star) {
|
|
174
|
+
// An optional value after a literal `.` takes the dot with it.
|
|
175
|
+
if (text.endsWith('.')) text = text.slice(0, -1);
|
|
176
|
+
if (!text) missing = name;
|
|
177
|
+
} else {
|
|
178
|
+
throw needs(name);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (filled && changesThePath(text)) throw cannotTake(filled, values[filled]);
|
|
182
|
+
if (text || missing) segments.push({ text, missing });
|
|
183
|
+
}
|
|
184
|
+
while (segments.at(-1)?.missing) segments.pop();
|
|
185
|
+
// A segment left out before one that stays: routing would not match, or would read the next value as this one.
|
|
186
|
+
const leftOut = segments.find((segment) => !segment.text)?.missing;
|
|
187
|
+
if (leftOut) throw needs(leftOut);
|
|
188
|
+
|
|
189
|
+
const query = new URLSearchParams();
|
|
190
|
+
appendSearch(query, Object.fromEntries(Object.entries(values).filter(([key]) => !named.has(key))));
|
|
191
|
+
appendSearch(query, search);
|
|
77
192
|
const queryString = query.toString();
|
|
78
|
-
return `/${segments.join('/')}${queryString ? `?${queryString}` : ''}`;
|
|
193
|
+
return `/${segments.map((s) => s.text).join('/')}${queryString ? `?${queryString}` : ''}`;
|
|
79
194
|
}
|
package/src/image.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ComponentPropsWithRef, createContext, useContext } from 'react';
|
|
2
2
|
import { preload } from 'react-dom';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -17,8 +17,8 @@ const ImageConfigContext = createContext<ImageConfig>(DEFAULT_CONFIG);
|
|
|
17
17
|
|
|
18
18
|
export const ImageConfigProvider = ImageConfigContext.Provider;
|
|
19
19
|
|
|
20
|
-
/** The props of `<Image>`: an `<img>`'s own, with a required size and `alt`, and the optimizer's options. */
|
|
21
|
-
export type ImageProps = Omit<
|
|
20
|
+
/** The props of `<Image>`: an `<img>`'s own and its ref, with a required size and `alt`, and the optimizer's options. */
|
|
21
|
+
export type ImageProps = Omit<ComponentPropsWithRef<'img'>, 'src' | 'srcSet' | 'width' | 'height' | 'loading' | 'alt'> & {
|
|
22
22
|
/** A path in `public/` (`/img/hero.png`) or a URL on a host in Netjes:Images:RemoteHosts. */
|
|
23
23
|
src: string;
|
|
24
24
|
/** Required, as for any image; `""` for decorative ones. */
|
package/src/index.ts
CHANGED
|
@@ -9,10 +9,11 @@ export {
|
|
|
9
9
|
usePageActionState,
|
|
10
10
|
} from './actions.tsx';
|
|
11
11
|
export { defineLayout, definePage } from './define-page.tsx';
|
|
12
|
+
export { CSP_SCRIPT_HASHES } from './deferred.ts';
|
|
12
13
|
export { useDraftMode } from './draft-mode.ts';
|
|
13
14
|
export { type DynamicOptions, clientOnly, dynamic } from './dynamic.tsx';
|
|
14
15
|
export { Form, type FormAction, type FormProps, SubmitButton, type SubmitButtonProps } from './form.tsx';
|
|
15
|
-
export { type Href, type Register, type SearchParamsInput, type SearchValue, buildHref, isRouteHref } from './href.ts';
|
|
16
|
+
export { type Href, type Register, type SearchParamsInput, type SearchValue, buildEndpointUrl, buildHref, isRouteHref } from './href.ts';
|
|
16
17
|
export { Image, type ImageProps } from './image.tsx';
|
|
17
18
|
export { Link, type LinkProps } from './link.tsx';
|
|
18
19
|
export { useLocale } from './locale.ts';
|
package/src/link.tsx
CHANGED
|
@@ -1,25 +1,50 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ComponentPropsWithRef, MouseEvent } from 'react';
|
|
2
2
|
import type { Href } from './href.ts';
|
|
3
|
-
import { useBasePath, useInternalRouter, usePathname, withBasePath } from './router.tsx';
|
|
3
|
+
import { useBasePath, useInternalRouter, usePathname, useSearchParams, withBasePath } from './router.tsx';
|
|
4
4
|
|
|
5
|
-
/** The props of `<Link>`: an anchor's own, with a typed `href` and the client navigation options. */
|
|
6
|
-
export type LinkProps = Omit<
|
|
5
|
+
/** The props of `<Link>`: an anchor's own and its ref, with a typed `href` and the client navigation options. */
|
|
6
|
+
export type LinkProps = Omit<ComponentPropsWithRef<'a'>, 'href'> & {
|
|
7
7
|
/** Typed by the app's routes once `netjes generate` has registered them. */
|
|
8
8
|
href: Href;
|
|
9
9
|
replace?: boolean;
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Scroll to the top after navigating, or to the #fragment's element, which then takes the focus. Default true. `false`
|
|
12
|
+
* keeps the scroll position, and the focus on the link while the link has it.
|
|
13
|
+
*/
|
|
11
14
|
scroll?: boolean;
|
|
12
|
-
/** `intent` (default) fetches the next page on hover or
|
|
15
|
+
/** `intent` (default) fetches the next page on hover, focus or touch. */
|
|
13
16
|
prefetch?: 'intent' | 'none';
|
|
14
|
-
/** Let the browser follow it with a full page load
|
|
17
|
+
/** Let the browser follow it with a full page load: for a page of the app that must load in full, or a page on another site. */
|
|
15
18
|
reloadDocument?: boolean;
|
|
16
19
|
};
|
|
17
20
|
|
|
18
21
|
const BASE = 'http://netjes.invalid';
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return
|
|
23
|
+
/** One trailing slash makes no other page: `/docs/` is `/docs`, and `/` stays `/`. */
|
|
24
|
+
function withoutTrailingSlash(path: string) {
|
|
25
|
+
return path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The same values, in any order, as often each. */
|
|
29
|
+
function sameValues(a: string[], b: string[]) {
|
|
30
|
+
const sorted = [...b].sort();
|
|
31
|
+
return a.length === b.length && [...a].sort().every((value, i) => value === sorted[i]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Whether the link goes to the page on screen. An href with a `#` points to a place in a page, so it never does.
|
|
36
|
+
* Otherwise its URL under the base path has the page's path, give or take one trailing slash, and the page's
|
|
37
|
+
* values for each query parameter it names; parameters it leaves out do not count. Only the path and the query
|
|
38
|
+
* enter, against a fixed origin, so the server and the browser agree.
|
|
39
|
+
*/
|
|
40
|
+
function isCurrentPage(href: string, pathname: string, search: URLSearchParams, basePath: string) {
|
|
41
|
+
if (href.includes('#')) return false;
|
|
42
|
+
const query = search.toString();
|
|
43
|
+
// The page's URL as the browser has it: the base path, then the page's path, even one that starts like the base path.
|
|
44
|
+
const page = new URL(BASE + basePath + pathname + (query ? `?${query}` : ''));
|
|
45
|
+
const target = new URL(withBasePath(href, basePath), page);
|
|
46
|
+
if (target.origin !== BASE || withoutTrailingSlash(target.pathname) !== withoutTrailingSlash(page.pathname)) return false;
|
|
47
|
+
return [...new Set(target.searchParams.keys())].every((key) => sameValues(target.searchParams.getAll(key), search.getAll(key)));
|
|
23
48
|
}
|
|
24
49
|
|
|
25
50
|
function isPlainLeftClick(event: MouseEvent<HTMLAnchorElement>) {
|
|
@@ -35,10 +60,16 @@ function isPlainLeftClick(event: MouseEvent<HTMLAnchorElement>) {
|
|
|
35
60
|
);
|
|
36
61
|
}
|
|
37
62
|
|
|
38
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* An `<a>` that navigates on the client. Ctrl/cmd-click, new tabs and external URLs behave like a normal link. It sets
|
|
65
|
+
* `aria-current="page"` on a link to the page on screen: the same path, one trailing slash aside, and the page's values
|
|
66
|
+
* for each query parameter the link names. A link with a `#` is never marked, and an `aria-current` prop wins. After a
|
|
67
|
+
* navigation to another page, the router announces the page and starts the focus over at the top of the document.
|
|
68
|
+
*/
|
|
39
69
|
export function Link({ href, replace = false, scroll = true, prefetch = 'intent', reloadDocument = false, onClick, onMouseEnter, onFocus, onTouchStart, ...rest }: LinkProps) {
|
|
40
70
|
const router = useInternalRouter();
|
|
41
71
|
const pathname = usePathname();
|
|
72
|
+
const search = useSearchParams();
|
|
42
73
|
const basePath = useBasePath();
|
|
43
74
|
const warm = () => {
|
|
44
75
|
if (prefetch === 'intent' && !reloadDocument) router.prefetch(href);
|
|
@@ -47,14 +78,17 @@ export function Link({ href, replace = false, scroll = true, prefetch = 'intent'
|
|
|
47
78
|
return (
|
|
48
79
|
<a
|
|
49
80
|
href={withBasePath(href, basePath)}
|
|
50
|
-
aria-current={
|
|
81
|
+
aria-current={isCurrentPage(href, pathname, search, basePath) ? 'page' : undefined}
|
|
51
82
|
{...rest}
|
|
52
83
|
onClick={(event) => {
|
|
84
|
+
// Read before the app's own onClick, which runs as the link is followed: where it moves the focus, such as
|
|
85
|
+
// back to the button of a menu that closes, the router leaves it.
|
|
86
|
+
const focusedAtStart = document.activeElement;
|
|
53
87
|
onClick?.(event);
|
|
54
88
|
if (event.defaultPrevented || reloadDocument || !isPlainLeftClick(event)) return;
|
|
55
89
|
if (new URL(href, window.location.href).origin !== window.location.origin) return;
|
|
56
90
|
event.preventDefault();
|
|
57
|
-
void (replace ? router.replace(href, { scroll }) : router.push(href, { scroll }));
|
|
91
|
+
void (replace ? router.replace(href, { scroll, focusedAtStart }) : router.push(href, { scroll, focusedAtStart }));
|
|
58
92
|
}}
|
|
59
93
|
onMouseEnter={(event) => {
|
|
60
94
|
onMouseEnter?.(event);
|
package/src/metadata.tsx
CHANGED
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
import { Fragment } from 'react';
|
|
5
5
|
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* A title, a layout's `template` (`'%s · Site'`) and `default`, or an `absolute` title that skips templates. A
|
|
8
|
+
* `default`, on a layout or a page, goes through the nearest `template` of the layouts above it like a string title,
|
|
9
|
+
* never through the one beside it; a layout's `default` is also the title of the pages below it that set none.
|
|
10
|
+
*/
|
|
7
11
|
export type MetadataTitle = string | { default?: string; template?: string; absolute?: string };
|
|
8
12
|
|
|
9
13
|
/** An image for a link preview: `og:image` with `og:image:width`, `og:image:height` and `og:image:alt` when given, or `twitter:image` with `twitter:image:alt`. */
|
|
@@ -48,12 +52,18 @@ export type ResolvedMetadata = Omit<Metadata, 'title' | 'openGraph' | 'twitter'>
|
|
|
48
52
|
twitter?: Omit<Twitter, 'images'> & { images?: ResolvedImage[] };
|
|
49
53
|
};
|
|
50
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The title that `title` stands for, given the template from the layouts above it. A string and a `default` go
|
|
57
|
+
* through the template, an `absolute` title does not, and an object with no `default` and no `absolute` (a layout
|
|
58
|
+
* that only sets a template) has no title of its own.
|
|
59
|
+
*/
|
|
51
60
|
function titleOf(title: MetadataTitle | undefined, template: string | undefined): string | undefined {
|
|
52
61
|
if (title === undefined) return undefined;
|
|
53
62
|
// A function replacer: a title such as "Save $& now" must not be read as a replacement pattern.
|
|
54
|
-
|
|
63
|
+
const inTemplate = (text: string) => (template ? template.replace('%s', () => text) : text);
|
|
64
|
+
if (typeof title === 'string') return inTemplate(title);
|
|
55
65
|
if (title.absolute !== undefined) return title.absolute;
|
|
56
|
-
return title.default;
|
|
66
|
+
return title.default === undefined ? undefined : inTemplate(title.default);
|
|
57
67
|
}
|
|
58
68
|
|
|
59
69
|
/** The images as a list of descriptors with string URLs; like Next.js, a null image or one without a URL is left out. */
|
|
@@ -72,7 +82,9 @@ function withImages<T extends { images?: MetadataImages }>(group: T): Omit<T, 'i
|
|
|
72
82
|
|
|
73
83
|
/**
|
|
74
84
|
* Merges the layouts' metadata (outermost first), the page's, and the server's. A layout's `title.template` applies
|
|
75
|
-
* to titles below it
|
|
85
|
+
* to the titles below it. `title.default`, on a layout or a page, goes through the templates above it like a string
|
|
86
|
+
* title and never through its own; on a layout it is the title when nothing below sets one. `title.absolute` skips
|
|
87
|
+
* templates.
|
|
76
88
|
*/
|
|
77
89
|
export function resolveMetadata(layouts: readonly (Metadata | undefined)[], page: Metadata | undefined, server?: Metadata): ResolvedMetadata {
|
|
78
90
|
let resolved: ResolvedMetadata = {};
|
|
@@ -82,8 +94,9 @@ export function resolveMetadata(layouts: readonly (Metadata | undefined)[], page
|
|
|
82
94
|
const { title, openGraph, twitter, alternates, icons, other, ...rest } = metadata;
|
|
83
95
|
resolved = { ...resolved, ...rest };
|
|
84
96
|
if (title !== undefined) {
|
|
85
|
-
//
|
|
86
|
-
|
|
97
|
+
// The template from above applies to every title but an absolute one, a default included. This title's own
|
|
98
|
+
// template is stored after its title is written, so it applies only to the titles below.
|
|
99
|
+
const own = titleOf(title, template);
|
|
87
100
|
if (own !== undefined) resolved.title = own;
|
|
88
101
|
if (typeof title === 'object' && title.template) template = title.template;
|
|
89
102
|
}
|
package/src/modules.ts
CHANGED
|
@@ -6,8 +6,21 @@ export type PageComponent = ComponentType<Record<string, unknown>>;
|
|
|
6
6
|
/** The props every layout gets: its `children` and the route `params`. The loader's props and parallel route slots arrive as further props. */
|
|
7
7
|
export type LayoutProps = { children: ReactNode; params: Params };
|
|
8
8
|
export type LayoutComponent = ComponentType<LayoutProps & Record<string, unknown>>;
|
|
9
|
-
/**
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The props of an `error.tsx`: what was thrown, and a way to try again.
|
|
11
|
+
*
|
|
12
|
+
* - `error.status` is the HTTP status of a server answer behind the error: the status of the error page the server
|
|
13
|
+
* rendered, or of a server action call that failed. An error thrown in the browser has none.
|
|
14
|
+
* - `error.withheld` is true when the message is Netjes's stand-in for one it does not show: the server's reason
|
|
15
|
+
* phrase for an exception while error details are off, "Something went wrong" for a `Page.Defer` failure the server
|
|
16
|
+
* withheld or a thrown value that has no message, or Netjes's own text for a server action whose answer had no
|
|
17
|
+
* message. Render `error.message` only when `!error.withheld`.
|
|
18
|
+
* - `error.stack` is the server's stack when error details are on, and undefined for a server error sent without one.
|
|
19
|
+
* - `reset()` renders the segment again after an error a segment's boundary caught. On an error page the server
|
|
20
|
+
* rendered it loads the page from the server again (`router.refresh()`), and on the page the router's own boundary
|
|
21
|
+
* shows it does nothing: offer a link or a full reload there.
|
|
22
|
+
*/
|
|
23
|
+
export type ErrorPageProps = { error: Error & { status?: number; withheld?: boolean }; reset: () => void };
|
|
11
24
|
|
|
12
25
|
export type LoadedSegment = {
|
|
13
26
|
id: string;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/** Input types the user does not type into: the focus can leave them when the page they are on is replaced. */
|
|
2
|
+
const NOT_TYPED_IN = new Set(['button', 'checkbox', 'color', 'file', 'hidden', 'image', 'radio', 'range', 'reset', 'submit']);
|
|
3
|
+
|
|
4
|
+
/** Whether the user types into the element, or picks a value in it from the keyboard: a text field, a select, editable content. */
|
|
5
|
+
export function isEditable(element: Element): boolean {
|
|
6
|
+
if (element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) return true;
|
|
7
|
+
if (element instanceof HTMLInputElement) return !NOT_TYPED_IN.has(element.type);
|
|
8
|
+
return element instanceof HTMLElement && element.isContentEditable;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Focuses an element without scrolling to it. One that takes no focus of its own, such as a heading or `main`, gets
|
|
13
|
+
* `tabindex="-1"` for it, which goes again when it loses the focus. Returns whether the element has the focus.
|
|
14
|
+
*/
|
|
15
|
+
export function focusElement(element: HTMLElement): boolean {
|
|
16
|
+
element.focus({ preventScroll: true });
|
|
17
|
+
if (document.activeElement === element) return true;
|
|
18
|
+
// A tabindex of the app's own did not help (a hidden or inert element): leave it as it is.
|
|
19
|
+
if (element.hasAttribute('tabindex')) return false;
|
|
20
|
+
element.setAttribute('tabindex', '-1');
|
|
21
|
+
element.focus({ preventScroll: true });
|
|
22
|
+
if (document.activeElement !== element) {
|
|
23
|
+
element.removeAttribute('tabindex');
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
element.addEventListener('blur', () => element.removeAttribute('tabindex'), { once: true });
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Starts the focus over at the top of the document, as a page load does: the next Tab goes to the first element that
|
|
32
|
+
* takes the focus, such as a skip link. `body` takes the focus for this only, without scrolling, and gets its own
|
|
33
|
+
* `tabindex` back, or none.
|
|
34
|
+
*/
|
|
35
|
+
export function resetFocus() {
|
|
36
|
+
const body = document.body;
|
|
37
|
+
const own = body.getAttribute('tabindex');
|
|
38
|
+
body.tabIndex = -1;
|
|
39
|
+
body.focus({ preventScroll: true, focusVisible: false });
|
|
40
|
+
if (own === null) body.removeAttribute('tabindex');
|
|
41
|
+
else body.setAttribute('tabindex', own);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type NavigationFocus = {
|
|
45
|
+
/** The element the URL's fragment names, which the router scrolled to; null without a fragment, or when none matched. */
|
|
46
|
+
target: HTMLElement | null;
|
|
47
|
+
/** The page on screen is another one: its page key changed. */
|
|
48
|
+
replaced: boolean;
|
|
49
|
+
/** The navigation keeps the scroll position: `scroll={false}`. */
|
|
50
|
+
keep: boolean;
|
|
51
|
+
/** What had the focus when the navigation started. */
|
|
52
|
+
focusedAtStart: Element | null;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Moves the focus after a navigation, once the router has scrolled, by the first of these rules that applies:
|
|
57
|
+
*
|
|
58
|
+
* 1. The fragment's target takes it, on this page or on another. One that cannot (a hidden element) leaves it to the
|
|
59
|
+
* rules below.
|
|
60
|
+
* 2. It stays on the same page: a query that changed, a refresh, a post that stays on its page, a modal opening or
|
|
61
|
+
* closing over the page under it.
|
|
62
|
+
* 3. It stays where it went while the navigation ran: to the new page (`autoFocus`), or where the user or a handler
|
|
63
|
+
* of the app (a menu that closes) put it.
|
|
64
|
+
* 4. It stays on what had it when the navigation started, with `scroll={false}` or in a field the user types into,
|
|
65
|
+
* such as a search box in a layout that navigates as the user types.
|
|
66
|
+
* 5. Otherwise it starts over at the top of the document, as after a page load.
|
|
67
|
+
*/
|
|
68
|
+
export function moveFocusAfterNavigation({ target, replaced, keep, focusedAtStart }: NavigationFocus) {
|
|
69
|
+
if (target && focusElement(target)) return;
|
|
70
|
+
if (!replaced) return;
|
|
71
|
+
const focused = document.activeElement;
|
|
72
|
+
if (focused && focused !== document.body && focused !== focusedAtStart) return;
|
|
73
|
+
if (focused && focused === focusedAtStart && (keep || isEditable(focused))) return;
|
|
74
|
+
resetFocus();
|
|
75
|
+
}
|