@estiva-app/ui 0.98.1 → 0.100.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/README.md +4 -2
  2. package/dist/AppProvider.d.ts +6 -3
  3. package/dist/AppProvider.d.ts.map +1 -1
  4. package/dist/ArchiveDialog.d.ts +35 -0
  5. package/dist/ArchiveDialog.d.ts.map +1 -0
  6. package/dist/AttachedCard.d.ts +13 -2
  7. package/dist/AttachedCard.d.ts.map +1 -1
  8. package/dist/Card.d.ts +6 -1
  9. package/dist/Card.d.ts.map +1 -1
  10. package/dist/ChipInput.d.ts +3 -1
  11. package/dist/ChipInput.d.ts.map +1 -1
  12. package/dist/CollapsibleSection.d.ts +27 -2
  13. package/dist/CollapsibleSection.d.ts.map +1 -1
  14. package/dist/CommentBox.d.ts +2 -2
  15. package/dist/CommentBox.d.ts.map +1 -1
  16. package/dist/ConfirmDialog.d.ts +15 -2
  17. package/dist/ConfirmDialog.d.ts.map +1 -1
  18. package/dist/ConversationCard.d.ts +12 -1
  19. package/dist/ConversationCard.d.ts.map +1 -1
  20. package/dist/ConversationLayout.d.ts +20 -5
  21. package/dist/ConversationLayout.d.ts.map +1 -1
  22. package/dist/ConversationMenu.d.ts +5 -1
  23. package/dist/ConversationMenu.d.ts.map +1 -1
  24. package/dist/ConversationPlace.d.ts +17 -0
  25. package/dist/ConversationPlace.d.ts.map +1 -0
  26. package/dist/DocumentEditor.d.ts +38 -0
  27. package/dist/DocumentEditor.d.ts.map +1 -1
  28. package/dist/DocumentField.d.ts +50 -0
  29. package/dist/DocumentField.d.ts.map +1 -0
  30. package/dist/EditableText.d.ts +38 -4
  31. package/dist/EditableText.d.ts.map +1 -1
  32. package/dist/EmptyState.d.ts +10 -5
  33. package/dist/EmptyState.d.ts.map +1 -1
  34. package/dist/FormDialog.d.ts +38 -0
  35. package/dist/FormDialog.d.ts.map +1 -0
  36. package/dist/InlineText.d.ts.map +1 -1
  37. package/dist/Menu.d.ts +11 -3
  38. package/dist/Menu.d.ts.map +1 -1
  39. package/dist/MoveDialog.d.ts +64 -0
  40. package/dist/MoveDialog.d.ts.map +1 -0
  41. package/dist/NavItem.d.ts +9 -2
  42. package/dist/NavItem.d.ts.map +1 -1
  43. package/dist/NavTree.d.ts +16 -3
  44. package/dist/NavTree.d.ts.map +1 -1
  45. package/dist/PinnedComment.d.ts +3 -1
  46. package/dist/PinnedComment.d.ts.map +1 -1
  47. package/dist/ReactionHorizon.d.ts +3 -1
  48. package/dist/ReactionHorizon.d.ts.map +1 -1
  49. package/dist/RepliesRow.d.ts +5 -3
  50. package/dist/RepliesRow.d.ts.map +1 -1
  51. package/dist/ResolveDialog.d.ts +3 -1
  52. package/dist/ResolveDialog.d.ts.map +1 -1
  53. package/dist/SelectionToolbar.d.ts +2 -2
  54. package/dist/Thread.d.ts +6 -3
  55. package/dist/Thread.d.ts.map +1 -1
  56. package/dist/commentBoxFrame.d.ts.map +1 -1
  57. package/dist/conversation.examples.d.ts +4 -1
  58. package/dist/conversation.examples.d.ts.map +1 -1
  59. package/dist/conversationScroll.d.ts +80 -0
  60. package/dist/conversationScroll.d.ts.map +1 -0
  61. package/dist/editor.d.ts +1 -0
  62. package/dist/editor.d.ts.map +1 -1
  63. package/dist/editor.js +228 -69
  64. package/dist/editor.js.map +4 -4
  65. package/dist/eslint/index.js +2 -2
  66. package/dist/eslint/index.js.map +2 -2
  67. package/dist/eslint/no-field-outside-a-form.d.ts +2 -2
  68. package/dist/eslint/no-field-outside-a-form.d.ts.map +1 -1
  69. package/dist/eslint/no-native-title.d.ts +1 -1
  70. package/dist/eslint/no-native-title.d.ts.map +1 -1
  71. package/dist/formBusy.d.ts +5 -2
  72. package/dist/formBusy.d.ts.map +1 -1
  73. package/dist/index.d.ts +14 -6
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +2242 -1471
  76. package/dist/index.js.map +4 -4
  77. package/dist/looks.d.ts +8 -0
  78. package/dist/looks.d.ts.map +1 -1
  79. package/dist/seenOnScreen.d.ts +27 -0
  80. package/dist/seenOnScreen.d.ts.map +1 -0
  81. package/dist/showInConversation.d.ts +31 -0
  82. package/dist/showInConversation.d.ts.map +1 -0
  83. package/dist/threadCard.d.ts +13 -0
  84. package/dist/threadCard.d.ts.map +1 -1
  85. package/package.json +1 -1
  86. package/registry.json +953 -126
  87. package/src/AppProvider.mdx +4 -0
  88. package/src/AppProvider.test.tsx +34 -2
  89. package/src/AppProvider.tsx +17 -6
  90. package/src/ArchiveDialog.mdx +70 -0
  91. package/src/ArchiveDialog.stories.tsx +64 -0
  92. package/src/ArchiveDialog.test.tsx +90 -0
  93. package/src/ArchiveDialog.tsx +70 -0
  94. package/src/AttachedCard.mdx +6 -0
  95. package/src/AttachedCard.stories.tsx +9 -1
  96. package/src/AttachedCard.test.tsx +111 -2
  97. package/src/AttachedCard.tsx +77 -7
  98. package/src/Card.mdx +20 -1
  99. package/src/Card.stories.tsx +20 -0
  100. package/src/Card.test.tsx +87 -1
  101. package/src/Card.tsx +51 -3
  102. package/src/ChipInput.mdx +7 -1
  103. package/src/ChipInput.stories.tsx +9 -0
  104. package/src/ChipInput.test.tsx +42 -0
  105. package/src/ChipInput.tsx +7 -3
  106. package/src/CollapsibleSection.mdx +20 -1
  107. package/src/CollapsibleSection.stories.tsx +19 -1
  108. package/src/CollapsibleSection.test.tsx +85 -2
  109. package/src/CollapsibleSection.tsx +54 -15
  110. package/src/CommentBox.mdx +2 -1
  111. package/src/CommentBox.test.tsx +81 -1
  112. package/src/CommentBox.tsx +32 -11
  113. package/src/ConfirmDialog.mdx +25 -3
  114. package/src/ConfirmDialog.stories.tsx +27 -0
  115. package/src/ConfirmDialog.test.tsx +87 -1
  116. package/src/ConfirmDialog.tsx +39 -8
  117. package/src/ConversationCard.mdx +7 -0
  118. package/src/ConversationCard.states.test.tsx +67 -0
  119. package/src/ConversationCard.stories.tsx +27 -1
  120. package/src/ConversationCard.test.tsx +76 -0
  121. package/src/ConversationCard.tsx +65 -53
  122. package/src/ConversationLayout.mdx +34 -4
  123. package/src/ConversationLayout.scroll.test.tsx +411 -0
  124. package/src/ConversationLayout.stories.tsx +107 -1
  125. package/src/ConversationLayout.test.tsx +133 -0
  126. package/src/ConversationLayout.tsx +141 -16
  127. package/src/ConversationMenu.mdx +3 -0
  128. package/src/ConversationMenu.stories.tsx +12 -0
  129. package/src/ConversationMenu.test.tsx +17 -0
  130. package/src/ConversationMenu.tsx +7 -3
  131. package/src/ConversationPlace.mdx +53 -0
  132. package/src/ConversationPlace.stories.tsx +48 -0
  133. package/src/ConversationPlace.test.tsx +51 -0
  134. package/src/ConversationPlace.tsx +30 -0
  135. package/src/DocumentEditor.mdx +2 -1
  136. package/src/DocumentEditor.tsx +118 -64
  137. package/src/DocumentField.mdx +83 -0
  138. package/src/DocumentField.stories.tsx +110 -0
  139. package/src/DocumentField.test.tsx +281 -0
  140. package/src/DocumentField.tsx +199 -0
  141. package/src/EditableText.buttons.test.tsx +318 -0
  142. package/src/EditableText.mdx +50 -2
  143. package/src/EditableText.stories.tsx +96 -2
  144. package/src/EditableText.tsx +189 -5
  145. package/src/EmptyState.mdx +33 -8
  146. package/src/EmptyState.stories.tsx +26 -1
  147. package/src/EmptyState.test.tsx +63 -2
  148. package/src/EmptyState.tsx +28 -6
  149. package/src/Form.mdx +3 -1
  150. package/src/FormDialog.mdx +78 -0
  151. package/src/FormDialog.stories.tsx +90 -0
  152. package/src/FormDialog.test.tsx +183 -0
  153. package/src/FormDialog.tsx +93 -0
  154. package/src/InlineText.mdx +1 -0
  155. package/src/InlineText.tsx +2 -1
  156. package/src/Menu.test.tsx +80 -0
  157. package/src/Menu.tsx +35 -14
  158. package/src/MenuItem.mdx +12 -0
  159. package/src/MenuItem.stories.tsx +39 -1
  160. package/src/MoveDialog.mdx +85 -0
  161. package/src/MoveDialog.stories.tsx +134 -0
  162. package/src/MoveDialog.test.tsx +239 -0
  163. package/src/MoveDialog.tsx +186 -0
  164. package/src/NavItem.mdx +15 -0
  165. package/src/NavItem.stories.tsx +15 -0
  166. package/src/NavItem.test.tsx +64 -1
  167. package/src/NavItem.tsx +10 -2
  168. package/src/NavTree.mdx +26 -4
  169. package/src/NavTree.stories.tsx +59 -0
  170. package/src/NavTree.test.tsx +232 -2
  171. package/src/NavTree.tsx +93 -43
  172. package/src/PinnedComment.mdx +6 -1
  173. package/src/PinnedComment.stories.tsx +11 -1
  174. package/src/PinnedComment.tsx +15 -2
  175. package/src/ReactionHorizon.mdx +2 -0
  176. package/src/ReactionHorizon.stories.tsx +3 -0
  177. package/src/ReactionHorizon.test.tsx +32 -0
  178. package/src/ReactionHorizon.tsx +28 -11
  179. package/src/RepliesRow.mdx +6 -3
  180. package/src/RepliesRow.stories.tsx +3 -0
  181. package/src/RepliesRow.test.tsx +24 -0
  182. package/src/RepliesRow.tsx +12 -6
  183. package/src/ReplyCard.mdx +8 -0
  184. package/src/ReplyCard.stories.tsx +45 -1
  185. package/src/ResolveDialog.mdx +2 -0
  186. package/src/ResolveDialog.test.tsx +21 -0
  187. package/src/ResolveDialog.tsx +11 -39
  188. package/src/SelectionToolbar.tsx +2 -2
  189. package/src/Textarea.mdx +1 -0
  190. package/src/Thread.mdx +20 -1
  191. package/src/Thread.stories.tsx +94 -3
  192. package/src/Thread.test.tsx +11 -0
  193. package/src/Thread.tsx +56 -12
  194. package/src/commentBoxFrame.tsx +2 -2
  195. package/src/conversation.examples.tsx +10 -2
  196. package/src/conversationScroll.ts +155 -0
  197. package/src/editor.ts +1 -0
  198. package/src/editorSelection.test.tsx +53 -0
  199. package/src/eslint/no-field-outside-a-form.test.ts +4 -0
  200. package/src/eslint/no-field-outside-a-form.ts +2 -2
  201. package/src/eslint/no-native-title.ts +1 -1
  202. package/src/formBusy.ts +5 -2
  203. package/src/index.ts +16 -6
  204. package/src/looks.ts +10 -0
  205. package/src/registry/registry.test.ts +12 -6
  206. package/src/seenOnScreen.test.tsx +241 -0
  207. package/src/seenOnScreen.ts +146 -0
  208. package/src/showInConversation.test.tsx +168 -0
  209. package/src/showInConversation.ts +93 -0
  210. package/src/threadCard.ts +16 -0
  211. package/stories/Choosing.mdx +8 -3
  212. package/tailwind-preset.js +7 -0
@@ -14,6 +14,9 @@ Where a conversation's cards sit: two columns, or one 384px column beside the pa
14
14
  - `threads="beside"`, two columns: the comments fill the page; an open thread stands beside them, 384px.
15
15
  - `threads="under"`, one column: the comments are the 384px column beside the page; a thread opens under its comment, and the two become one card.
16
16
  - With two columns, `header` puts the comments' own **PageHeader** over their column only, and the thread runs the page's full height beside it.
17
+ - With two columns, `aside` is what the 384px column holds while no thread is open: the page's own **Panel** beside its comments (an overview). A thread opened takes its place.
18
+
19
+ <Canvas of={Stories.TwoColumnsAside} />
17
20
  - With two columns, each comment's `day` names its stretch with a line that stays at the top while the stretch scrolls (Peek's day lines).
18
21
 
19
22
  <Canvas of={Stories.TwoColumnsDays} />
@@ -26,6 +29,10 @@ Where a conversation's cards sit: two columns, or one 384px column beside the pa
26
29
 
27
30
  <Canvas of={Stories.Apart} />
28
31
 
32
+ - With one column too, `below` is under the comments, after the groups: a **ReactionHorizon**, or the app's own note. It stands at the column's foot, so with few comments it sits on the box, as a short conversation's last line does with two columns; with many it comes after them and scrolls with them. The comments still start at the top. Not while the comments are read.
33
+
34
+ <Canvas of={Stories.OneColumnBelow} />
35
+
29
36
  ## When not
30
37
 
31
38
  - One thread on its own → **Thread**.
@@ -42,16 +49,37 @@ import { ConversationLayout } from '@estiva-app/ui'
42
49
  - Which layout is the app's: where a conversation sits is its own.
43
50
  - The right column is 384px in every app.
44
51
  - The app keeps `open`; the layout says when it changes, with `onOpenChange`. Give every comment a `thread`, a
45
- **Thread** with its box, even one with no replies yet: pressing it opens it to its box.
52
+ **Thread** with its box, even one with no replies yet: pressing it opens it to its box. An app that builds only the
53
+ open thread gives a function, `thread: () => threadOf(comment)`: it is called only while that one is open.
54
+ - Draw no column of your own for the thread or for the panel beside the comments: `aside`. The column catches what
55
+ throws in it, and says so in its place; the comments stay.
46
56
  - Write nothing on the cards for opening: no `onClick`, `selected`, `onReply`, or the replies row's `onOpen` and
47
57
  `open`. The layout gives each **ConversationCard** its place, and the card opens its thread from it. What a card is
48
58
  given still wins.
49
- - Two columns, an app that keeps its own place in the comments (where they were left, a "New" line, following what
50
- arrives) has their scrolling box with `bodyRef` and `onBodyScroll`. Each comment's `day` draws the app's day lines,
59
+ - Two columns, where the comments scroll is the layout's, the same in every app: they open at the newest; they
60
+ follow what arrives only while the reader is at the bottom (within 80px); the reader's own comment, sent from `box`,
61
+ brings them down to it as it is drawn, and a refusal leaves them where they are; and they hold still while cards and
62
+ pictures fill in. Give the conversation its name, `rememberAs` (its address), and going back to it lands where it was
63
+ left, in this tab. `useLeftAt(name)` says when the reader left it away from the newest, for your "New" line in
64
+ `before` over the first comment written since. Keep no place of your own: `bodyRef` and `onBodyScroll` only read the box.
65
+
66
+ <Canvas of={Stories.TwoColumnsLong} />
67
+
68
+ - Each comment's `day` draws the app's day lines,
51
69
  each staying at the top while its own day scrolls under it; `before` is a line just above one comment (a "New"
52
70
  line); `below` is under the comments (a **ReactionHorizon**); `top` stands at the top however few the comments are
53
71
  (an **ArchivedNotice**). Few comments sit on the box, as a chat does. `empty` may be the app's own **EmptyState**,
54
72
  or `null` for nothing.
73
+ - A comment is read by being seen: `useSeenOnScreen(key)` for the conversation, handed to `seen`, holds the ids of the
74
+ comments that have been on screen a moment (half of the card, or 120px of a taller one, for 1.5 seconds, in a tab
75
+ that is not hidden). Draw `isNew` from it; mark the conversation read from it. `key` `null` watches nothing, until you
76
+ have judged what is new. Never an observer of your own.
77
+
78
+ <Canvas of={Stories.TwoColumnsSeen} />
79
+
80
+ - A link to a comment shows it: `showInConversation(id, { comments: true })` scrolls it to the middle and flashes it
81
+ (the open thread's pinned comment first, when it is that one's); `false` when it is not drawn. Each comment carries
82
+ its `id` as `data-conversation-item`. See **Thread** for replies.
55
83
  - The box at the foot of a thread that opens takes the caret, at its end, without scrolling: the layout tells it with
56
84
  `caretOnOpen`, which **CommentBox** reads. Nothing to pass; a box outside a layout never takes it this way.
57
85
 
@@ -65,8 +93,10 @@ import { ConversationLayout } from '@estiva-app/ui'
65
93
  | Opens and closes in place: the comment is never redrawn and does not move, open or closed | a wrapper that changes when it opens | |
66
94
  | One column: resolved comments and comments on deleted blocks closed under their names at the end, opened for the thread open in them | the groups, their counts, opening one | |
67
95
  | Puts the caret in the box of a thread that opens, without scrolling | a focus call | |
96
+ | Two columns: opens at the newest, comes back to where it was left, follows only a reader at the bottom, goes down to the reader's own comment, holds still while cards load | a scroll policy, a place per conversation, a ResizeObserver | |
68
97
  | Placeholders while reading, and a line when there are none | a loading and an empty state | |
69
- | The right column at 384px, in every app | a width | |
98
+ | Says which comments have been on screen a moment, with `seen` | an IntersectionObserver and a timer per card | |
99
+ | The right column at 384px, in every app: the open thread, or the page's `aside` | a width, a column, its error boundary | |
70
100
 
71
101
  ## Props
72
102
 
@@ -0,0 +1,411 @@
1
+ // @vitest-environment jsdom
2
+ /**
3
+ * Where a conversation scrolls (C1 of the audit of 2 November): Peek's own, moved into ConversationLayout and Thread so
4
+ * every app's conversation reads the same. Two columns open at the newest comment, come back to where the reader left
5
+ * them (by the name the app gives, `rememberAs`), follow what arrives only while the reader is at the bottom, and go
6
+ * down to the reader's own comment as it is sent. A thread opens at its newest reply and goes to each new one. jsdom
7
+ * has no layout: every box here says the same heights, and only the scrolling box's offset is read.
8
+ */
9
+ import { useContext, type ReactNode } from 'react'
10
+ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
11
+ import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
12
+ import { ConversationCard, ReplyCard } from './ConversationCard'
13
+ import { ConversationLayout, type ConversationItem } from './ConversationLayout'
14
+ import { isNearBottom, NEAR_BOTTOM_PX, places, sendsInLayout, useLeftAt } from './conversationScroll'
15
+ import { Thread } from './Thread'
16
+
17
+ const ONE = { name: 'Person one' }
18
+
19
+ /** What every box says of its heights, as a test sets them. */
20
+ const heights = { scroll: 1000, client: 400 }
21
+ const props = ['scrollHeight', 'clientHeight'] as const
22
+ const kept = props.map((p) => [p, Object.getOwnPropertyDescriptor(HTMLElement.prototype, p)] as const)
23
+ beforeAll(() => {
24
+ Object.defineProperty(HTMLElement.prototype, 'scrollHeight', { configurable: true, get: () => heights.scroll })
25
+ Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, get: () => heights.client })
26
+ })
27
+ afterAll(() => {
28
+ for (const [p, d] of kept) if (d) Object.defineProperty(HTMLElement.prototype, p, d)
29
+ })
30
+ beforeEach(() => {
31
+ heights.scroll = 1000
32
+ heights.client = 400
33
+ })
34
+ afterEach(cleanup)
35
+
36
+ const card = (id: string) => ({
37
+ id,
38
+ card: (
39
+ <ConversationCard author={ONE} time="11:08 AM">
40
+ {`Comment ${id}`}
41
+ </ConversationCard>
42
+ ),
43
+ })
44
+ const many = (n: number): ConversationItem[] => Array.from({ length: n }, (_, i) => card(`c${i}`))
45
+
46
+ /** The comments' scrolling box, as the layout lends it. */
47
+ function Layout({ items, loading = false, rememberAs, box, boxRef }: { items: ConversationItem[]; loading?: boolean; rememberAs?: string; box?: ReactNode; boxRef: { current: HTMLDivElement | null } }) {
48
+ return <ConversationLayout threads="beside" items={items} loading={loading} rememberAs={rememberAs} box={box} bodyRef={boxRef} />
49
+ }
50
+
51
+ /** The reader scrolls the box to `top`. */
52
+ function scrollTo(el: HTMLElement, top: number) {
53
+ el.scrollTop = top
54
+ fireEvent.scroll(el)
55
+ }
56
+
57
+ describe('isNearBottom (Peek’s scroll policy, ported)', () => {
58
+ const el = (scrollHeight: number, scrollTop: number, clientHeight: number) => ({ scrollHeight, scrollTop, clientHeight }) as HTMLElement
59
+
60
+ it('is true when pinned to the bottom', () => {
61
+ expect(isNearBottom(el(1000, 600, 400))).toBe(true)
62
+ })
63
+
64
+ it('is false once you have scrolled up to read', () => {
65
+ expect(isNearBottom(el(1000, 200, 400))).toBe(false)
66
+ })
67
+
68
+ it('tolerates the sub-pixel gap a strict check would trip on', () => {
69
+ expect(isNearBottom(el(1000.4, 599.7, 400))).toBe(true)
70
+ })
71
+
72
+ it('holds exactly at the threshold, and lets go one pixel past it', () => {
73
+ expect(isNearBottom(el(1000, 600 - NEAR_BOTTOM_PX, 400))).toBe(true)
74
+ expect(isNearBottom(el(1000, 600 - NEAR_BOTTOM_PX - 1, 400))).toBe(false)
75
+ })
76
+
77
+ it('is true when there is nothing to scroll', () => {
78
+ expect(isNearBottom(el(400, 0, 400))).toBe(true)
79
+ })
80
+ })
81
+
82
+ describe('ConversationLayout, two columns, where it scrolls', () => {
83
+ it('opens at the newest comment, once the comments are read', () => {
84
+ const box = { current: null as HTMLDivElement | null }
85
+ const { rerender } = render(<Layout items={[]} loading boxRef={box} />)
86
+ expect(box.current!.scrollTop).toBe(0)
87
+ rerender(<Layout items={many(8)} boxRef={box} />)
88
+ expect(box.current!.scrollTop).toBe(1000)
89
+ })
90
+
91
+ it('follows a new comment while the reader is at the bottom, and not once they scrolled up to read', () => {
92
+ const box = { current: null as HTMLDivElement | null }
93
+ const { rerender } = render(<Layout items={many(8)} boxRef={box} />)
94
+ heights.scroll = 1200
95
+ rerender(<Layout items={many(9)} boxRef={box} />)
96
+ expect(box.current!.scrollTop).toBe(1200)
97
+ scrollTo(box.current!, 300)
98
+ heights.scroll = 1400
99
+ rerender(<Layout items={many(10)} boxRef={box} />)
100
+ expect(box.current!.scrollTop).toBe(300)
101
+ })
102
+
103
+ it('comes back to where it was left, by the name the app gave it', () => {
104
+ const box = { current: null as HTMLDivElement | null }
105
+ const { unmount } = render(<Layout items={many(8)} rememberAs="left-up" boxRef={box} />)
106
+ scrollTo(box.current!, 250)
107
+ unmount()
108
+ render(<Layout items={many(8)} rememberAs="left-up" boxRef={box} />)
109
+ expect(box.current!.scrollTop).toBe(250)
110
+ })
111
+
112
+ it('comes back to the newest when it was left at the bottom, with what arrived meanwhile in view', () => {
113
+ const box = { current: null as HTMLDivElement | null }
114
+ const { unmount } = render(<Layout items={many(8)} rememberAs="left-down" boxRef={box} />)
115
+ unmount()
116
+ heights.scroll = 1300
117
+ render(<Layout items={many(10)} rememberAs="left-down" boxRef={box} />)
118
+ expect(box.current!.scrollTop).toBe(1300)
119
+ })
120
+
121
+ it('opens another conversation at its newest, and the first where it was left, in one layout', () => {
122
+ const box = { current: null as HTMLDivElement | null }
123
+ const { rerender } = render(<Layout items={many(8)} rememberAs="one" boxRef={box} />)
124
+ scrollTo(box.current!, 200)
125
+ rerender(<Layout items={many(5)} rememberAs="two" boxRef={box} />)
126
+ expect(box.current!.scrollTop).toBe(1000)
127
+ rerender(<Layout items={many(8)} rememberAs="one" boxRef={box} />)
128
+ expect(box.current!.scrollTop).toBe(200)
129
+ })
130
+
131
+ it('comes back to where it was left after another conversation that was still being read', () => {
132
+ const box = { current: null as HTMLDivElement | null }
133
+ const { rerender } = render(<Layout items={many(8)} rememberAs="before-a-read" boxRef={box} />)
134
+ scrollTo(box.current!, 200)
135
+ rerender(<Layout items={[]} loading rememberAs="being-read" boxRef={box} />)
136
+ // The placeholders are shorter: the browser pulls the offset in, and says so.
137
+ scrollTo(box.current!, 0)
138
+ rerender(<Layout items={many(8)} rememberAs="before-a-read" boxRef={box} />)
139
+ expect(box.current!.scrollTop).toBe(200)
140
+ })
141
+
142
+ it('read again under the same name, lands where the reader left it, not where the placeholders pulled it', () => {
143
+ const box = { current: null as HTMLDivElement | null }
144
+ const { rerender } = render(<Layout items={many(8)} rememberAs="read-again" boxRef={box} />)
145
+ scrollTo(box.current!, 200)
146
+ rerender(<Layout items={[]} loading rememberAs="read-again" boxRef={box} />)
147
+ scrollTo(box.current!, 0)
148
+ rerender(<Layout items={many(8)} rememberAs="read-again" boxRef={box} />)
149
+ expect(box.current!.scrollTop).toBe(200)
150
+ })
151
+
152
+ it('without a name, opens at the newest each time it is drawn', () => {
153
+ const box = { current: null as HTMLDivElement | null }
154
+ const { unmount } = render(<Layout items={many(8)} boxRef={box} />)
155
+ scrollTo(box.current!, 250)
156
+ unmount()
157
+ render(<Layout items={many(8)} boxRef={box} />)
158
+ expect(box.current!.scrollTop).toBe(1000)
159
+ })
160
+
161
+ it('goes down to the reader’s own comment as it is sent, scrolled up or not; a refusal leaves them where they are', () => {
162
+ let sends: { sending: () => void; refused: () => void } | null = null
163
+ function Box() {
164
+ sends = useContext(sendsInLayout)
165
+ return <p>The box</p>
166
+ }
167
+ const box = { current: null as HTMLDivElement | null }
168
+ const { rerender } = render(<Layout items={many(8)} box={<Box />} boxRef={box} />)
169
+ expect(sends).not.toBeNull()
170
+ scrollTo(box.current!, 300)
171
+ act(() => sends!.sending())
172
+ heights.scroll = 1200
173
+ rerender(<Layout items={many(9)} box={<Box />} boxRef={box} />)
174
+ expect(box.current!.scrollTop).toBe(1200)
175
+ // Scrolled up again; a send that was refused: where the reader is decides what follows next.
176
+ scrollTo(box.current!, 300)
177
+ act(() => sends!.sending())
178
+ act(() => sends!.refused())
179
+ heights.scroll = 1400
180
+ rerender(<Layout items={many(10)} box={<Box />} boxRef={box} />)
181
+ expect(box.current!.scrollTop).toBe(300)
182
+ })
183
+
184
+ it('one column keeps where the reader is: it opens at the top, and follows nothing', () => {
185
+ const box = { current: null as HTMLDivElement | null }
186
+ function Under({ n }: { n: number }) {
187
+ return <ConversationLayout threads="under" items={many(n)} bodyRef={box} />
188
+ }
189
+ const { rerender } = render(<Under n={8} />)
190
+ expect(box.current!.scrollTop).toBe(0)
191
+ heights.scroll = 1200
192
+ rerender(<Under n={9} />)
193
+ expect(box.current!.scrollTop).toBe(0)
194
+ })
195
+ })
196
+
197
+ describe('useLeftAt: when the reader left a conversation, for its "New" line', () => {
198
+ function LeftAt({ at, boxRef, items }: { at: string; boxRef: { current: HTMLDivElement | null }; items: ConversationItem[] }) {
199
+ const left = useLeftAt(at)
200
+ return (
201
+ <>
202
+ <output data-testid="left">{left === null ? 'none' : String(left)}</output>
203
+ <Layout items={items} rememberAs={at} boxRef={boxRef} />
204
+ </>
205
+ )
206
+ }
207
+
208
+ beforeEach(() => {
209
+ vi.useFakeTimers({ toFake: ['Date'] })
210
+ vi.setSystemTime(1_800_000_000_000)
211
+ })
212
+ afterEach(() => vi.useRealTimers())
213
+
214
+ it('says when it was left away from the newest, taken once as it opens; none when it was left at the bottom or never opened', () => {
215
+ const box = { current: null as HTMLDivElement | null }
216
+ const first = render(<LeftAt at="read-up" items={many(8)} boxRef={box} />)
217
+ expect(screen.getByTestId('left').textContent).toBe('none')
218
+ scrollTo(box.current!, 200)
219
+ vi.setSystemTime(1_800_000_060_000)
220
+ first.unmount()
221
+ vi.setSystemTime(1_800_000_120_000)
222
+ const back = render(<LeftAt at="read-up" items={many(9)} boxRef={box} />)
223
+ expect(screen.getByTestId('left').textContent).toBe('1800000060000')
224
+ // Later scrolling moves the record, not the line.
225
+ scrollTo(box.current!, 1000)
226
+ back.rerender(<LeftAt at="read-up" items={many(9)} boxRef={box} />)
227
+ expect(screen.getByTestId('left').textContent).toBe('1800000060000')
228
+ back.unmount()
229
+ render(<LeftAt at="read-up" items={many(9)} boxRef={box} />)
230
+ expect(screen.getByTestId('left').textContent).toBe('none')
231
+ })
232
+
233
+ it('takes it again on the next visit when nothing was shown between, not the last visit’s', () => {
234
+ function Left({ at }: { at: string | null }) {
235
+ const left = useLeftAt(at)
236
+ return <output data-testid="left">{left === null ? 'none' : String(left)}</output>
237
+ }
238
+ places.set('between', { atBottom: false, offset: 10, leftAt: 111 })
239
+ const page = render(<Left at="between" />)
240
+ expect(screen.getByTestId('left').textContent).toBe('111')
241
+ page.rerender(<Left at={null} />)
242
+ expect(screen.getByTestId('left').textContent).toBe('none')
243
+ // Meanwhile the reader read it to the newest and left it there.
244
+ places.set('between', { atBottom: true, offset: 900, leftAt: 222 })
245
+ page.rerender(<Left at="between" />)
246
+ expect(screen.getByTestId('left').textContent).toBe('none')
247
+ })
248
+ })
249
+
250
+ const START = { id: 't1', author: ONE, time: '11:08 AM', words: 'The comment' }
251
+ function Replies({ n, start = START, boxRef }: { n: number; start?: typeof START; boxRef: { current: HTMLDivElement | null } }) {
252
+ return (
253
+ <Thread start={start} bodyRef={boxRef}>
254
+ {Array.from({ length: n }, (_, i) => (
255
+ <ReplyCard key={i} id={`r${i}`} author={ONE} time="11:30 AM">
256
+ {`Reply ${i}`}
257
+ </ReplyCard>
258
+ ))}
259
+ {/* Not a reply: an app's own line among them (Peek's resolve history) takes it nowhere. */}
260
+ <p>A line of the app’s</p>
261
+ </Thread>
262
+ )
263
+ }
264
+
265
+ describe('Thread, beside: where it scrolls', () => {
266
+ it('opens at its newest reply', () => {
267
+ const box = { current: null as HTMLDivElement | null }
268
+ render(<Replies n={4} boxRef={box} />)
269
+ expect(box.current!.scrollTop).toBe(1000)
270
+ })
271
+
272
+ it('goes to each new reply, wherever the reader is in it', () => {
273
+ const box = { current: null as HTMLDivElement | null }
274
+ const { rerender } = render(<Replies n={4} boxRef={box} />)
275
+ scrollTo(box.current!, 100)
276
+ heights.scroll = 1200
277
+ rerender(<Replies n={5} boxRef={box} />)
278
+ expect(box.current!.scrollTop).toBe(1200)
279
+ })
280
+
281
+ it('stays where the reader is when nothing new is a reply', () => {
282
+ const box = { current: null as HTMLDivElement | null }
283
+ const { rerender } = render(<Replies n={4} boxRef={box} />)
284
+ scrollTo(box.current!, 100)
285
+ heights.scroll = 1200
286
+ rerender(<Replies n={4} boxRef={box} />)
287
+ expect(box.current!.scrollTop).toBe(100)
288
+ })
289
+
290
+ it('opens another thread at its newest reply, as many replies as it has', () => {
291
+ const box = { current: null as HTMLDivElement | null }
292
+ const { rerender } = render(<Replies n={4} boxRef={box} />)
293
+ scrollTo(box.current!, 100)
294
+ rerender(<Replies n={4} start={{ ...START, id: 't2' }} boxRef={box} />)
295
+ expect(box.current!.scrollTop).toBe(1000)
296
+ })
297
+
298
+ it('stays where the reader is when a reply is taken away (a delete)', () => {
299
+ const box = { current: null as HTMLDivElement | null }
300
+ const { rerender } = render(<Replies n={4} boxRef={box} />)
301
+ scrollTo(box.current!, 100)
302
+ heights.scroll = 800
303
+ rerender(<Replies n={3} boxRef={box} />)
304
+ expect(box.current!.scrollTop).toBe(100)
305
+ })
306
+
307
+ it('opens another thread at its newest reply with no start id too, as many replies as it has', () => {
308
+ const box = { current: null as HTMLDivElement | null }
309
+ // Each reply keyed by its own id, as an app's list is.
310
+ function Other({ who }: { who: string }) {
311
+ return (
312
+ <Thread bodyRef={box}>
313
+ {Array.from({ length: 4 }, (_, i) => (
314
+ <ReplyCard key={`${who}${i}`} id={`${who}${i}`} author={ONE} time="11:30 AM">
315
+ {`Reply ${i} of ${who}`}
316
+ </ReplyCard>
317
+ ))}
318
+ </Thread>
319
+ )
320
+ }
321
+ const { rerender } = render(<Other who="a" />)
322
+ scrollTo(box.current!, 100)
323
+ rerender(<Other who="b" />)
324
+ expect(box.current!.scrollTop).toBe(1000)
325
+ })
326
+ })
327
+
328
+ /**
329
+ * Holding still while cards, chips and pictures fill in (Peek's OPT-10): the box is watched for changes of size. jsdom
330
+ * has none, and no layout: here a stand-in watcher is told when something changed size, the box keeps its offset
331
+ * between 0 and its height less its view as a browser does, and each card says where its top is.
332
+ */
333
+ describe('holding still while cards load', () => {
334
+ const watchers = new Set<() => void>()
335
+ class Watcher {
336
+ private readonly changed: () => void
337
+ constructor(changed: () => void) {
338
+ this.changed = changed
339
+ }
340
+ observe() {
341
+ watchers.add(this.changed)
342
+ }
343
+ disconnect() {
344
+ watchers.delete(this.changed)
345
+ }
346
+ unobserve() {}
347
+ }
348
+ /** Something in the box changed size: every watcher is told, as a browser tells them after layout. */
349
+ const resized = () => act(() => watchers.forEach((changed) => changed()))
350
+ const offsets = new WeakMap<Element, number>()
351
+ const tops = new WeakMap<Element, number>()
352
+ const hadWatcher = globalThis.ResizeObserver
353
+ beforeAll(() => {
354
+ globalThis.ResizeObserver = Watcher as unknown as typeof ResizeObserver
355
+ Object.defineProperty(HTMLElement.prototype, 'scrollTop', {
356
+ configurable: true,
357
+ get(this: HTMLElement) {
358
+ return offsets.get(this) ?? 0
359
+ },
360
+ set(this: HTMLElement, top: number) {
361
+ offsets.set(this, Math.max(0, Math.min(top, this.scrollHeight - this.clientHeight)))
362
+ },
363
+ })
364
+ vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function (this: HTMLElement) {
365
+ return new DOMRect(0, tops.get(this) ?? 0, 300, 40)
366
+ })
367
+ })
368
+ afterAll(() => {
369
+ globalThis.ResizeObserver = hadWatcher
370
+ delete (HTMLElement.prototype as { scrollTop?: number }).scrollTop
371
+ vi.restoreAllMocks()
372
+ delete (document as { elementFromPoint?: unknown }).elementFromPoint
373
+ })
374
+ afterEach(() => watchers.clear())
375
+
376
+ it('two columns, at the newest: a picture filling in above keeps the newest comments in view', () => {
377
+ const box = { current: null as HTMLDivElement | null }
378
+ render(<Layout items={many(8)} boxRef={box} />)
379
+ expect(box.current!.scrollTop).toBe(600)
380
+ heights.scroll = 1300
381
+ resized()
382
+ expect(box.current!.scrollTop).toBe(900)
383
+ })
384
+
385
+ it('two columns, scrolled up to read: the comment at the top of the view stays at its height', () => {
386
+ const box = { current: null as HTMLDivElement | null }
387
+ render(<Layout items={many(8)} boxRef={box} />)
388
+ const card = document.querySelector('[data-conversation-item="c3"]')!.firstElementChild!
389
+ tops.set(card, 30)
390
+ document.elementFromPoint = () => card
391
+ scrollTo(box.current!, 200)
392
+ // A card above it grows by 50px: it is pushed down, and the view follows it.
393
+ tops.set(card, 80)
394
+ heights.scroll = 1050
395
+ resized()
396
+ expect(box.current!.scrollTop).toBe(250)
397
+ })
398
+
399
+ it('a thread at its newest reply stays there while a reply’s picture fills in, and a reader who scrolled up stays put', () => {
400
+ const box = { current: null as HTMLDivElement | null }
401
+ render(<Replies n={4} boxRef={box} />)
402
+ expect(box.current!.scrollTop).toBe(600)
403
+ heights.scroll = 1300
404
+ resized()
405
+ expect(box.current!.scrollTop).toBe(900)
406
+ scrollTo(box.current!, 100)
407
+ heights.scroll = 1500
408
+ resized()
409
+ expect(box.current!.scrollTop).toBe(100)
410
+ })
411
+ })
@@ -1,5 +1,5 @@
1
1
  import type { Meta, StoryObj } from '@storybook/react-vite'
2
- import { useState } from 'react'
2
+ import { useState, type ReactNode } from 'react'
3
3
  import { ConversationCard, ReplyCard } from './ConversationCard'
4
4
  import { ConversationLayout, type ConversationItem } from './ConversationLayout'
5
5
  import { ONE, SHORT, SOME, THREE, TWO, WORDS, WriteBox, YOU, useReactions } from './conversation.examples'
@@ -8,6 +8,7 @@ import { Panel } from './Panel'
8
8
  import { ReactionHorizon } from './ReactionHorizon'
9
9
  import { QuotedLine } from './QuotedLine'
10
10
  import { SkeletonBar } from './Skeleton'
11
+ import { useSeenOnScreen } from './seenOnScreen'
11
12
  import { Thread } from './Thread'
12
13
 
13
14
  /**
@@ -107,6 +108,31 @@ export const TwoColumns: Story = {
107
108
  },
108
109
  }
109
110
 
111
+ /**
112
+ * Two columns with the page's own panel: while no thread is open, the 384px column holds it (Peek's Overview); a
113
+ * thread opened takes its place, and closing it brings the panel back. Each thread is drawn only while it is open.
114
+ */
115
+ export const TwoColumnsAside: Story = {
116
+ name: 'Two columns, with a panel',
117
+ render: function Render() {
118
+ const [open, setOpen] = useState<string | undefined>(undefined)
119
+ const items = useItems('beside', open, setOpen, false).map((item) => {
120
+ const thread = item.thread as ReactNode
121
+ return { ...item, thread: () => thread }
122
+ })
123
+ return (
124
+ <ConversationLayout
125
+ threads="beside"
126
+ items={items}
127
+ open={open}
128
+ onOpenChange={setOpen}
129
+ aside={<Page />}
130
+ box={<WriteBox label="Comment" />}
131
+ />
132
+ )
133
+ },
134
+ }
135
+
110
136
  /**
111
137
  * Two columns, as Peek's file page and DMs: each day's comments under a line naming it, which stays at the top while
112
138
  * its stretch scrolls; the reactions note under the comments. Few comments sit on the box, as a chat does.
@@ -129,6 +155,58 @@ export const TwoColumnsDays: Story = {
129
155
  },
130
156
  }
131
157
 
158
+ /**
159
+ * Two columns, a long conversation: it opens at the newest comment; scrolled up, it stays where the reader is while
160
+ * comments arrive; at the bottom, it follows them. Write one: it goes down to it as it is drawn, scrolled up or not.
161
+ * Its place is remembered by `rememberAs`: open another story and come back, and it is where it was left.
162
+ */
163
+ export const TwoColumnsLong: Story = {
164
+ name: 'Two columns, long',
165
+ render: function Render() {
166
+ const [said, setSaid] = useState<{ who: typeof ONE; words: string }[]>(() =>
167
+ Array.from({ length: 14 }, (_, i) => ({ who: [ONE, TWO, THREE][i % 3], words: i % 4 === 0 ? WORDS : `Comment ${i + 1} of a long conversation.` })),
168
+ )
169
+ const items: ConversationItem[] = said.map((c, i) => ({
170
+ id: `l${i}`,
171
+ day: i < 7 ? 'Monday' : 'Today',
172
+ card: (
173
+ <ConversationCard author={c.who} time={`${8 + (i % 7)}:${i % 2 ? '30' : '05'} AM`}>
174
+ {c.words}
175
+ </ConversationCard>
176
+ ),
177
+ }))
178
+ return (
179
+ <ConversationLayout
180
+ threads="beside"
181
+ rememberAs="story-long"
182
+ items={items}
183
+ box={<WriteBox label="Comment" onSend={(words) => setSaid((now) => [...now, { who: YOU, words: words || 'Your comment.' }])} />}
184
+ />
185
+ )
186
+ },
187
+ }
188
+
189
+ /**
190
+ * Two columns, read by being seen: each comment new to the reader keeps its dot until it has been on screen a moment
191
+ * (`useSeenOnScreen`, handed to `seen`). Scroll, and the ones that show lose it after 1.5 seconds.
192
+ */
193
+ export const TwoColumnsSeen: Story = {
194
+ name: 'Two columns, new until seen',
195
+ render: function Render() {
196
+ const seen = useSeenOnScreen('story')
197
+ const words = [WORDS, 'A second comment.', 'A third comment, further down.', 'A fourth comment.', 'A fifth comment.', 'A sixth comment.', 'The newest comment.']
198
+ const items: ConversationItem[] = words.map((said, i) => ({
199
+ id: `n${i}`,
200
+ card: (
201
+ <ConversationCard author={i % 2 ? TWO : ONE} time={`${9 + i}:00 AM`} isNew={i >= 3 && !seen.ids.has(`n${i}`)}>
202
+ {said}
203
+ </ConversationCard>
204
+ ),
205
+ }))
206
+ return <ConversationLayout threads="beside" seen={seen} items={items} box={<WriteBox label="Comment" />} />
207
+ },
208
+ }
209
+
132
210
  /** Two columns, an archived file: its card at the top however few the comments, the box's line saying why nothing can be written. */
133
211
  export const TwoColumnsArchived: Story = {
134
212
  name: 'Two columns, archived',
@@ -188,6 +266,34 @@ export const Apart: Story = {
188
266
  },
189
267
  }
190
268
 
269
+ /**
270
+ * One column, a note under the comments (`below`), here a `ReactionHorizon`: after the groups, at the column's foot, so
271
+ * with few comments it sits on the box. The comments stay at the top.
272
+ */
273
+ export const OneColumnBelow: Story = {
274
+ name: 'One column, a note under the comments',
275
+ render: function Render() {
276
+ const [open, setOpen] = useState<string | undefined>(undefined)
277
+ return (
278
+ <>
279
+ <div className="flex min-w-0 flex-1 flex-col">
280
+ <Page />
281
+ </div>
282
+ <div className="flex border-l border-border-subtle">
283
+ <ConversationLayout
284
+ threads="under"
285
+ items={useItems('under', open, setOpen, true, true)}
286
+ open={open}
287
+ onOpenChange={setOpen}
288
+ below={<ReactionHorizon limit={100} omitted={3} />}
289
+ box={<WriteBox label="Comment" />}
290
+ />
291
+ </div>
292
+ </>
293
+ )
294
+ },
295
+ }
296
+
191
297
  /** Nobody has commented yet. */
192
298
  export const Empty: Story = {
193
299
  render: () => (