@edux-design/comments 0.1.4 → 0.2.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.
@@ -1,593 +0,0 @@
1
- import React from "react";
2
- import { Comment, CommentComposer, CommentMention } from "../index";
3
-
4
- export default {
5
- title: "Content/Comment",
6
- component: Comment,
7
- };
8
-
9
- export const Basic = (args) => (
10
- <div className="max-w-xl space-y-12">
11
- <Comment
12
- {...args}
13
- author="Alex Johnson"
14
- timestamp="2h ago"
15
- body="Left a few notes on the introduction. Let me know if you want me to draft the edits."
16
- avatarProps={{ name: "Alex Johnson" }}
17
- >
18
- <Comment.Actions>
19
- <Comment.Reply />
20
- <Comment.Edit />
21
- </Comment.Actions>
22
- </Comment>
23
-
24
- <Comment
25
- author="Priya Singh"
26
- timestamp="Yesterday"
27
- body="Marked as resolved after the update."
28
- avatarProps={{ name: "Priya Singh" }}
29
- variant="resolved"
30
- replies={
31
- <>
32
- <Comment
33
- author="Taylor"
34
- timestamp="2h ago"
35
- body="Thanks! I added the sources in section 3."
36
- avatarProps={{ name: "Taylor" }}
37
- >
38
- <Comment.Actions>
39
- <Comment.Reply />
40
- <Comment.Edit />
41
- </Comment.Actions>
42
- </Comment>
43
- <Comment
44
- author="Jordan"
45
- timestamp="1h ago"
46
- body="Confirmed the citations are correct."
47
- avatarProps={{ name: "Jordan" }}
48
- />
49
- <Comment
50
- author="Morgan"
51
- timestamp="45m ago"
52
- body="All set from my end."
53
- avatarProps={{ name: "Morgan" }}
54
- />
55
- </>
56
- }
57
- >
58
- <Comment.Actions>
59
- <Comment.Resolve />
60
- <Comment.Edit />
61
- </Comment.Actions>
62
- </Comment>
63
-
64
- <Comment
65
- author="Jordan Lee"
66
- timestamp="Yesterday"
67
- body="Resolved and ready to re-open if needed."
68
- avatarProps={{ name: "Jordan Lee" }}
69
- resolved
70
- >
71
- <Comment.Actions>
72
- <Comment.Resolve />
73
- <Comment.Resolve label="Re-open thread" />
74
- </Comment.Actions>
75
- </Comment>
76
- </div>
77
- );
78
-
79
- Basic.args = {};
80
-
81
- export const Thread = () => {
82
- const [composerValue, setComposerValue] = React.useState("");
83
- const [lastAddedId, setLastAddedId] = React.useState(null);
84
- const [resolvedIds, setResolvedIds] = React.useState(new Set());
85
- const [animationPhase, setAnimationPhase] = React.useState("idle");
86
- const [comments, setComments] = React.useState([
87
- {
88
- id: "c-1",
89
- author: "Alex Johnson",
90
- timestamp: "2h ago",
91
- body: "Left a few notes on the introduction. Let me know if you want me to draft the edits.",
92
- replies: [
93
- {
94
- id: "c-1-1",
95
- author: "Taylor",
96
- timestamp: "1h ago",
97
- body: "Happy to take the edits.",
98
- },
99
- ],
100
- },
101
- ]);
102
- const [activeReplyId, setActiveReplyId] = React.useState(null);
103
- const [recentlyResolvedId, setRecentlyResolvedId] = React.useState(null);
104
- const [recentlyReopenedId, setRecentlyReopenedId] = React.useState(null);
105
-
106
- const handleAddComment = (event) => {
107
- event.preventDefault();
108
- const trimmed = composerValue.trim();
109
- if (!trimmed) return;
110
-
111
- const newComment = {
112
- id: `c-${Date.now()}`,
113
- author: "You",
114
- timestamp: "Just now",
115
- body: trimmed,
116
- replies: [],
117
- };
118
-
119
- setComments((prev) => [newComment, ...prev]);
120
- setLastAddedId(newComment.id);
121
- setComposerValue("");
122
- };
123
-
124
- const handleAddReply = (commentId, replyText) => {
125
- const trimmed = replyText.trim();
126
- if (!trimmed) return;
127
-
128
- const replyId = `${commentId}-${Date.now()}`;
129
- setComments((prev) =>
130
- prev.map((comment) =>
131
- comment.id === commentId
132
- ? {
133
- ...comment,
134
- replies: [
135
- ...comment.replies,
136
- {
137
- id: replyId,
138
- author: "You",
139
- timestamp: "Just now",
140
- body: trimmed,
141
- },
142
- ],
143
- }
144
- : comment
145
- )
146
- );
147
- setLastAddedId(replyId);
148
- };
149
-
150
- const handleResolve = (commentId) => {
151
- setResolvedIds((prev) => {
152
- const next = new Set(prev);
153
- if (next.has(commentId)) {
154
- next.delete(commentId);
155
- setRecentlyReopenedId(commentId);
156
- } else {
157
- next.add(commentId);
158
- setRecentlyResolvedId(commentId);
159
- }
160
- return next;
161
- });
162
- setActiveReplyId((prev) => (prev === commentId ? null : prev));
163
- setLastAddedId(commentId);
164
- };
165
-
166
- React.useEffect(() => {
167
- if (!lastAddedId) return;
168
- setAnimationPhase("enter");
169
- const raf = requestAnimationFrame(() => {
170
- setAnimationPhase("entered");
171
- });
172
- const timeout = setTimeout(() => {
173
- setAnimationPhase("idle");
174
- setLastAddedId(null);
175
- }, 320);
176
- return () => {
177
- cancelAnimationFrame(raf);
178
- clearTimeout(timeout);
179
- };
180
- }, [lastAddedId]);
181
-
182
- React.useEffect(() => {
183
- if (!recentlyResolvedId) return;
184
- const timeout = setTimeout(() => {
185
- setRecentlyResolvedId(null);
186
- }, 320);
187
- return () => clearTimeout(timeout);
188
- }, [recentlyResolvedId]);
189
-
190
- React.useEffect(() => {
191
- if (!recentlyReopenedId) return;
192
- const timeout = setTimeout(() => {
193
- setRecentlyReopenedId(null);
194
- }, 320);
195
- return () => clearTimeout(timeout);
196
- }, [recentlyReopenedId]);
197
-
198
- const getAnimationProps = (id) => {
199
- if (id !== lastAddedId) return {};
200
- return {
201
- className: [
202
- "transition-all duration-300 ease-out",
203
- animationPhase === "enter" && "opacity-0 translate-y-2",
204
- animationPhase === "entered" && "opacity-100 translate-y-0",
205
- id === recentlyResolvedId && "ring-1 ring-border-success-base",
206
- id === recentlyReopenedId && "ring-1 ring-border-info-base",
207
- ]
208
- .filter(Boolean)
209
- .join(" "),
210
- };
211
- };
212
-
213
- return (
214
- <div className="max-w-2xl space-y-16">
215
- <CommentComposer
216
- value={composerValue}
217
- onChange={(event) => setComposerValue(event.target.value)}
218
- onSubmit={handleAddComment}
219
- submitLabel="Add comment"
220
- avatarProps={{ name: "You" }}
221
- rows={3}
222
- />
223
-
224
- <div className="space-y-12">
225
- {comments.map((comment) => (
226
- <Comment
227
- key={comment.id}
228
- author={comment.author}
229
- timestamp={comment.timestamp}
230
- body={comment.body}
231
- avatarProps={{ name: comment.author }}
232
- resolved={resolvedIds.has(comment.id)}
233
- {...getAnimationProps(comment.id)}
234
- onResolve={() => handleResolve(comment.id)}
235
- replies={
236
- <div className="space-y-8">
237
- {comment.replies.map((reply) => (
238
- <Comment
239
- key={reply.id}
240
- author={reply.author}
241
- timestamp={reply.timestamp}
242
- body={reply.body}
243
- avatarProps={{ name: reply.author }}
244
- {...getAnimationProps(reply.id)}
245
- />
246
- ))}
247
- {activeReplyId === comment.id && (
248
- <ReplyComposer
249
- onSubmit={(value) => {
250
- handleAddReply(comment.id, value);
251
- setActiveReplyId(null);
252
- }}
253
- />
254
- )}
255
- </div>
256
- }
257
- >
258
- <Comment.Actions>
259
- {!resolvedIds.has(comment.id) ? (
260
- <Comment.Reply
261
- onClick={() =>
262
- setActiveReplyId((prev) =>
263
- prev === comment.id ? null : comment.id
264
- )
265
- }
266
- />
267
- ) : null}
268
- <Comment.Resolve />
269
- </Comment.Actions>
270
- </Comment>
271
- ))}
272
- </div>
273
- </div>
274
- );
275
- };
276
-
277
- const PEOPLE = [
278
- {
279
- id: "alex",
280
- name: "Alex Johnson",
281
- handle: "alex",
282
- avatarProps: { name: "Alex Johnson" },
283
- },
284
- {
285
- id: "priya",
286
- name: "Priya Singh",
287
- handle: "priya",
288
- avatarProps: { name: "Priya Singh" },
289
- },
290
- {
291
- id: "taylor",
292
- name: "Taylor Reed",
293
- handle: "taylor",
294
- avatarProps: { name: "Taylor Reed" },
295
- },
296
- ];
297
-
298
- export const ThreadInteractive = () => {
299
- const [composerValue, setComposerValue] = React.useState("");
300
- const [lastAddedId, setLastAddedId] = React.useState(null);
301
- const [resolvedIds, setResolvedIds] = React.useState(new Set());
302
- const [animationPhase, setAnimationPhase] = React.useState("idle");
303
- const [comments, setComments] = React.useState([
304
- {
305
- id: "c-1",
306
- author: "Alex Johnson",
307
- timestamp: "2h ago",
308
- body: "Left a few notes on the introduction. Let me know if you want me to draft the edits.",
309
- replies: [
310
- {
311
- id: "c-1-1",
312
- author: "Taylor",
313
- timestamp: "1h ago",
314
- body: "Happy to take the edits.",
315
- },
316
- ],
317
- },
318
- ]);
319
- const [activeReplyId, setActiveReplyId] = React.useState(null);
320
- const [editingId, setEditingId] = React.useState(null);
321
- const [editingValue, setEditingValue] = React.useState("");
322
- const [recentlyResolvedId, setRecentlyResolvedId] = React.useState(null);
323
- const [recentlyReopenedId, setRecentlyReopenedId] = React.useState(null);
324
-
325
- const handleAddComment = (event) => {
326
- event.preventDefault();
327
- const trimmed = composerValue.trim();
328
- if (!trimmed) return;
329
-
330
- const newComment = {
331
- id: `c-${Date.now()}`,
332
- author: "You",
333
- timestamp: "Just now",
334
- body: trimmed,
335
- replies: [],
336
- };
337
-
338
- setComments((prev) => [newComment, ...prev]);
339
- setLastAddedId(newComment.id);
340
- setComposerValue("");
341
- };
342
-
343
- const handleAddReply = (commentId, replyText) => {
344
- const trimmed = replyText.trim();
345
- if (!trimmed) return;
346
-
347
- const replyId = `${commentId}-${Date.now()}`;
348
- setComments((prev) =>
349
- prev.map((comment) =>
350
- comment.id === commentId
351
- ? {
352
- ...comment,
353
- replies: [
354
- ...comment.replies,
355
- {
356
- id: replyId,
357
- author: "You",
358
- timestamp: "Just now",
359
- body: trimmed,
360
- },
361
- ],
362
- }
363
- : comment
364
- )
365
- );
366
- setLastAddedId(replyId);
367
- };
368
-
369
- const handleResolve = (commentId) => {
370
- setResolvedIds((prev) => {
371
- const next = new Set(prev);
372
- if (next.has(commentId)) {
373
- next.delete(commentId);
374
- setRecentlyReopenedId(commentId);
375
- } else {
376
- next.add(commentId);
377
- setRecentlyResolvedId(commentId);
378
- }
379
- return next;
380
- });
381
- setActiveReplyId((prev) => (prev === commentId ? null : prev));
382
- setLastAddedId(commentId);
383
- };
384
-
385
- const handleStartEdit = (comment) => {
386
- setEditingId(comment.id);
387
- setEditingValue(comment.body);
388
- setActiveReplyId((prev) => (prev === comment.id ? null : prev));
389
- };
390
-
391
- const handleSaveEdit = (commentId) => {
392
- const trimmed = editingValue.trim();
393
- if (!trimmed) return;
394
- setComments((prev) =>
395
- prev.map((comment) =>
396
- comment.id === commentId ? { ...comment, body: trimmed } : comment
397
- )
398
- );
399
- setEditingId(null);
400
- setEditingValue("");
401
- };
402
-
403
- const handleCancelEdit = () => {
404
- setEditingId(null);
405
- setEditingValue("");
406
- };
407
-
408
- React.useEffect(() => {
409
- if (!lastAddedId) return;
410
- setAnimationPhase("enter");
411
- const raf = requestAnimationFrame(() => {
412
- setAnimationPhase("entered");
413
- });
414
- const timeout = setTimeout(() => {
415
- setAnimationPhase("idle");
416
- setLastAddedId(null);
417
- }, 320);
418
- return () => {
419
- cancelAnimationFrame(raf);
420
- clearTimeout(timeout);
421
- };
422
- }, [lastAddedId]);
423
-
424
- React.useEffect(() => {
425
- if (!recentlyResolvedId) return;
426
- const timeout = setTimeout(() => {
427
- setRecentlyResolvedId(null);
428
- }, 320);
429
- return () => clearTimeout(timeout);
430
- }, [recentlyResolvedId]);
431
-
432
- React.useEffect(() => {
433
- if (!recentlyReopenedId) return;
434
- const timeout = setTimeout(() => {
435
- setRecentlyReopenedId(null);
436
- }, 320);
437
- return () => clearTimeout(timeout);
438
- }, [recentlyReopenedId]);
439
-
440
- const getAnimationProps = (id) => {
441
- if (id !== lastAddedId) return {};
442
- return {
443
- className: [
444
- "transition-all duration-300 ease-out",
445
- animationPhase === "enter" && "opacity-0 translate-y-2",
446
- animationPhase === "entered" && "opacity-100 translate-y-0",
447
- id === recentlyResolvedId && "ring-1 ring-border-success-base",
448
- id === recentlyReopenedId && "ring-1 ring-border-info-base",
449
- ]
450
- .filter(Boolean)
451
- .join(" "),
452
- };
453
- };
454
-
455
- return (
456
- <div className="max-w-2xl space-y-16">
457
- <CommentComposer
458
- value={composerValue}
459
- onChange={(event) => setComposerValue(event.target.value)}
460
- onSubmit={handleAddComment}
461
- onCancel={() => setComposerValue("")}
462
- submitLabel="Add comment"
463
- avatarProps={{ name: "You" }}
464
- rows={4}
465
- >
466
- <CommentMention peopleList={PEOPLE} />
467
- </CommentComposer>
468
-
469
- <div className="space-y-12">
470
- {comments.map((comment) => (
471
- <Comment
472
- key={comment.id}
473
- author={comment.author}
474
- timestamp={comment.timestamp}
475
- body={
476
- editingId === comment.id ? (
477
- <CommentComposer
478
- value={editingValue}
479
- onChange={(event) => setEditingValue(event.target.value)}
480
- onSubmit={(event) => {
481
- event.preventDefault();
482
- handleSaveEdit(comment.id);
483
- }}
484
- onCancel={handleCancelEdit}
485
- submitLabel="Save"
486
- cancelLabel="Cancel"
487
- rows={3}
488
- />
489
- ) : (
490
- comment.body
491
- )
492
- }
493
- avatarProps={{ name: comment.author }}
494
- resolved={resolvedIds.has(comment.id)}
495
- {...getAnimationProps(comment.id)}
496
- onResolve={() => handleResolve(comment.id)}
497
- replies={
498
- <div className="space-y-8">
499
- {comment.replies.map((reply) => (
500
- <Comment
501
- key={reply.id}
502
- author={reply.author}
503
- timestamp={reply.timestamp}
504
- body={reply.body}
505
- avatarProps={{ name: reply.author }}
506
- {...getAnimationProps(reply.id)}
507
- />
508
- ))}
509
- {activeReplyId === comment.id && (
510
- <ReplyComposerWithMentions
511
- onSubmit={(value) => {
512
- handleAddReply(comment.id, value);
513
- setActiveReplyId(null);
514
- }}
515
- onCancel={() => setActiveReplyId(null)}
516
- />
517
- )}
518
- </div>
519
- }
520
- >
521
- <Comment.Actions>
522
- {!resolvedIds.has(comment.id) ? (
523
- <Comment.Reply
524
- onClick={() =>
525
- setActiveReplyId((prev) =>
526
- prev === comment.id ? null : comment.id
527
- )
528
- }
529
- />
530
- ) : null}
531
- {!resolvedIds.has(comment.id) ? (
532
- <Comment.Edit onClick={() => handleStartEdit(comment)} />
533
- ) : null}
534
- <Comment.Resolve />
535
- </Comment.Actions>
536
- </Comment>
537
- ))}
538
- </div>
539
- </div>
540
- );
541
- };
542
-
543
- const ReplyComposerWithMentions = ({ onSubmit, onCancel }) => {
544
- const [value, setValue] = React.useState("");
545
-
546
- const handleSubmit = (event) => {
547
- event.preventDefault();
548
- const trimmed = value.trim();
549
- if (!trimmed) return;
550
- onSubmit(trimmed);
551
- setValue("");
552
- };
553
-
554
- return (
555
- <CommentComposer
556
- value={value}
557
- onChange={(event) => setValue(event.target.value)}
558
- onSubmit={handleSubmit}
559
- onCancel={() => {
560
- setValue("");
561
- onCancel?.();
562
- }}
563
- submitLabel="Add reply"
564
- avatarProps={{ name: "You" }}
565
- rows={3}
566
- >
567
- <CommentMention peopleList={PEOPLE} />
568
- </CommentComposer>
569
- );
570
- };
571
-
572
- const ReplyComposer = ({ onSubmit }) => {
573
- const [value, setValue] = React.useState("");
574
-
575
- const handleSubmit = (event) => {
576
- event.preventDefault();
577
- const trimmed = value.trim();
578
- if (!trimmed) return;
579
- onSubmit(trimmed);
580
- setValue("");
581
- };
582
-
583
- return (
584
- <CommentComposer
585
- value={value}
586
- onChange={(event) => setValue(event.target.value)}
587
- onSubmit={handleSubmit}
588
- submitLabel="Add reply"
589
- avatarProps={{ name: "You" }}
590
- rows={2}
591
- />
592
- );
593
- };
@@ -1,66 +0,0 @@
1
- import React, { useState } from "react";
2
- import { CommentComposer, CommentMention } from "../index";
3
-
4
- export default {
5
- title: "Content/Comment Composer",
6
- component: CommentComposer,
7
- };
8
-
9
- export const Basic = () => {
10
- const [value, setValue] = useState("");
11
-
12
- return (
13
- <div className="max-w-xl">
14
- <CommentComposer
15
- value={value}
16
- onChange={(event) => setValue(event.target.value)}
17
- onSubmit={() => setValue("")}
18
- onCancel={() => setValue("")}
19
- avatarProps={{ name: "Alex Johnson" }}
20
- actions={<span className="text-xs text-fg-subtle">Markdown OK</span>}
21
- />
22
- </div>
23
- );
24
- };
25
-
26
- const PEOPLE = [
27
- {
28
- id: "alex",
29
- name: "Alex Johnson",
30
- handle: "alex",
31
- avatarProps: { name: "Alex Johnson" },
32
- },
33
- {
34
- id: "priya",
35
- name: "Priya Singh",
36
- handle: "priya",
37
- avatarProps: { name: "Priya Singh" },
38
- },
39
- {
40
- id: "taylor",
41
- name: "Taylor Reed",
42
- handle: "taylor",
43
- avatarProps: { name: "Taylor Reed" },
44
- },
45
- ];
46
-
47
- export const WithMentions = () => {
48
- const [value, setValue] = useState("");
49
- const [query, setQuery] = useState("");
50
-
51
- return (
52
- <div className="max-w-xl relative">
53
- <CommentComposer
54
- value={value}
55
- onChange={(event) => setValue(event.target.value)}
56
- placeholder="Write a comment..."
57
- avatarProps={{ name: "Alex Johnson" }}
58
- >
59
- <CommentMention peopleList={PEOPLE} onQueryChange={setQuery} />
60
- </CommentComposer>
61
- <div className="mt-8 text-xs text-fg-subtle">
62
- Mention query: <span className="font-semibold">{query || "—"}</span>
63
- </div>
64
- </div>
65
- );
66
- };