shelving 1.322.2 → 1.323.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/guides/building-with-shelving.md +1 -1
- package/docs/llms-full.txt +62 -48
- package/docs/ui/BackButton.md +1 -1
- package/docs/ui/Card.md +1 -1
- package/docs/ui/Meta.md +2 -2
- package/docs/ui/Navigation.md +4 -4
- package/docs/ui/NavigationEntryState.md +2 -2
- package/docs/ui/NavigationPosition.md +2 -2
- package/docs/ui/NavigationProps.md +3 -3
- package/docs/ui/NavigationStore/back.md +1 -1
- package/docs/ui/NavigationStore/forward.md +1 -1
- package/docs/ui/NavigationStore/history.md +3 -2
- package/docs/ui/NavigationStore/replace.md +1 -1
- package/docs/ui/NavigationStore.md +26 -15
- package/docs/ui/RouteCache.md +7 -5
- package/docs/ui/RouteCacheProps.md +1 -1
- package/docs/ui/StackButton.md +5 -5
- package/docs/ui/mergeMeta.md +1 -1
- package/package.json +1 -1
- package/ui/block/Card.md +1 -1
- package/ui/block/Card.module.css +10 -5
- package/ui/router/BackButton.md +1 -1
- package/ui/router/Navigation.d.ts +4 -3
- package/ui/router/Navigation.js +1 -1
- package/ui/router/Navigation.md +2 -2
- package/ui/router/Navigation.test.tsx +11 -1
- package/ui/router/Navigation.tsx +6 -5
- package/ui/router/NavigationStore.d.ts +32 -24
- package/ui/router/NavigationStore.js +42 -27
- package/ui/router/NavigationStore.md +18 -10
- package/ui/router/NavigationStore.test.ts +90 -23
- package/ui/router/NavigationStore.tsx +58 -41
- package/ui/router/RouteCache.d.ts +5 -3
- package/ui/router/RouteCache.js +20 -7
- package/ui/router/RouteCache.md +3 -2
- package/ui/router/RouteCache.tsx +30 -9
- package/ui/router/StackButton.d.ts +2 -2
- package/ui/router/StackButton.js +3 -3
- package/ui/router/StackButton.md +3 -3
- package/ui/router/StackButton.tsx +3 -3
- package/ui/util/meta.d.ts +2 -2
- package/ui/util/meta.ts +2 -2
|
@@ -7,19 +7,20 @@ import { matchURLPrefix, requireURL } from "../../util/url.js";
|
|
|
7
7
|
*
|
|
8
8
|
* - Extends `URLStore`, so its `value` is the current URL.
|
|
9
9
|
* - 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.
|
|
10
|
-
* - `stack` is a sub-store that holds the root of the current stack. It only changes when the stack changes.
|
|
10
|
+
* - `stack` is a sub-store that holds the root of the current stack, or `null` for a floating page. It only changes when the stack changes.
|
|
11
|
+
* - A page outside the listed stacks is floating: its stack is `null`. The floating stack has no root page and no tab.
|
|
12
|
+
* - In a listed stack, depth `0` is always the stack's root path.
|
|
11
13
|
* - 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.
|
|
12
14
|
* - Owned by `<Navigation>`. Read it with `requireNavigation()`.
|
|
13
15
|
*
|
|
14
16
|
* @see https://shelving.cc/ui/NavigationStore
|
|
15
17
|
*/
|
|
16
18
|
export class NavigationStore extends URLStore {
|
|
17
|
-
/** Root path of each stack, in tab order.
|
|
19
|
+
/** Root path of each stack, in tab order. With none, every page floats. */
|
|
18
20
|
stacks;
|
|
19
21
|
/**
|
|
20
|
-
* Root path of the current stack.
|
|
21
|
-
* -
|
|
22
|
-
* - It starts at the `stack` given to the constructor (default `"/"`). Without the Navigation API, it stays there.
|
|
22
|
+
* Root path of the current stack, or `null` for a floating page.
|
|
23
|
+
* - It starts at the `stack` given to the constructor (default `null`). Without the Navigation API, it stays there.
|
|
23
24
|
*/
|
|
24
25
|
stack;
|
|
25
26
|
/**
|
|
@@ -35,12 +36,20 @@ export class NavigationStore extends URLStore {
|
|
|
35
36
|
_busy = false;
|
|
36
37
|
/** Stack of the page the app opens at, when its history entry has no saved stack. */
|
|
37
38
|
_start;
|
|
38
|
-
constructor(url = typeof window === "undefined" ? "/" : window.location.href, base, stack =
|
|
39
|
+
constructor(url = typeof window === "undefined" ? "/" : window.location.href, base, stack = null, ...stacks) {
|
|
39
40
|
super(url, base);
|
|
40
|
-
this.stacks = stacks
|
|
41
|
-
this._start = stack;
|
|
41
|
+
this.stacks = stacks;
|
|
42
|
+
this._start = stack !== null && stacks.includes(stack) ? stack : null;
|
|
42
43
|
this._navigation = _getNavigation();
|
|
43
|
-
this.stack = new Store(this._navigation ? this._getState(this._navigation.currentEntry).stack :
|
|
44
|
+
this.stack = new Store(this._navigation ? this._getState(this._navigation.currentEntry).stack : this._start);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Path of the current URL relative to the app root (without its `?query`), or `undefined` if the URL is outside the app.
|
|
48
|
+
*
|
|
49
|
+
* @see https://shelving.cc/ui/NavigationStore/path
|
|
50
|
+
*/
|
|
51
|
+
get path() {
|
|
52
|
+
return this._getPath(this.value);
|
|
44
53
|
}
|
|
45
54
|
/**
|
|
46
55
|
* Where the app is now: the current stack and depth, and the top depth of each stack.
|
|
@@ -52,11 +61,11 @@ export class NavigationStore extends URLStore {
|
|
|
52
61
|
if (!this._navigation)
|
|
53
62
|
return undefined;
|
|
54
63
|
const { stack, depth } = this._getState(this._navigation.currentEntry);
|
|
55
|
-
const tops =
|
|
64
|
+
const tops = new Map();
|
|
56
65
|
for (const s of this._getStacks()) {
|
|
57
66
|
const top = this._getPages(s).at(-1);
|
|
58
67
|
if (top)
|
|
59
|
-
tops
|
|
68
|
+
tops.set(s, top.depth);
|
|
60
69
|
}
|
|
61
70
|
return { stack, depth, tops, parent: this.history(stack).at(-2) };
|
|
62
71
|
}
|
|
@@ -111,10 +120,11 @@ export class NavigationStore extends URLStore {
|
|
|
111
120
|
/**
|
|
112
121
|
* Get the pages in a stack, root first, or every page in every stack.
|
|
113
122
|
* - Read from the browser history entries up to the current one.
|
|
114
|
-
* - A stack whose root entry is
|
|
123
|
+
* - A listed stack whose root entry is not in the history (a deep link, or the browser keeps a limited history) still starts with its root.
|
|
124
|
+
* - The floating stack (`null`) has no root, so it starts with its oldest page.
|
|
115
125
|
* - Without the Navigation API this is always empty.
|
|
116
126
|
*
|
|
117
|
-
* @param stack Root path of the stack. Defaults to the current stack.
|
|
127
|
+
* @param stack Root path of the stack, or `null` for the floating stack. Defaults to the current stack.
|
|
118
128
|
* @returns Paths relative to the app root, each with its `?query`.
|
|
119
129
|
* @example nav.history("/home") // ["/home", "/home/post/2"]
|
|
120
130
|
* @see https://shelving.cc/ui/NavigationStore/history
|
|
@@ -122,7 +132,7 @@ export class NavigationStore extends URLStore {
|
|
|
122
132
|
history(stack = this.stack.value) {
|
|
123
133
|
const pages = this._getPages(stack);
|
|
124
134
|
const paths = pages.map(p => _getPathWithQuery(p));
|
|
125
|
-
if (pages[0] && pages[0].depth > 0)
|
|
135
|
+
if (stack !== null && pages[0] && pages[0].depth > 0)
|
|
126
136
|
paths.unshift(stack);
|
|
127
137
|
return paths;
|
|
128
138
|
}
|
|
@@ -134,7 +144,7 @@ export class NavigationStore extends URLStore {
|
|
|
134
144
|
* - A change of stack slides by tab order. Else it slides forward.
|
|
135
145
|
*
|
|
136
146
|
* @param possible The destination URL, resolved against `base`.
|
|
137
|
-
* @param stack Root path of the stack to go to. Defaults to the current stack.
|
|
147
|
+
* @param stack Root path of the stack to go to, or `null` for the floating stack. Defaults to the current stack.
|
|
138
148
|
* @throws RequiredError If `possible` cannot be resolved to a valid URL.
|
|
139
149
|
* @example nav.forward("/settings");
|
|
140
150
|
* @see https://shelving.cc/ui/NavigationStore/forward
|
|
@@ -151,7 +161,7 @@ export class NavigationStore extends URLStore {
|
|
|
151
161
|
return;
|
|
152
162
|
}
|
|
153
163
|
const top = this._getPages(stack).at(-1);
|
|
154
|
-
const depth = top ? (top.url.href === url.href ? top.depth : top.depth + 1) :
|
|
164
|
+
const depth = top ? (top.url.href === url.href ? top.depth : top.depth + 1) : this._getBottomDepth(url, stack);
|
|
155
165
|
this._push(url, this._getOrder(current, stack) ?? "forward", { stack, depth });
|
|
156
166
|
}
|
|
157
167
|
/**
|
|
@@ -162,7 +172,7 @@ export class NavigationStore extends URLStore {
|
|
|
162
172
|
* - It works when the app was opened at the current page, so a back button always has somewhere to go.
|
|
163
173
|
*
|
|
164
174
|
* @param possible The destination URL, resolved against `base`.
|
|
165
|
-
* @param stack Root path of the stack to go back in. Defaults to the current stack.
|
|
175
|
+
* @param stack Root path of the stack to go back in, or `null` for the floating stack. Defaults to the current stack.
|
|
166
176
|
* @throws RequiredError If `possible` cannot be resolved to a valid URL.
|
|
167
177
|
* @example nav.back("/profile");
|
|
168
178
|
* @see https://shelving.cc/ui/NavigationStore/back
|
|
@@ -182,7 +192,7 @@ export class NavigationStore extends URLStore {
|
|
|
182
192
|
return;
|
|
183
193
|
}
|
|
184
194
|
// The URL is an earlier page in the stack, else the root of the stack, else it takes the place of the top page.
|
|
185
|
-
const depth = page ? page.depth : this._getPath(url) === stack ? 0 : (pages.at(-1)?.depth ??
|
|
195
|
+
const depth = page ? page.depth : this._getPath(url) === stack ? 0 : (pages.at(-1)?.depth ?? this._getBottomDepth(url, stack));
|
|
186
196
|
this._push(url, this._getOrder(current, stack) ?? "back", { stack, depth });
|
|
187
197
|
}
|
|
188
198
|
/**
|
|
@@ -192,7 +202,7 @@ export class NavigationStore extends URLStore {
|
|
|
192
202
|
* - Pass a `stack` to move the page into that stack: at its root if the URL is the root, else on top of its pages. For example, a login page calls `replace("/home", "/home")`.
|
|
193
203
|
*
|
|
194
204
|
* @param possible The destination URL, resolved against `base`.
|
|
195
|
-
* @param stack Root path of the stack to move the page into. Defaults to the current stack.
|
|
205
|
+
* @param stack Root path of the stack to move the page into, or `null` for the floating stack. Defaults to the current stack.
|
|
196
206
|
* @throws RequiredError If `possible` cannot be resolved to a valid URL.
|
|
197
207
|
* @example nav.replace("/login");
|
|
198
208
|
* @see https://shelving.cc/ui/NavigationStore/replace
|
|
@@ -205,7 +215,7 @@ export class NavigationStore extends URLStore {
|
|
|
205
215
|
const top = this._getPages(stack)
|
|
206
216
|
.filter(p => p.index !== this._navigation?.currentEntry?.index)
|
|
207
217
|
.at(-1);
|
|
208
|
-
state = { stack, depth: this._getPath(url) === stack ? 0 : top ? top.depth + 1 :
|
|
218
|
+
state = { stack, depth: this._getPath(url) === stack ? 0 : top ? top.depth + 1 : this._getBottomDepth(url, stack) };
|
|
209
219
|
}
|
|
210
220
|
this._write(() => window.history.replaceState(null, "", url.href), state);
|
|
211
221
|
this._change(url, null, state?.stack);
|
|
@@ -241,7 +251,7 @@ export class NavigationStore extends URLStore {
|
|
|
241
251
|
/** Set the stack first, then the URL, so both are ready when `<Navigation>` reads them. */
|
|
242
252
|
_change(url, transition, stack) {
|
|
243
253
|
this.transition = transition;
|
|
244
|
-
if (stack)
|
|
254
|
+
if (stack !== undefined)
|
|
245
255
|
this.stack.value = stack;
|
|
246
256
|
this.value = url;
|
|
247
257
|
}
|
|
@@ -269,16 +279,21 @@ export class NavigationStore extends URLStore {
|
|
|
269
279
|
}
|
|
270
280
|
return pages;
|
|
271
281
|
}
|
|
272
|
-
/** Get the saved state of an entry, or for an entry with none (the page the app opened at), the starting stack at depth
|
|
282
|
+
/** Get the saved state of an entry, or for an entry with none (for example the page the app opened at), the starting stack at its bottom depth. */
|
|
273
283
|
_getState(entry) {
|
|
274
284
|
const state = entry?.getState();
|
|
275
285
|
if (_isEntryState(state) && this._getStacks().includes(state.stack))
|
|
276
286
|
return state;
|
|
277
|
-
|
|
287
|
+
const url = entry?.url ? requireURL(entry.url, undefined, this._getState) : undefined;
|
|
288
|
+
return { stack: this._start, depth: url ? this._getBottomDepth(url, this._start) : 0 };
|
|
289
|
+
}
|
|
290
|
+
/** Get the depth of a URL that is the first page of a stack: `0` for the stack's root path or for the floating stack, else `1` so the root stays below it. */
|
|
291
|
+
_getBottomDepth(url, stack) {
|
|
292
|
+
return stack === null || this._getPath(url) === stack ? 0 : 1;
|
|
278
293
|
}
|
|
279
|
-
/** Get every stack a page can be in: the listed stacks, plus the
|
|
294
|
+
/** Get every stack a page can be in: the listed stacks, plus the floating stack (`null`). */
|
|
280
295
|
_getStacks() {
|
|
281
|
-
return
|
|
296
|
+
return [...this.stacks, null];
|
|
282
297
|
}
|
|
283
298
|
/** Get the path of a URL relative to the app root, or `undefined` if it is outside the app. */
|
|
284
299
|
_getPath(url) {
|
|
@@ -299,7 +314,7 @@ export class NavigationStore extends URLStore {
|
|
|
299
314
|
}
|
|
300
315
|
/** Slide direction between two stacks by tab order, or `undefined` for the same stack. */
|
|
301
316
|
_getOrder(from, to) {
|
|
302
|
-
if (from === to)
|
|
317
|
+
if (from === to || from === null || to === null)
|
|
303
318
|
return;
|
|
304
319
|
const a = this.stacks.indexOf(from);
|
|
305
320
|
const b = this.stacks.indexOf(to);
|
|
@@ -325,7 +340,7 @@ function _isEntryState(state) {
|
|
|
325
340
|
return (typeof state === "object" &&
|
|
326
341
|
state !== null &&
|
|
327
342
|
"stack" in state &&
|
|
328
|
-
typeof state.stack === "string" &&
|
|
343
|
+
(typeof state.stack === "string" || state.stack === null) &&
|
|
329
344
|
"depth" in state &&
|
|
330
345
|
typeof state.depth === "number");
|
|
331
346
|
}
|
|
@@ -4,9 +4,11 @@ The store that holds the current URL, drives browser history, and keeps one stac
|
|
|
4
4
|
|
|
5
5
|
**Things to know:**
|
|
6
6
|
|
|
7
|
-
- `NavigationStore.stacks` is the root path of each stack, in tab order, from the `stacks` prop on `<Navigation>`.
|
|
8
|
-
- A page that is not in a listed stack is **
|
|
9
|
-
-
|
|
7
|
+
- `NavigationStore.stacks` is the root path of each stack, in tab order, from the `stacks` prop on `<Navigation>`. `/` is a normal stack root, like any other path.
|
|
8
|
+
- A page that is not in a listed stack is **floating**: its stack is `null`. See [Floating pages](#floating-pages).
|
|
9
|
+
- In a listed stack, depth `0` is always the stack's root path. A deeper page always has the root below it.
|
|
10
|
+
- `NavigationStore.stack` is a sub-store that holds the root of the current stack, or `null` on a floating page. It changes only when the stack changes, so `useStore(nav.stack)` re-renders only on a tab change.
|
|
11
|
+
- `NavigationStore.path` is the current path relative to the app root, without its `?query`.
|
|
10
12
|
- `NavigationStore.history()` returns the pages in the current stack (or a given stack), root first.
|
|
11
13
|
- `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.
|
|
12
14
|
- All URLs resolve against the store's `base`. Each method also takes a full same-origin URL.
|
|
@@ -21,17 +23,23 @@ The store that holds the current URL, drives browser history, and keeps one stac
|
|
|
21
23
|
| `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 |
|
|
22
24
|
| `history(stack?)` | Returns the pages in `stack`, root first. | — |
|
|
23
25
|
|
|
24
|
-
`stack` defaults to the current stack.
|
|
26
|
+
`stack` defaults to the current stack. Pass `null` to name the floating stack.
|
|
25
27
|
|
|
26
|
-
##
|
|
28
|
+
## Floating pages
|
|
27
29
|
|
|
28
|
-
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
|
|
30
|
+
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 it **floats**: its stack is `null`. For example: a deep link into the app, a fresh open of `/post/9`, or a login page.
|
|
31
|
+
|
|
32
|
+
- The floating stack is outside every tab. It has no root page and no `<StackButton>`, so no stack button is selected on a floating page.
|
|
33
|
+
- Links from a floating page push deeper in the floating stack, even a link to a stack root.
|
|
34
|
+
- To leave the floating stack, tap a `<StackButton>`, or name the stack. To move a page back into it, pass `null` as the `stack`.
|
|
35
|
+
- `<RouteCache>` gives floating pages no protection: they are dropped like any other page past `maxCached`.
|
|
36
|
+
- With no `stacks`, every page floats.
|
|
37
|
+
|
|
38
|
+
## Opening the app
|
|
29
39
|
|
|
30
|
-
- On an unstacked page, no `<StackButton>` is selected.
|
|
31
|
-
- Links from an unstacked page push deeper in the `/` stack, even a link to a stack root.
|
|
32
|
-
- To leave the `/` stack, tap a `<StackButton>`, or name the stack.
|
|
33
40
|
- 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.
|
|
34
|
-
-
|
|
41
|
+
- A deep page the app opens at, in a listed stack, starts at depth 1, so the stack root is its parent. A `<StackButton>` or a back gesture then goes to the root. A stack root starts at depth 0.
|
|
42
|
+
- A `stack` that is not in `stacks` is ignored: the page floats.
|
|
35
43
|
|
|
36
44
|
For example, a login page sends the user home after they log in, and names the Home stack:
|
|
37
45
|
|
|
@@ -78,7 +78,7 @@ const globals = globalThis as { navigation?: unknown; window?: unknown };
|
|
|
78
78
|
let fake: FakeNavigation;
|
|
79
79
|
|
|
80
80
|
/** Open the app at `path`, in `stack` (by default the stack whose root is `path`, else `/`). */
|
|
81
|
-
function setup(path = "/home", stack = (STACKS as readonly string[]).includes(path) ? path :
|
|
81
|
+
function setup(path = "/home", stack: string | null = (STACKS as readonly string[]).includes(path) ? path : null): NavigationStore {
|
|
82
82
|
fake = new FakeNavigation(`${ORIGIN}${path}`);
|
|
83
83
|
globals.navigation = fake;
|
|
84
84
|
globals.window = {
|
|
@@ -90,14 +90,14 @@ function setup(path = "/home", stack = (STACKS as readonly string[]).includes(pa
|
|
|
90
90
|
addEventListener: () => undefined,
|
|
91
91
|
removeEventListener: () => undefined,
|
|
92
92
|
};
|
|
93
|
-
const nav = new NavigationStore(`${ORIGIN}${path}`, `${ORIGIN}/`, stack as `/${string}
|
|
93
|
+
const nav = new NavigationStore(`${ORIGIN}${path}`, `${ORIGIN}/`, stack as `/${string}` | null, ...STACKS);
|
|
94
94
|
nav.listen();
|
|
95
95
|
return nav;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
function getEntries(): string[] {
|
|
99
99
|
return fake.list.map(e => {
|
|
100
|
-
const s = e.state as { stack: string; depth: number } | undefined;
|
|
100
|
+
const s = e.state as { stack: string | null; depth: number } | undefined;
|
|
101
101
|
return `${new URL(e.url).pathname}${s ? ` ${s.stack}@${s.depth}` : ""}`;
|
|
102
102
|
});
|
|
103
103
|
}
|
|
@@ -120,11 +120,14 @@ describe("NavigationStore", () => {
|
|
|
120
120
|
expect(nav.position).toBe(undefined);
|
|
121
121
|
});
|
|
122
122
|
|
|
123
|
-
test("
|
|
124
|
-
expect(new NavigationStore(`${ORIGIN}/home`, `${ORIGIN}/`).stack.value).toBe(
|
|
123
|
+
test("floats every page with no stacks", () => {
|
|
124
|
+
expect(new NavigationStore(`${ORIGIN}/home`, `${ORIGIN}/`).stack.value).toBe(null);
|
|
125
125
|
fake = new FakeNavigation(`${ORIGIN}/a/b`);
|
|
126
126
|
globals.navigation = fake;
|
|
127
|
-
|
|
127
|
+
globals.window = { location: { href: `${ORIGIN}/a/b` } };
|
|
128
|
+
const nav = new NavigationStore(`${ORIGIN}/a/b`, `${ORIGIN}/`);
|
|
129
|
+
expect(nav.stack.value).toBe(null);
|
|
130
|
+
expect(nav.history()).toEqual(["/a/b"]);
|
|
128
131
|
});
|
|
129
132
|
|
|
130
133
|
test("keeps the starting stack without the Navigation API", () => {
|
|
@@ -141,13 +144,49 @@ describe("NavigationStore", () => {
|
|
|
141
144
|
test("opens in the starting stack", () => {
|
|
142
145
|
const nav = setup("/profile/settings", "/profile");
|
|
143
146
|
expect(nav.stack.value).toBe("/profile");
|
|
144
|
-
expect(getEntries()).toEqual(["/profile/settings /profile@
|
|
147
|
+
expect(getEntries()).toEqual(["/profile/settings /profile@1"]);
|
|
145
148
|
});
|
|
146
149
|
|
|
147
|
-
test("opens a stack root
|
|
148
|
-
|
|
149
|
-
expect(
|
|
150
|
-
|
|
150
|
+
test("opens a stack root at depth 0", () => {
|
|
151
|
+
setup("/profile", "/profile");
|
|
152
|
+
expect(getEntries()).toEqual(["/profile /profile@0"]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("keeps the stack root below a deep page the app opens at", () => {
|
|
156
|
+
const nav = setup("/discover/fitness", "/discover");
|
|
157
|
+
expect(nav.history()).toEqual(["/discover", "/discover/fitness"]);
|
|
158
|
+
expect(nav.position?.parent).toBe("/discover");
|
|
159
|
+
nav.back("/discover");
|
|
160
|
+
expect(getEntries()).toEqual(["/discover/fitness /discover@1", "/discover /discover@0"]);
|
|
161
|
+
expect(nav.history()).toEqual(["/discover"]);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("keeps the root below a deep page in a single-stack app", () => {
|
|
165
|
+
fake = new FakeNavigation(`${ORIGIN}/a/b`);
|
|
166
|
+
globals.navigation = fake;
|
|
167
|
+
globals.window = { location: { href: `${ORIGIN}/a/b` } };
|
|
168
|
+
const nav = new NavigationStore(`${ORIGIN}/a/b`, `${ORIGIN}/`, "/", "/");
|
|
169
|
+
expect(nav.history()).toEqual(["/", "/a/b"]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("treats / as a normal stack when it is listed", () => {
|
|
173
|
+
fake = new FakeNavigation(`${ORIGIN}/post/1`);
|
|
174
|
+
globals.navigation = fake;
|
|
175
|
+
globals.window = { location: { href: `${ORIGIN}/post/1` } };
|
|
176
|
+
const nav = new NavigationStore(`${ORIGIN}/post/1`, `${ORIGIN}/`, null, "/", "/discover");
|
|
177
|
+
expect(nav.stack.value).toBe(null);
|
|
178
|
+
expect(nav.history("/")).toEqual([]);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("opens a stack root as floating with no starting stack", () => {
|
|
182
|
+
const nav = setup("/profile", null);
|
|
183
|
+
expect(nav.stack.value).toBe(null);
|
|
184
|
+
expect(getEntries()).toEqual(["/profile null@0"]);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("floats a starting stack that is not listed", () => {
|
|
188
|
+
const nav = setup("/other/1", "/other");
|
|
189
|
+
expect(nav.stack.value).toBe(null);
|
|
151
190
|
});
|
|
152
191
|
|
|
153
192
|
test("keeps the saved stack after a reload", () => {
|
|
@@ -159,27 +198,34 @@ describe("NavigationStore", () => {
|
|
|
159
198
|
expect(nav.stack.value).toBe("/home");
|
|
160
199
|
});
|
|
161
200
|
|
|
162
|
-
describe("
|
|
163
|
-
test("open any other page in the
|
|
201
|
+
describe("floating pages", () => {
|
|
202
|
+
test("open any other page in the floating stack", () => {
|
|
164
203
|
const nav = setup("/profile/settings");
|
|
165
|
-
expect(nav.stack.value).toBe(
|
|
166
|
-
expect(getEntries()).toEqual(["/profile/settings
|
|
204
|
+
expect(nav.stack.value).toBe(null);
|
|
205
|
+
expect(getEntries()).toEqual(["/profile/settings null@0"]);
|
|
167
206
|
});
|
|
168
207
|
|
|
169
|
-
test("push links deeper in the
|
|
208
|
+
test("push links deeper in the floating stack", () => {
|
|
170
209
|
const nav = setup("/login");
|
|
171
210
|
nav.forward("/login/reset");
|
|
172
|
-
expect(nav.stack.value).toBe(
|
|
211
|
+
expect(nav.stack.value).toBe(null);
|
|
173
212
|
expect(nav.history()).toEqual(["/login", "/login/reset"]);
|
|
174
213
|
});
|
|
175
214
|
|
|
176
|
-
test("stay
|
|
215
|
+
test("stay floating when a link or a replace goes to a stack root", () => {
|
|
177
216
|
const nav = setup("/login");
|
|
178
217
|
nav.forward("/home");
|
|
179
|
-
expect(getEntries().at(-1)).toBe("/home
|
|
218
|
+
expect(getEntries().at(-1)).toBe("/home null@1");
|
|
180
219
|
nav.replace("/profile");
|
|
181
|
-
expect(getEntries().at(-1)).toBe("/profile
|
|
182
|
-
expect(nav.stack.value).toBe(
|
|
220
|
+
expect(getEntries().at(-1)).toBe("/profile null@1");
|
|
221
|
+
expect(nav.stack.value).toBe(null);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test("move back into the floating stack when forward names it", () => {
|
|
225
|
+
const nav = setup("/home");
|
|
226
|
+
nav.forward("/login", null);
|
|
227
|
+
expect(nav.stack.value).toBe(null);
|
|
228
|
+
expect(getEntries().at(-1)).toBe("/login null@0");
|
|
183
229
|
});
|
|
184
230
|
|
|
185
231
|
test("move into a stack when forward names it", () => {
|
|
@@ -199,7 +245,7 @@ describe("NavigationStore", () => {
|
|
|
199
245
|
test("move onto the top of a stack when a replace names it", () => {
|
|
200
246
|
const nav = setup("/home");
|
|
201
247
|
nav.forward("/home/post/1");
|
|
202
|
-
nav.forward("/login",
|
|
248
|
+
nav.forward("/login", null);
|
|
203
249
|
nav.replace("/home/post/2", "/home");
|
|
204
250
|
expect(getEntries().at(-1)).toBe("/home/post/2 /home@2");
|
|
205
251
|
expect(nav.history("/home")).toEqual(["/home", "/home/post/1", "/home/post/2"]);
|
|
@@ -219,6 +265,13 @@ describe("NavigationStore", () => {
|
|
|
219
265
|
});
|
|
220
266
|
|
|
221
267
|
describe("forward()", () => {
|
|
268
|
+
test("keeps the root below the first page of an empty stack", () => {
|
|
269
|
+
const nav = setup("/login");
|
|
270
|
+
nav.forward("/discover/fitness", "/discover");
|
|
271
|
+
expect(getEntries().at(-1)).toBe("/discover/fitness /discover@1");
|
|
272
|
+
expect(nav.history("/discover")).toEqual(["/discover", "/discover/fitness"]);
|
|
273
|
+
});
|
|
274
|
+
|
|
222
275
|
test("pushes one level deeper in the current stack", () => {
|
|
223
276
|
const nav = setup();
|
|
224
277
|
nav.forward("/home/post/1");
|
|
@@ -374,11 +427,25 @@ describe("NavigationStore", () => {
|
|
|
374
427
|
});
|
|
375
428
|
});
|
|
376
429
|
|
|
430
|
+
test("path is the current path relative to the root", () => {
|
|
431
|
+
const nav = setup("/home");
|
|
432
|
+
nav.forward("/home/post/1?x=1");
|
|
433
|
+
expect(nav.path).toBe("/home/post/1");
|
|
434
|
+
});
|
|
435
|
+
|
|
377
436
|
test("position has the depth of each stack's top page and the parent", () => {
|
|
378
437
|
const nav = setup();
|
|
379
438
|
nav.forward("/home/post/1");
|
|
380
439
|
nav.forward("/profile", "/profile");
|
|
381
440
|
nav.forward("/home/post/1", "/home");
|
|
382
|
-
expect(nav.position).toEqual({
|
|
441
|
+
expect(nav.position).toEqual({
|
|
442
|
+
stack: "/home",
|
|
443
|
+
depth: 1,
|
|
444
|
+
tops: new Map([
|
|
445
|
+
["/home", 1],
|
|
446
|
+
["/profile", 0],
|
|
447
|
+
]),
|
|
448
|
+
parent: "/home",
|
|
449
|
+
});
|
|
383
450
|
});
|
|
384
451
|
});
|