@tldraw/commenting 5.7.0-canary.dcd1f2f4ac29 → 5.7.0-canary.e3e1885f07be

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 (46) hide show
  1. package/commenting.css +21 -3
  2. package/dist-cjs/canvas/comments-sidebar.js +12 -2
  3. package/dist-cjs/canvas/comments-sidebar.js.map +2 -2
  4. package/dist-cjs/canvas/options.js.map +2 -2
  5. package/dist-cjs/canvas/pending-composer.js +4 -2
  6. package/dist-cjs/canvas/pending-composer.js.map +2 -2
  7. package/dist-cjs/canvas/thread-pin.js +2 -1
  8. package/dist-cjs/canvas/thread-pin.js.map +2 -2
  9. package/dist-cjs/canvas/thread-view.js +37 -4
  10. package/dist-cjs/canvas/thread-view.js.map +2 -2
  11. package/dist-cjs/index.d.ts +32 -4
  12. package/dist-cjs/index.js +1 -1
  13. package/dist-cjs/ui/byline.js +2 -2
  14. package/dist-cjs/ui/byline.js.map +2 -2
  15. package/dist-cjs/ui/comment-card.js +3 -1
  16. package/dist-cjs/ui/comment-card.js.map +2 -2
  17. package/dist-cjs/ui/comments-list.js +2 -1
  18. package/dist-cjs/ui/comments-list.js.map +2 -2
  19. package/dist-esm/canvas/comments-sidebar.mjs +12 -2
  20. package/dist-esm/canvas/comments-sidebar.mjs.map +2 -2
  21. package/dist-esm/canvas/options.mjs.map +2 -2
  22. package/dist-esm/canvas/pending-composer.mjs +4 -2
  23. package/dist-esm/canvas/pending-composer.mjs.map +2 -2
  24. package/dist-esm/canvas/thread-pin.mjs +3 -2
  25. package/dist-esm/canvas/thread-pin.mjs.map +2 -2
  26. package/dist-esm/canvas/thread-view.mjs +37 -4
  27. package/dist-esm/canvas/thread-view.mjs.map +2 -2
  28. package/dist-esm/index.d.mts +32 -4
  29. package/dist-esm/index.mjs +1 -1
  30. package/dist-esm/ui/byline.mjs +2 -2
  31. package/dist-esm/ui/byline.mjs.map +2 -2
  32. package/dist-esm/ui/comment-card.mjs +3 -1
  33. package/dist-esm/ui/comment-card.mjs.map +2 -2
  34. package/dist-esm/ui/comments-list.mjs +2 -1
  35. package/dist-esm/ui/comments-list.mjs.map +2 -2
  36. package/package.json +5 -5
  37. package/src/canvas/comments-sidebar.tsx +15 -3
  38. package/src/canvas/options.ts +21 -2
  39. package/src/canvas/pending-composer.tsx +6 -2
  40. package/src/canvas/thread-pin.tsx +5 -2
  41. package/src/canvas/thread-view.test.ts +39 -0
  42. package/src/canvas/thread-view.tsx +50 -5
  43. package/src/ui/byline.tsx +5 -2
  44. package/src/ui/comment-card.tsx +7 -1
  45. package/src/ui/comments-list.tsx +4 -1
  46. package/src/ui/comments.css +21 -3
@@ -1,4 +1,5 @@
1
1
  import type { CommentAuthor } from '@tldraw/mentions'
2
+ import { isValidElement, type ReactElement } from 'react'
2
3
  import type { TLComment } from 'tldraw'
3
4
  import { describe, expect, it } from 'vitest'
4
5
  import { absoluteThreadLink, toCardProps } from './thread-view'
@@ -41,6 +42,44 @@ describe('toCardProps', () => {
41
42
  expect(card.you).toBe(false)
42
43
  expect(card.author).not.toBe(AUTHOR)
43
44
  })
45
+
46
+ it('leaves the byline and avatar to the card when no slots are set', () => {
47
+ const card = toCardProps(comment(), context, {}, resolveName)
48
+ expect(card.byline).toBeUndefined()
49
+ expect(card.renderAvatar).toBeUndefined()
50
+ })
51
+
52
+ it('renders the Avatar slot for the author, with the id they were resolved from', () => {
53
+ const Avatar = () => null
54
+ const card = toCardProps(comment(), context, { Avatar }, resolveName)
55
+ const avatar = card.renderAvatar?.(AUTHOR) as ReactElement<Record<string, unknown>>
56
+ expect(avatar.type).toBe(Avatar)
57
+ expect(avatar.props).toEqual({ author: AUTHOR, authorId: 'user:1' })
58
+ })
59
+
60
+ it('renders the CommentByline slot with the byline props and the comment record', () => {
61
+ const CommentByline = () => null
62
+ const record = comment({ editedAt: Date.parse('2026-07-30T13:00:00.000Z') })
63
+ const card = toCardProps(record, context, { CommentByline }, resolveName)
64
+ expect(isValidElement(card.byline)).toBe(true)
65
+ const byline = card.byline as ReactElement<Record<string, unknown>>
66
+ expect(byline.type).toBe(CommentByline)
67
+ expect(byline.props).toEqual({
68
+ author: AUTHOR,
69
+ date: '2026-07-30T12:00:00.000Z',
70
+ edited: true,
71
+ renderAvatar: undefined,
72
+ comment: record,
73
+ })
74
+ })
75
+
76
+ it('hands the CommentByline slot the Avatar slot, so a spread into Byline keeps it', () => {
77
+ const Avatar = () => null
78
+ const CommentByline = () => null
79
+ const card = toCardProps(comment(), context, { Avatar, CommentByline }, resolveName)
80
+ const byline = card.byline as ReactElement<Record<string, unknown>>
81
+ expect(byline.props.renderAvatar).toBe(card.renderAvatar)
82
+ })
44
83
  })
45
84
 
46
85
  describe('absoluteThreadLink', () => {
@@ -1,3 +1,4 @@
1
+ import { type CommentAuthor } from '@tldraw/mentions'
1
2
  import { memo, ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react'
2
3
  import {
3
4
  createComment,
@@ -185,6 +186,19 @@ function useCopyLink(href: string | undefined) {
185
186
  return [copied, copy] as const
186
187
  }
187
188
 
189
+ /**
190
+ * The `Avatar` component slot as a `renderAvatar` callback for the presentational components, bound
191
+ * to the id the avatar stands for. Undefined without an override, so they draw their own default.
192
+ */
193
+ export function avatarRenderer(
194
+ components: CommentingComponents,
195
+ authorId: string | null
196
+ ): ((author: CommentAuthor) => ReactNode) | undefined {
197
+ const Avatar = components.Avatar
198
+ if (!Avatar) return undefined
199
+ return (author) => <Avatar author={author} authorId={authorId} />
200
+ }
201
+
188
202
  export function toCardProps(
189
203
  comment: TLComment,
190
204
  props: Pick<CommentingContext, 'currentUserId' | 'resolveAuthor'>,
@@ -199,12 +213,27 @@ export function toCardProps(
199
213
  ) : (
200
214
  <CommentBody richText={comment.body} resolveName={resolveName} />
201
215
  )
216
+ const author = props.resolveAuthor(comment.authorId) ?? UNKNOWN_COMMENT_AUTHOR
217
+ const date = new Date(comment.createdAt).toISOString()
218
+ const edited = comment.editedAt != null
219
+ const renderAvatar = avatarRenderer(components, comment.authorId)
220
+ const Byline = components.CommentByline
202
221
  return {
203
- author: props.resolveAuthor(comment.authorId) ?? UNKNOWN_COMMENT_AUTHOR,
222
+ author,
204
223
  body,
205
- date: new Date(comment.createdAt).toISOString(),
224
+ date,
206
225
  you: comment.authorId === props.currentUserId,
207
- edited: comment.editedAt != null,
226
+ edited,
227
+ renderAvatar,
228
+ byline: Byline ? (
229
+ <Byline
230
+ author={author}
231
+ date={date}
232
+ edited={edited}
233
+ renderAvatar={renderAvatar}
234
+ comment={comment}
235
+ />
236
+ ) : undefined,
208
237
  }
209
238
  }
210
239
 
@@ -345,6 +374,9 @@ export const ThreadView = memo(function ThreadView({
345
374
  // Resolved when the composer closes rather than held as an element: the card unmounts while the
346
375
  // composer stands in its place, so the node captured on the way in is gone by the way out.
347
376
  const editReturnFocus = useRef<(() => HTMLElement | null) | null>(null)
377
+ // Whether the edit was last driven from the keyboard (Escape, or Enter to save) rather than by a
378
+ // click on Save. See the focus hand-back below.
379
+ const editEndedByKeyboard = useRef(false)
348
380
 
349
381
  // Tab from the canvas drops the caret in the reply box: a pin click leaves focus on the editor
350
382
  // container, so the first Tab would otherwise walk the app's UI. Capture phase, once per open thread.
@@ -395,8 +427,12 @@ export const ThreadView = memo(function ThreadView({
395
427
  if (editingId !== null) return
396
428
  const resolve = editReturnFocus.current
397
429
  editReturnFocus.current = null
398
- // Runs after React has swapped the card back in, so the target is mounted again by now.
399
- resolve?.()?.focus()
430
+ // Runs after React has swapped the card back in, so the target is mounted again by now. Shown
431
+ // as keyboard focus only after a keyboard edit: that is what reveals a card's actions, so after
432
+ // a click on Save they'd stay up for a pointer that has moved away, but after Escape or Enter a
433
+ // keyboard user would otherwise be left on an invisible ⋯ button with no ring.
434
+ resolve?.()?.focus({ focusVisible: editEndedByKeyboard.current })
435
+ editEndedByKeyboard.current = false
400
436
  }, [editingId])
401
437
 
402
438
  // Reported as one batch so the host can record the receipts in a single write. The write flips
@@ -473,7 +509,11 @@ export const ThreadView = memo(function ThreadView({
473
509
  return (
474
510
  <div
475
511
  className="tlui-cmt-editing"
512
+ onPointerDown={() => {
513
+ editEndedByKeyboard.current = false
514
+ }}
476
515
  onKeyDown={(e) => {
516
+ editEndedByKeyboard.current = true
477
517
  if (e.key === 'Escape') {
478
518
  setEditingId(null)
479
519
  e.stopPropagation()
@@ -482,6 +522,7 @@ export const ThreadView = memo(function ThreadView({
482
522
  >
483
523
  <CommentComposer
484
524
  author={card.author}
525
+ leading={card.renderAvatar?.(card.author)}
485
526
  placeholder={msg(messages.commentsEditPlaceholder)}
486
527
  value={editText}
487
528
  onChange={setEditText}
@@ -600,6 +641,10 @@ export const ThreadView = memo(function ThreadView({
600
641
  canReply
601
642
  ? {
602
643
  author: me ?? UNKNOWN_COMMENT_AUTHOR,
644
+ leading: avatarRenderer(
645
+ options.components,
646
+ currentUserId
647
+ )?.(me ?? UNKNOWN_COMMENT_AUTHOR),
603
648
  placeholder: msg(messages.commentsReplyPlaceholder),
604
649
  sendLabel: msg(messages.commentsSend),
605
650
  value: reply,
package/src/ui/byline.tsx CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Avatar, type CommentAuthor } from '@tldraw/mentions'
2
+ import { type ReactNode } from 'react'
2
3
  import { TldrawUiTooltip, useCurrentTranslation } from 'tldraw'
3
4
  import { formatFullDateTime, formatRelativeTime } from './format-time'
4
5
 
@@ -9,6 +10,8 @@ export interface BylineProps {
9
10
  date: string
10
11
  /** Shows an "edited" marker when the comment has been edited. */
11
12
  edited?: boolean
13
+ /** Draws the author's avatar. Defaults to `<Avatar>`. */
14
+ renderAvatar?(author: CommentAuthor): ReactNode
12
15
  }
13
16
 
14
17
  /**
@@ -16,13 +19,13 @@ export interface BylineProps {
16
19
  * shows the full date and time in a tooltip.
17
20
  * @public @react
18
21
  */
19
- export function Byline({ author, date, edited }: BylineProps) {
22
+ export function Byline({ author, date, edited, renderAvatar }: BylineProps) {
20
23
  // The relative time is the one piece of a byline that isn't the host's data, so it follows the
21
24
  // same translation context as every other string in the UI rather than a prop of its own.
22
25
  const { locale } = useCurrentTranslation()
23
26
  return (
24
27
  <div className="tlui-cmt-head">
25
- <Avatar author={author} />
28
+ {renderAvatar ? renderAvatar(author) : <Avatar author={author} />}
26
29
  <span className="tlui-cmt-author">{author.name}</span>
27
30
  <TldrawUiTooltip content={formatFullDateTime(date, locale)}>
28
31
  <span className="tlui-cmt-time">
@@ -13,6 +13,10 @@ export interface CommentCardProps {
13
13
  you: boolean
14
14
  /** Whether the comment has been edited (shows an "edited" marker). */
15
15
  edited?: boolean
16
+ /** Replaces the default `<Byline>` built from `author`, `date`, and `edited`. */
17
+ byline?: ReactNode
18
+ /** Draws the default byline's avatar. Defaults to `<Avatar>`. */
19
+ renderAvatar?(author: CommentAuthor): ReactNode
16
20
  /** Hover-revealed controls at the card's top-right (e.g. an edit affordance). */
17
21
  actions?: ReactNode
18
22
  /** Content under the body, aligned with it rather than the avatar (e.g. a `<Reactions>` row). */
@@ -26,12 +30,14 @@ export function CommentCard({
26
30
  date,
27
31
  you,
28
32
  edited,
33
+ byline,
34
+ renderAvatar,
29
35
  actions,
30
36
  footer,
31
37
  }: CommentCardProps) {
32
38
  return (
33
39
  <div className={you ? 'tlui-cmt-card tlui-cmt-card--you' : 'tlui-cmt-card'}>
34
- <Byline author={author} date={date} edited={edited} />
40
+ {byline ?? <Byline author={author} date={date} edited={edited} renderAvatar={renderAvatar} />}
35
41
  <div className="tlui-cmt-body">
36
42
  {body}
37
43
  {footer}
@@ -24,6 +24,8 @@ export interface CommentListItemProps {
24
24
  /** Tallied reactions for the row (a thread's, or a single comment's when the row is one
25
25
  * comment), shown as inert pills under the preview. Omit to hide. */
26
26
  reactions?: ReactionSummary[]
27
+ /** Draws the author's avatar. Defaults to `<Avatar>`. */
28
+ renderAvatar?(author: CommentAuthor): ReactNode
27
29
  /**
28
30
  * Link target for the item. When set, the row renders as an anchor so browser affordances
29
31
  * (ctrl/cmd-click, middle-click) open it in a new tab; a plain click still calls `onSelect`.
@@ -122,6 +124,7 @@ export function CommentListItem({
122
124
  selected,
123
125
  reactions,
124
126
  href,
127
+ renderAvatar,
125
128
  resolvedLabel = 'Resolved',
126
129
  onSelect,
127
130
  }: CommentListItemRenderProps) {
@@ -143,7 +146,7 @@ export function CommentListItem({
143
146
  data-resolved={resolved || undefined}
144
147
  onClick={handleClick}
145
148
  >
146
- <Avatar author={author} />
149
+ {renderAvatar ? renderAvatar(author) : <Avatar author={author} />}
147
150
  <div className="tlui-cmt-list__item-body">
148
151
  <Byline author={author} date={date} />
149
152
  <div className="tlui-cmt-list__item-preview">{preview}</div>
@@ -28,12 +28,22 @@
28
28
  opacity: 0;
29
29
  }
30
30
  .tlui-cmt-card:hover .tlui-cmt-card__actions,
31
- .tlui-cmt-card:focus-within .tlui-cmt-card__actions,
32
- /* an open menu is portaled out of the card, so neither :hover nor :focus-within survives moving
33
- the pointer into it — keep the controls up while their menu is open */
31
+ /* Keyboard focus, not any focus: closing an edit hands focus back to the card's ⋯ button, and that
32
+ alone mustn't leave the controls up for a pointer that has moved away. */
33
+ .tlui-cmt-card:has(:focus-visible) .tlui-cmt-card__actions,
34
+ /* an open menu is portaled out of the card, so neither :hover nor focus survives moving the
35
+ pointer into it — keep the controls up while their menu is open */
34
36
  .tlui-cmt-card__actions:has([data-state='open']) {
35
37
  opacity: 1;
36
38
  }
39
+ /* The editor only rings a focused button once Tab or an arrow key has turned its focus ring on, but
40
+ leaving an edit with Escape or Enter hands keyboard focus to the card's ⋯ button without either,
41
+ which would leave the keyboard user nothing showing where focus went. Same ring as the editor's. */
42
+ .tlui-cmt-card__actions .tlui-button:focus-visible {
43
+ border-radius: 10px;
44
+ outline: 2px solid var(--tl-color-focus);
45
+ outline-offset: -5px;
46
+ }
37
47
 
38
48
  .tlui-cmt-body {
39
49
  flex: 1;
@@ -618,6 +628,14 @@ button.tlui-cmt-reaction--active:hover {
618
628
  .tlui-button.tlui-cmt-thread__action:hover {
619
629
  color: var(--tl-color-text-1);
620
630
  }
631
+ /* An open menu's trigger, shaded the way the editor's own menu triggers are. */
632
+ .tlui-button.tlui-cmt-thread__action[data-state='open'] {
633
+ color: var(--tl-color-text-1);
634
+ }
635
+ .tlui-button.tlui-cmt-thread__action[data-state='open']::after {
636
+ background: linear-gradient(180deg, rgba(144, 144, 144, 0) 0%, var(--tl-color-muted-2) 100%);
637
+ opacity: 1;
638
+ }
621
639
  .tlui-cmt-thread__resolved {
622
640
  margin: 4px 12px;
623
641
  padding: 6px 10px;