@estiva-app/ui 0.119.0 → 0.120.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.
@@ -20,9 +20,22 @@ export declare const REVEAL_MS = 200;
20
20
  * `block: 'nearest'` does (a row is never that tall).
21
21
  */
22
22
  export declare function scrollToRow(row: HTMLElement): void;
23
+ /** For tests: every list forgets where it was. */
24
+ export declare function forgetListPlaces(): void;
25
+ /** How long a list drawn again keeps trying to reach where it was left, while its rows are still being drawn. */
26
+ export declare const RESTORE_MS = 2000;
27
+ /** A named list's place, for the reveal inside it: its name, and whether it is still going back to where it was. */
28
+ export interface ListPlace {
29
+ name?: string;
30
+ restoring: RefObject<boolean>;
31
+ }
32
+ /** The list a row is revealed in: a `ListColumn` gives its name to the tree inside it, which reveals nothing itself. */
33
+ export declare const ListPlaceContext: import("react").Context<ListPlace | null>;
23
34
  /**
24
- * Brings the current row inside `box` into sight, once per current row. `current` names it when the caller knows it
25
- * (`NavTree`'s `selected`); without it, the row is known by its link.
35
+ * Goes back to where the named list was left, and keeps note of where the reader leaves it. Its rows may still be
36
+ * drawing (a skeleton, a fold that reads its own rows): it tries again as the list grows, until it is there, the reader
37
+ * scrolls, something else moves it (a row brought into sight), or `RESTORE_MS` has passed.
26
38
  */
27
- export declare function useRevealCurrentRow(box: RefObject<HTMLElement | null>, current?: string): void;
39
+ export declare function useListPlace(box: RefObject<HTMLElement | null>, name: string | undefined): ListPlace;
40
+ export declare function useRevealCurrentRow(box: RefObject<HTMLElement | null>, current?: string, place?: ListPlace): void;
28
41
  //# sourceMappingURL=revealCurrentRow.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"revealCurrentRow.d.ts","sourceRoot":"","sources":["../src/revealCurrentRow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AAEzD;;;;;;;;;;;GAWG;AAEH,qGAAqG;AACrG,eAAO,MAAM,SAAS,MAAM,CAAA;AAE5B;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,WAAW,QAQ3C;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CA6B9F"}
1
+ {"version":3,"file":"revealCurrentRow.d.ts","sourceRoot":"","sources":["../src/revealCurrentRow.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0E,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AAE9G;;;;;;;;;;;GAWG;AAEH,qGAAqG;AACrG,eAAO,MAAM,SAAS,MAAM,CAAA;AAE5B;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,WAAW,QAK3C;AAyBD,kDAAkD;AAClD,wBAAgB,gBAAgB,IAAI,IAAI,CAEvC;AAED,iHAAiH;AACjH,eAAO,MAAM,UAAU,OAAO,CAAA;AAE9B,oHAAoH;AACpH,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,SAAS,CAAC,OAAO,CAAC,CAAA;CAC9B;AAED,wHAAwH;AACxH,eAAO,MAAM,gBAAgB,2CAAwC,CAAA;AAErE;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,CAwDpG;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,SAAS,GAAG,IAAI,CA0CjH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@estiva-app/ui",
3
- "version": "0.119.0",
3
+ "version": "0.120.0",
4
4
  "description": "Estiva's design tokens (the contract) and a small set of primitives (a convenience) for every Estiva app.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/registry.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "kind": "package",
5
5
  "repo": "estiva-ui",
6
6
  "package": "@estiva-app/ui",
7
- "packageVersion": "0.119.0",
7
+ "packageVersion": "0.120.0",
8
8
  "exports": 256,
9
9
  "typeExports": 270,
10
10
  "files": null
@@ -6709,6 +6709,12 @@
6709
6709
  "required": true,
6710
6710
  "note": "The rows, 2px apart. Groups are headed by a `CollapsibleSection` or a `SectionHeader`."
6711
6711
  },
6712
+ {
6713
+ "name": "rememberAs",
6714
+ "takes": "text",
6715
+ "required": false,
6716
+ "note": "The list's name in the app (`teams`, `dms`): where it was scrolled is remembered by it in this tab, and the same column drawn again (on another page) goes back there, its open row still brought into sight when it is a new one."
6717
+ },
6712
6718
  {
6713
6719
  "name": "className",
6714
6720
  "takes": "text",
@@ -65,6 +65,11 @@ import { ListColumn, Toolbar, ToolbarButton } from '@estiva-app/ui'
65
65
  side.
66
66
  - The rows are yours. It scrolls them, so a list that grows with its data is
67
67
  never cut off at the fold, even when today's data fits.
68
+ - Give the column its name in the app, `rememberAs` (`teams`, `dms`): where it
69
+ was scrolled is remembered by it in this tab, and the same column drawn again
70
+ on another page opens where the reader left it, not at the top. An open row it
71
+ already brought into sight is not brought back; a new one is. Without a name,
72
+ it opens at the top each time it is drawn.
68
73
  - **Planned:** dragging its edge to resize it, with `SplitLayout` in
69
74
  `@estiva-app/ui/layout` (migration stage 8). The 380px column beside a
70
75
  conversation gets the same treatment then.
@@ -132,7 +132,8 @@ function OpenRows({ open: first, late = false }: { open: number; late?: boolean
132
132
  {Array.from({ length: 80 }, (_, i) => (
133
133
  <NavItem
134
134
  key={i}
135
- href="#"
135
+ // Each its own address, as an app's rows are: a list knows its open row by it.
136
+ href={`#item-${i + 1}`}
136
137
  label={`Item ${i + 1}`}
137
138
  icon={icon}
138
139
  active={i === open}
@@ -169,6 +170,30 @@ export const OpenRowDrawnLate: Story = {
169
170
  ),
170
171
  }
171
172
 
173
+ /**
174
+ * Drawn again where it was left (`rememberAs`): scroll down, then "Another page" draws the column anew, as an app does
175
+ * when it moves to another page that shows the same column. It opens where you left it, not at the top, and its open
176
+ * row, already brought into sight once, is not brought back.
177
+ */
178
+ export const DrawnAgainWhereItWasLeft: Story = {
179
+ args: { title: 'Items' },
180
+ render: function Render(args) {
181
+ const [page, setPage] = useState(0)
182
+ return (
183
+ <div className="flex h-full">
184
+ <ListColumn key={page} {...args} rememberAs="story-items">
185
+ <OpenRows open={5} />
186
+ </ListColumn>
187
+ <div className="p-4">
188
+ <Button size="small" onClick={() => setPage((n) => n + 1)}>
189
+ Another page
190
+ </Button>
191
+ </div>
192
+ </div>
193
+ )
194
+ },
195
+ }
196
+
172
197
  /** While the list is on its way: a skeleton shaped like the rows. */
173
198
  export const Loading: Story = {
174
199
  args: { title: 'Items' },
@@ -1,6 +1,6 @@
1
1
  import { useRef, type ReactNode } from 'react'
2
2
  import { cn } from './cn'
3
- import { useRevealCurrentRow } from './revealCurrentRow'
3
+ import { ListPlaceContext, useListPlace, useRevealCurrentRow } from './revealCurrentRow'
4
4
  import { ContainerHeader } from './ContainerHeader'
5
5
  import { ErrorBoundary } from './ErrorBoundary'
6
6
  import { FOLDED_CLASSES, FOLD_MOTION_CLASSES, UNFOLDED_CLASSES } from './looks'
@@ -51,13 +51,20 @@ export interface ListColumnProps {
51
51
  collapsed?: boolean
52
52
  /** The rows, 2px apart. Groups are headed by a `CollapsibleSection` or a `SectionHeader`. */
53
53
  children: ReactNode
54
+ /**
55
+ * The list's name in the app (`teams`, `dms`): where it was scrolled is remembered by it in this tab, and the same
56
+ * column drawn again (on another page) goes back there, its open row still brought into sight when it is a new one.
57
+ * Without it, it opens at the top each time it is drawn.
58
+ */
59
+ rememberAs?: string
54
60
  /** Placement only. */
55
61
  className?: string
56
62
  }
57
63
 
58
- function Column({ title, chevron = false, titleMenu, actions, above, collapsed = false, children, className, broken = false }: ListColumnProps & { broken?: boolean }) {
64
+ function Column({ title, chevron = false, titleMenu, actions, above, collapsed = false, children, rememberAs, className, broken = false }: ListColumnProps & { broken?: boolean }) {
59
65
  const rows = useRef<HTMLDivElement>(null)
60
- useRevealCurrentRow(rows)
66
+ const place = useListPlace(rows, broken ? undefined : rememberAs)
67
+ useRevealCurrentRow(rows, undefined, place)
61
68
  return (
62
69
  <div
63
70
  className={cn(
@@ -78,7 +85,8 @@ function Column({ title, chevron = false, titleMenu, actions, above, collapsed =
78
85
  <div className="flex min-h-0 flex-1 flex-col px-3">{children}</div>
79
86
  ) : (
80
87
  <ScrollArea className="flex-1" viewportRef={rows} contentClassName="flex flex-col gap-0.5 px-3 pt-4 pb-3">
81
- {children}
88
+ {/* A tree in it leaves bringing its open row into sight to the column, which knows where it was left. */}
89
+ <ListPlaceContext.Provider value={place}>{children}</ListPlaceContext.Provider>
82
90
  </ScrollArea>
83
91
  )}
84
92
  </div>
@@ -6,12 +6,12 @@
6
6
  * back after the reader scrolls away.
7
7
  */
8
8
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
9
- import { act, cleanup, render } from '@testing-library/react'
9
+ import { act, cleanup, fireEvent, render } from '@testing-library/react'
10
10
  import { useEffect, useState } from 'react'
11
11
  import { ListColumn } from './ListColumn'
12
12
  import { NavItem } from './NavItem'
13
13
  import { NavTree } from './NavTree'
14
- import { REVEAL_MS } from './revealCurrentRow'
14
+ import { forgetListPlaces, REVEAL_MS } from './revealCurrentRow'
15
15
 
16
16
  afterEach(cleanup)
17
17
  beforeEach(() => vi.useFakeTimers())
@@ -166,3 +166,119 @@ describe('a list column brings its current row into sight (DMs, the Desk)', () =
166
166
  }
167
167
  })
168
168
  })
169
+
170
+ /*
171
+ The same column drawn again on another page (Peek's Teams column on a team's page, then on a topic's): it goes back
172
+ to where the reader left it, by the name the app gives it, and a row it already brought into sight is not brought
173
+ back (Katerina, 17 November: "I have scrolled down a little and then click on something and it scrolls up back to
174
+ top"). A new open row still comes into sight.
175
+ */
176
+ describe('a list column drawn again goes back to where it was left (rememberAs)', () => {
177
+ afterEach(() => forgetListPlaces())
178
+ function Named({ open, name }: { open: string; name?: string }) {
179
+ return (
180
+ <ListColumn title="DMs" {...(name ? { rememberAs: name } : {})}>
181
+ {Array.from({ length: 30 }, (_, i) => `Person ${i}`).map((person) => (
182
+ <NavItem key={person} href={`#${person}`} label={person} active={person === open} />
183
+ ))}
184
+ </ListColumn>
185
+ )
186
+ }
187
+ const viewport = () => document.querySelector<HTMLElement>('[data-id$="-viewport"]') ?? document.querySelector<HTMLElement>('[style*="overflow"]')!
188
+
189
+ /** Draws it, lets it bring its open row into sight, and the reader scrolls to `to`; then it is taken away. */
190
+ async function leaveAt(to: number, open = 'Person 25', name = 'dms') {
191
+ const first = render(<Named open={open} name={name} />)
192
+ const layout = lay(viewport(), { 'Person 25': 800, 'Person 2': 64 })
193
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
194
+ layout.scroll(to)
195
+ fireEvent.scroll(viewport())
196
+ first.unmount()
197
+ layout.restore()
198
+ }
199
+
200
+ it('drawn again under the same name, it opens where the reader left it, and does not pull them back to its open row', async () => {
201
+ await leaveAt(300)
202
+ render(<Named open="Person 25" name="dms" />)
203
+ expect(viewport().scrollTop).toBe(300)
204
+ const layout = lay(viewport(), { 'Person 25': 800, 'Person 2': 64 })
205
+ layout.scroll(300)
206
+ try {
207
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
208
+ expect(layout.scrolled()).toBe(300)
209
+ } finally {
210
+ layout.restore()
211
+ }
212
+ })
213
+
214
+ it('a new open row still comes into sight', async () => {
215
+ await leaveAt(300)
216
+ render(<Named open="Person 2" name="dms" />)
217
+ const layout = lay(viewport(), { 'Person 25': 800, 'Person 2': 64 })
218
+ layout.scroll(300)
219
+ try {
220
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
221
+ expect(layout.scrolled()).toBe(64)
222
+ } finally {
223
+ layout.restore()
224
+ }
225
+ })
226
+
227
+ it('a new open row is brought into sight at once, even when the place it was left at can no longer be reached', async () => {
228
+ await leaveAt(900)
229
+ // The list is shorter now (a fold that was open is shut): it scrolls no further than 300.
230
+ const tops = new WeakMap<HTMLElement, number>()
231
+ Object.defineProperty(HTMLElement.prototype, 'scrollTop', {
232
+ configurable: true,
233
+ get(this: HTMLElement) {
234
+ return tops.get(this) ?? 0
235
+ },
236
+ set(this: HTMLElement, next: number) {
237
+ tops.set(this, Math.max(0, Math.min(next, 300)))
238
+ },
239
+ })
240
+ try {
241
+ render(<Named open="Person 2" name="dms" />)
242
+ expect(viewport().scrollTop).toBe(300)
243
+ const layout = lay(viewport(), { 'Person 25': 800, 'Person 2': 64 })
244
+ layout.scroll(300)
245
+ try {
246
+ // At the reveal's moment, not once going back gives up (RESTORE_MS).
247
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
248
+ expect(layout.scrolled()).toBe(64)
249
+ } finally {
250
+ layout.restore()
251
+ }
252
+ } finally {
253
+ delete (HTMLElement.prototype as { scrollTop?: number }).scrollTop
254
+ }
255
+ })
256
+
257
+ // Smooth (Katerina, 17 November, "lets smooth it"): no stop at the old place first, then a move 200ms later.
258
+ it('drawn again with a new open row already drawn, it is at its one place as soon as it is drawn', async () => {
259
+ await leaveAt(300)
260
+ // Laid out before it is drawn: the box 100px tall at the page's top; Person 2 64px down the list, the rest at its top.
261
+ const box = (y: number, height: number) => ({ top: y, bottom: y + height, height, left: 0, right: 0, width: 0, x: 0, y, toJSON: () => ({}) }) as DOMRect
262
+ const rect = vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) {
263
+ const scroller = viewport()
264
+ if (this === scroller) return box(0, 100)
265
+ return this.textContent?.trim() === 'Person 2' ? box(64 - (scroller?.scrollTop ?? 0), 32) : box(0, 32)
266
+ })
267
+ try {
268
+ render(<Named open="Person 2" name="dms" />)
269
+ // Back at 300, the row is above the view: the least move shows it, before any timer has run.
270
+ expect(viewport().scrollTop).toBe(64)
271
+ } finally {
272
+ rect.mockRestore()
273
+ }
274
+ })
275
+
276
+ it('without a name, or under another, it opens at the top', async () => {
277
+ await leaveAt(300)
278
+ const unnamed = render(<Named open="Person 1" />)
279
+ expect(viewport().scrollTop).toBe(0)
280
+ unnamed.unmount()
281
+ render(<Named open="Person 1" name="desk" />)
282
+ expect(viewport().scrollTop).toBe(0)
283
+ })
284
+ })
@@ -1,4 +1,4 @@
1
- import { useEffect, useRef, type RefObject } from 'react'
1
+ import { createContext, useContext, useEffect, useLayoutEffect, useMemo, useRef, type RefObject } from 'react'
2
2
 
3
3
  /**
4
4
  * Showing where the open page lives in a list: its row (`aria-current="page"`, what `NavItem`'s `active` sets) brought
@@ -26,31 +26,140 @@ export function scrollToRow(row: HTMLElement) {
26
26
  let box = row.parentElement
27
27
  while (box && !(/(auto|scroll)/.test(getComputedStyle(box).overflowY) && box.scrollHeight > box.clientHeight)) box = box.parentElement
28
28
  if (!box || box === document.documentElement || box === document.body) return
29
+ scrollRowInto(box, row)
30
+ }
31
+
32
+ /** Brings a row into a box's sight by the least move: to its top edge from above, to its bottom edge from below. */
33
+ function scrollRowInto(box: HTMLElement, row: HTMLElement) {
29
34
  const at = row.getBoundingClientRect()
30
35
  const view = box.getBoundingClientRect()
31
36
  if (at.top < view.top) box.scrollTop -= view.top - at.top
32
37
  else if (at.bottom > view.bottom) box.scrollTop += Math.min(at.bottom - view.bottom, at.top - view.top)
33
38
  }
34
39
 
40
+ /** The key a list's open row is known by, from one drawing to the next: what a reveal compares. */
41
+ const keyOf = (row: HTMLElement, current?: string) => current ?? row.getAttribute('href') ?? row.textContent ?? ''
42
+
35
43
  /**
36
44
  * Brings the current row inside `box` into sight, once per current row. `current` names it when the caller knows it
37
45
  * (`NavTree`'s `selected`); without it, the row is known by its link.
38
46
  */
39
- export function useRevealCurrentRow(box: RefObject<HTMLElement | null>, current?: string): void {
47
+ /**
48
+ * Where each named list was left, in this tab: how far it was scrolled, and the open row it last brought into sight. A
49
+ * list drawn again under the same name (the same column on another page) goes back there, and does not show again a
50
+ * row it already showed (Katerina, 17 November: "I have scrolled down a little and then click on something and it
51
+ * scrolls up back to top").
52
+ */
53
+ const places = new Map<string, { top?: number; shown?: string }>()
54
+
55
+ /** For tests: every list forgets where it was. */
56
+ export function forgetListPlaces(): void {
57
+ places.clear()
58
+ }
59
+
60
+ /** How long a list drawn again keeps trying to reach where it was left, while its rows are still being drawn. */
61
+ export const RESTORE_MS = 2000
62
+
63
+ /** A named list's place, for the reveal inside it: its name, and whether it is still going back to where it was. */
64
+ export interface ListPlace {
65
+ name?: string
66
+ restoring: RefObject<boolean>
67
+ }
68
+
69
+ /** The list a row is revealed in: a `ListColumn` gives its name to the tree inside it, which reveals nothing itself. */
70
+ export const ListPlaceContext = createContext<ListPlace | null>(null)
71
+
72
+ /**
73
+ * Goes back to where the named list was left, and keeps note of where the reader leaves it. Its rows may still be
74
+ * drawing (a skeleton, a fold that reads its own rows): it tries again as the list grows, until it is there, the reader
75
+ * scrolls, something else moves it (a row brought into sight), or `RESTORE_MS` has passed.
76
+ */
77
+ export function useListPlace(box: RefObject<HTMLElement | null>, name: string | undefined): ListPlace {
78
+ const restoring = useRef(false)
79
+ useLayoutEffect(() => {
80
+ const el = box.current
81
+ if (!el || !name) return
82
+ const want = places.get(name)?.top ?? 0
83
+ restoring.current = want > 0
84
+ let set = el.scrollTop
85
+ const keep = () => places.set(name, { ...places.get(name), top: el.scrollTop })
86
+ const finish = () => {
87
+ restoring.current = false
88
+ }
89
+ const restore = () => {
90
+ if (!restoring.current) return
91
+ el.scrollTop = want
92
+ set = el.scrollTop
93
+ if (Math.abs(el.scrollTop - want) < 1) finish()
94
+ }
95
+ restore()
96
+ /*
97
+ A new open row already drawn is brought into sight in the same moment, before the column is painted, so the reader
98
+ sees one place: where they left it, or the least move from it that shows the row (Katerina, 17 November: "lets
99
+ smooth it"). The row it showed last stays where they put it; one drawn later is the reveal's.
100
+ */
101
+ const open = el.querySelector<HTMLElement>('[aria-current="page"]')
102
+ const key = open && open.getBoundingClientRect().height > 0 ? keyOf(open) : undefined
103
+ if (open && key !== undefined && key !== places.get(name)?.shown) {
104
+ finish()
105
+ places.set(name, { ...places.get(name), shown: key })
106
+ scrollRowInto(el, open)
107
+ set = el.scrollTop
108
+ }
109
+ const grows = typeof ResizeObserver === 'undefined' ? undefined : new ResizeObserver(restore)
110
+ if (el.firstElementChild) grows?.observe(el.firstElementChild)
111
+ const until = setTimeout(finish, RESTORE_MS)
112
+ // The reader takes over.
113
+ const took = () => finish()
114
+ const scrolled = () => {
115
+ // Moved by anything but the restore: what moved it stands.
116
+ if (restoring.current && Math.abs(el.scrollTop - set) > 1) finish()
117
+ if (!restoring.current) keep()
118
+ }
119
+ const takes = ['wheel', 'touchstart', 'pointerdown', 'keydown'] as const
120
+ el.addEventListener('scroll', scrolled, { passive: true })
121
+ for (const type of takes) el.addEventListener(type, took, { passive: true })
122
+ return () => {
123
+ el.removeEventListener('scroll', scrolled)
124
+ for (const type of takes) el.removeEventListener(type, took)
125
+ grows?.disconnect()
126
+ clearTimeout(until)
127
+ if (!restoring.current) keep()
128
+ restoring.current = false
129
+ }
130
+ }, [box, name])
131
+ // One object per name: the reveal and the tree inside take it, and look again only when the name changes.
132
+ return useMemo(() => ({ name, restoring }), [name])
133
+ }
134
+
135
+ export function useRevealCurrentRow(box: RefObject<HTMLElement | null>, current?: string, place?: ListPlace): void {
40
136
  const shown = useRef<string | undefined>(undefined)
137
+ // Inside a `ListColumn`, the column brings its rows into sight: a tree in it reveals nothing itself.
138
+ const within = useContext(ListPlaceContext)
139
+ const deferred = !place && within !== null
41
140
  useEffect(() => {
42
141
  const root = box.current
43
- if (!root) return
142
+ if (!root || deferred) return
143
+ // The open row it last showed: the named list's, which outlives the column being drawn again; else this one's.
144
+ const was = () => (place?.name ? places.get(place.name)?.shown : shown.current)
145
+ const showing = (key: string) => {
146
+ if (place?.name) places.set(place.name, { ...places.get(place.name), shown: key })
147
+ else shown.current = key
148
+ }
44
149
  let timer: ReturnType<typeof setTimeout> | undefined
45
150
  const look = () => {
46
151
  timer = undefined
47
152
  const row = root.querySelector<HTMLElement>('[aria-current="page"]')
48
153
  if (!row) return
49
- const key = current ?? row.getAttribute('href') ?? row.textContent ?? ''
50
- if (shown.current === key) return
154
+ const key = keyOf(row, current)
155
+ // The row it showed last: the list goes back to where it was left, and the row stays where the reader put it.
156
+ if (was() === key) return
51
157
  // Not drawn yet, or inside a fold that is shut: whatever opens it changes the list, and it is looked for again.
52
158
  if (row.getBoundingClientRect().height === 0) return
53
- shown.current = key
159
+ // A new open row wins at once, the reader asked for it: going back to where the list was left stops here, even
160
+ // when that place can no longer be reached (a fold that was open is shut).
161
+ if (place) place.restoring.current = false
162
+ showing(key)
54
163
  scrollToRow(row)
55
164
  }
56
165
  // One look a moment after the list changes, however often it does: a live list changes all the time.
@@ -64,5 +173,5 @@ export function useRevealCurrentRow(box: RefObject<HTMLElement | null>, current?
64
173
  watch?.disconnect()
65
174
  if (timer !== undefined) clearTimeout(timer)
66
175
  }
67
- }, [box, current])
176
+ }, [box, current, place, deferred])
68
177
  }