shelving 1.319.1 → 1.321.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.
Files changed (106) hide show
  1. package/docs/guides/building-with-shelving.md +26 -0
  2. package/docs/llms-full.txt +752 -265
  3. package/docs/ui/BackButton.md +75 -0
  4. package/docs/ui/BackButtonProps.md +21 -0
  5. package/docs/ui/ClickableProps.md +0 -2
  6. package/docs/ui/FADE_TRANSITION_CLASS.md +9 -0
  7. package/docs/ui/FadeTransition.md +13 -0
  8. package/docs/ui/Head.md +2 -2
  9. package/docs/ui/HorizontalTransition.md +21 -5
  10. package/docs/ui/LinkClickable.md +0 -1
  11. package/docs/ui/Meta.md +3 -1
  12. package/docs/ui/Navigation.md +93 -37
  13. package/docs/ui/NavigationEntryState.md +22 -0
  14. package/docs/ui/NavigationPosition.md +24 -0
  15. package/docs/ui/NavigationPositionContext.md +8 -0
  16. package/docs/ui/NavigationProps.md +12 -2
  17. package/docs/ui/NavigationStore/back.md +6 -4
  18. package/docs/ui/NavigationStore/forward.md +6 -2
  19. package/docs/ui/NavigationStore/history.md +15 -0
  20. package/docs/ui/NavigationStore/listen.md +14 -0
  21. package/docs/ui/NavigationStore/replace.md +15 -0
  22. package/docs/ui/NavigationStore.md +100 -19
  23. package/docs/ui/RouteCache.md +72 -36
  24. package/docs/ui/RouteCacheProps.md +3 -3
  25. package/docs/ui/Router.md +4 -2
  26. package/docs/ui/SLIDE_DOWN_TRANSITION_CLASS.md +9 -0
  27. package/docs/ui/SLIDE_LEFT_TRANSITION_CLASS.md +9 -0
  28. package/docs/ui/SLIDE_RIGHT_TRANSITION_CLASS.md +9 -0
  29. package/docs/ui/SLIDE_UP_TRANSITION_CLASS.md +9 -0
  30. package/docs/ui/StackButton.md +64 -0
  31. package/docs/ui/StackButtonProps.md +21 -0
  32. package/docs/ui/VerticalTransition.md +21 -5
  33. package/docs/ui/mergeMeta.md +2 -1
  34. package/docs/ui.md +18 -10
  35. package/package.json +1 -1
  36. package/ui/button/Clickable.d.ts +1 -3
  37. package/ui/button/Clickable.js +2 -2
  38. package/ui/button/Clickable.tsx +0 -4
  39. package/ui/dialog/Dialog.d.ts +0 -1
  40. package/ui/dialog/Dialog.js +2 -2
  41. package/ui/dialog/Dialog.tsx +2 -2
  42. package/ui/notice/NoticesTransition.module.css +1 -1
  43. package/ui/page/Head.d.ts +2 -2
  44. package/ui/page/Head.js +9 -8
  45. package/ui/page/Head.tsx +8 -6
  46. package/ui/router/BackButton.d.ts +22 -0
  47. package/ui/router/BackButton.js +19 -0
  48. package/ui/router/BackButton.md +47 -0
  49. package/ui/router/BackButton.tsx +34 -0
  50. package/ui/router/Navigation.d.ts +22 -12
  51. package/ui/router/Navigation.js +67 -51
  52. package/ui/router/Navigation.md +84 -27
  53. package/ui/router/Navigation.test.tsx +27 -7
  54. package/ui/router/Navigation.tsx +90 -53
  55. package/ui/router/NavigationContext.d.ts +9 -1
  56. package/ui/router/NavigationContext.js +9 -0
  57. package/ui/router/NavigationContext.tsx +11 -1
  58. package/ui/router/NavigationStore.d.ts +126 -19
  59. package/ui/router/NavigationStore.js +301 -30
  60. package/ui/router/NavigationStore.md +82 -9
  61. package/ui/router/NavigationStore.test.ts +384 -0
  62. package/ui/router/NavigationStore.tsx +347 -30
  63. package/ui/router/RouteCache.d.ts +15 -22
  64. package/ui/router/RouteCache.js +36 -47
  65. package/ui/router/RouteCache.md +59 -16
  66. package/ui/router/RouteCache.tsx +55 -62
  67. package/ui/router/Router.d.ts +2 -1
  68. package/ui/router/Router.js +9 -14
  69. package/ui/router/Router.md +2 -1
  70. package/ui/router/Router.tsx +9 -20
  71. package/ui/router/StackButton.d.ts +24 -0
  72. package/ui/router/StackButton.js +35 -0
  73. package/ui/router/StackButton.md +34 -0
  74. package/ui/router/StackButton.tsx +44 -0
  75. package/ui/router/index.d.ts +2 -2
  76. package/ui/router/index.js +2 -2
  77. package/ui/router/index.ts +2 -2
  78. package/ui/transition/FadeTransition.d.ts +9 -1
  79. package/ui/transition/FadeTransition.js +12 -2
  80. package/ui/transition/FadeTransition.md +13 -0
  81. package/ui/transition/{FadeTransition.css → FadeTransition.module.css} +9 -2
  82. package/ui/transition/FadeTransition.tsx +13 -2
  83. package/ui/transition/HorizontalTransition.d.ts +18 -1
  84. package/ui/transition/HorizontalTransition.js +21 -2
  85. package/ui/transition/HorizontalTransition.md +21 -5
  86. package/ui/transition/{HorizontalTransition.css → HorizontalTransition.module.css} +17 -5
  87. package/ui/transition/HorizontalTransition.tsx +30 -2
  88. package/ui/transition/VerticalTransition.d.ts +18 -1
  89. package/ui/transition/VerticalTransition.js +21 -2
  90. package/ui/transition/VerticalTransition.md +21 -5
  91. package/ui/transition/{VerticalTransition.css → VerticalTransition.module.css} +17 -5
  92. package/ui/transition/VerticalTransition.tsx +25 -2
  93. package/ui/util/meta.d.ts +4 -1
  94. package/ui/util/meta.ts +4 -1
  95. package/docs/ui/NavigationStore/redirect.md +0 -12
  96. package/docs/ui/RouteConfig.md +0 -75
  97. package/docs/ui/RouteConfigProps.md +0 -24
  98. package/docs/ui/RouteContext.md +0 -6
  99. package/docs/ui/RouteState.md +0 -30
  100. package/ui/router/RouteConfig.d.ts +0 -28
  101. package/ui/router/RouteConfig.js +0 -30
  102. package/ui/router/RouteConfig.md +0 -42
  103. package/ui/router/RouteConfig.tsx +0 -44
  104. package/ui/router/RouteContext.d.ts +0 -22
  105. package/ui/router/RouteContext.js +0 -8
  106. package/ui/router/RouteContext.tsx +0 -26
package/docs/ui.md CHANGED
@@ -193,13 +193,14 @@ The components below are listed in the index following this page; this is the sh
193
193
  - [Empty](ui/Empty.md): Message for a list with nothing in it: centred, grey content with space around it.
194
194
  - [Notice](ui/Notice.md): Block-level status callout with an icon and message, used to highlight feedback.
195
195
  - [Notices](ui/Notices.md): Render the global list of notices and subscribe to incoming "notice" events.
196
- - [Head](ui/Head.md): Emit the current page's head metadata and sync browser history to its URL.
196
+ - [Head](ui/Head.md): Emit the current page's head metadata, and replace the browser URL with the page's canonical URL.
197
197
  - [HTML](ui/HTML.md): Render the full <html> document shell wrapping <head> and <body>.
198
198
  - [Page](ui/Page.md): Wrap a single page (or screen) within an app, applying its meta and head tags.
199
- - [Navigation](ui/Navigation.md): Top-level navigation provider.
200
- - [RouteCache](ui/RouteCache.md): Keep-alive page cache keyed by the current URL — drop it into a layout around its scrolling content region.
201
- - [RouteConfig](ui/RouteConfig.md): Configure the current page for the surrounding <Router> (and <RouteCache>).
199
+ - [BackButton](ui/BackButton.md): Back button for the current stack.
200
+ - [Navigation](ui/Navigation.md): Top-level navigation provider, with one stack of pages for each tab.
201
+ - [RouteCache](ui/RouteCache.md): Keep-alive page cache for the pages in the navigation stacks. Drop it into a layout around its scrolling content region.
202
202
  - [Router](ui/Router.md): Match the current URL against routes and render the matched element.
203
+ - [StackButton](ui/StackButton.md): Button for one stack in a tab bar.
203
204
  - [Scroll](ui/Scroll.md): Wrap children in a scrollable container with horizontal and/or vertical scrolling enabled.
204
205
  - [Cell](ui/Cell.md): Table cell.
205
206
  - [Table](ui/Table.md): Table block — rendered as <table>.
@@ -321,7 +322,7 @@ The components below are listed in the index following this page; this is the sh
321
322
  - [FormStore](ui/FormStore.md): Reactive store holding the current (possibly partial and invalid) value of a form, plus its field messages.
322
323
  - [NoticesStore](ui/NoticesStore.md): Store holding the live list of NoticeStore notices currently shown.
323
324
  - [NoticeStore](ui/NoticeStore.md): Store holding a single notice's content and status, owned by a NoticesStore.
324
- - [NavigationStore](ui/NavigationStore.md): Store holding the current navigation URL and driving browser history.
325
+ - [NavigationStore](ui/NavigationStore.md): Store that holds the current navigation URL, drives browser history, and keeps a stack of pages for each tab.
325
326
 
326
327
  ## Interfaces
327
328
 
@@ -455,11 +456,13 @@ The components below are listed in the index following this page; this is the sh
455
456
  - [NoticesProps](ui/NoticesProps.md): Props for <Notices> — the place for the list, and flex styling variants for the notices container.
456
457
  - [HTMLProps](ui/HTMLProps.md): Props for <HTML> — initial Meta (language/root/app) plus the page children.
457
458
  - [PageProps](ui/PageProps.md): Props for <Page> — per-page Meta (title, description, etc.) plus the page children.
458
- - [NavigationProps](ui/NavigationProps.md): Props for <Navigation> — initial Meta (url/base) plus optional children.
459
- - [RouteCacheProps](ui/RouteCacheProps.md): Props for <RouteCache> — the maxCached size and the content children to keep alive per URL.
460
- - [RouteConfigProps](ui/RouteConfigProps.md): Props for <RouteConfig> — the settings of a page for the surrounding <Router> and <RouteCache>, and the page content.
461
- - [RouteState](ui/RouteState.md): The route state of one page, shared by <Router>, <RouteCache> and <RouteConfig>.
459
+ - [BackButtonProps](ui/BackButtonProps.md): Props for <BackButton>: the href to go back to when the stack has no page before this one, plus the usual Button props except onClick.
460
+ - [NavigationProps](ui/NavigationProps.md): Props for <Navigation>: the initial Meta (url, root and stack), the stacks, and optional children.
461
+ - [NavigationEntryState](ui/NavigationEntryState.md): State that NavigationStore saves on each browser history entry.
462
+ - [NavigationPosition](ui/NavigationPosition.md): Where the app is now in its stacks: the current stack and depth, the top depth of each stack, and the page before the current one.
463
+ - [RouteCacheProps](ui/RouteCacheProps.md): Props for <RouteCache>: the maxCached size and the content children to keep alive for each page in the stacks.
462
464
  - [RouterProps](ui/RouterProps.md): Props for <Router> — the routes to match against plus an optional fallback.
465
+ - [StackButtonProps](ui/StackButtonProps.md): Props for <StackButton>: the stack it opens, plus the usual Button props except href and onClick.
463
466
  - [BlockVariants](ui/BlockVariants.md): Variants for block level elements.
464
467
  - [ColorVariants](ui/ColorVariants.md): Variant props for colouring an element, e.g. color="purple".
465
468
  - [DurationVariants](ui/DurationVariants.md): Variant props for the transition duration of an element, e.g. duration="fast".
@@ -560,9 +563,14 @@ The components below are listed in the index following this page; this is the sh
560
563
  - [LOADING_NOTICE](ui/LOADING_NOTICE.md): Shared loading <Notice> element showing the loading spinner.
561
564
  - [NOTICES](ui/NOTICES.md): Global NoticesStore shown by <Notices>, accepting any of the default statuses.
562
565
  - [NavigationContext](ui/NavigationContext.md): React context holding the current NavigationStore, provided by <Navigation>.
563
- - [RouteContext](ui/RouteContext.md): React context holding the RouteState of the current page, provided by <Router> and <RouteCache>.
566
+ - [NavigationPositionContext](ui/NavigationPositionContext.md): React context holding where the app is in its stacks, published by <Navigation> with each URL change.
564
567
  - [STATUSES](ui/STATUSES.md): Tuple of the status names selectable via the status variant prop.
565
568
  - [TINT_CLASS](ui/TINT_CLASS.md): CSS class that (re)calculates the full ladder of tint shades from the current global tint colour.
569
+ - [FADE_TRANSITION_CLASS](ui/FADE_TRANSITION_CLASS.md): View transition class that fades content in and out.
570
+ - [SLIDE_RIGHT_TRANSITION_CLASS](ui/SLIDE_RIGHT_TRANSITION_CLASS.md): View transition class that slides content right: the new content comes in from the right and the old content leaves to the left.
571
+ - [SLIDE_LEFT_TRANSITION_CLASS](ui/SLIDE_LEFT_TRANSITION_CLASS.md): View transition class that slides content left: the new content comes in from the left and the old content leaves to the right.
572
+ - [SLIDE_DOWN_TRANSITION_CLASS](ui/SLIDE_DOWN_TRANSITION_CLASS.md): View transition class that slides content down: the new content comes in from the top and the old content leaves to the bottom.
573
+ - [SLIDE_UP_TRANSITION_CLASS](ui/SLIDE_UP_TRANSITION_CLASS.md): View transition class that slides content up: the new content comes in from the bottom and the old content leaves to the top.
566
574
  - [TreeContext](ui/TreeContext.md): React context holding a flattened key → element map of the surrounding tree(s).
567
575
  - [TREE_CODE_RULE](ui/TREE_CODE_RULE.md): Markup rule that renders an inline code span as a tree-resolved <TreeLink> instead of a plain <code>.
568
576
  - [TREE_MARKUP_RULES](ui/TREE_MARKUP_RULES.md): Default markup rules with the inline-code rule swapped for TREE_CODE_RULE — every backtick span auto-links to its documented token.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.319.1",
3
+ "version": "1.321.0",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,8 +32,6 @@ export interface ClickableProps extends OptionalChildProps {
32
32
  title?: string | undefined;
33
33
  /** Whether this is the selected item in a group. Sets `aria-pressed` on a `<button>`, or `aria-current` on an `<a>`. */
34
34
  selected?: boolean | undefined;
35
- /** If `href` is present, then `<Navigation>` opens the link with a back transition. Use it for a "back to" link. Sets `data-back` on the `<a>`. */
36
- back?: boolean | undefined;
37
35
  }
38
36
  /**
39
37
  * Props for a clickable that also accepts a `className` for styling.
@@ -62,7 +60,7 @@ export declare function Clickable(props: StylableClickableProps): ReactElement;
62
60
  * @example <LinkClickable href="/about" className="link">About</LinkClickable>
63
61
  * @see https://shelving.cc/ui/LinkClickable
64
62
  */
65
- export declare function LinkClickable({ href, disabled, target, download, title, selected, back, children, className, }: StylableClickableProps): ReactElement;
63
+ export declare function LinkClickable({ href, disabled, target, download, title, selected, children, className, }: StylableClickableProps): ReactElement;
66
64
  /**
67
65
  * Render a `<button>` element that runs its `onClick` handler through a `BusyStore` and shows a loading spinner while busy.
68
66
  * - Notifies the user of the handler's returned value (success) or thrown value (error).
@@ -30,13 +30,13 @@ export function Clickable(props) {
30
30
  * @example <LinkClickable href="/about" className="link">About</LinkClickable>
31
31
  * @see https://shelving.cc/ui/LinkClickable
32
32
  */
33
- export function LinkClickable({ href, disabled = !href, target, download, title, selected, back, children = "Go", className, }) {
33
+ export function LinkClickable({ href, disabled = !href, target, download, title, selected, children = "Go", className, }) {
34
34
  // Resolve `href` against the current page URL and site root so site-absolute paths (`/foo`) honour the base subfolder.
35
35
  const { url, root } = requireMeta();
36
36
  const link = disabled ? undefined : getLink(href, url, root);
37
37
  // Is this link "active" compared to the current URL?
38
38
  const active = isURLActive(link, url);
39
- return (_jsx("a", { href: link?.href, title: title, download: download, target: target, className: getClass(className) || undefined, "aria-current": active ? "page" : selected ? "true" : undefined, "data-back": back || undefined, children: children }));
39
+ return (_jsx("a", { href: link?.href, title: title, download: download, target: target, className: getClass(className) || undefined, "aria-current": active ? "page" : selected ? "true" : undefined, children: children }));
40
40
  }
41
41
  /**
42
42
  * Render a `<button>` element that runs its `onClick` handler through a `BusyStore` and shows a loading spinner while busy.
@@ -43,8 +43,6 @@ export interface ClickableProps extends OptionalChildProps {
43
43
  title?: string | undefined;
44
44
  /** Whether this is the selected item in a group. Sets `aria-pressed` on a `<button>`, or `aria-current` on an `<a>`. */
45
45
  selected?: boolean | undefined;
46
- /** If `href` is present, then `<Navigation>` opens the link with a back transition. Use it for a "back to" link. Sets `data-back` on the `<a>`. */
47
- back?: boolean | undefined;
48
46
  }
49
47
 
50
48
  /**
@@ -90,7 +88,6 @@ export function LinkClickable({
90
88
  download,
91
89
  title,
92
90
  selected,
93
- back,
94
91
  children = "Go",
95
92
  className,
96
93
  }: StylableClickableProps): ReactElement {
@@ -109,7 +106,6 @@ export function LinkClickable({
109
106
  target={target}
110
107
  className={getClass(className) || undefined}
111
108
  aria-current={active ? "page" : selected ? "true" : undefined}
112
- data-back={back || undefined}
113
109
  >
114
110
  {children}
115
111
  </a>
@@ -2,7 +2,6 @@ import { type ReactElement } from "react";
2
2
  import type { Callback } from "../../util/function.js";
3
3
  import { type ButtonVariants } from "../button/Button.js";
4
4
  import type { ClassProps, OptionalChildProps } from "../util/props.js";
5
- import "../transition/FadeTransition.css";
6
5
  /**
7
6
  * Props for `<Dialog>` — optional `children` content and an `onClose` callback.
8
7
  *
@@ -2,8 +2,8 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { memo, Suspense, ViewTransition } from "react";
3
3
  import { CloseIcon } from "shelving/icon/CloseIcon";
4
4
  import { getButtonClass } from "../button/Button.js";
5
+ import { FADE_TRANSITION_CLASS } from "../transition/FadeTransition.js";
5
6
  import { getClass, getModuleClass } from "../util/css.js";
6
- import "../transition/FadeTransition.css";
7
7
  import styles from "./Dialog.module.css";
8
8
  import modalStyles from "./Modal.module.css";
9
9
  /**
@@ -21,7 +21,7 @@ import modalStyles from "./Modal.module.css";
21
21
  * @see https://shelving.cc/ui/Dialog
22
22
  */
23
23
  export const Dialog = memo(({ children, onClose, ...props }) => {
24
- return (_jsx(Suspense, { fallback: null, children: _jsx(ViewTransition, { default: "none", enter: "fade", exit: "fade", children: _jsxs("dialog", { ref: _showModal, className: getModuleClass(styles, "dialog"), onClick: _closeOnClick, onCancel: e => {
24
+ return (_jsx(Suspense, { fallback: null, children: _jsx(ViewTransition, { default: "none", enter: FADE_TRANSITION_CLASS, exit: FADE_TRANSITION_CLASS, children: _jsxs("dialog", { ref: _showModal, className: getModuleClass(styles, "dialog"), onClick: _closeOnClick, onCancel: e => {
25
25
  // Keep the dialog open until the parent unmounts it. An uncancelable request closes natively and fires `onClose` from the `close` event.
26
26
  if (!onClose || !e.cancelable)
27
27
  return;
@@ -2,9 +2,9 @@ import { type MouseEvent, memo, type ReactElement, Suspense, ViewTransition } fr
2
2
  import { CloseIcon } from "shelving/icon/CloseIcon";
3
3
  import type { Callback } from "../../util/function.js";
4
4
  import { type ButtonVariants, getButtonClass } from "../button/Button.js";
5
+ import { FADE_TRANSITION_CLASS } from "../transition/FadeTransition.js";
5
6
  import { getClass, getModuleClass } from "../util/css.js";
6
7
  import type { ClassProps, OptionalChildProps } from "../util/props.js";
7
- import "../transition/FadeTransition.css";
8
8
  import styles from "./Dialog.module.css";
9
9
  import modalStyles from "./Modal.module.css";
10
10
 
@@ -36,7 +36,7 @@ export const Dialog = memo(({ children, onClose, ...props }: DialogProps) => {
36
36
  return (
37
37
  <Suspense fallback={null}>
38
38
  {/* The transition must wrap the `<dialog>`: React only animates a `<ViewTransition>` that comes before any DOM element in the inserted or deleted tree. Fade only on enter and exit, so an open dialog stays still while another dialog opens or closes. No update transition: the page ignores taps while a view transition runs, so a change inside an open dialog must not start one. */}
39
- <ViewTransition default="none" enter="fade" exit="fade">
39
+ <ViewTransition default="none" enter={FADE_TRANSITION_CLASS} exit={FADE_TRANSITION_CLASS}>
40
40
  {/** biome-ignore lint/a11y/useKeyWithClickEvents: Dialogs also show a close button. */}
41
41
  <dialog
42
42
  ref={_showModal}
@@ -44,7 +44,7 @@
44
44
 
45
45
  /*
46
46
  * A new notice slides in from the side of its corner (or from the edge for a centred list), and a closed notice slides out the same way.
47
- * - `:only-child` stops Safari from also starting the animation on the missing image, which stops it later on the real one (see `FadeTransition.css`).
47
+ * - `:only-child` stops Safari from also starting the animation on the missing image, which stops it later on the real one (see `FadeTransition.module.css`).
48
48
  * - Longhands, not the `animation` shorthand: Bun rewrites `animation-name` to the hashed keyframes name but leaves a shorthand that holds a `var()` untouched.
49
49
  */
50
50
  ::view-transition-new(.notice-left):only-child {
package/ui/page/Head.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { type ReactElement } from "react";
2
2
  /**
3
- * Emit the current page's head metadata and sync browser history to its URL.
3
+ * Emit the current page's head metadata, and replace the browser URL with the page's canonical URL.
4
4
  * - Emits hoistable head elements (title, meta, links, stylesheets, scripts) inline; React 19 hoists each one into the document `<head>`.
5
5
  * - Does not render `<base>` (not hoistable — that lives in `<Head>` in the `<HTML>` shell component).
6
- * - Updates `window.history` to match the page URL.
6
+ * - When the meta `url` differs from the browser URL (a page that knows its canonical URL), calls `NavigationStore.replace()` with it. Outside `<Navigation>` it does not touch history.
7
7
  *
8
8
  * @kind component
9
9
  * @see https://shelving.cc/ui/Head
package/ui/page/Head.js CHANGED
@@ -1,15 +1,16 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useEffect } from "react";
2
+ import { use, useEffect } from "react";
3
3
  import { getProps } from "../../util/object.js";
4
4
  import { requireMeta } from "../misc/MetaContext.js";
5
+ import { NavigationContext } from "../router/NavigationContext.js";
5
6
  import { joinTitles } from "../util/meta.js";
6
7
  /** Meta tags with a capital first letter and hyphens, e.g. `Content-Security-Policy` or `Accept`, are `http-equiv=""` tags. */
7
8
  const R_HTTP_EQUIV = /^[A-Z][a-zA-Z0-9]*(-[A-Z][a-zA-Z0-9]*)*$/;
8
9
  /**
9
- * Emit the current page's head metadata and sync browser history to its URL.
10
+ * Emit the current page's head metadata, and replace the browser URL with the page's canonical URL.
10
11
  * - Emits hoistable head elements (title, meta, links, stylesheets, scripts) inline; React 19 hoists each one into the document `<head>`.
11
12
  * - Does not render `<base>` (not hoistable — that lives in `<Head>` in the `<HTML>` shell component).
12
- * - Updates `window.history` to match the page URL.
13
+ * - When the meta `url` differs from the browser URL (a page that knows its canonical URL), calls `NavigationStore.replace()` with it. Outside `<Navigation>` it does not touch history.
13
14
  *
14
15
  * @kind component
15
16
  * @see https://shelving.cc/ui/Head
@@ -17,12 +18,12 @@ const R_HTTP_EQUIV = /^[A-Z][a-zA-Z0-9]*(-[A-Z][a-zA-Z0-9]*)*$/;
17
18
  export function Head() {
18
19
  const meta = requireMeta();
19
20
  const { url, title, app, description, links, tags, stylesheets, modules, scripts } = meta;
21
+ // A page can set its canonical URL in meta. Replace the browser URL with it, through the navigation store so the history entry keeps its stack.
22
+ const nav = use(NavigationContext);
20
23
  useEffect(() => {
21
- if (typeof window === "undefined")
22
- return;
23
- if (url)
24
- window.history.replaceState(null, "", url);
25
- }, [url]);
24
+ if (nav && url && url.href !== nav.value.href)
25
+ nav.replace(url);
26
+ }, [nav, url]);
26
27
  const fullTitle = joinTitles(title, app);
27
28
  return (_jsxs(_Fragment, { children: [fullTitle && _jsx("title", { children: fullTitle }), description && _jsx("meta", { name: "description", content: description }), tags && getProps(tags).map(_renderTag), links && getProps(links).map(_renderLink), stylesheets?.map(_renderStylesheet), modules?.map(_renderModule), scripts?.map(_renderScript)] }));
28
29
  }
package/ui/page/Head.tsx CHANGED
@@ -1,17 +1,18 @@
1
- import { type ReactElement, useEffect } from "react";
1
+ import { type ReactElement, use, useEffect } from "react";
2
2
  import type { ArrayItem } from "../../util/array.js";
3
3
  import { getProps, type Prop } from "../../util/object.js";
4
4
  import { requireMeta } from "../misc/MetaContext.js";
5
+ import { NavigationContext } from "../router/NavigationContext.js";
5
6
  import { joinTitles, type MetaAssets, type MetaLinks, type MetaTags } from "../util/meta.js";
6
7
 
7
8
  /** Meta tags with a capital first letter and hyphens, e.g. `Content-Security-Policy` or `Accept`, are `http-equiv=""` tags. */
8
9
  const R_HTTP_EQUIV = /^[A-Z][a-zA-Z0-9]*(-[A-Z][a-zA-Z0-9]*)*$/;
9
10
 
10
11
  /**
11
- * Emit the current page's head metadata and sync browser history to its URL.
12
+ * Emit the current page's head metadata, and replace the browser URL with the page's canonical URL.
12
13
  * - Emits hoistable head elements (title, meta, links, stylesheets, scripts) inline; React 19 hoists each one into the document `<head>`.
13
14
  * - Does not render `<base>` (not hoistable — that lives in `<Head>` in the `<HTML>` shell component).
14
- * - Updates `window.history` to match the page URL.
15
+ * - When the meta `url` differs from the browser URL (a page that knows its canonical URL), calls `NavigationStore.replace()` with it. Outside `<Navigation>` it does not touch history.
15
16
  *
16
17
  * @kind component
17
18
  * @see https://shelving.cc/ui/Head
@@ -20,10 +21,11 @@ export function Head(): ReactElement {
20
21
  const meta = requireMeta();
21
22
  const { url, title, app, description, links, tags, stylesheets, modules, scripts } = meta;
22
23
 
24
+ // A page can set its canonical URL in meta. Replace the browser URL with it, through the navigation store so the history entry keeps its stack.
25
+ const nav = use(NavigationContext);
23
26
  useEffect(() => {
24
- if (typeof window === "undefined") return;
25
- if (url) window.history.replaceState(null, "", url);
26
- }, [url]);
27
+ if (nav && url && url.href !== nav.value.href) nav.replace(url);
28
+ }, [nav, url]);
27
29
 
28
30
  const fullTitle = joinTitles(title, app);
29
31
  return (
@@ -0,0 +1,22 @@
1
+ import { type ReactElement } from "react";
2
+ import type { AbsolutePath } from "../../util/path.js";
3
+ import { type ButtonProps } from "../button/Button.js";
4
+ /**
5
+ * Props for `<BackButton>`: the `href` to go back to when the stack has no page before this one, plus the usual `Button` props except `onClick`.
6
+ *
7
+ * @see https://shelving.cc/ui/BackButtonProps
8
+ */
9
+ export interface BackButtonProps extends Omit<ButtonProps, "href" | "onClick"> {
10
+ /** Where to go back to when the current stack has no page before this one (for example, the app was opened at this page). */
11
+ readonly href: AbsolutePath;
12
+ }
13
+ /**
14
+ * Back button for the current stack.
15
+ * - Goes back to the page before this one in the current stack, wherever you came from.
16
+ * - Goes to `href` when the stack has no page before this one: the app was opened at this page, or the browser has no Navigation API.
17
+ * - It is a plain link with `className="back"`, so it slides back, and it works in a server render and with a middle-click.
18
+ *
19
+ * @kind component
20
+ * @see https://shelving.cc/ui/BackButton
21
+ */
22
+ export declare function BackButton({ href, className, title, children, ...props }: BackButtonProps): ReactElement;
@@ -0,0 +1,19 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { use } from "react";
3
+ import { LeftIcon } from "../../icon/LeftIcon.js";
4
+ import { Button } from "../button/Button.js";
5
+ import { getClass } from "../util/css.js";
6
+ import { NavigationPositionContext } from "./NavigationContext.js";
7
+ /**
8
+ * Back button for the current stack.
9
+ * - Goes back to the page before this one in the current stack, wherever you came from.
10
+ * - Goes to `href` when the stack has no page before this one: the app was opened at this page, or the browser has no Navigation API.
11
+ * - It is a plain link with `className="back"`, so it slides back, and it works in a server render and with a middle-click.
12
+ *
13
+ * @kind component
14
+ * @see https://shelving.cc/ui/BackButton
15
+ */
16
+ export function BackButton({ href, className, title = "Back", children = _jsx(LeftIcon, {}), ...props }) {
17
+ const position = use(NavigationPositionContext);
18
+ return (_jsx(Button, { ...props, title: title, href: position?.parent ?? href, className: getClass("back", className), children: children }));
19
+ }
@@ -0,0 +1,47 @@
1
+ # BackButton
2
+
3
+ The back button for a page header. It goes back to the page before this one in the current stack, wherever you came from.
4
+
5
+ **Things to know:**
6
+
7
+ - It goes to the page before this one in the current stack. If you opened a post from a search, it goes back to the search.
8
+ - It goes to `href` when the stack has no page before this one: for example, when the app was opened at this page. So a back button always has somewhere to go. Set `href` to the page's natural parent. On an unstacked page (for example a deep link), it goes to `href` and stays unstacked. To go into a stack, the user taps a `<StackButton>`.
9
+ - It is a plain link with `className="back"`. `<Navigation>` turns a click into `NavigationStore.back()`, so the page slides back. A middle-click opens the link in a new browser tab.
10
+ - Its default content is a `<LeftIcon>` and its default `title` is "Back". Pass `children` and `title` to change them.
11
+ - It takes the usual `<Button>` props, except `onClick`.
12
+ - Without the browser Navigation API, and in a server render, it links to `href`.
13
+ - In a server-rendered app the server always renders `href`, and the browser may render the page before this one. React then warns about a hydration mismatch on the first render. A client-only app (with `<App>`) has no mismatch.
14
+
15
+ ## Usage
16
+
17
+ ```tsx
18
+ import { BackButton } from "shelving/ui";
19
+
20
+ function PostPage({ id }: { id: string }) {
21
+ return (
22
+ <>
23
+ <header>
24
+ <BackButton href="/home"/>
25
+ <h1>Post</h1>
26
+ </header>
27
+ {/* … */}
28
+ </>
29
+ );
30
+ }
31
+ ```
32
+
33
+ With a label:
34
+
35
+ ```tsx
36
+ <BackButton href="/profile">Back to profile</BackButton>
37
+ ```
38
+
39
+ To go back to one fixed page (not to the page before), use a link with `className="back"`:
40
+
41
+ ```tsx
42
+ <Button href="/profile" className="back">Profile</Button>
43
+ ```
44
+
45
+ ## Styling
46
+
47
+ `<BackButton>` renders a `<Button>`, so it has the same styling hooks. See `<Button>`.
@@ -0,0 +1,34 @@
1
+ import { type ReactElement, use } from "react";
2
+ import { LeftIcon } from "../../icon/LeftIcon.js";
3
+ import type { AbsolutePath } from "../../util/path.js";
4
+ import { Button, type ButtonProps } from "../button/Button.js";
5
+ import { getClass } from "../util/css.js";
6
+ import { NavigationPositionContext } from "./NavigationContext.js";
7
+
8
+ /**
9
+ * Props for `<BackButton>`: the `href` to go back to when the stack has no page before this one, plus the usual `Button` props except `onClick`.
10
+ *
11
+ * @see https://shelving.cc/ui/BackButtonProps
12
+ */
13
+ export interface BackButtonProps extends Omit<ButtonProps, "href" | "onClick"> {
14
+ /** Where to go back to when the current stack has no page before this one (for example, the app was opened at this page). */
15
+ readonly href: AbsolutePath;
16
+ }
17
+
18
+ /**
19
+ * Back button for the current stack.
20
+ * - Goes back to the page before this one in the current stack, wherever you came from.
21
+ * - Goes to `href` when the stack has no page before this one: the app was opened at this page, or the browser has no Navigation API.
22
+ * - It is a plain link with `className="back"`, so it slides back, and it works in a server render and with a middle-click.
23
+ *
24
+ * @kind component
25
+ * @see https://shelving.cc/ui/BackButton
26
+ */
27
+ export function BackButton({ href, className, title = "Back", children = <LeftIcon />, ...props }: BackButtonProps): ReactElement {
28
+ const position = use(NavigationPositionContext);
29
+ return (
30
+ <Button {...props} title={title} href={position?.parent ?? href} className={getClass("back", className)}>
31
+ {children}
32
+ </Button>
33
+ );
34
+ }
@@ -1,27 +1,37 @@
1
1
  import { type ReactElement } from "react";
2
+ import type { AbsolutePath } from "../../util/path.js";
2
3
  import { type PossibleMeta } from "../util/index.js";
3
4
  import type { OptionalChildProps } from "../util/props.js";
4
5
  /**
5
- * Props for `<Navigation>` — initial `Meta` (url/base) plus optional `children`.
6
+ * Props for `<Navigation>`: the initial `Meta` (url, root and stack), the `stacks`, and optional `children`.
6
7
  *
7
8
  * @see https://shelving.cc/ui/NavigationProps
8
9
  */
9
10
  export interface NavigationProps extends PossibleMeta, OptionalChildProps {
11
+ /**
12
+ * Root path of each stack, in tab order, for an app with a tab bar (for example `["/home", "/discover", "/profile"]`).
13
+ * - Each stack keeps its own pages, so each tab remembers where you left it, and a back button goes back within its tab.
14
+ * - With no stacks, the app has one stack at `"/"`, and `stack` in `Meta` is `"/"`.
15
+ */
16
+ readonly stacks?: readonly AbsolutePath[] | undefined;
17
+ /**
18
+ * Stack of the page the app opens at (for example `"/home"`), like `url` and `root`.
19
+ * - Read from this prop, else from the surrounding `<Meta>`, else `"/"`.
20
+ * - Used only when the opening history entry has no saved stack. A reload keeps the stack it had.
21
+ */
22
+ readonly stack?: AbsolutePath | undefined;
10
23
  }
11
24
  /**
12
- * Top-level navigation provider.
13
- * - Owns a single `NavigationStore` initialised from the surrounding `<Meta>` url/base.
14
- * - Intercepts same-origin anchor clicks (excluding `download` anchors) and turns them into `forward()` calls, or `back()` calls for an anchor with a `data-back` attribute.
15
- * - Listens for `popstate` to sync the store with browser back/forward.
16
- * - Publishes the live URL into the `Meta` context via `mergeMeta()`, so descendant `<Router>`s re-render on navigation and merge invariants hold (e.g. `root` defaults to the live URL's origin when unset).
17
- * - Publishes each URL change inside `startTransition()` with a `"forward"` or `"back"` transition type, so a `<Transition>` around the routes runs a view transition.
18
- * - Skips the transition when the browser already animated the change (`PopStateEvent.hasUAVisualTransition`, e.g. a swipe-back gesture).
25
+ * Top-level navigation provider, with one stack of pages for each tab.
26
+ * - Owns a single `NavigationStore` made from the surrounding `<Meta>` url and root.
27
+ * - Turns clicks on same-origin links into `NavigationStore.forward()`, or `NavigationStore.back()` for a link with `className="back"`, or `NavigationStore.replace()` for a link with `className="replace"`. Clicks with a modifier key, or on links with `download` or a `target`, are left to the browser.
28
+ * - Publishes the URL and the current `stack` into `Meta`, and the position in the stacks into `NavigationPositionContext`, all in one render.
29
+ * - Publishes each change inside `startTransition()` with a `"forward"` or `"back"` transition type, so a `<Transition>` around the routes runs a view transition. A replace, or a change the browser already animated (for example a swipe-back), has no transition.
30
+ * - Uses the browser Navigation API. Without it (older browsers, server renders) there are no stacks and no transitions.
19
31
  *
20
- * Exactly one `<Navigation>` per app — nested routers share this single store.
21
- *
22
- * TODO: switch click/popstate handling to the browser Navigation API when broadly supported.
32
+ * Exactly one `<Navigation>` per app. Nested routers share its store.
23
33
  *
24
34
  * @kind component
25
35
  * @see https://shelving.cc/ui/Navigation
26
36
  */
27
- export declare function Navigation({ children, ...meta }: NavigationProps): ReactElement;
37
+ export declare function Navigation({ children, stacks, ...meta }: NavigationProps): ReactElement;
@@ -4,73 +4,89 @@ import { useInstance } from "../../react/useInstance.js";
4
4
  import { MetaContext, requireMeta } from "../misc/MetaContext.js";
5
5
  import { setTransitionType } from "../transition/util.js";
6
6
  import { mergeMeta } from "../util/index.js";
7
- import { NavigationContext } from "./NavigationContext.js";
7
+ import { NavigationContext, NavigationPositionContext } from "./NavigationContext.js";
8
8
  import { NavigationStore } from "./NavigationStore.js";
9
9
  /**
10
- * Top-level navigation provider.
11
- * - Owns a single `NavigationStore` initialised from the surrounding `<Meta>` url/base.
12
- * - Intercepts same-origin anchor clicks (excluding `download` anchors) and turns them into `forward()` calls, or `back()` calls for an anchor with a `data-back` attribute.
13
- * - Listens for `popstate` to sync the store with browser back/forward.
14
- * - Publishes the live URL into the `Meta` context via `mergeMeta()`, so descendant `<Router>`s re-render on navigation and merge invariants hold (e.g. `root` defaults to the live URL's origin when unset).
15
- * - Publishes each URL change inside `startTransition()` with a `"forward"` or `"back"` transition type, so a `<Transition>` around the routes runs a view transition.
16
- * - Skips the transition when the browser already animated the change (`PopStateEvent.hasUAVisualTransition`, e.g. a swipe-back gesture).
10
+ * Top-level navigation provider, with one stack of pages for each tab.
11
+ * - Owns a single `NavigationStore` made from the surrounding `<Meta>` url and root.
12
+ * - Turns clicks on same-origin links into `NavigationStore.forward()`, or `NavigationStore.back()` for a link with `className="back"`, or `NavigationStore.replace()` for a link with `className="replace"`. Clicks with a modifier key, or on links with `download` or a `target`, are left to the browser.
13
+ * - Publishes the URL and the current `stack` into `Meta`, and the position in the stacks into `NavigationPositionContext`, all in one render.
14
+ * - Publishes each change inside `startTransition()` with a `"forward"` or `"back"` transition type, so a `<Transition>` around the routes runs a view transition. A replace, or a change the browser already animated (for example a swipe-back), has no transition.
15
+ * - Uses the browser Navigation API. Without it (older browsers, server renders) there are no stacks and no transitions.
17
16
  *
18
- * Exactly one `<Navigation>` per app — nested routers share this single store.
19
- *
20
- * TODO: switch click/popstate handling to the browser Navigation API when broadly supported.
17
+ * Exactly one `<Navigation>` per app. Nested routers share its store.
21
18
  *
22
19
  * @kind component
23
20
  * @see https://shelving.cc/ui/Navigation
24
21
  */
25
- export function Navigation({ children, ...meta }) {
22
+ export function Navigation({ children, stacks = [], ...meta }) {
26
23
  const current = requireMeta(meta);
27
- const nav = useInstance(NavigationStore, current.url, current.root);
24
+ const nav = useInstance(NavigationStore, current.url, current.root, current.stack ?? "/", ...stacks);
28
25
  // React runs a `<ViewTransition>` only for a transition update, and `useSyncExternalStore()` always renders a sync update.
29
- // So keep the published URL in state and copy each store change into it inside `startTransition()`.
30
- const [url, setURL] = useState(nav.value);
26
+ // So keep the published values in state, and copy each store change into it inside `startTransition()`.
27
+ const [published, setPublished] = useState(() => _getPublished(nav));
31
28
  useEffect(() => {
32
- if (typeof document === "undefined" || typeof window === "undefined")
33
- return;
34
- // Read the transition type the store set for this change, then reset it.
35
- const stop = nav.subscribe(value => {
36
- const t = nav.transition;
37
- nav.transition = "forward";
38
- if (!t)
39
- return setURL(value);
40
- // React renders a transition that starts inside a `popstate` event as a sync update with no view transition, so leave the event first.
41
- setTimeout(() => startTransition(() => {
42
- setTransitionType(t);
43
- setURL(value);
44
- }));
45
- });
46
- const onClick = (e) => {
47
- if (e.target instanceof Element) {
48
- const anchor = e.target.closest("a") ||
49
- (!e.target.closest("button, label") && e.target.closest(".targeted")?.querySelector("a.target[href]"));
50
- if (anchor instanceof HTMLAnchorElement && anchor.origin === window.location.origin && !anchor.hasAttribute("download")) {
51
- e.preventDefault();
52
- if (anchor.hasAttribute("data-back"))
53
- nav.back(anchor.href);
54
- else
55
- nav.forward(anchor.href);
56
- return false; // `return false` stops iOS web app opening every link in a new window.
57
- }
29
+ const stopListening = nav.listen();
30
+ // Publish the latest values once for each change, even when the URL and the stack both change.
31
+ let last = nav.value.href;
32
+ let lastStack = nav.stack.value;
33
+ let pending = false;
34
+ const publish = () => {
35
+ const { value, transition } = nav;
36
+ if (value.href === last && nav.stack.value === lastStack)
37
+ return;
38
+ last = value.href;
39
+ lastStack = nav.stack.value;
40
+ if (pending)
41
+ return;
42
+ pending = true;
43
+ if (!transition) {
44
+ pending = false;
45
+ return setPublished(_getPublished(nav));
58
46
  }
47
+ // React renders a transition that starts inside a browser history event as a sync update with no view transition, so leave the event first.
48
+ setTimeout(() => {
49
+ pending = false;
50
+ startTransition(() => {
51
+ setTransitionType(transition);
52
+ setPublished(_getPublished(nav));
53
+ });
54
+ });
59
55
  };
60
- const onPopState = (e) => {
61
- const href = window.location.href;
62
- // `"back"` for a `popstate`. `null` means no transition, because the browser already animated the change (e.g. a swipe-back gesture).
63
- if (href !== nav.value.href)
64
- nav.transition = e.hasUAVisualTransition ? null : "back";
65
- nav.value = href;
56
+ const stopURL = nav.subscribe(publish);
57
+ const stopStack = nav.stack.subscribe(publish);
58
+ // The first render may have run before the store saw the current history entry.
59
+ setPublished(p => (p.url.href === nav.value.href && p.stack === nav.stack.value ? p : _getPublished(nav)));
60
+ const onClick = (e) => {
61
+ if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || !(e.target instanceof Element))
62
+ return;
63
+ const anchor = e.target.closest("a") ||
64
+ (!e.target.closest("button, label") && e.target.closest(".targeted")?.querySelector("a.target[href]"));
65
+ if (!(anchor instanceof HTMLAnchorElement) || anchor.origin !== window.location.origin)
66
+ return;
67
+ if (anchor.hasAttribute("download") || (anchor.target && anchor.target !== "_self"))
68
+ return;
69
+ e.preventDefault();
70
+ const { classList, href } = anchor;
71
+ if (classList.contains("back"))
72
+ nav.back(href);
73
+ else if (classList.contains("replace"))
74
+ nav.replace(href);
75
+ else
76
+ nav.forward(href);
77
+ return false; // `return false` stops iOS web app opening every link in a new window.
66
78
  };
67
79
  document.addEventListener("click", onClick);
68
- window.addEventListener("popstate", onPopState);
69
80
  return () => {
70
81
  document.removeEventListener("click", onClick);
71
- window.removeEventListener("popstate", onPopState);
72
- stop();
82
+ stopURL();
83
+ stopStack();
84
+ stopListening();
73
85
  };
74
86
  }, [nav]);
75
- return (_jsx(NavigationContext, { value: nav, children: _jsx(MetaContext, { value: mergeMeta(current, { url }, Navigation), children: children }) }));
87
+ const { url, stack, position } = published;
88
+ return (_jsx(NavigationContext, { value: nav, children: _jsx(NavigationPositionContext, { value: position, children: _jsx(MetaContext, { value: mergeMeta(current, { url, stack }, Navigation), children: children }) }) }));
89
+ }
90
+ function _getPublished(nav) {
91
+ return { url: nav.value, stack: nav.stack.value, position: nav.position };
76
92
  }