@estiva-app/ui 0.118.0 → 0.119.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.
@@ -0,0 +1,168 @@
1
+ // @vitest-environment jsdom
2
+ /**
3
+ * What the page claims (Katerina, 17 November): a list shows where the open page lives. Its current row is brought into
4
+ * the column's sight once it is drawn and shown, however late — a row a part inside the tree draws itself once its read
5
+ * lands (a team's Files fold), a plain list column's row (DMs, the Desk) — once per new current row, and never pulled
6
+ * back after the reader scrolls away.
7
+ */
8
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
9
+ import { act, cleanup, render } from '@testing-library/react'
10
+ import { useEffect, useState } from 'react'
11
+ import { ListColumn } from './ListColumn'
12
+ import { NavItem } from './NavItem'
13
+ import { NavTree } from './NavTree'
14
+ import { REVEAL_MS } from './revealCurrentRow'
15
+
16
+ afterEach(cleanup)
17
+ beforeEach(() => vi.useFakeTimers())
18
+ afterEach(() => vi.useRealTimers())
19
+
20
+ /**
21
+ * Lays out a 100px column that scrolls 1000px, with every row 32px tall, each `at` px down its list, and the rest at its
22
+ * top. `shut` rows are in a fold that is closed: 0px tall.
23
+ */
24
+ function lay(column: HTMLElement, at: Record<string, number>, shut: Set<string> = new Set()) {
25
+ let top = 0
26
+ Object.defineProperty(column, 'scrollHeight', { configurable: true, get: () => 1000 })
27
+ Object.defineProperty(column, 'clientHeight', { configurable: true, get: () => 100 })
28
+ Object.defineProperty(column, 'scrollTop', { configurable: true, get: () => top, set: (next: number) => (top = next) })
29
+ column.style.overflowY = 'auto'
30
+ const box = (y: number, height: number) => ({ top: y, bottom: y + height, height, left: 0, right: 0, width: 0, x: 0, y, toJSON: () => ({}) }) as DOMRect
31
+ const rect = vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) {
32
+ if (this === column) return box(0, 100)
33
+ const label = this.textContent?.trim() ?? ''
34
+ if (shut.has(label)) return box(0, 0)
35
+ return label in at ? box(at[label]! - top, 32) : box(0, 32)
36
+ })
37
+ return { scrolled: () => top, scroll: (to: number) => (top = to), restore: () => rect.mockRestore() }
38
+ }
39
+
40
+ /** Its rows a moment after it is drawn, as a fold that reads them once it opens: the tree is not drawn again for them. */
41
+ function LateRows({ open }: { open: string }) {
42
+ const [rows, setRows] = useState<string[] | null>(null)
43
+ useEffect(() => {
44
+ const timer = setTimeout(() => setRows(['Project 1', 'Project 2', 'Project 9']), 600)
45
+ return () => clearTimeout(timer)
46
+ }, [])
47
+ return <>{rows?.map((row) => <NavItem key={row} href={`#${row}`} label={row} active={row === open} />)}</>
48
+ }
49
+
50
+ describe('a tree brings its current row into sight', () => {
51
+ it('when a part inside it draws the row late, after its read lands (a team’s Files fold)', async () => {
52
+ const { getByTestId } = render(
53
+ <div data-testid="column">
54
+ <NavTree selected="p9">
55
+ <NavItem href="#team" label="Team" />
56
+ <LateRows open="Project 9" />
57
+ </NavTree>
58
+ </div>,
59
+ )
60
+ const layout = lay(getByTestId('column'), { 'Project 9': 900 })
61
+ try {
62
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
63
+ expect(layout.scrolled()).toBe(0)
64
+ // The read lands: its rows are drawn, the tree is not.
65
+ await act(async () => vi.advanceTimersByTime(600))
66
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
67
+ // Its bottom edge at the column's: 900 + 32 − 100.
68
+ expect(layout.scrolled()).toBe(832)
69
+ } finally {
70
+ layout.restore()
71
+ }
72
+ })
73
+
74
+ it('once: scrolled away from, it is not brought back while it stays the current row', async () => {
75
+ const { getByTestId, rerender } = render(
76
+ <div data-testid="column">
77
+ <NavTree selected="b">
78
+ <NavItem href="#a" label="Item a" />
79
+ <NavItem href="#b" label="Item b" active />
80
+ </NavTree>
81
+ </div>,
82
+ )
83
+ const layout = lay(getByTestId('column'), { 'Item b': 500 })
84
+ try {
85
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
86
+ expect(layout.scrolled()).toBe(432)
87
+ layout.scroll(0)
88
+ rerender(
89
+ <div data-testid="column">
90
+ <NavTree selected="b">
91
+ <NavItem href="#a" label="Item a (2)" />
92
+ <NavItem href="#b" label="Item b" active />
93
+ </NavTree>
94
+ </div>,
95
+ )
96
+ await act(async () => {})
97
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
98
+ expect(layout.scrolled()).toBe(0)
99
+ } finally {
100
+ layout.restore()
101
+ }
102
+ })
103
+
104
+ it('a row in a shut fold waits for the fold to open', async () => {
105
+ const shut = new Set(['Item b'])
106
+ const { getByTestId } = render(
107
+ <div data-testid="column">
108
+ <NavTree selected="b">
109
+ <NavItem href="#b" label="Item b" active />
110
+ </NavTree>
111
+ </div>,
112
+ )
113
+ const layout = lay(getByTestId('column'), { 'Item b': 500 }, shut)
114
+ try {
115
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
116
+ expect(layout.scrolled()).toBe(0)
117
+ // The fold opens: its content is shown, which changes the list.
118
+ shut.clear()
119
+ await act(async () => getByTestId('column').querySelector('a')!.setAttribute('hidden', ''))
120
+ await act(async () => getByTestId('column').querySelector('a')!.removeAttribute('hidden'))
121
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
122
+ expect(layout.scrolled()).toBe(432)
123
+ } finally {
124
+ layout.restore()
125
+ }
126
+ })
127
+ })
128
+
129
+ describe('a list column brings its current row into sight (DMs, the Desk)', () => {
130
+ function People({ open }: { open: string }) {
131
+ return (
132
+ <ListColumn title="DMs">
133
+ {Array.from({ length: 30 }, (_, i) => `Person ${i}`).map((name) => (
134
+ <NavItem key={name} href={`#${name}`} label={name} active={name === open} />
135
+ ))}
136
+ </ListColumn>
137
+ )
138
+ }
139
+ const viewport = () => document.querySelector<HTMLElement>('[data-id$="-viewport"]') ?? document.querySelector<HTMLElement>('[style*="overflow"]')!
140
+
141
+ it('its open row below the fold, and each new open row after it', async () => {
142
+ const { rerender } = render(<People open="Person 25" />)
143
+ const layout = lay(viewport(), { 'Person 25': 800, 'Person 2': 64 })
144
+ try {
145
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
146
+ expect(layout.scrolled()).toBe(732)
147
+ // Another opens, above: the column goes up to it.
148
+ rerender(<People open="Person 2" />)
149
+ // What changed in the list is heard first, then the look a moment later.
150
+ await act(async () => {})
151
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
152
+ expect(layout.scrolled()).toBe(64)
153
+ } finally {
154
+ layout.restore()
155
+ }
156
+ })
157
+
158
+ it('a row already in sight stays put', async () => {
159
+ render(<People open="Person 1" />)
160
+ const layout = lay(viewport(), { 'Person 1': 32 })
161
+ try {
162
+ await act(async () => vi.advanceTimersByTime(REVEAL_MS + 10))
163
+ expect(layout.scrolled()).toBe(0)
164
+ } finally {
165
+ layout.restore()
166
+ }
167
+ })
168
+ })
@@ -0,0 +1,68 @@
1
+ import { useEffect, useRef, type RefObject } from 'react'
2
+
3
+ /**
4
+ * Showing where the open page lives in a list: its row (`aria-current="page"`, what `NavItem`'s `active` sets) brought
5
+ * into the column's sight, once for each new current row, whenever it is drawn and shown (Katerina, 17 November: "the
6
+ * selected [row] could be somewhere above or below the fold … a general problem we need to fix").
7
+ *
8
+ * However late: a row a fold reads only once it opens, a group sliding open, rows that arrive with a read. The list is
9
+ * watched for what changes in it, not for its own draws, so a row drawn by a part inside it is seen too (a team's Files
10
+ * fold, which reads its files itself, never drew the tree again: its open file stayed below the fold).
11
+ *
12
+ * Once: a row brought into sight is not brought back after the reader scrolls away, while it stays the current one;
13
+ * a new current row is.
14
+ */
15
+
16
+ /** How long it waits to look, after the list changes: a group above the row slides open in 150ms. */
17
+ export const REVEAL_MS = 200
18
+
19
+ /**
20
+ * Scroll the column the row is in, and only that box — never the page, nor a
21
+ * drawer that is shut — so the row is in sight. A row in sight stays put.
22
+ * A group taller than the column keeps its top in sight, as the browser's
23
+ * `block: 'nearest'` does (a row is never that tall).
24
+ */
25
+ export function scrollToRow(row: HTMLElement) {
26
+ let box = row.parentElement
27
+ while (box && !(/(auto|scroll)/.test(getComputedStyle(box).overflowY) && box.scrollHeight > box.clientHeight)) box = box.parentElement
28
+ if (!box || box === document.documentElement || box === document.body) return
29
+ const at = row.getBoundingClientRect()
30
+ const view = box.getBoundingClientRect()
31
+ if (at.top < view.top) box.scrollTop -= view.top - at.top
32
+ else if (at.bottom > view.bottom) box.scrollTop += Math.min(at.bottom - view.bottom, at.top - view.top)
33
+ }
34
+
35
+ /**
36
+ * Brings the current row inside `box` into sight, once per current row. `current` names it when the caller knows it
37
+ * (`NavTree`'s `selected`); without it, the row is known by its link.
38
+ */
39
+ export function useRevealCurrentRow(box: RefObject<HTMLElement | null>, current?: string): void {
40
+ const shown = useRef<string | undefined>(undefined)
41
+ useEffect(() => {
42
+ const root = box.current
43
+ if (!root) return
44
+ let timer: ReturnType<typeof setTimeout> | undefined
45
+ const look = () => {
46
+ timer = undefined
47
+ const row = root.querySelector<HTMLElement>('[aria-current="page"]')
48
+ if (!row) return
49
+ const key = current ?? row.getAttribute('href') ?? row.textContent ?? ''
50
+ if (shown.current === key) return
51
+ // Not drawn yet, or inside a fold that is shut: whatever opens it changes the list, and it is looked for again.
52
+ if (row.getBoundingClientRect().height === 0) return
53
+ shown.current = key
54
+ scrollToRow(row)
55
+ }
56
+ // One look a moment after the list changes, however often it does: a live list changes all the time.
57
+ const soon = () => {
58
+ if (timer === undefined) timer = setTimeout(look, REVEAL_MS)
59
+ }
60
+ const watch = typeof MutationObserver === 'undefined' ? undefined : new MutationObserver(soon)
61
+ watch?.observe(root, { subtree: true, childList: true, attributes: true, attributeFilter: ['aria-current', 'hidden'] })
62
+ soon()
63
+ return () => {
64
+ watch?.disconnect()
65
+ if (timer !== undefined) clearTimeout(timer)
66
+ }
67
+ }, [box, current])
68
+ }
@@ -59,6 +59,15 @@ function show(id: string, options: ShowOptions): boolean {
59
59
  const el = find(id, options)
60
60
  if (!el) return false
61
61
  el.scrollIntoView({ behavior: 'smooth', block: 'center' })
62
+ markShown(el)
63
+ return true
64
+ }
65
+
66
+ /**
67
+ * Marks one drawn comment or reply a moment (`data-shown`, the kit's flash): a show's mark, and the one a
68
+ * `ConversationLayout` gives the comment it lands on when it opens at a thread. The package's own, not exported.
69
+ */
70
+ export function markShown(el: HTMLElement): void {
62
71
  const previous = owed.get(el)
63
72
  if (previous !== undefined) window.clearTimeout(previous)
64
73
  el.dataset.shown = 'true'
@@ -69,7 +78,6 @@ function show(id: string, options: ShowOptions): boolean {
69
78
  owed.delete(el)
70
79
  }, SHOWN_MS),
71
80
  )
72
- return true
73
81
  }
74
82
 
75
83
  /**