@edux-design/comments 0.1.1

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,472 @@
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.Edit />
60
+ </Comment.Actions>
61
+ </Comment>
62
+ </div>
63
+ );
64
+
65
+ Basic.args = {};
66
+
67
+ export const Thread = () => {
68
+ const [composerValue, setComposerValue] = React.useState("");
69
+ const [lastAddedId, setLastAddedId] = React.useState(null);
70
+ const [resolvedIds, setResolvedIds] = React.useState(new Set());
71
+ const [animationPhase, setAnimationPhase] = React.useState("idle");
72
+ const [comments, setComments] = React.useState([
73
+ {
74
+ id: "c-1",
75
+ author: "Alex Johnson",
76
+ timestamp: "2h ago",
77
+ body: "Left a few notes on the introduction. Let me know if you want me to draft the edits.",
78
+ replies: [
79
+ {
80
+ id: "c-1-1",
81
+ author: "Taylor",
82
+ timestamp: "1h ago",
83
+ body: "Happy to take the edits.",
84
+ },
85
+ ],
86
+ },
87
+ ]);
88
+ const [activeReplyId, setActiveReplyId] = React.useState(null);
89
+
90
+ const handleAddComment = (event) => {
91
+ event.preventDefault();
92
+ const trimmed = composerValue.trim();
93
+ if (!trimmed) return;
94
+
95
+ const newComment = {
96
+ id: `c-${Date.now()}`,
97
+ author: "You",
98
+ timestamp: "Just now",
99
+ body: trimmed,
100
+ replies: [],
101
+ };
102
+
103
+ setComments((prev) => [newComment, ...prev]);
104
+ setLastAddedId(newComment.id);
105
+ setComposerValue("");
106
+ };
107
+
108
+ const handleAddReply = (commentId, replyText) => {
109
+ const trimmed = replyText.trim();
110
+ if (!trimmed) return;
111
+
112
+ const replyId = `${commentId}-${Date.now()}`;
113
+ setComments((prev) =>
114
+ prev.map((comment) =>
115
+ comment.id === commentId
116
+ ? {
117
+ ...comment,
118
+ replies: [
119
+ ...comment.replies,
120
+ {
121
+ id: replyId,
122
+ author: "You",
123
+ timestamp: "Just now",
124
+ body: trimmed,
125
+ },
126
+ ],
127
+ }
128
+ : comment
129
+ )
130
+ );
131
+ setLastAddedId(replyId);
132
+ };
133
+
134
+ const handleResolve = (commentId) => {
135
+ setResolvedIds((prev) => new Set(prev).add(commentId));
136
+ setActiveReplyId((prev) => (prev === commentId ? null : prev));
137
+ };
138
+
139
+ React.useEffect(() => {
140
+ if (!lastAddedId) return;
141
+ setAnimationPhase("enter");
142
+ const raf = requestAnimationFrame(() => {
143
+ setAnimationPhase("entered");
144
+ });
145
+ const timeout = setTimeout(() => {
146
+ setAnimationPhase("idle");
147
+ setLastAddedId(null);
148
+ }, 320);
149
+ return () => {
150
+ cancelAnimationFrame(raf);
151
+ clearTimeout(timeout);
152
+ };
153
+ }, [lastAddedId]);
154
+
155
+ const getAnimationProps = (id) => {
156
+ if (id !== lastAddedId) return {};
157
+ return {
158
+ className: [
159
+ "transition-all duration-300 ease-out",
160
+ animationPhase === "enter" && "opacity-0 translate-y-2",
161
+ animationPhase === "entered" && "opacity-100 translate-y-0",
162
+ ]
163
+ .filter(Boolean)
164
+ .join(" "),
165
+ };
166
+ };
167
+
168
+ return (
169
+ <div className="max-w-2xl space-y-16">
170
+ <CommentComposer
171
+ value={composerValue}
172
+ onChange={(event) => setComposerValue(event.target.value)}
173
+ onSubmit={handleAddComment}
174
+ submitLabel="Add comment"
175
+ avatarProps={{ name: "You" }}
176
+ rows={3}
177
+ />
178
+
179
+ <div className="space-y-12">
180
+ {comments.map((comment) => (
181
+ <Comment
182
+ key={comment.id}
183
+ author={comment.author}
184
+ timestamp={comment.timestamp}
185
+ body={comment.body}
186
+ avatarProps={{ name: comment.author }}
187
+ resolved={resolvedIds.has(comment.id)}
188
+ {...getAnimationProps(comment.id)}
189
+ onResolve={() => handleResolve(comment.id)}
190
+ replies={
191
+ <div className="space-y-8">
192
+ {comment.replies.map((reply) => (
193
+ <Comment
194
+ key={reply.id}
195
+ author={reply.author}
196
+ timestamp={reply.timestamp}
197
+ body={reply.body}
198
+ avatarProps={{ name: reply.author }}
199
+ {...getAnimationProps(reply.id)}
200
+ />
201
+ ))}
202
+ {activeReplyId === comment.id && (
203
+ <ReplyComposer
204
+ onSubmit={(value) => {
205
+ handleAddReply(comment.id, value);
206
+ setActiveReplyId(null);
207
+ }}
208
+ />
209
+ )}
210
+ </div>
211
+ }
212
+ >
213
+ <Comment.Actions>
214
+ {!resolvedIds.has(comment.id) ? (
215
+ <Comment.Reply
216
+ onClick={() =>
217
+ setActiveReplyId((prev) =>
218
+ prev === comment.id ? null : comment.id
219
+ )
220
+ }
221
+ />
222
+ ) : null}
223
+ {!resolvedIds.has(comment.id) ? <Comment.Resolve /> : null}
224
+ </Comment.Actions>
225
+ </Comment>
226
+ ))}
227
+ </div>
228
+ </div>
229
+ );
230
+ };
231
+
232
+ const PEOPLE = [
233
+ {
234
+ id: "alex",
235
+ name: "Alex Johnson",
236
+ handle: "alex",
237
+ avatarProps: { name: "Alex Johnson" },
238
+ },
239
+ {
240
+ id: "priya",
241
+ name: "Priya Singh",
242
+ handle: "priya",
243
+ avatarProps: { name: "Priya Singh" },
244
+ },
245
+ {
246
+ id: "taylor",
247
+ name: "Taylor Reed",
248
+ handle: "taylor",
249
+ avatarProps: { name: "Taylor Reed" },
250
+ },
251
+ ];
252
+
253
+ export const ThreadInteractive = () => {
254
+ const [composerValue, setComposerValue] = React.useState("");
255
+ const [lastAddedId, setLastAddedId] = React.useState(null);
256
+ const [resolvedIds, setResolvedIds] = React.useState(new Set());
257
+ const [animationPhase, setAnimationPhase] = React.useState("idle");
258
+ const [comments, setComments] = React.useState([
259
+ {
260
+ id: "c-1",
261
+ author: "Alex Johnson",
262
+ timestamp: "2h ago",
263
+ body: "Left a few notes on the introduction. Let me know if you want me to draft the edits.",
264
+ replies: [
265
+ {
266
+ id: "c-1-1",
267
+ author: "Taylor",
268
+ timestamp: "1h ago",
269
+ body: "Happy to take the edits.",
270
+ },
271
+ ],
272
+ },
273
+ ]);
274
+ const [activeReplyId, setActiveReplyId] = React.useState(null);
275
+
276
+ const handleAddComment = (event) => {
277
+ event.preventDefault();
278
+ const trimmed = composerValue.trim();
279
+ if (!trimmed) return;
280
+
281
+ const newComment = {
282
+ id: `c-${Date.now()}`,
283
+ author: "You",
284
+ timestamp: "Just now",
285
+ body: trimmed,
286
+ replies: [],
287
+ };
288
+
289
+ setComments((prev) => [newComment, ...prev]);
290
+ setLastAddedId(newComment.id);
291
+ setComposerValue("");
292
+ };
293
+
294
+ const handleAddReply = (commentId, replyText) => {
295
+ const trimmed = replyText.trim();
296
+ if (!trimmed) return;
297
+
298
+ const replyId = `${commentId}-${Date.now()}`;
299
+ setComments((prev) =>
300
+ prev.map((comment) =>
301
+ comment.id === commentId
302
+ ? {
303
+ ...comment,
304
+ replies: [
305
+ ...comment.replies,
306
+ {
307
+ id: replyId,
308
+ author: "You",
309
+ timestamp: "Just now",
310
+ body: trimmed,
311
+ },
312
+ ],
313
+ }
314
+ : comment
315
+ )
316
+ );
317
+ setLastAddedId(replyId);
318
+ };
319
+
320
+ const handleResolve = (commentId) => {
321
+ setResolvedIds((prev) => new Set(prev).add(commentId));
322
+ setActiveReplyId((prev) => (prev === commentId ? null : prev));
323
+ };
324
+
325
+ React.useEffect(() => {
326
+ if (!lastAddedId) return;
327
+ setAnimationPhase("enter");
328
+ const raf = requestAnimationFrame(() => {
329
+ setAnimationPhase("entered");
330
+ });
331
+ const timeout = setTimeout(() => {
332
+ setAnimationPhase("idle");
333
+ setLastAddedId(null);
334
+ }, 320);
335
+ return () => {
336
+ cancelAnimationFrame(raf);
337
+ clearTimeout(timeout);
338
+ };
339
+ }, [lastAddedId]);
340
+
341
+ const getAnimationProps = (id) => {
342
+ if (id !== lastAddedId) return {};
343
+ return {
344
+ className: [
345
+ "transition-all duration-300 ease-out",
346
+ animationPhase === "enter" && "opacity-0 translate-y-2",
347
+ animationPhase === "entered" && "opacity-100 translate-y-0",
348
+ ]
349
+ .filter(Boolean)
350
+ .join(" "),
351
+ };
352
+ };
353
+
354
+ return (
355
+ <div className="max-w-2xl space-y-16">
356
+ <CommentComposer
357
+ value={composerValue}
358
+ onChange={(event) => setComposerValue(event.target.value)}
359
+ onSubmit={handleAddComment}
360
+ onCancel={() => setComposerValue("")}
361
+ submitLabel="Add comment"
362
+ avatarProps={{ name: "You" }}
363
+ rows={4}
364
+ >
365
+ <CommentMention peopleList={PEOPLE} />
366
+ </CommentComposer>
367
+
368
+ <div className="space-y-12">
369
+ {comments.map((comment) => (
370
+ <Comment
371
+ key={comment.id}
372
+ author={comment.author}
373
+ timestamp={comment.timestamp}
374
+ body={comment.body}
375
+ avatarProps={{ name: comment.author }}
376
+ resolved={resolvedIds.has(comment.id)}
377
+ {...getAnimationProps(comment.id)}
378
+ onResolve={() => handleResolve(comment.id)}
379
+ replies={
380
+ <div className="space-y-8">
381
+ {comment.replies.map((reply) => (
382
+ <Comment
383
+ key={reply.id}
384
+ author={reply.author}
385
+ timestamp={reply.timestamp}
386
+ body={reply.body}
387
+ avatarProps={{ name: reply.author }}
388
+ {...getAnimationProps(reply.id)}
389
+ />
390
+ ))}
391
+ {activeReplyId === comment.id && (
392
+ <ReplyComposerWithMentions
393
+ onSubmit={(value) => {
394
+ handleAddReply(comment.id, value);
395
+ setActiveReplyId(null);
396
+ }}
397
+ onCancel={() => setActiveReplyId(null)}
398
+ />
399
+ )}
400
+ </div>
401
+ }
402
+ >
403
+ <Comment.Actions>
404
+ {!resolvedIds.has(comment.id) ? (
405
+ <Comment.Reply
406
+ onClick={() =>
407
+ setActiveReplyId((prev) =>
408
+ prev === comment.id ? null : comment.id
409
+ )
410
+ }
411
+ />
412
+ ) : null}
413
+ {!resolvedIds.has(comment.id) ? <Comment.Resolve /> : null}
414
+ </Comment.Actions>
415
+ </Comment>
416
+ ))}
417
+ </div>
418
+ </div>
419
+ );
420
+ };
421
+
422
+ const ReplyComposerWithMentions = ({ onSubmit, onCancel }) => {
423
+ const [value, setValue] = React.useState("");
424
+
425
+ const handleSubmit = (event) => {
426
+ event.preventDefault();
427
+ const trimmed = value.trim();
428
+ if (!trimmed) return;
429
+ onSubmit(trimmed);
430
+ setValue("");
431
+ };
432
+
433
+ return (
434
+ <CommentComposer
435
+ value={value}
436
+ onChange={(event) => setValue(event.target.value)}
437
+ onSubmit={handleSubmit}
438
+ onCancel={() => {
439
+ setValue("");
440
+ onCancel?.();
441
+ }}
442
+ submitLabel="Add reply"
443
+ avatarProps={{ name: "You" }}
444
+ rows={3}
445
+ >
446
+ <CommentMention peopleList={PEOPLE} />
447
+ </CommentComposer>
448
+ );
449
+ };
450
+
451
+ const ReplyComposer = ({ onSubmit }) => {
452
+ const [value, setValue] = React.useState("");
453
+
454
+ const handleSubmit = (event) => {
455
+ event.preventDefault();
456
+ const trimmed = value.trim();
457
+ if (!trimmed) return;
458
+ onSubmit(trimmed);
459
+ setValue("");
460
+ };
461
+
462
+ return (
463
+ <CommentComposer
464
+ value={value}
465
+ onChange={(event) => setValue(event.target.value)}
466
+ onSubmit={handleSubmit}
467
+ submitLabel="Add reply"
468
+ avatarProps={{ name: "You" }}
469
+ rows={2}
470
+ />
471
+ );
472
+ };
@@ -0,0 +1,62 @@
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
+
50
+ return (
51
+ <div className="max-w-xl relative">
52
+ <CommentComposer
53
+ value={value}
54
+ onChange={(event) => setValue(event.target.value)}
55
+ placeholder="Write a comment..."
56
+ avatarProps={{ name: "Alex Johnson" }}
57
+ >
58
+ <CommentMention peopleList={PEOPLE} />
59
+ </CommentComposer>
60
+ </div>
61
+ );
62
+ };