Skip to content

Commit 5992e69

Browse files
mahanticodexbaxenART
authored
Polish composer reply previews and attachment transitions (#1361)
Signed-off-by: Arjun Mahanti <[email protected]> Signed-off-by: Bradley Axen <[email protected]> Co-authored-by: Codex <[email protected]> Co-authored-by: Bradley Axen <[email protected]> Co-authored-by: ART <ea39c1c8acf57180161ef4ba4ed68abf8589f5291956b909a5aa991fc66664ca@buzz.block.builderlab.xyz>
1 parent 70a3e5e commit 5992e69

7 files changed

Lines changed: 236 additions & 33 deletions

File tree

‎src/features/messages/ComposerAttachments.test.tsx‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,33 @@ it("removes immediately without playing a poof sound", () => {
289289
expect(document.querySelector("[data-attachment-poof]")).toBeNull();
290290
});
291291

292+
it("shows a returning draft at once and animates only attachments picked later", async () => {
293+
const props = {
294+
disabled: false,
295+
remove: vi.fn(),
296+
retry: vi.fn(),
297+
media: () => undefined,
298+
};
299+
const item = (name: string): DraftAttachment => ({
300+
id: name,
301+
file: new File(["text"], name),
302+
status: "queued",
303+
});
304+
const row = (name: string) =>
305+
screen.getByText(name).closest("li") as HTMLElement;
306+
const view = render(
307+
<ComposerAttachments {...props} items={[item("restored.txt")]} />,
308+
);
309+
// The composer remounted with this draft (switching back to a channel).
310+
expect(row("restored.txt").style.opacity).toBe("1");
311+
view.rerender(<ComposerAttachments {...props} items={[]} />);
312+
view.rerender(
313+
<ComposerAttachments {...props} items={[item("picked.txt")]} />,
314+
);
315+
expect(row("picked.txt").style.opacity).toBe("0");
316+
await waitFor(() => expect(row("picked.txt").style.opacity).toBe("1"));
317+
});
318+
292319
it("dismisses draft preview while retaining the original file and preview URL", async () => {
293320
const revoke = vi.fn();
294321
vi.stubGlobal(

‎src/features/messages/ComposerAttachments.tsx‎

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ export function ComposerAttachments({
4949
});
5050
const [selected, setSelected] = useState<string>();
5151
const restoreFocus = useRef<HTMLElement | null>(null);
52-
const instant = useReducedMotion();
52+
const reducedMotion = useReducedMotion();
53+
const instant =
54+
reducedMotion ||
55+
document.documentElement.hasAttribute("data-keyboard-navigation");
56+
// Items present when the composer mounts are a returning draft, not new picks.
57+
const [restored, setRestored] = useState(items.length > 0);
58+
if (restored && !items.length) setRestored(false);
5359
if (selected && (!active || !items.some((item) => item.id === selected)))
5460
setSelected(undefined);
5561
return (
@@ -63,19 +69,28 @@ export function ComposerAttachments({
6369
key="attachments"
6470
aria-label="Attachments"
6571
className={styles.attachments}
66-
initial={false}
67-
animate={{ height: "auto", opacity: 1 }}
68-
exit={{ height: 0, opacity: 0 }}
72+
initial={instant ? false : { height: 0, overflow: "hidden" }}
73+
animate={{
74+
height: "auto",
75+
opacity: 1,
76+
transitionEnd: { overflow: "visible" },
77+
}}
78+
exit={{
79+
height: 0,
80+
opacity: 0,
81+
overflow: "hidden",
82+
transition: { duration: instant ? 0 : 0.15 },
83+
}}
6984
transition={{
70-
duration: instant ? 0 : 0.18,
85+
duration: instant ? 0 : 0.22,
7186
ease: [0.23, 1, 0.32, 1],
7287
}}
7388
>
7489
<ul
7590
className={`${styles.list} buzz-thin-scrollbar`}
7691
data-compact-media={mixed || undefined}
7792
>
78-
<AnimatePresence initial={false} propagate>
93+
<AnimatePresence initial={!instant && !restored} propagate>
7994
{items.map((item) => (
8095
<AttachmentItem
8196
renderers={renderers}
@@ -129,7 +144,28 @@ function AttachmentItem({
129144
retry(id: string): void;
130145
}) {
131146
const present = useIsPresent();
132-
const instant = useReducedMotion();
147+
const reducedMotion = useReducedMotion();
148+
const instant =
149+
reducedMotion ||
150+
document.documentElement.hasAttribute("data-keyboard-navigation");
151+
const appearance = {
152+
initial: instant
153+
? (false as const)
154+
: { opacity: 0, transform: "translateY(6px)" },
155+
animate: { opacity: 1, transform: "translateY(0px)" },
156+
exit: {
157+
opacity: 0,
158+
transform: instant ? "translateY(0px)" : "translateY(6px)",
159+
transition: { duration: instant ? 0 : 0.15 },
160+
},
161+
transition: {
162+
duration: instant ? 0 : 0.22,
163+
ease: [0.23, 1, 0.32, 1] as const,
164+
opacity: { duration: instant ? 0 : 0.15 },
165+
},
166+
inert: !present,
167+
"aria-hidden": !present || undefined,
168+
};
133169
const { source, image, video } = useDraftAttachmentSource(item, media);
134170
const [failedSource, setFailedSource] = useState<string>();
135171
const element = useMediaElementSource(video ? source : undefined);
@@ -158,7 +194,7 @@ function AttachmentItem({
158194
item.status === "uploading";
159195
if (item.voice && source && renderers)
160196
return (
161-
<li className={styles.voiceItem} data-voice-draft>
197+
<motion.li {...appearance} className={styles.voiceItem} data-voice-draft>
162198
<AttachmentView
163199
registry={renderers}
164200
attachment={{
@@ -196,16 +232,12 @@ function AttachmentItem({
196232
</button>
197233
</>
198234
)}
199-
</li>
235+
</motion.li>
200236
);
201237
return (
202238
<motion.li
203-
layout
204-
initial={false}
205-
exit={{ scale: instant ? 1 : 0.75, y: instant ? 0 : 12, opacity: 0 }}
206-
transition={{ duration: instant ? 0 : 0.18, ease: [0.23, 1, 0.32, 1] }}
207-
inert={!present}
208-
aria-hidden={!present || undefined}
239+
layout={!instant}
240+
{...appearance}
209241
className={styles.item}
210242
data-media={image || video ? "" : undefined}
211243
>

‎src/features/messages/MessageComposer.test.tsx‎

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,31 @@ function mount(
475475
};
476476
}
477477

478+
it("retires reply controls immediately and preserves the draft when replying again", () => {
479+
const h = mount({
480+
replyContext: <button type="button">Cancel reply to Honey</button>,
481+
});
482+
fireEvent.change(h.input(), { target: { value: "Draft in progress" } });
483+
const previous = screen.getByRole("button", {
484+
name: "Cancel reply to Honey",
485+
});
486+
487+
h.retarget({ replyContext: undefined });
488+
expect(previous.closest("[inert]")).not.toBeNull();
489+
expect(
490+
screen.queryByRole("button", { name: "Cancel reply to Honey" }),
491+
).toBeNull();
492+
expect(h.input()).toHaveValue("Draft in progress");
493+
494+
h.retarget({
495+
replyContext: <button type="button">Cancel reply to Wes</button>,
496+
});
497+
const current = screen.getByRole("button", { name: "Cancel reply to Wes" });
498+
expect(current.closest("[inert]")).toBeNull();
499+
expect(screen.queryByText("Cancel reply to Honey")).toBeNull();
500+
expect(h.input()).toHaveValue("Draft in progress");
501+
});
502+
478503
it("shows a local draft in the cached composer without completion, typing or transport reads", async () => {
479504
const viewer = keypair(),
480505
relay = keypair();
@@ -1455,7 +1480,9 @@ it.each(["never", "before the failure", "after the failure"])(
14551480
if (remounted === "after the failure") remount();
14561481
const input = () => within(container).getByRole("textbox");
14571482
await waitFor(() => expect(input()).toHaveValue("think hard@Honey "));
1458-
expect(within(container).getByText("tuned.txt")).toBeVisible();
1483+
await waitFor(() =>
1484+
expect(within(container).getByText("tuned.txt")).toBeVisible(),
1485+
);
14591486
// The restored draft brings its pick back with it, not the default run.
14601487
const button = await picker();
14611488
expect(button).toHaveAttribute("data-selected");
@@ -1536,7 +1563,9 @@ it.each(["cleanup", "restore"] as const)(
15361563
await waitFor(() =>
15371564
expect(h.input()).toHaveValue("recover this caption"),
15381565
);
1539-
expect(within(h.form()).getByText("retained.txt")).toBeVisible();
1566+
await waitFor(() =>
1567+
expect(within(h.form()).getByText("retained.txt")).toBeVisible(),
1568+
);
15401569
expect(
15411570
screen.queryByRole("button", { name: "Retry draft cleanup" }),
15421571
).toBeNull();
@@ -1607,7 +1636,9 @@ it("does not overwrite a later local edit when a background upload fails", async
16071636
await userEvent.clear(h.input());
16081637
await userEvent.click(retry);
16091638
await waitFor(() => expect(h.input()).toHaveValue("original caption"));
1610-
expect(within(h.form()).getByText("original.txt")).toBeVisible();
1639+
await waitFor(() =>
1640+
expect(within(h.form()).getByText("original.txt")).toBeVisible(),
1641+
);
16111642
expect(
16121643
screen.queryByRole("button", { name: "Retry failed send recovery" }),
16131644
).toBeNull();
@@ -1636,7 +1667,9 @@ it("restores a failed upload after the composer remounts in the same session", a
16361667
"remount caption",
16371668
),
16381669
);
1639-
expect(within(again.container).getByText("remount.txt")).toBeVisible();
1670+
await waitFor(() =>
1671+
expect(within(again.container).getByText("remount.txt")).toBeVisible(),
1672+
);
16401673
});
16411674

16421675
it("reconciles failed-cleanup recovery after remount before stale cleanup can overwrite the caption", async () => {
@@ -1676,7 +1709,9 @@ it("reconciles failed-cleanup recovery after remount before stale cleanup can ov
16761709
expect(replacement).toHaveValue("recover this caption"),
16771710
);
16781711
expect(replacement).toHaveAttribute("contenteditable", "true");
1679-
expect(within(again.container).getByText("retained.txt")).toBeVisible();
1712+
await waitFor(() =>
1713+
expect(within(again.container).getByText("retained.txt")).toBeVisible(),
1714+
);
16801715
expect(
16811716
within(again.container).queryByRole("button", {
16821717
name: "Retry draft cleanup",
@@ -1724,7 +1759,9 @@ it("retains a remounted later edit conflict until clearing it and explicitly ret
17241759
await userEvent.click(retry);
17251760
await waitFor(() => expect(replacement).toHaveValue("original caption"));
17261761
expect(replacement).toHaveAttribute("contenteditable", "true");
1727-
expect(within(again.container).getByText("original.txt")).toBeVisible();
1762+
await waitFor(() =>
1763+
expect(within(again.container).getByText("original.txt")).toBeVisible(),
1764+
);
17281765
expect(
17291766
screen.queryByRole("button", { name: "Retry failed send recovery" }),
17301767
).toBeNull();
@@ -1765,7 +1802,9 @@ it("does not replace an active message edit when a remounted upload recovers", a
17651802
within(again.container).getByRole("button", { name: "Cancel edit" }),
17661803
);
17671804
await waitFor(() => expect(replacement).toHaveValue("original caption"));
1768-
expect(within(again.container).getByText("original.txt")).toBeVisible();
1805+
await waitFor(() =>
1806+
expect(within(again.container).getByText("original.txt")).toBeVisible(),
1807+
);
17691808
expect(h.publish).not.toHaveBeenCalled();
17701809
});
17711810

@@ -1807,7 +1846,9 @@ it("defers a failed upload recovery until an empty active message edit ends", as
18071846
within(h.form()).getByRole("button", { name: "Cancel edit" }),
18081847
);
18091848
await waitFor(() => expect(editor).toHaveValue("original caption"));
1810-
expect(within(h.form()).getByText("original.txt")).toBeVisible();
1849+
await waitFor(() =>
1850+
expect(within(h.form()).getByText("original.txt")).toBeVisible(),
1851+
);
18111852
});
18121853

18131854
it("retains preparation retry when recovery is deferred by an edit then unmounted", async () => {
@@ -1860,7 +1901,9 @@ it("retains preparation retry when recovery is deferred by an edit then unmounte
18601901
);
18611902
const composer = within(returned.container);
18621903
expect(composer.getByRole("textbox")).toHaveValue("caption :party:");
1863-
expect(composer.getByText("edit-remount.txt")).toBeVisible();
1904+
await waitFor(() =>
1905+
expect(composer.getByText("edit-remount.txt")).toBeVisible(),
1906+
);
18641907
expect(composer.getByRole("alert")).toHaveTextContent(
18651908
"Community emoji unavailable",
18661909
);
@@ -1901,7 +1944,9 @@ it("offers message preparation retry after a failed attachment send remounts", a
19011944
await waitFor(() =>
19021945
expect(composer.getByRole("textbox")).toHaveValue("caption :party:"),
19031946
);
1904-
expect(composer.getByText("emoji-remount.txt")).toBeVisible();
1947+
await waitFor(() =>
1948+
expect(composer.getByText("emoji-remount.txt")).toBeVisible(),
1949+
);
19051950
expect(composer.getByRole("alert")).toHaveTextContent(
19061951
"Community emoji unavailable",
19071952
);
@@ -1913,7 +1958,9 @@ it("offers message preparation retry after a failed attachment send remounts", a
19131958
expect(h.owner.session.emoji.snapshot().status).toBe("ready"),
19141959
);
19151960
expect(composer.getByRole("textbox")).toHaveValue("caption :party:");
1916-
expect(composer.getByText("emoji-remount.txt")).toBeVisible();
1961+
await waitFor(() =>
1962+
expect(composer.getByText("emoji-remount.txt")).toBeVisible(),
1963+
);
19171964
expect(h.publish).not.toHaveBeenCalled();
19181965
again.unmount();
19191966
const returned = render(
@@ -7227,9 +7274,11 @@ it("keeps snapshot paste from taking the attachment slot reserved for a recordin
72277274
screen.queryByRole("button", { name: "Remove helper.agent.png" }),
72287275
).toBeNull();
72297276
fireEvent.click(finish);
7230-
expect(
7231-
await screen.findByRole("button", { name: "Remove voice-note.wav" }),
7232-
).toBeVisible();
7277+
await waitFor(() =>
7278+
expect(
7279+
screen.getByRole("button", { name: "Remove voice-note.wav" }),
7280+
).toBeVisible(),
7281+
);
72337282
await waitFor(() => expect(h.uploadCalls).toHaveLength(1));
72347283
expect(h.uploadCalls[0]?.file.name).toBe("draft-0.txt");
72357284
expect(h.publish).not.toHaveBeenCalled();
@@ -7305,7 +7354,9 @@ it("drops the attachments mark while a row-requested edit holds pending items",
73057354
);
73067355
expect(form).not.toHaveAttribute("data-editing");
73077356
expect(form).toHaveAttribute("data-attachments");
7308-
expect(within(view.container).getByText("pending.txt")).toBeVisible();
7357+
await waitFor(() =>
7358+
expect(within(view.container).getByText("pending.txt")).toBeVisible(),
7359+
);
73097360
});
73107361

73117362
it("stages session cards above an ordinary caption editor, persists them, and sends both", async () => {

‎src/features/messages/MessageComposer.tsx‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ function Composer({
15141514
}
15151515
const host = fullSession(session);
15161516
const readingOnly = !outbox?.supports(9) && !cached;
1517-
// The attachments section animates out over 180 ms; the form's gap and
1517+
// The attachments section animates out over 150 ms; the form's gap and
15181518
// padding (data-attachments) collapse when it has, not when the list empties.
15191519
// An edit unmounts the section at once, so no exit completes during one.
15201520
const hasAttachments = attachments.items.length > 0;
@@ -1694,6 +1694,9 @@ function Composer({
16941694
onDismiss={() => setAttachmentError(undefined)}
16951695
/>
16961696
)}
1697+
<ComposerReplyContext>
1698+
{!editing.target && replyContext}
1699+
</ComposerReplyContext>
16971700
<div className={styles.composerContent}>
16981701
{!editing.target && (
16991702
<ComposerAttachments
@@ -1867,7 +1870,6 @@ function Composer({
18671870
/>
18681871
</div>
18691872
</div>
1870-
{!editing.target && replyContext}
18711873
{!editing.target &&
18721874
threadRootId &&
18731875
mediaTimeSeconds !== undefined &&
@@ -2055,6 +2057,27 @@ function Composer({
20552057
);
20562058
}
20572059

2060+
/** Retain the last preview for collapse; reply semantics clear immediately. */
2061+
function ComposerReplyContext({ children }: { children: ReactNode }) {
2062+
const open = !!children;
2063+
const [lastContext, setLastContext] = useState(children);
2064+
if (open && children !== lastContext) setLastContext(children);
2065+
return (
2066+
<div
2067+
className={styles.composerReplySlot}
2068+
data-open={open || undefined}
2069+
inert={!open}
2070+
aria-hidden={!open || undefined}
2071+
>
2072+
<div className={styles.composerReplyClip}>
2073+
<div className={styles.composerReplyContent}>
2074+
{open ? children : lastContext}
2075+
</div>
2076+
</div>
2077+
</div>
2078+
);
2079+
}
2080+
20582081
/** Presentation stays host-owned even when the optional mention tool is disabled. */
20592082
function RecipientAvatars({
20602083
session,

0 commit comments

Comments
 (0)