@charcuterie/ui 3.29.0 → 3.30.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/dist/Main/Main.d.ts +35 -1
- package/dist/Main/Main.d.ts.map +1 -1
- package/dist/Main/Main.js +38 -3
- package/dist/Main/Main.js.map +1 -1
- package/dist/Main/scrollMemory.d.ts +40 -0
- package/dist/Main/scrollMemory.d.ts.map +1 -0
- package/dist/Main/scrollMemory.js +63 -0
- package/dist/Main/scrollMemory.js.map +1 -0
- package/dist/Main/useScrollMemory.d.ts +29 -0
- package/dist/Main/useScrollMemory.d.ts.map +1 -0
- package/dist/Main/useScrollMemory.js +131 -0
- package/dist/Main/useScrollMemory.js.map +1 -0
- package/dist/SearchInput/SearchInput.d.ts +3 -1
- package/dist/SearchInput/SearchInput.d.ts.map +1 -1
- package/dist/SearchInput/SearchInput.js +0 -1
- package/dist/SearchInput/SearchInput.js.map +1 -1
- package/package.json +1 -1
- package/src/Main/Main.mdx +53 -0
- package/src/Main/Main.stories.tsx +152 -0
- package/src/Main/Main.test.tsx +195 -4
- package/src/Main/Main.tsx +58 -1
- package/src/Main/scrollMemory.test.ts +59 -0
- package/src/Main/scrollMemory.ts +73 -0
- package/src/Main/useScrollMemory.ts +164 -0
- package/src/SearchInput/SearchInput.mdx +3 -0
- package/src/SearchInput/SearchInput.stories.tsx +2 -2
- package/src/SearchInput/SearchInput.tsx +5 -3
package/dist/Main/Main.d.ts
CHANGED
|
@@ -2,6 +2,16 @@ import { type ComponentPropsWithRef, type ReactNode } from "react";
|
|
|
2
2
|
import { type ContentWidth } from "../Shell/contentWidth.ts";
|
|
3
3
|
export type MainProps = ComponentPropsWithRef<"main"> & {
|
|
4
4
|
contentWidth?: ContentWidth;
|
|
5
|
+
/**
|
|
6
|
+
* The history entry this page belongs to — react-router's
|
|
7
|
+
* `useLocation().key`, and the same value under a different
|
|
8
|
+
* router. Given it, `<main>` remembers where each entry was
|
|
9
|
+
* scrolled to and puts the offset back on Back and Forward.
|
|
10
|
+
*
|
|
11
|
+
* Omit it and nothing is remembered, which is the right default
|
|
12
|
+
* for a page with no history to remember.
|
|
13
|
+
*/
|
|
14
|
+
scrollKey?: string;
|
|
5
15
|
};
|
|
6
16
|
/**
|
|
7
17
|
* The `<main>` landmark and the content column.
|
|
@@ -80,6 +90,30 @@ export type MainProps = ComponentPropsWithRef<"main"> & {
|
|
|
80
90
|
* an app's own fixed chrome belongs in `Shell`, beside `Main`
|
|
81
91
|
* rather than in it.
|
|
82
92
|
*
|
|
93
|
+
* ## It remembers where each history entry was scrolled to
|
|
94
|
+
*
|
|
95
|
+
* `Shell` gives this element the page's only vertical scrollport,
|
|
96
|
+
* so the header and the rails stay put while the content moves.
|
|
97
|
+
* The browser's own scroll restoration does not follow it there: a
|
|
98
|
+
* browser remembers **the document scroller's** offset per history
|
|
99
|
+
* entry, and `<main>` is not the document scroller. It also does
|
|
100
|
+
* not restore anything at all across a same-document `pushState`
|
|
101
|
+
* navigation, which is every link press in a single-page app. Back
|
|
102
|
+
* therefore lands at the top of a list the reader had scrolled
|
|
103
|
+
* half-way down, and no browser setting changes that.
|
|
104
|
+
*
|
|
105
|
+
* Pass `scrollKey` — react-router's `useLocation().key` — and the
|
|
106
|
+
* offset comes back. `useScrollMemory.ts` holds the mechanism and
|
|
107
|
+
* `scrollMemory.ts` the reasoning.
|
|
108
|
+
*
|
|
109
|
+
* ```tsx
|
|
110
|
+
* const location = useLocation()
|
|
111
|
+
*
|
|
112
|
+
* <Main scrollKey={location.key}>
|
|
113
|
+
* <Outlet />
|
|
114
|
+
* </Main>
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
83
117
|
* ## Two more things it does, both one line
|
|
84
118
|
*
|
|
85
119
|
* `tabIndex={-1}` so `Shell`'s skip link moves **focus** and not
|
|
@@ -109,5 +143,5 @@ export type MainProps = ComponentPropsWithRef<"main"> & {
|
|
|
109
143
|
* wrap, so a wide one goes in an `overflow-x-auto` wrapper, which
|
|
110
144
|
* `Main.mdx` shows.
|
|
111
145
|
*/
|
|
112
|
-
export declare const Main: ({ children, className, contentWidth, id, ...mainProps }: MainProps) => ReactNode;
|
|
146
|
+
export declare const Main: ({ children, className, contentWidth, id, ref, scrollKey, ...mainProps }: MainProps) => ReactNode;
|
|
113
147
|
//# sourceMappingURL=Main.d.ts.map
|
package/dist/Main/Main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Main.d.ts","sourceRoot":"","sources":["../../src/Main/Main.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Main.d.ts","sourceRoot":"","sources":["../../src/Main/Main.tsx"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,qBAAqB,EAC1B,KAAK,SAAS,EAGf,MAAM,OAAO,CAAA;AAEd,OAAO,EACL,KAAK,YAAY,EAGlB,MAAM,0BAA0B,CAAA;AAKjC,MAAM,MAAM,SAAS,GAAG,qBAAqB,CAAC,MAAM,CAAC,GAAG;IACtD,YAAY,CAAC,EAAE,YAAY,CAAA;IAC3B;;;;;;;;OAQG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiIG;AACH,eAAO,MAAM,IAAI,GAAI,yEAQlB,SAAS,KAAG,SAyDd,CAAA"}
|
package/dist/Main/Main.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useUniqueId } from "@charcuterie/logic";
|
|
2
|
+
import { mergeRefs, useUniqueId, } from "@charcuterie/logic";
|
|
3
3
|
import { useContext, } from "react";
|
|
4
4
|
import { DEFAULT_CONTENT_WIDTH, toMaxInlineSize, } from "../Shell/contentWidth.js";
|
|
5
5
|
import { ShellContext } from "../Shell/shellContext.js";
|
|
6
6
|
import { toClassName } from "../toClassName.js";
|
|
7
|
+
import { useScrollMemory } from "./useScrollMemory.js";
|
|
7
8
|
/**
|
|
8
9
|
* The `<main>` landmark and the content column.
|
|
9
10
|
*
|
|
@@ -81,6 +82,30 @@ import { toClassName } from "../toClassName.js";
|
|
|
81
82
|
* an app's own fixed chrome belongs in `Shell`, beside `Main`
|
|
82
83
|
* rather than in it.
|
|
83
84
|
*
|
|
85
|
+
* ## It remembers where each history entry was scrolled to
|
|
86
|
+
*
|
|
87
|
+
* `Shell` gives this element the page's only vertical scrollport,
|
|
88
|
+
* so the header and the rails stay put while the content moves.
|
|
89
|
+
* The browser's own scroll restoration does not follow it there: a
|
|
90
|
+
* browser remembers **the document scroller's** offset per history
|
|
91
|
+
* entry, and `<main>` is not the document scroller. It also does
|
|
92
|
+
* not restore anything at all across a same-document `pushState`
|
|
93
|
+
* navigation, which is every link press in a single-page app. Back
|
|
94
|
+
* therefore lands at the top of a list the reader had scrolled
|
|
95
|
+
* half-way down, and no browser setting changes that.
|
|
96
|
+
*
|
|
97
|
+
* Pass `scrollKey` — react-router's `useLocation().key` — and the
|
|
98
|
+
* offset comes back. `useScrollMemory.ts` holds the mechanism and
|
|
99
|
+
* `scrollMemory.ts` the reasoning.
|
|
100
|
+
*
|
|
101
|
+
* ```tsx
|
|
102
|
+
* const location = useLocation()
|
|
103
|
+
*
|
|
104
|
+
* <Main scrollKey={location.key}>
|
|
105
|
+
* <Outlet />
|
|
106
|
+
* </Main>
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
84
109
|
* ## Two more things it does, both one line
|
|
85
110
|
*
|
|
86
111
|
* `tabIndex={-1}` so `Shell`'s skip link moves **focus** and not
|
|
@@ -110,14 +135,24 @@ import { toClassName } from "../toClassName.js";
|
|
|
110
135
|
* wrap, so a wide one goes in an `overflow-x-auto` wrapper, which
|
|
111
136
|
* `Main.mdx` shows.
|
|
112
137
|
*/
|
|
113
|
-
export const Main = ({ children, className, contentWidth, id, ...mainProps }) => {
|
|
138
|
+
export const Main = ({ children, className, contentWidth, id, ref, scrollKey, ...mainProps }) => {
|
|
114
139
|
const shell = useContext(ShellContext);
|
|
140
|
+
const setScrollport = useScrollMemory(scrollKey);
|
|
115
141
|
// Always called, never conditionally: a `Main` outside a `Shell`
|
|
116
142
|
// still needs an id an app can point its own skip link at.
|
|
117
143
|
const fallbackId = useUniqueId();
|
|
118
144
|
const maxInlineSize = toMaxInlineSize(contentWidth ??
|
|
119
145
|
shell?.contentWidth ??
|
|
120
146
|
DEFAULT_CONTENT_WIDTH);
|
|
121
|
-
return (_jsx("main", { ...mainProps, className: toClassName("charcuterie-scrollbar col-start-1 row-start-3 min-h-0 min-w-0 overflow-y-auto md:col-start-2 md:row-start-2", className), id: id ?? shell?.mainId ?? fallbackId,
|
|
147
|
+
return (_jsx("main", { ...mainProps, className: toClassName("charcuterie-scrollbar col-start-1 row-start-3 min-h-0 min-w-0 overflow-y-auto md:col-start-2 md:row-start-2", className), id: id ?? shell?.mainId ?? fallbackId,
|
|
148
|
+
// A caller's `ref` is a subscription, not a value, so the
|
|
149
|
+
// scroll memory composes with it rather than replacing it.
|
|
150
|
+
//
|
|
151
|
+
// The cast is `mergeRefs`' prop-merging signature, where
|
|
152
|
+
// every value is `unknown`; a caller's `ref` is narrower than
|
|
153
|
+
// that, never wider.
|
|
154
|
+
ref: ref
|
|
155
|
+
? mergeRefs(ref, setScrollport)
|
|
156
|
+
: setScrollport, tabIndex: -1, children: _jsx("div", { className: "@container mx-auto flex w-full min-w-0 flex-col gap-6 wrap-anywhere px-4 py-6 sm:px-6", style: { maxInlineSize }, children: children }) }));
|
|
122
157
|
};
|
|
123
158
|
//# sourceMappingURL=Main.js.map
|
package/dist/Main/Main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Main.js","sourceRoot":"","sources":["../../src/Main/Main.tsx"],"names":[],"mappings":";AA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ACrB,GAAG,EACD,GAAG;YACD,CAAC,CAAE,SAAS,CACR,GAAmB,EACnB,aAAa,CACe;YAChC,CAAC,CAAC,aAAa,EAEnB,QAAQ,EAAE,CAAC,CAAC,YAaZ,cACE,SAAS,EAAC,uFAAuF,EACjG,KAAK,EAAE,EAAE,aAAa,EAAE,YAEvB,QAAQ,GACL,GACD,CACR,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the browser will not remember for you.
|
|
3
|
+
*
|
|
4
|
+
* `Shell` gives `<main>` the page's only vertical scrollport, so
|
|
5
|
+
* the header and the rails stay put while the content moves —
|
|
6
|
+
* that is the whole reason `Shell.test.tsx` asserts "only main
|
|
7
|
+
* scrolls vertically, keeping the rail available".
|
|
8
|
+
*
|
|
9
|
+
* It costs exactly one thing, and this module is it. A browser
|
|
10
|
+
* remembers **the document scroller's** offset per history entry
|
|
11
|
+
* and puts it back on Back. It does that for a cross-document
|
|
12
|
+
* load, and `history.scrollRestoration` is the switch for it. It
|
|
13
|
+
* does **not** do it for an arbitrary element with
|
|
14
|
+
* `overflow-y: auto`, and it does not do it at all for a
|
|
15
|
+
* same-document `pushState` navigation, which is every link press
|
|
16
|
+
* in a single-page app. So a page that scrolls its own `<main>`
|
|
17
|
+
* loses the reader's place on Back, and no browser setting brings
|
|
18
|
+
* it back:
|
|
19
|
+
*
|
|
20
|
+
* > *"If I scroll, click on an item, then go back, it resets me to
|
|
21
|
+
* > the top. […] It should remember where the scroll position was
|
|
22
|
+
* > previously. Isn't this handled by Chrome already?"*
|
|
23
|
+
*
|
|
24
|
+
* The offsets are held **in memory**, not in `sessionStorage`. A
|
|
25
|
+
* history key survives a reload, but the DOM it described does
|
|
26
|
+
* not: the app re-fetches, re-measures and often re-orders, so a
|
|
27
|
+
* pixel offset from before the reload points at a different row.
|
|
28
|
+
* Restoring a stale number is worse than restoring nothing,
|
|
29
|
+
* because it looks deliberate.
|
|
30
|
+
*/
|
|
31
|
+
export declare const rememberScrollOffset: (key: string, offset: number) => void;
|
|
32
|
+
/**
|
|
33
|
+
* `0` for an entry nobody has scrolled, which is also the right
|
|
34
|
+
* answer for an entry nobody has *seen* — a link press lands at
|
|
35
|
+
* the top of the new page.
|
|
36
|
+
*/
|
|
37
|
+
export declare const recallScrollOffset: (key: string) => number;
|
|
38
|
+
/** Test-only. Module state outlives a mount; a test must not. */
|
|
39
|
+
export declare const forgetScrollOffsets: () => void;
|
|
40
|
+
//# sourceMappingURL=scrollMemory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scrollMemory.d.ts","sourceRoot":"","sources":["../../src/Main/scrollMemory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAaH,eAAO,MAAM,oBAAoB,GAC/B,KAAK,MAAM,EACX,QAAQ,MAAM,SAef,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,GAAI,KAAK,MAAM,WACb,CAAA;AAEjC,iEAAiE;AACjE,eAAO,MAAM,mBAAmB,YAE/B,CAAA"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the browser will not remember for you.
|
|
3
|
+
*
|
|
4
|
+
* `Shell` gives `<main>` the page's only vertical scrollport, so
|
|
5
|
+
* the header and the rails stay put while the content moves —
|
|
6
|
+
* that is the whole reason `Shell.test.tsx` asserts "only main
|
|
7
|
+
* scrolls vertically, keeping the rail available".
|
|
8
|
+
*
|
|
9
|
+
* It costs exactly one thing, and this module is it. A browser
|
|
10
|
+
* remembers **the document scroller's** offset per history entry
|
|
11
|
+
* and puts it back on Back. It does that for a cross-document
|
|
12
|
+
* load, and `history.scrollRestoration` is the switch for it. It
|
|
13
|
+
* does **not** do it for an arbitrary element with
|
|
14
|
+
* `overflow-y: auto`, and it does not do it at all for a
|
|
15
|
+
* same-document `pushState` navigation, which is every link press
|
|
16
|
+
* in a single-page app. So a page that scrolls its own `<main>`
|
|
17
|
+
* loses the reader's place on Back, and no browser setting brings
|
|
18
|
+
* it back:
|
|
19
|
+
*
|
|
20
|
+
* > *"If I scroll, click on an item, then go back, it resets me to
|
|
21
|
+
* > the top. […] It should remember where the scroll position was
|
|
22
|
+
* > previously. Isn't this handled by Chrome already?"*
|
|
23
|
+
*
|
|
24
|
+
* The offsets are held **in memory**, not in `sessionStorage`. A
|
|
25
|
+
* history key survives a reload, but the DOM it described does
|
|
26
|
+
* not: the app re-fetches, re-measures and often re-orders, so a
|
|
27
|
+
* pixel offset from before the reload points at a different row.
|
|
28
|
+
* Restoring a stale number is worse than restoring nothing,
|
|
29
|
+
* because it looks deliberate.
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* How many history entries keep an offset. A reader goes back
|
|
33
|
+
* through a handful of screens, never fifty, so this is a ceiling
|
|
34
|
+
* against a long session rather than a working set — the map holds
|
|
35
|
+
* one small number per entry and evicts the least recently
|
|
36
|
+
* written.
|
|
37
|
+
*/
|
|
38
|
+
const REMEMBERED_ENTRY_LIMIT = 50;
|
|
39
|
+
const rememberedOffsets = new Map();
|
|
40
|
+
export const rememberScrollOffset = (key, offset) => {
|
|
41
|
+
// Delete before set, so insertion order is least-recent-first
|
|
42
|
+
// and the eviction below takes the right one. A plain `set` on
|
|
43
|
+
// an existing key keeps its original position.
|
|
44
|
+
rememberedOffsets.delete(key);
|
|
45
|
+
rememberedOffsets.set(key, offset);
|
|
46
|
+
for (const oldestKey of rememberedOffsets.keys()) {
|
|
47
|
+
if (rememberedOffsets.size <= REMEMBERED_ENTRY_LIMIT) {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
rememberedOffsets.delete(oldestKey);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* `0` for an entry nobody has scrolled, which is also the right
|
|
55
|
+
* answer for an entry nobody has *seen* — a link press lands at
|
|
56
|
+
* the top of the new page.
|
|
57
|
+
*/
|
|
58
|
+
export const recallScrollOffset = (key) => rememberedOffsets.get(key) ?? 0;
|
|
59
|
+
/** Test-only. Module state outlives a mount; a test must not. */
|
|
60
|
+
export const forgetScrollOffsets = () => {
|
|
61
|
+
rememberedOffsets.clear();
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=scrollMemory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scrollMemory.js","sourceRoot":"","sources":["../../src/Main/scrollMemory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH;;;;;;GAMG;AACH,MAAM,sBAAsB,GAAG,EAAE,CAAA;AAEjC,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAkB,CAAA;AAEnD,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAClC,GAAW,EACX,MAAc,EACd,EAAE;IACF,8DAA8D;IAC9D,+DAA+D;IAC/D,+CAA+C;IAC/C,iBAAiB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IAC7B,iBAAiB,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;IAElC,KAAK,MAAM,SAAS,IAAI,iBAAiB,CAAC,IAAI,EAAE,EAAE,CAAC;QACjD,IAAI,iBAAiB,CAAC,IAAI,IAAI,sBAAsB,EAAE,CAAC;YACrD,MAAK;QACP,CAAC;QAED,iBAAiB,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACrC,CAAC;AACH,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,GAAW,EAAE,EAAE,CAChD,iBAAiB,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;AAEjC,iEAAiE;AACjE,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,EAAE;IACtC,iBAAiB,CAAC,KAAK,EAAE,CAAA;AAC3B,CAAC,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remember where this history entry was scrolled to, and put it
|
|
3
|
+
* back when the reader returns to it.
|
|
4
|
+
*
|
|
5
|
+
* `scrollMemory.ts` says why the browser will not. This is the
|
|
6
|
+
* part that has to survive the two things a real page does:
|
|
7
|
+
*
|
|
8
|
+
* 1. **The content arrives late.** An offset applied against a
|
|
9
|
+
* 600px skeleton clamps to `0` and looks like it did nothing.
|
|
10
|
+
* A `ResizeObserver` on the scrollport and on its content
|
|
11
|
+
* column re-applies the offset every time either grows, until
|
|
12
|
+
* the offset lands or the window closes.
|
|
13
|
+
* 2. **The reader scrolls during all that.** Any wheel, touch,
|
|
14
|
+
* key or pointer press ends the restore immediately. The
|
|
15
|
+
* alternative is a page that jumps out from under somebody who
|
|
16
|
+
* has already started reading.
|
|
17
|
+
*
|
|
18
|
+
* The save is a passive `scroll` listener, suppressed while a
|
|
19
|
+
* restore is in flight. Without that suppression the feature eats
|
|
20
|
+
* itself: writing `scrollTop` on a still-short page clamps to `0`
|
|
21
|
+
* and fires a `scroll` event reading `0`, which would be saved
|
|
22
|
+
* over the very offset being restored.
|
|
23
|
+
*
|
|
24
|
+
* @param scrollKey Identifies the history entry — react-router's
|
|
25
|
+
* `useLocation().key`. `undefined` turns the memory off, which is
|
|
26
|
+
* what an app with no router gets.
|
|
27
|
+
*/
|
|
28
|
+
export declare const useScrollMemory: (scrollKey: string | undefined) => (node: unknown) => void;
|
|
29
|
+
//# sourceMappingURL=useScrollMemory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useScrollMemory.d.ts","sourceRoot":"","sources":["../../src/Main/useScrollMemory.ts"],"names":[],"mappings":"AA6BA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,eAAe,GAC1B,WAAW,MAAM,GAAG,SAAS,YAaS,OAAO,SA6F9C,CAAA"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useRef } from "react";
|
|
2
|
+
import { recallScrollOffset, rememberScrollOffset, } from "./scrollMemory.js";
|
|
3
|
+
/**
|
|
4
|
+
* How long a restore keeps trying while the content is still
|
|
5
|
+
* arriving.
|
|
6
|
+
*
|
|
7
|
+
* The offset cannot be applied until the scrollport is tall enough
|
|
8
|
+
* to hold it, and on a client-rendered page it is not: the commit
|
|
9
|
+
* that changes the route draws a skeleton, and the rows land a
|
|
10
|
+
* fetch later. So the restore re-applies as the content grows, and
|
|
11
|
+
* gives up after this. A page that takes longer than three seconds
|
|
12
|
+
* to fill has already been read from the top, and moving it then
|
|
13
|
+
* is a worse answer than leaving it where the reader put it.
|
|
14
|
+
*/
|
|
15
|
+
const RESTORE_WINDOW_MS = 3000;
|
|
16
|
+
/** A scroll the reader asked for. It ends the restore at once. */
|
|
17
|
+
const READER_INTENT_EVENTS = [
|
|
18
|
+
"keydown",
|
|
19
|
+
"pointerdown",
|
|
20
|
+
"touchstart",
|
|
21
|
+
"wheel",
|
|
22
|
+
];
|
|
23
|
+
/**
|
|
24
|
+
* Remember where this history entry was scrolled to, and put it
|
|
25
|
+
* back when the reader returns to it.
|
|
26
|
+
*
|
|
27
|
+
* `scrollMemory.ts` says why the browser will not. This is the
|
|
28
|
+
* part that has to survive the two things a real page does:
|
|
29
|
+
*
|
|
30
|
+
* 1. **The content arrives late.** An offset applied against a
|
|
31
|
+
* 600px skeleton clamps to `0` and looks like it did nothing.
|
|
32
|
+
* A `ResizeObserver` on the scrollport and on its content
|
|
33
|
+
* column re-applies the offset every time either grows, until
|
|
34
|
+
* the offset lands or the window closes.
|
|
35
|
+
* 2. **The reader scrolls during all that.** Any wheel, touch,
|
|
36
|
+
* key or pointer press ends the restore immediately. The
|
|
37
|
+
* alternative is a page that jumps out from under somebody who
|
|
38
|
+
* has already started reading.
|
|
39
|
+
*
|
|
40
|
+
* The save is a passive `scroll` listener, suppressed while a
|
|
41
|
+
* restore is in flight. Without that suppression the feature eats
|
|
42
|
+
* itself: writing `scrollTop` on a still-short page clamps to `0`
|
|
43
|
+
* and fires a `scroll` event reading `0`, which would be saved
|
|
44
|
+
* over the very offset being restored.
|
|
45
|
+
*
|
|
46
|
+
* @param scrollKey Identifies the history entry — react-router's
|
|
47
|
+
* `useLocation().key`. `undefined` turns the memory off, which is
|
|
48
|
+
* what an app with no router gets.
|
|
49
|
+
*/
|
|
50
|
+
export const useScrollMemory = (scrollKey) => {
|
|
51
|
+
const elementRef = useRef(null);
|
|
52
|
+
const isRestoringRef = useRef(false);
|
|
53
|
+
// A ref rather than a query for the `<main>` id: the effect
|
|
54
|
+
// below runs in the same commit that mounts the node, and only a
|
|
55
|
+
// ref can say which node that was.
|
|
56
|
+
//
|
|
57
|
+
// `unknown` in, narrowed here, so the returned function is both a
|
|
58
|
+
// `RefCallback<HTMLElement>` and something `mergeRefs` accepts —
|
|
59
|
+
// that library's merge is typed for the prop-merging path, where
|
|
60
|
+
// every value is `unknown`.
|
|
61
|
+
const setElement = useCallback((node) => {
|
|
62
|
+
elementRef.current =
|
|
63
|
+
node instanceof HTMLElement ? node : null;
|
|
64
|
+
}, []);
|
|
65
|
+
useLayoutEffect(() => {
|
|
66
|
+
const element = elementRef.current;
|
|
67
|
+
if (!element || scrollKey === undefined) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
const offset = recallScrollOffset(scrollKey);
|
|
71
|
+
// The arrow defers the read of `applyOffset`, so the three
|
|
72
|
+
// pieces can be declared in the order they depend on each
|
|
73
|
+
// other rather than in the order they are used.
|
|
74
|
+
const observer = new ResizeObserver(() => {
|
|
75
|
+
applyOffset();
|
|
76
|
+
});
|
|
77
|
+
const deadline = globalThis.setTimeout(() => {
|
|
78
|
+
stopRestoring();
|
|
79
|
+
}, RESTORE_WINDOW_MS);
|
|
80
|
+
const stopRestoring = () => {
|
|
81
|
+
if (!isRestoringRef.current) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
isRestoringRef.current = false;
|
|
85
|
+
observer.disconnect();
|
|
86
|
+
globalThis.clearTimeout(deadline);
|
|
87
|
+
for (const eventName of READER_INTENT_EVENTS) {
|
|
88
|
+
element.removeEventListener(eventName, stopRestoring);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const applyOffset = () => {
|
|
92
|
+
if (!isRestoringRef.current) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
element.scrollTop = offset;
|
|
96
|
+
// Landed. Anything further would only fight the reader.
|
|
97
|
+
if (element.scrollTop === offset) {
|
|
98
|
+
stopRestoring();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const onScroll = () => {
|
|
102
|
+
if (isRestoringRef.current) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
rememberScrollOffset(scrollKey, element.scrollTop);
|
|
106
|
+
};
|
|
107
|
+
isRestoringRef.current = true;
|
|
108
|
+
for (const eventName of READER_INTENT_EVENTS) {
|
|
109
|
+
element.addEventListener(eventName, stopRestoring, {
|
|
110
|
+
passive: true,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
// The scrollport's own height changes with the window; its
|
|
114
|
+
// content column's changes when the rows land. Both move the
|
|
115
|
+
// ceiling the offset is clamped against.
|
|
116
|
+
observer.observe(element);
|
|
117
|
+
for (const child of Array.from(element.children)) {
|
|
118
|
+
observer.observe(child);
|
|
119
|
+
}
|
|
120
|
+
element.addEventListener("scroll", onScroll, {
|
|
121
|
+
passive: true,
|
|
122
|
+
});
|
|
123
|
+
applyOffset();
|
|
124
|
+
return () => {
|
|
125
|
+
stopRestoring();
|
|
126
|
+
element.removeEventListener("scroll", onScroll);
|
|
127
|
+
};
|
|
128
|
+
}, [scrollKey]);
|
|
129
|
+
return setElement;
|
|
130
|
+
};
|
|
131
|
+
//# sourceMappingURL=useScrollMemory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useScrollMemory.js","sourceRoot":"","sources":["../../src/Main/useScrollMemory.ts"],"names":[],"mappings":"AAAA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A;QAEzB,KAAK,MAAM,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;YACjD,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACzB,CAAC;QAED,OAAO,CAAC,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,EAAE;YAC3C,OAAO,EAAE,IAAI;SACd,CAAC,CAAA;QAEF,WAAW,EAAE,CAAA;QAEb,OAAO,GAAG,EAAE;YACV,aAAa,EAAE,CAAA;YACf,OAAO,CAAC,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAA;QACjD,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAA;IAEf,OAAO,UAAU,CAAA;AACnB,CAAC,CAAA"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ControlSize } from "@charcuterie/tokens";
|
|
2
2
|
import { type ComponentPropsWithRef, type ReactNode } from "react";
|
|
3
|
+
export type SearchInputSize = Exclude<ControlSize, "sm">;
|
|
3
4
|
export type SearchInputProps = Omit<ComponentPropsWithRef<"input">, "size" | "type"> & {
|
|
4
5
|
/** The app-owned glyph inside the clear button. */
|
|
5
6
|
clearIcon: ReactNode;
|
|
@@ -7,7 +8,8 @@ export type SearchInputProps = Omit<ComponentPropsWithRef<"input">, "size" | "ty
|
|
|
7
8
|
/** Override value-derived visibility for an uncontrolled input. */
|
|
8
9
|
isClearVisible?: boolean;
|
|
9
10
|
onClear: () => void;
|
|
10
|
-
size
|
|
11
|
+
/** Search and clear targets are never smaller than the medium control size. */
|
|
12
|
+
size?: SearchInputSize;
|
|
11
13
|
};
|
|
12
14
|
/**
|
|
13
15
|
* A search field whose clear affordance is a real, labelled icon
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SearchInput.d.ts","sourceRoot":"","sources":["../../src/SearchInput/SearchInput.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACtD,OAAO,EACL,KAAK,qBAAqB,EAC1B,KAAK,SAAS,EAEf,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"SearchInput.d.ts","sourceRoot":"","sources":["../../src/SearchInput/SearchInput.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACtD,OAAO,EACL,KAAK,qBAAqB,EAC1B,KAAK,SAAS,EAEf,MAAM,OAAO,CAAA;AASd,MAAM,MAAM,eAAe,GAAG,OAAO,CAAC,WAAW,EAAE,IAAI,CAAC,CAAA;AAOxD,MAAM,MAAM,gBAAgB,GAAG,IAAI,CACjC,qBAAqB,CAAC,OAAO,CAAC,EAC9B,MAAM,GAAG,MAAM,CAChB,GAAG;IACF,mDAAmD;IACnD,SAAS,EAAE,SAAS,CAAA;IACpB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,mEAAmE;IACnE,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,+EAA+E;IAC/E,IAAI,CAAC,EAAE,eAAe,CAAA;CACvB,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,WAAW,GAAI,8GAWzB,gBAAgB,KAAG,SAmDrB,CAAA"}
|
|
@@ -4,7 +4,6 @@ import { IconButton } from "../IconButton/IconButton.js";
|
|
|
4
4
|
import { DISABLED_CLASS, FOCUS_RING_CLASS, } from "../intentStyles.js";
|
|
5
5
|
import { toClassName } from "../toClassName.js";
|
|
6
6
|
const INPUT_SIZE_CLASS = {
|
|
7
|
-
sm: "h-(--control-height-sm) ps-(--control-padding-inline-sm) pe-(--control-height-sm) text-sm",
|
|
8
7
|
md: "h-(--control-height-md) ps-(--control-padding-inline-md) pe-(--control-height-md) text-md",
|
|
9
8
|
lg: "h-(--control-height-lg) ps-(--control-padding-inline-lg) pe-(--control-height-lg) text-lg",
|
|
10
9
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SearchInput.js","sourceRoot":"","sources":["../../src/SearchInput/SearchInput.tsx"],"names":[],"mappings":";AACA,OAAO,EAGL,MAAM,GACP,MAAM,OAAO,CAAA;AAEd,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA8B,CAAA;AACzD,OAAO,EACL,cAAc,EACd,gBAAgB,GACjB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"SearchInput.js","sourceRoot":"","sources":["../../src/SearchInput/SearchInput.tsx"],"names":[],"mappings":";AACA,OAAO,EAGL,MAAM,GACP,MAAM,OAAO,CAAA;AAEd,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA8B,CAAA;AACzD,OAAO,EACL,cAAc,EACd,gBAAgB,GACjB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAI/C,MAAM,gBAAgB,GAAoC;IACxD,EAAE,EAAE,2FAA2F;IAC/F,EAAE,EAAE,2FAA2F;CAChG,CAAA;AAgBD;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,EAC1B,SAAS,EACT,SAAS,EACT,UAAU,GAAG,cAAc,EAC3B,YAAY,EACZ,cAAc,EACd,OAAO,EACP,GAAG,EACH,IAAI,GAAG,IAAI,EACX,KAAK,EACL,GAAG,UAAU,EACI,EAAa,EAAE;IAChC,MAAM,QAAQ,GAAG,MAAM,CAAmB,IAAI,CAAC,CAAA;IAC/C,MAAM,QAAQ,GACZ,cAAc;QACd,MAAM,CAAC,KAAK,IAAI,YAAY,IAAI,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAA;IAEhD,MAAM,WAAW,GAAG,CAAC,IAA6B,EAAE,EAAE;QACpD,QAAQ,CAAC,OAAO,GAAG,IAAI,CAAA;QAEvB,IAAI,OAAO,GAAG,KAAK,UAAU;YAAE,GAAG,CAAC,IAAI,CAAC,CAAA;aACnC,IAAI,GAAG;YAAE,GAAG,CAAC,OAAO,GAAG,IAAI,CAAA;IAClC,CAAC,CAAA;IAED,OAAO,CACL,gBACE,SAAS,EAAE,WAAW,CAAC,gBAAgB,EAAE,SAAS,CAAC,aAEnD,mBACM,UAAU,EACd,SAAS,EAAE,WAAW,CACpB,+TAA+T,EAC/T,gBAAgB,CAAC,IAAI,CAAC,EACtB,gBAAgB,EAChB,cAAc,CACf,EACD,YAAY,EAAE,YAAY,EAC1B,GAAG,EAAE,WAAW,EAChB,IAAI,EAAC,QAAQ,EACb,KAAK,EAAE,KAAK,GACZ,EAED,QAAQ,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAClC,eAAM,SAAS,EAAC,4CAA4C,YAC1D,KAAC,UAAU,IACT,UAAU,EAAC,OAAO,EAClB,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,GAAG,EAAE;wBACZ,OAAO,EAAE,CAAA;wBACT,QAAQ,CAAC,OAAO,EAAE,KAAK,EAAE,CAAA;oBAC3B,CAAC,EACD,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE,CACvB,KAAK,CAAC,cAAc,EAAE,EAExB,IAAI,EAAE,IAAI,YAET,SAAS,GACC,GACR,CACR,CAAC,CAAC,CAAC,IAAI,IACH,CACR,CAAA;AACH,CAAC,CAAA"}
|
package/package.json
CHANGED
package/src/Main/Main.mdx
CHANGED
|
@@ -147,3 +147,56 @@ region, so the table is announced as a thing rather than as loose content.
|
|
|
147
147
|
`<main>` accepts focus so `Shell`'s skip link moves **focus** and not merely the scroll
|
|
148
148
|
position. Without it, Safari and Firefox scroll to `<main>` and leave the next Tab back up
|
|
149
149
|
in the header — which looks exactly like a skip link that works.
|
|
150
|
+
|
|
151
|
+
## `scrollKey` — the reader's place, kept
|
|
152
|
+
|
|
153
|
+
<Canvas of={MainStories.ScrollMemory} />
|
|
154
|
+
|
|
155
|
+
`Shell` gives this element the page's only vertical scrollport, so the header and the rails
|
|
156
|
+
stay put while the content moves. That is what `Shell.test.tsx` asserts, and this is what it
|
|
157
|
+
costs.
|
|
158
|
+
|
|
159
|
+
**The browser will not put the offset back.** A browser remembers the **document scroller's**
|
|
160
|
+
position per history entry, and `<main>` is not the document scroller. It also restores
|
|
161
|
+
nothing at all across a same-document `pushState` navigation, which is every link press in a
|
|
162
|
+
single-page app. So Back lands at the top of a list the reader had scrolled half-way down,
|
|
163
|
+
`history.scrollRestoration` has no bearing on it, and there is no browser setting that
|
|
164
|
+
changes it. The owner, on Docket:
|
|
165
|
+
|
|
166
|
+
> "If I scroll, click on an item, then go back, it resets me to the top. […] It should
|
|
167
|
+
> remember where the scroll position was previously. Isn't this handled by Chrome already?"
|
|
168
|
+
|
|
169
|
+
Pass the history entry's key and it is:
|
|
170
|
+
|
|
171
|
+
```tsx
|
|
172
|
+
const location = useLocation()
|
|
173
|
+
|
|
174
|
+
<Main scrollKey={location.key}>
|
|
175
|
+
<Outlet />
|
|
176
|
+
</Main>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Omit it and nothing is remembered, which is right for a page with no history to remember.
|
|
180
|
+
|
|
181
|
+
### It has to wait for the content
|
|
182
|
+
|
|
183
|
+
<Canvas of={MainStories.ScrollMemoryWithLateContent} />
|
|
184
|
+
|
|
185
|
+
A one-shot `scrollTop = offset` does **nothing** on a real page. The commit that changes the
|
|
186
|
+
route draws a skeleton, the rows land a fetch later, and an offset written against an empty
|
|
187
|
+
scrollport is clamped to `0` by the browser. Every check a one-shot restore could make still
|
|
188
|
+
passes — the offset was remembered, the write happened — and the reader is looking at the top
|
|
189
|
+
of the list.
|
|
190
|
+
|
|
191
|
+
So the restore re-applies through a `ResizeObserver` on the scrollport and on its content
|
|
192
|
+
column, and stops at the first of three things:
|
|
193
|
+
|
|
194
|
+
1. **The offset lands.** Anything past that would only fight the reader.
|
|
195
|
+
2. **The reader scrolls.** Any `wheel`, `touchstart`, `keydown` or `pointerdown` on the
|
|
196
|
+
scrollport ends it, because a page that jumps out from under somebody who has already
|
|
197
|
+
started reading is a worse bug than the one being fixed.
|
|
198
|
+
3. **Three seconds pass.** A page that slow has been read from the top already.
|
|
199
|
+
|
|
200
|
+
The offsets live in memory, not in `sessionStorage`. A history key survives a reload; the DOM
|
|
201
|
+
it described does not, and a pixel offset from before the reload points at a different row.
|
|
202
|
+
Restoring a stale number is worse than restoring nothing, because it looks deliberate.
|