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
@@ -4,46 +4,120 @@
4
4
  - Import: `import { NavigationStore } from "shelving/ui";`
5
5
  - Extends: `URLStore`
6
6
 
7
- Store holding the current navigation URL and driving browser history.
7
+ Store that holds the current navigation URL, drives browser history, and keeps a stack of pages for each tab.
8
8
 
9
- - Extends `URLStore`; the current location is its `value`.
10
- - `forward()` pushes a new history entry; `redirect()` replaces the current one.
11
- - `back()` pushes a new history entry like `forward()`, but `<Navigation>` animates it as a back transition.
12
- - TODO: switch to the browser Navigation API when broadly supported.
9
+ - Extends `URLStore`, so its `value` is the current URL.
10
+ - With the browser Navigation API, each history entry saves a `NavigationEntryState`, and each stack is read from `navigation.entries()` when it is needed. Nothing else is kept.
11
+ - `stack` is a sub-store that holds the root of the current stack. It only changes when the stack changes.
12
+ - Without the Navigation API (older browsers and server renders) there are no stacks: `stack` stays the starting stack, `history()` is empty, and `stack` arguments are ignored.
13
+ - Owned by `<Navigation>`. Read it with `requireNavigation()`.
13
14
 
14
- The store holding the current navigation URL and driving browser history. It extends `URLStore` — the current location is its `value` — and is owned by `<Navigation>`. Reach for it via `requireNavigation()` rather than constructing your own.
15
+ The store that holds the current URL, drives browser history, and keeps one stack of pages for each tab. It extends `URLStore`, so its `value` is the current URL. `<Navigation>` owns it. Read it with `requireNavigation()`, and do not make your own.
15
16
 
16
17
  **Things to know:**
17
18
 
18
- - `forward()` pushes a new browser history entry; `redirect()` replaces the current one. Both resolve the destination against the store's `base`.
19
- - `back()` pushes a new browser history entry like `forward()`, but `<Navigation>` gives it the `"back"` transition type, so the page slides back. It does not go back in the browser history, so it works when the page was opened directly.
20
- - It is a `shelving/store` `URLStore`, so components can subscribe to it for re-renders.
19
+ - `NavigationStore.stacks` is the root path of each stack, in tab order, from the `stacks` prop on `<Navigation>`. With no stacks the app has one stack at `/`.
20
+ - A page that is not in a listed stack is **unstacked**: it is in the `/` stack. See [Unstacked pages](#unstacked-pages).
21
+ - `NavigationStore.stack` is a sub-store that holds the root of the current stack. It is always set: with no stacks it is `"/"`. It changes only when the stack changes, so `useStore(nav.stack)` re-renders only on a tab change.
22
+ - `NavigationStore.history()` returns the pages in the current stack (or a given stack), root first.
23
+ - `NavigationStore.position` is the current stack and depth, the depth of the top page of each stack, and the page before this one. `<RouteCache>` and `<BackButton>` use it.
24
+ - All URLs resolve against the store's `base`. Each method also takes a full same-origin URL.
25
+ - Without the browser Navigation API there is no stack history: `stack` stays the starting stack, `history()` returns `[]`, `position` is `undefined`, and every `stack` argument is ignored. `forward()` and `back()` push a history entry, and `replace()` replaces it.
26
+
27
+ ## Methods
28
+
29
+ | Method | What it does | Slide |
30
+ |---|---|---|
31
+ | `forward(href, stack?)` | Opens `href` as a new page on top of the stack. If `href` is the top page of another stack, it switches to that stack (a tab switch). If `href` is the current URL, it is a replace. | Forward. Across stacks, by tab order. |
32
+ | `back(href, stack?)` | Goes back to `href`. If `href` is in the stack, the stack goes back to it, and the pages above it leave the stack. If `href` is the root of the stack, the stack goes to its root. Otherwise `href` takes the place of the top page of the stack. | Back |
33
+ | `replace(href, stack?)` | Changes the URL of the current page. The stack, the depth and the mounted page stay the same. No new history entry. With another `stack`, the page moves into that stack: at its root if `href` is the root, else on top of its pages. | None |
34
+ | `history(stack?)` | Returns the pages in `stack`, root first. | — |
35
+
36
+ `stack` defaults to the current stack.
37
+
38
+ ## Unstacked pages
39
+
40
+ A page changes stack only when you say so: with a `<StackButton>`, or with a `stack` argument to `forward()`, `back()` or `replace()`. Links never change stack. The page the app opens at starts in the `stack` set on `<Navigation>` (or the surrounding `<Meta>`), else in the `/` stack. A page in the `/` stack is **unstacked**. For example: a deep link into the app, a fresh open of `/post/9`, or a login page.
41
+
42
+ - On an unstacked page, no `<StackButton>` is selected.
43
+ - Links from an unstacked page push deeper in the `/` stack, even a link to a stack root.
44
+ - To leave the `/` stack, tap a `<StackButton>`, or name the stack.
45
+ - To select a tab when the app starts, set `stack` on `<Navigation>`. The app decides which stack the opening URL belongs to. After a reload, each history entry keeps the stack it had.
46
+ - If you list `/` as one of the `stacks`, it is a normal tab, and unstacked pages open in it.
47
+
48
+ For example, a login page sends the user home after they log in, and names the Home stack:
49
+
50
+ ```tsx
51
+ const nav = requireNavigation();
52
+ nav.replace("/home", "/home");
53
+ ```
54
+
55
+ ## How stacks work
56
+
57
+ The store saves a small state on each browser history entry: the `stack` the page is in, and its `depth` in that stack (`0` is the root). See `NavigationEntryState`. To read a stack, the store walks back through the browser history from the current entry. It takes the newest entry in that stack, then the newest earlier entry with a lower depth, and so on down to depth `0`.
58
+
59
+ Why depth? A browser history entry cannot be changed or removed. So when you go back in one stack while another stack's pages are after it in the history, the store cannot go back in the browser. It pushes a new entry for the page you went back to, at that page's depth. The pages that were above it now have a higher depth than the newest entry, so they are no longer in the stack.
60
+
61
+ For example, with tabs `/home` and `/profile`:
62
+
63
+ | Step | New history entry | Home stack | Profile stack |
64
+ |---|---|---|---|
65
+ | Start at `/home` | `/home` (depth 0) | `/home` | — |
66
+ | Open a post | `/home/post/1` (depth 1) | `/home`, `/home/post/1` | — |
67
+ | Tap the Profile tab | `/profile` (depth 0) | `/home`, `/home/post/1` | `/profile` |
68
+ | Tap the Home tab | `/home/post/1` (depth 1) | `/home`, `/home/post/1` | `/profile` |
69
+ | Tap the back button | `/home` (depth 0) | `/home` | `/profile` |
70
+
71
+ When the page you go back to is the entry just before the current one, `back()` goes back in the browser history and pushes nothing.
72
+
73
+ The browser back and forward buttons walk the history entries one at a time, as usual. Each entry knows its stack and depth, so the stacks are always right and the slide goes the right way.
21
74
 
22
75
  ## Usage
23
76
 
24
77
  ```tsx
25
- import { requireNavigation } from "shelving/ui";
78
+ import { Button, requireNavigation } from "shelving/ui";
26
79
 
27
80
  function LogoutButton() {
28
81
  const nav = requireNavigation();
29
- return <Button onClick={() => nav.redirect("/login")}>Log out</Button>;
82
+ return <Button onClick={() => nav.replace("/login")}>Log out</Button>;
30
83
  }
31
84
  ```
32
85
 
33
- Constructed directly only when wiring navigation by hand (e.g. tests):
86
+ Read the current stack in a component:
87
+
88
+ ```tsx
89
+ import { useStore } from "shelving/react";
90
+ import { requireNavigation } from "shelving/ui";
91
+
92
+ function TabTitle() {
93
+ const nav = requireNavigation();
94
+ const stack = useStore(nav.stack).value;
95
+ return <h1>{stack === "/profile" ? "Profile" : "Home"}</h1>;
96
+ }
97
+ ```
98
+
99
+ List the pages in a stack (for example, for breadcrumbs):
100
+
101
+ ```tsx
102
+ const nav = requireNavigation();
103
+ nav.history("/home"); // ["/home", "/home/post/1", "/home/post/1/comments"]
104
+ ```
105
+
106
+ Make a store by hand only in tests:
34
107
 
35
108
  ```tsx
36
109
  import { NavigationStore } from "shelving/ui";
37
110
 
38
- const nav = new NavigationStore("/");
39
- nav.forward("/home");
111
+ // URL, root, starting stack, then the stacks in tab order.
112
+ const nav = new NavigationStore("https://example.com/home", "https://example.com/", "/home", "/home", "/profile");
113
+ nav.forward("/home/post/1");
40
114
  ```
41
115
 
42
116
 
43
117
  ## Signatures
44
118
 
45
119
  ```ts
46
- new NavigationStore(url: PossibleURL = typeof window === "undefined" ? "/" : window.location.href, base?: PossibleURL)
120
+ new NavigationStore(url: PossibleURL = typeof window === "undefined" ? "/" : window.location.href, base?: PossibleURL, stack: AbsolutePath = "/", ...stacks: AbsolutePath[])
47
121
  ```
48
122
 
49
123
  ## Parameters
@@ -52,6 +126,8 @@ new NavigationStore(url: PossibleURL = typeof window === "undefined" ? "/" : win
52
126
  | --- | --- | --- |
53
127
  | `url` | `PossibleURL` | Defaults to `typeof window === "undefined" ? "/" : window.location.href`. |
54
128
  | `base` | `PossibleURL` | Optional. |
129
+ | `stack` | `AbsolutePath` | Defaults to `"/"`. |
130
+ | `stacks` | `AbsolutePath[]` | |
55
131
 
56
132
  ## Returns
57
133
 
@@ -63,10 +139,15 @@ new NavigationStore(url: PossibleURL = typeof window === "undefined" ? "/" : win
63
139
 
64
140
  | Property | Type | Description |
65
141
  | --- | --- | --- |
66
- | `transition` | `TransitionType \| null` | Transition type of the next URL change. `null` means no transition. `<Navigation>` reads it, then sets it back to `"forward"`. Defaults to `"forward"`. |
142
+ | `stacks` | `readonly AbsolutePath[]` | Root path of each stack, in tab order. A single-stack app has one root. Read only. |
143
+ | `stack` | `Store<AbsolutePath>` | Root path of the current stack. - Always set. With no `stacks`, it is `"/"`. - It starts at the `stack` given to the constructor (default `"/"`). Without the Navigation API, it stays there. Read only. |
144
+ | `transition` | `TransitionType \| null` | Transition type of the most recent change, read by `<Navigation>` when the value changes. - `null` means no transition: a replace, a change the browser already animated (for example a swipe-back), or no Navigation API. Defaults to `null`. |
145
+ | `position` | `NavigationPosition \| undefined` | Where the app is now: the current stack and depth, and the top depth of each stack. Read only. |
67
146
 
68
147
  ## Methods
69
148
 
70
- - [NavigationStore.forward()](NavigationStore/forward.md): Navigate forward to a URL, pushing a new browser history entry.
71
- - [NavigationStore.redirect()](NavigationStore/redirect.md): Redirect to a URL, replacing the current browser history entry.
72
- - [NavigationStore.back()](NavigationStore/back.md): Navigate back to a URL, pushing a new browser history entry with the "back" transition type.
149
+ - [NavigationStore.listen()](NavigationStore/listen.md): Start listening to the browser (back, forward and swipe).
150
+ - [NavigationStore.history()](NavigationStore/history.md): Get the pages in a stack, root first, or every page in every stack.
151
+ - [NavigationStore.forward()](NavigationStore/forward.md): Go forward to a URL, as a link does.
152
+ - [NavigationStore.back()](NavigationStore/back.md): Go back to a URL, as a back button does.
153
+ - [NavigationStore.replace()](NavigationStore/replace.md): Replace the current URL, with no transition.
@@ -3,50 +3,86 @@
3
3
  - Kind: component
4
4
  - Import: `import { RouteCache } from "shelving/ui";`
5
5
 
6
- Keep-alive page cache keyed by the current URL — drop it into a layout around its scrolling content region.
7
-
8
- - Reads the live URL from the surrounding `<Meta>` context and keeps up to `maxCached` recently-visited
9
- pages mounted but hidden (via React's `<Activity>`), so navigating back/forward to a page restores its
10
- entire DOM and component state — scroll position, toggles, searches, inputs, focus — untouched.
11
- - Pages are kept in a least-recently-used map keyed by `path`; the oldest is unmounted past the limit.
12
- - A page that owns other paths with `<RouteConfig path>` stays mounted when the URL changes to one of them.
13
- - A page with `<RouteConfig cache={false}>` is dropped as soon as you leave it, so the next visit shows a new page.
14
- - Each snapshot is frozen under its own `<MetaContext>`, so the same single `children` element resolves a
15
- different page per path and a hidden page never re-renders for someone else's URL.
16
- - `<Activity mode="hidden">` preserves a hidden page's state while unmounting its effects, so its
17
- subscriptions/observers/timers pause and resume cleanly as it is hidden and shown.
18
- - Because it wraps the scroll container itself (rather than sitting below it inside the router), the
19
- scroll position of every cached page is preserved — surrounding chrome (sidebar, drawer state) stays
20
- outside the cache, so it is neither duplicated nor remounted on navigation.
21
- - When `maxCached <= 0` the page is rendered directly with no caching (it unmounts as you leave it).
22
-
23
- A keep-alive page cache. Drop it into a layout around its scrolling content region: it reads the current URL from the surrounding `<Meta>` context and keeps a handful of recently-visited pages mounted but *hidden* (using React's [`<Activity>`](https://react.dev/reference/react/Activity)), so navigating back or forward to a page restores its entire DOM and component state — scroll position of every scroll container, open/closed toggles, in-progress searches, form inputs, focus — instead of remounting it fresh at the top.
24
-
25
- Shelving's `<SidebarLayout>` and `<CenteredLayout>` already wrap their scrollable content column in one for you, so pages rendered inside them keep their state across navigation automatically. Reach for it by hand only when building a custom layout.
6
+ Keep-alive page cache for the pages in the navigation stacks. Drop it into a layout around its scrolling content region.
7
+
8
+ - Keeps each page in the stacks mounted but hidden (with React's `<Activity>`), so going back to a page, or switching back to a tab, shows it exactly as you left it.
9
+ - Each page is keyed by its stack and its depth in that stack, not by its URL. So `NavigationStore.replace()` (a canonical URL, an in-page tab) keeps the same page mounted, and the same URL open in two tabs is two separate pages.
10
+ - A page leaves the cache as soon as it leaves its stack: when you go back from it, or when a stack is popped to its root. After a browser back, a browser forward shows that page fresh.
11
+ - The top page of each stack is never dropped. Other pages can be dropped past `maxCached`, and show fresh (from the top) when you come back to them.
12
+ - Each kept page is frozen under its own `<MetaContext>`, so the same `children` element renders a different page for each URL, and a hidden page never re-renders for another page's URL.
13
+ - `<Activity mode="hidden">` keeps a hidden page's state but unmounts its effects, so its subscriptions and timers pause and resume as it is hidden and shown.
14
+ - Without the browser Navigation API (older browsers, server renders), or with `maxCached` of `0`, it renders the current page directly with no cache.
15
+
16
+ A keep-alive cache for the pages in the navigation stacks. Put it in a layout around the scrolling content region. It keeps each page in the stacks mounted but hidden (with React's [`<Activity>`](https://react.dev/reference/react/Activity)), so when you go back to a page, or switch back to a tab, the page is exactly as you left it: scroll position, toggles, searches, form input and focus.
17
+
18
+ `<SidebarLayout>` and `<CenteredLayout>` already wrap their content in a `<RouteCache>`. Use it by hand only in a custom layout.
26
19
 
27
20
  **Things to know:**
28
21
 
29
- - Pages are kept in a least-recently-used map keyed by `path`. Once `maxCached` pages are retained the least-recently-visited one is unmounted (and loses its state). A never-seen or evicted page mounts fresh at the top.
30
- - A page can own other paths with `<RouteConfig path>`, for example its tabs (`/dave/{tab}`). A navigation to one of those paths keeps the same page mounted: it keeps its scroll and state, and a `<ViewTransition>` around the page does not slide.
31
- - A page with `<RouteConfig cache={false}>` is not kept: it unmounts as you leave it, so the next visit starts again. Use it for a form that makes or edits something, so it never shows old input.
32
- - The cache key is the path only, not the `?query`. So `/new?from=a` and `/new?from=b` are one page.
33
- - Pass `maxCached={0}` (or less) to disable caching entirely — the page renders directly and unmounts as soon as you leave it.
34
- - `<Activity mode="hidden">` preserves a hidden page's *state* while unmounting its *effects*, so subscriptions, observers (e.g. infinite-scroll), and timers pause and resume cleanly as the page is hidden and shown.
35
- - Each snapshot is frozen under its own `<Meta>` context, so the same single `children` element resolves a different page per path and a hidden page never re-renders for someone else's URL.
36
- - For per-page **scroll** to be preserved the cache has to wrap the scroll container itself — which is why it lives in the layout (around the scrollable column) rather than below it inside the router. Surrounding chrome (sidebar, drawer state) stays outside the cache, so it is neither duplicated nor remounted on navigation.
22
+ - Each page is keyed by its stack and its depth in that stack, not by its URL. See `NavigationStore` for how stacks work.
23
+ - A page leaves the cache when it leaves its stack: when you go back from it, or when its stack goes back to the root. So a page you went back from mounts fresh the next time you open it.
24
+ - After a browser back, the browser forward button shows the next page fresh (from the top), because that page left its stack when you went back.
25
+ - The top page of each stack is never dropped, so each tab always shows the page you left. Other pages are dropped (least recently used first) past `maxCached`, and mount fresh when you come back to them.
26
+ - `NavigationStore.replace()` and `className="replace"` links keep the same page mounted. A replace to another URL for the same component (for example from `/post/1` to `/post/2`) keeps its state. To reset the state, wrap the page content in an element with a `key`, for example `<Fragment key={id}>`.
27
+ - The same URL open in two stacks is two separate pages.
28
+ - A form that makes something stays in the cache with its input. Clear it after a successful submit, so it is empty the next time you see it (see the example below).
29
+ - Each kept page is frozen under its own `<Meta>`, so a hidden page never re-renders for another page's URL.
30
+ - `<Activity mode="hidden">` keeps a hidden page's state but unmounts its effects, so subscriptions, observers (for example infinite scroll) and timers pause and resume as the page is hidden and shown.
31
+ - The cache must wrap the scroll container itself to keep each page's scroll. So it goes in the layout around the scrolling column, not inside the router. Chrome outside the cache (a sidebar, a tab bar) is not duplicated or remounted.
32
+ - Put a page `<Transition>` around the `<RouteCache>`, not inside it. Safari (iOS 18) loses the exit animation when each page has its own `<ViewTransition>`.
33
+ - Without the browser Navigation API, or with `maxCached={0}`, there is no cache: the current page renders directly and unmounts when you leave it.
37
34
 
38
35
  ## Usage
39
36
 
40
37
  ```tsx
41
- import { RouteCache } from "shelving/ui";
38
+ import { HorizontalTransition, RouteCache, Router } from "shelving/ui";
39
+
40
+ function MyLayout() {
41
+ return (
42
+ <HorizontalTransition>
43
+ <RouteCache>
44
+ <main className="scroll">
45
+ <Router routes={ROUTES}/>
46
+ </main>
47
+ </RouteCache>
48
+ </HorizontalTransition>
49
+ );
50
+ }
51
+ ```
52
+
53
+ Keep fewer pages:
42
54
 
43
- <SidebarLayout sidebar={<Menu/>}>
44
- <RouteCache>
45
- <Router routes={routes}/>
46
- </RouteCache>
47
- </SidebarLayout>
55
+ ```tsx
56
+ <RouteCache maxCached={5}>
57
+ <Router routes={ROUTES}/>
58
+ </RouteCache>
48
59
  ```
49
60
 
61
+ Clear a create form after it saves. Change the `key` of the `<Form>`, so it mounts again with no input:
62
+
63
+ ```tsx
64
+ import { useState } from "react";
65
+ import { Form, requireNavigation } from "shelving/ui";
66
+
67
+ function NewPostPage() {
68
+ const nav = requireNavigation();
69
+ const [formKey, setFormKey] = useState(0);
70
+ return (
71
+ <Form
72
+ key={formKey}
73
+ schema={POST_SCHEMA}
74
+ onSubmit={async data => {
75
+ const id = await createPost(data);
76
+ setFormKey(k => k + 1);
77
+ nav.forward(`/post/${id}`);
78
+ }}
79
+ />
80
+ );
81
+ }
82
+ ```
83
+
84
+ For a plain HTML `<form>`, call `form.reset()` after the submit.
85
+
50
86
 
51
87
  ## Signatures
52
88
 
@@ -59,8 +95,8 @@ RouteCache({ maxCached = 10, children }: RouteCacheProps): ReactNode
59
95
  | Param | Type | Description |
60
96
  | --- | --- | --- |
61
97
  | `props` | `RouteCacheProps` | |
62
- | `.maxCached` | `number \| undefined` | Number of recently-visited URLs to keep mounted (but hidden) so the entire state of their subtree — scroll position of every scroll container, open/closed toggles, in-progress searches, form inputs, focus — is restored intact when navigating back or forward to them. - Defaults to `10`. Once the limit is reached the least-recently-visited entry is unmounted. - Set to `0` (or less) to disable caching and unmount the subtree as you leave each URL. Read only. Optional. |
63
- | `.children` | `ReactNode` | The content to render for the current URL and keep alive (hidden) for recently-visited URLs. Read only. |
98
+ | `.maxCached` | `number \| undefined` | Number of pages to keep mounted (but hidden), so the whole state of each one (scroll position, toggles, searches, form input, focus) is still there when you come back to it. - The top page of each stack is never dropped, even past the limit, so a tab always shows the page you left. - Set to `0` (or less) to turn the cache off and unmount each page as you leave it. Read only. Defaults to `10`. |
99
+ | `.children` | `ReactNode` | The content to render for the current page and keep alive (hidden) for the other pages in the stacks. Read only. |
64
100
 
65
101
  ## Returns
66
102
 
@@ -3,7 +3,7 @@
3
3
  - Kind: interface
4
4
  - Import: `import { RouteCacheProps } from "shelving/ui";`
5
5
 
6
- Props for `<RouteCache>` — the `maxCached` size and the content `children` to keep alive per URL.
6
+ Props for `<RouteCache>`: the `maxCached` size and the content `children` to keep alive for each page in the stacks.
7
7
 
8
8
  ## Signatures
9
9
 
@@ -18,5 +18,5 @@ Props for `<RouteCache>` — the `maxCached` size and the content `children` to
18
18
 
19
19
  | Property | Type | Description |
20
20
  | --- | --- | --- |
21
- | `maxCached` | `number \| undefined` | Number of recently-visited URLs to keep mounted (but hidden) so the entire state of their subtree — scroll position of every scroll container, open/closed toggles, in-progress searches, form inputs, focus — is restored intact when navigating back or forward to them. - Defaults to `10`. Once the limit is reached the least-recently-visited entry is unmounted. - Set to `0` (or less) to disable caching and unmount the subtree as you leave each URL. Read only. Optional. |
22
- | `children` | `ReactNode` | The content to render for the current URL and keep alive (hidden) for recently-visited URLs. Read only. |
21
+ | `maxCached` | `number \| undefined` | Number of pages to keep mounted (but hidden), so the whole state of each one (scroll position, toggles, searches, form input, focus) is still there when you come back to it. - The top page of each stack is never dropped, even past the limit, so a tab always shows the page you left. - Set to `0` (or less) to turn the cache off and unmount each page as you leave it. Read only. Defaults to `10`. |
22
+ | `children` | `ReactNode` | The content to render for the current page and keep alive (hidden) for the other pages in the stacks. Read only. |
package/docs/ui/Router.md CHANGED
@@ -8,7 +8,8 @@ Match the current URL against `routes` and render the matched element.
8
8
  - When `base` is set, the effective path is the URL after stripping the base prefix.
9
9
  - Nest by putting another `<Router>` inside a route's value; pass `base="/section"` (or wrap in `<Meta>`) to scope.
10
10
  - Route `{placeholders}` are passed as props to function/component route values along with merged URL `?query` params. They are not published into context — descendants of a `ReactElement`-valued route can't see them automatically.
11
- - Each path is a new page, so a component route remounts when the path changes. A page that owns other paths with `<RouteConfig path>` stays mounted when the path changes to one of them.
11
+ - Inside `<Navigation>` with the Navigation API, each page in a stack is a new page: a link mounts a new page, and `NavigationStore.replace()` keeps the same page mounted (it re-renders with the new URL). A replace that renders another component still mounts it fresh.
12
+ - Elsewhere, each path is a new page, so a component route remounts when the path changes.
12
13
  - Returns `null` when there's no URL in context or the URL is outside the base.
13
14
 
14
15
  A pure URL matcher: it reads the current URL from the surrounding `<Meta>` context, matches it against a `routes` table, and renders the matched element. It has no client requirements, so it works for SSR, static rendering, and tests with no `<Navigation>` at all — wrap it in `<Navigation>` on the client to get live URL updates.
@@ -19,6 +20,7 @@ A pure URL matcher: it reads the current URL from the surrounding `<Meta>` conte
19
20
  - `<Router>` accepts `PossibleMeta` props (`url`, `base`, etc.) to override the surrounding context — this is how nested routers scope themselves.
20
21
  - With a `base` set, the path used for matching is the URL after `matchURLPrefix()` strips the base prefix; URLs outside the base render as `null`.
21
22
  - Pass `fallback` to control no-match behaviour. An explicit `null` renders nothing; leaving it `undefined` throws a `NotFoundError`.
23
+ - Inside `<Navigation>` with stacks, each page in a stack is a new page: a link mounts a new page, and `NavigationStore.replace()` keeps the same page mounted (it re-renders with the new URL). Elsewhere each path is a new page.
22
24
  - `<Router>` itself does not cache pages — wrap it in a `<RouteCache>` (or use a layout that already does) to keep recently-visited pages mounted but hidden so back/forward navigation restores their state.
23
25
 
24
26
  ## Usage
@@ -133,7 +135,7 @@ const SIDEBARRED_ROUTES = {
133
135
 
134
136
  ### Keeping page state
135
137
 
136
- By default a page is unmounted when you navigate away and mounted fresh when you return — so scroll position, open/closed toggles, in-progress searches, form inputs, and focus are all lost, and you land back at the top. Wrap the router (or its scrolling content region) in a `<RouteCache>` to keep recently-visited pages mounted but hidden so their state is restored on back/forward navigation. Shelving's `<SidebarLayout>` and `<CenteredLayout>` already do this for you.
138
+ By default a page is unmounted when you navigate away and mounted fresh when you return — so scroll position, open/closed toggles, in-progress searches, form inputs, and focus are all lost, and you land back at the top. Wrap the router (or its scrolling content region) in a `<RouteCache>` to keep recently-visited pages mounted but hidden so their state is still there when you go back to them, or switch back to their tab. Shelving's `<SidebarLayout>` and `<CenteredLayout>` already do this for you.
137
139
 
138
140
  ### SSR / static rendering
139
141
 
@@ -0,0 +1,9 @@
1
+ # SLIDE_DOWN_TRANSITION_CLASS
2
+
3
+ - Kind: constant
4
+ - Import: `import { SLIDE_DOWN_TRANSITION_CLASS } from "shelving/ui";`
5
+
6
+ View transition class that slides content down: the new content comes in from the top and the old content leaves to the bottom.
7
+
8
+ - Pass it to React's `<ViewTransition>` (for example `enter` or `exit`) to use this animation without the component.
9
+ - Also adds the plain global `slide-down` class, so app CSS can target it with `::view-transition-group(.slide-down)`.
@@ -0,0 +1,9 @@
1
+ # SLIDE_LEFT_TRANSITION_CLASS
2
+
3
+ - Kind: constant
4
+ - Import: `import { SLIDE_LEFT_TRANSITION_CLASS } from "shelving/ui";`
5
+
6
+ View transition class that slides content left: the new content comes in from the left and the old content leaves to the right.
7
+
8
+ - Pass it to React's `<ViewTransition>` (for example `enter` or `exit`) to use this animation without the component.
9
+ - Also adds the plain global `slide-left` class, so app CSS can target it with `::view-transition-group(.slide-left)`.
@@ -0,0 +1,9 @@
1
+ # SLIDE_RIGHT_TRANSITION_CLASS
2
+
3
+ - Kind: constant
4
+ - Import: `import { SLIDE_RIGHT_TRANSITION_CLASS } from "shelving/ui";`
5
+
6
+ View transition class that slides content right: the new content comes in from the right and the old content leaves to the left.
7
+
8
+ - Pass it to React's `<ViewTransition>` (for example `enter` or `exit`) to use this animation without the component.
9
+ - Also adds the plain global `slide-right` class, so app CSS can target it with `::view-transition-group(.slide-right)`.
@@ -0,0 +1,9 @@
1
+ # SLIDE_UP_TRANSITION_CLASS
2
+
3
+ - Kind: constant
4
+ - Import: `import { SLIDE_UP_TRANSITION_CLASS } from "shelving/ui";`
5
+
6
+ View transition class that slides content up: the new content comes in from the bottom and the old content leaves to the top.
7
+
8
+ - Pass it to React's `<ViewTransition>` (for example `enter` or `exit`) to use this animation without the component.
9
+ - Also adds the plain global `slide-up` class, so app CSS can target it with `::view-transition-group(.slide-up)`.
@@ -0,0 +1,64 @@
1
+ # StackButton
2
+
3
+ - Kind: component
4
+ - Import: `import { StackButton } from "shelving/ui";`
5
+
6
+ Button for one stack in a tab bar.
7
+ - Tap another stack's button: go to the page you left in that stack. The page slides left or right by tab order.
8
+ - Tap the current stack's button: go back to its root.
9
+ - Tap it again at the root: scroll the page to the top.
10
+ - It is selected while its stack is the current stack.
11
+ - Without the browser Navigation API (and in a server render) it is a plain link to the stack's root.
12
+
13
+ A button for one stack in a tab bar. It acts like a tab button in a phone app.
14
+
15
+ **Things to know:**
16
+
17
+ - Tap another stack's button: go to the page you left in that stack. The page slides left or right, by tab order.
18
+ - Tap the current stack's button: go back to the root of the stack. The page slides back.
19
+ - Tap it again at the root: scroll the page to the top. It scrolls the window, and each visible `<main>` or `[data-scroll]` element.
20
+ - It is selected (`aria-current`) while its stack is the current stack. On an unstacked page (for example a deep link or a login page), no stack button is selected.
21
+ - `stack` must be one of the `stacks` on `<Navigation>`.
22
+ - It takes the usual `<Button>` props, except `href`, `onClick` and `selected`.
23
+ - Without the browser Navigation API, and in a server render, it is a plain link to the stack's root. It is selected while its stack is the starting stack.
24
+ - In a server-rendered app, the server renders a link and the browser renders a button, so React warns about a hydration mismatch on the first render. A client-only app (with `<App>`) has no mismatch.
25
+
26
+ ## Usage
27
+
28
+ ```tsx
29
+ import { Navigation, StackButton } from "shelving/ui";
30
+ import { HomeIcon, SearchIcon, UserIcon } from "./icons.js";
31
+
32
+ <Navigation stacks={["/home", "/discover", "/profile"]}>
33
+ {/* … */}
34
+ <nav className="tab-bar">
35
+ <StackButton stack="/home" title="Home"><HomeIcon/></StackButton>
36
+ <StackButton stack="/discover" title="Discover"><SearchIcon/></StackButton>
37
+ <StackButton stack="/profile" title="Profile"><UserIcon/></StackButton>
38
+ </nav>
39
+ </Navigation>
40
+ ```
41
+
42
+ ## Styling
43
+
44
+ `<StackButton>` renders a `<Button>`, so it has the same styling hooks. See `<Button>`.
45
+
46
+
47
+ ## Signatures
48
+
49
+ ```ts
50
+ StackButton({ stack, ...props }: StackButtonProps): ReactElement
51
+ ```
52
+
53
+ ## Parameters
54
+
55
+ | Param | Type | Description |
56
+ | --- | --- | --- |
57
+ | `props` | `StackButtonProps` | |
58
+ | `.stack` | `AbsolutePath` | Root path of the stack this button opens. It must be one of the `stacks` on `<Navigation>`. Read only. |
59
+
60
+ ## Returns
61
+
62
+ | Type | Description |
63
+ | --- | --- |
64
+ | `ReactElement` | |
@@ -0,0 +1,21 @@
1
+ # StackButtonProps
2
+
3
+ - Kind: interface
4
+ - Import: `import { StackButtonProps } from "shelving/ui";`
5
+ - Extends: `Omit<ButtonProps, "href" | "onClick" | "selected">`
6
+
7
+ Props for `<StackButton>`: the `stack` it opens, plus the usual `Button` props except `href` and `onClick`.
8
+
9
+ ## Signatures
10
+
11
+ ```ts
12
+ {
13
+ readonly stack: AbsolutePath;
14
+ }
15
+ ```
16
+
17
+ ## Properties
18
+
19
+ | Property | Type | Description |
20
+ | --- | --- | --- |
21
+ | `stack` | `AbsolutePath` | Root path of the stack this button opens. It must be one of the `stacks` on `<Navigation>`. Read only. |
@@ -10,16 +10,19 @@ A direction-aware `<Transition>` preset that slides its children vertically —
10
10
  **Things to know:**
11
11
 
12
12
  - Slides down by default and when the type is `"forward"`; slides up when the type is `"back"`.
13
- - Inside `<Navigation>` the direction is automatic: a link click, `NavigationStore.forward()` or `NavigationStore.redirect()` sets the `"forward"` type, and the browser back or forward button sets `"back"`. Outside `<Navigation>`, set the direction with `setTransitionType("forward" | "back")` inside a `startTransition()` callback — see `<Transition>`.
13
+ - Inside `<Navigation>` the direction is automatic: a link click or `NavigationStore.forward()` sets the `"forward"` type, and a `className="back"` link or `NavigationStore.back()` sets `"back"`. The browser back and forward buttons set the type from the page's place in its stack. `NavigationStore.replace()` has no transition. Outside `<Navigation>`, set the direction with `setTransitionType("forward" | "back")` inside a `startTransition()` callback — see `<Transition>`.
14
14
  - Pass `overlay` to raise the transition group above surrounding content during the animation (`z-index: 100`).
15
15
  - The old and new content slide a full height apart, so they sit edge to edge and never overlap.
16
16
  - The slide is clipped to the element's own box (`overflow: clip` on the group), so it does not paint over the content around it.
17
- - During the slide, the snapshots paint above fixed elements such as a bottom bar. To keep a fixed element on top, give it its own `view-transition-name` and put the slide groups below it:
17
+ - During the slide, the snapshots paint above fixed elements such as a bottom bar. To keep a fixed element on top, give it its own `view-transition-name` and raise its group:
18
18
 
19
19
  ```css
20
- ::view-transition-group(.slide-up),
21
- ::view-transition-group(.slide-down) {
22
- z-index: -1;
20
+ .bottom-bar {
21
+ view-transition-name: bottom-bar;
22
+ }
23
+
24
+ ::view-transition-group(bottom-bar) {
25
+ z-index: 1;
23
26
  }
24
27
  ```
25
28
 
@@ -35,6 +38,19 @@ import { VerticalTransition } from "shelving/ui";
35
38
  </VerticalTransition>
36
39
  ```
37
40
 
41
+ ## Use the class without the component
42
+
43
+ The CSS is a CSS module, so its class names are hashed. To use the same animation on your own `<ViewTransition>`, import `SLIDE_DOWN_TRANSITION_CLASS` and `SLIDE_UP_TRANSITION_CLASS`. Each one also holds a plain global class name, so your CSS can still target it.
44
+
45
+ ```tsx
46
+ import { ViewTransition } from "react";
47
+ import { SLIDE_DOWN_TRANSITION_CLASS } from "shelving/ui";
48
+
49
+ <ViewTransition enter={SLIDE_DOWN_TRANSITION_CLASS} exit={SLIDE_DOWN_TRANSITION_CLASS}>
50
+ <div className="panel">…</div>
51
+ </ViewTransition>
52
+ ```
53
+
38
54
  ## Styling
39
55
 
40
56
  | Variable | Styles | Default |
@@ -22,7 +22,8 @@ mergeMeta(meta1: Meta, meta2: PossibleMeta, caller: AnyCaller = mergeMeta): Meta
22
22
  | --- | --- | --- |
23
23
  | `meta1` | `Meta` | The existing fully-resolved `Meta` to merge into. |
24
24
  | `.root` | `ImmutableURL \| undefined` | Base URL for the app (used to resolve `url` and set as `<base>` tag in `<Head>`). Read only. Optional. |
25
- | `.url` | `ImmutableURL \| undefined` | URL of the current page (used to update history API and as the initial URL for routing). Read only. Optional. |
25
+ | `.url` | `ImmutableURL \| undefined` | URL of the current page (used as the initial URL for routing, and as the page's canonical URL in `<Head>`). Read only. Optional. |
26
+ | `.stack` | `AbsolutePath \| undefined` | Root path of the current stack (for example `"/home"`). Set it above `<Navigation>` to choose the stack the app opens in. `<Navigation>` publishes the live stack, which is `"/"` for an unstacked page. Read only. Optional. |
26
27
  | `.app` | `string \| undefined` | Title of the entire application. Read only. Optional. |
27
28
  | `.title` | `string \| undefined` | Title of the current page (set as `<title>` in `<Head>` Read only. Optional. |
28
29
  | `.description` | `string \| undefined` | Description of the current page. Read only. Optional. |