Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(email): return focus to the attachment chip when the calendar file dialog closes (#8731)
gbirmanfadb08731a17
diff --git a/apps/web/src/features/email-message/components/attachment-pill.tsx b/apps/web/src/features/email-message/components/attachment-pill.tsxindex 291a598330..1014557c41 100644--- a/apps/web/src/features/email-message/components/attachment-pill.tsx+++ b/apps/web/src/features/email-message/components/attachment-pill.tsx@@ -13,7 +13,8 @@ type EmailAttachmentPillProps = {attachment: { fileName: string; mimeType?: string };removable?: boolean;onRemove?: () => void;- onClick?: () => void;+ /** Receives the chip, so a dialog it opens can return focus to it. */+ onClick?: (chip: HTMLElement) => void;};export function EmailAttachmentPill(props: EmailAttachmentPillProps) {@@ -42,7 +43,11 @@ export function EmailAttachmentPill(props: EmailAttachmentPillProps) {component={props.onClick ? 'button' : 'div'}type={props.onClick ? 'button' : undefined}class="flex min-w-0 flex-1 items-center text-left"- onClick={props.onClick}+ onClick={(event: MouseEvent) => {+ if (event.currentTarget instanceof HTMLElement) {+ props.onClick?.(event.currentTarget);+ }+ }}onKeyDown={(e: KeyboardEvent) => {if (e.key === 'Enter' || e.key === ' ') e.stopPropagation();}}diff --git a/apps/web/src/features/email-message/components/calendar-file-preview.tsx b/apps/web/src/features/email-message/components/calendar-file-preview.tsxindex 165c1e2279..a843c92230 100644--- a/apps/web/src/features/email-message/components/calendar-file-preview.tsx+++ b/apps/web/src/features/email-message/components/calendar-file-preview.tsx@@ -12,8 +12,12 @@ export function CalendarFilePreview(props: {fileName: string;open: boolean;onClose: () => void;- /** Whether closing returns focus to the attachment; false when leaving it. */- restoreFocus?: () => boolean;+ /**+ * The attachment chip closing returns focus to; none when a card action+ * leaves the email. The dialog opens without a trigger, so nothing else+ * would.+ */+ returnFocus?: () => HTMLElement | undefined;onDownload?: () => void;children: JSX.Element;}) {@@ -24,7 +28,9 @@ export function CalendarFilePreview(props: {if (!open) props.onClose();}}onCloseAutoFocus={(event) => {- if (props.restoreFocus?.() === false) event.preventDefault();+ event.preventDefault();+ const chip = props.returnFocus?.();+ if (chip?.isConnected) chip.focus({ preventScroll: true });}}position="center"class="w-140"diff --git a/apps/web/src/features/email-message/views/email-message.test.tsx b/apps/web/src/features/email-message/views/email-message.test.tsxindex b6f5b36d51..30f3c6d7a4 100644--- a/apps/web/src/features/email-message/views/email-message.test.tsx+++ b/apps/web/src/features/email-message/views/email-message.test.tsx@@ -1,4 +1,10 @@-import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';+import {+ cleanup,+ fireEvent,+ render,+ screen,+ waitFor,+} from '@solidjs/testing-library';import { createSignal, type JSX } from 'solid-js';import { afterEach, expect, it, vi } from 'vitest';import type { CalendarInvitation } from '../core/calendar-invitation';@@ -23,9 +29,12 @@ vi.mock('@app/features/email-message/components/collapsed-message', () => ({vi.mock('@app/features/email-message/components/attachment-pill', () => ({EmailAttachmentPill: (props: {attachment: { fileName: string };- onClick?: () => void;+ onClick?: (chip: HTMLElement) => void;}) => (- <button type="button" onClick={() => props.onClick?.()}>+ <button+ type="button"+ onClick={(event) => props.onClick?.(event.currentTarget)}+ >{props.attachment.fileName}</button>),@@ -179,7 +188,61 @@ it('keeps the opened calendar file card mounted when the thread refreshes the meexpect(screen.getByTestId('dialog-card')).toBe(card);});-it('closes the calendar file dialog when its card opens the calendar', () => {+it('returns focus to the calendar file chip when its dialog closes', async () => {+ render(() => (+ <EmailMessageView+ message={message([invitationFixture])}+ renderInvitation={(_message, invitation) => <div>{invitation.id}</div>}+ onOpenAttachment={vi.fn()}+ isTouch={false}+ isPersonal={false}+ isSelected={false}+ allowHover={false}+ isExpanded+ />+ ));+ const chip = screen.getByRole('button', { name: 'Planning.ics' });+ fireEvent.click(chip);+ fireEvent.click(screen.getByRole('button', { name: 'Close' }));+ expect(screen.queryByRole('dialog')).toBeNull();+ await waitFor(() => expect(document.activeElement).toBe(chip));++ chip.blur();+ fireEvent.click(chip);+ fireEvent.keyDown(screen.getByRole('dialog'), { key: 'Escape' });+ expect(screen.queryByRole('dialog')).toBeNull();+ await waitFor(() => expect(document.activeElement).toBe(chip));+});++it('keeps the chip to return focus to when the thread refreshes its attachments', async () => {+ const [current, setCurrent] = createSignal(message([invitationFixture]));+ render(() => (+ <EmailMessageView+ message={current()}+ renderInvitation={(_message, invitation) => <div>{invitation.id}</div>}+ onOpenAttachment={vi.fn()}+ isTouch={false}+ isPersonal={false}+ isSelected={false}+ allowHover={false}+ isExpanded+ />+ ));+ const chip = screen.getByRole('button', { name: 'Planning.ics' });+ fireEvent.click(chip);+ setCurrent({+ ...message([invitationFixture]),+ attachments: [structuredClone(calendarFile), structuredClone(pdfFile)],+ });+ // The open dialog hides the page behind it from the accessibility tree.+ expect(+ screen.getByRole('button', { name: 'Planning.ics', hidden: true })+ ).toBe(chip);+ fireEvent.click(screen.getByRole('button', { name: 'Close' }));+ await waitFor(() => expect(document.activeElement).toBe(chip));+});++it('closes the calendar file dialog when its card opens the calendar', async () => {let navigate: (() => void) | undefined;render(() => (<EmailMessageView@@ -196,10 +259,13 @@ it('closes the calendar file dialog when its card opens the calendar', () => {isExpanded/>));- fireEvent.click(screen.getByRole('button', { name: 'Planning.ics' }));+ const chip = screen.getByRole('button', { name: 'Planning.ics' });+ fireEvent.click(chip);expect(screen.getByRole('dialog')).toBeTruthy();navigate?.();expect(screen.queryByRole('dialog')).toBeNull();+ await new Promise((resolve) => setTimeout(resolve, 20));+ expect(document.activeElement).not.toBe(chip);});it('opens a cloud-reference calendar file through the host', () => {diff --git a/apps/web/src/features/email-message/views/email-message.tsx b/apps/web/src/features/email-message/views/email-message.tsxindex 6ce604e334..3238bbe826 100644--- a/apps/web/src/features/email-message/views/email-message.tsx+++ b/apps/web/src/features/email-message/views/email-message.tsx@@ -69,14 +69,15 @@ export function EmailMessageView(props: EmailMessageViewProps) {// event; any other file, and a cloud reference with no bytes to download,// goes to the host.const [calendarFile, setCalendarFile] = createSignal<EmailAttachment>();- let navigatingAway = false;- const openAttachment = (attachment: EmailAttachment) => {+ // Closing the dialog returns focus to the chip that opened it.+ let calendarFileChip: HTMLElement | undefined;+ const openAttachment = (attachment: EmailAttachment, chip: HTMLElement) => {if (invitationGroups().length > 0 &&!attachment.reference_url &&isCalendarFile(attachment.filename, attachment.mime_type)) {- navigatingAway = false;+ calendarFileChip = chip;setCalendarFile(attachment);return;}@@ -85,7 +86,7 @@ export function EmailMessageView(props: EmailMessageViewProps) {// A card action that opens the calendar closes the dialog first, so it// neither covers the destination nor pulls focus back to the email.const closeForNavigation = () => {- navigatingAway = true;+ calendarFileChip = undefined;setCalendarFile(undefined);};const renderInvitation = (@@ -255,21 +256,26 @@ export function EmailMessageView(props: EmailMessageViewProps) {{/* Other attachments (non-media or without sfs_id) */}<Show when={otherAttachments().length > 0}><div class="flex flex-row overflow-x-scroll mt-2 gap-2">- <For each={otherAttachments()}>+ {/* Keyed so a thread refresh keeps each chip, and with it the+ calendar file dialog's focus target. */}+ <Key+ each={otherAttachments()}+ by={(attachment) => attachment.db_id}+ >{(attachment) => (<EmailAttachmentPillattachment={{- fileName: attachment.filename ?? '',- mimeType: attachment.mime_type ?? undefined,+ fileName: attachment().filename ?? '',+ mimeType: attachment().mime_type ?? undefined,}}onClick={props.onOpenAttachment- ? () => openAttachment(attachment)+ ? (chip) => openAttachment(attachment(), chip): undefined}/>)}- </For>+ </Key></div></Show>@@ -312,7 +318,7 @@ export function EmailMessageView(props: EmailMessageViewProps) {fileName={file().filename ?? ''}openonClose={() => setCalendarFile(undefined)}- restoreFocus={() => !navigatingAway}+ returnFocus={() => calendarFileChip}onDownload={props.onDownloadAttachment? () => props.onDownloadAttachment?.(file())diff --git a/docs/agent-guide/calendar.md b/docs/agent-guide/calendar.mdindex 6703c519dc..6b9dc1dc23 100644--- a/docs/agent-guide/calendar.md+++ b/docs/agent-guide/calendar.md@@ -1264,8 +1264,10 @@ works there, and `Download .ics`, which saves the file without adding it to Drivcard falls back to the saved invitation (the event matches nothing on your calendars, as witha forwarded event), its status line reads `Saved invitation.` in the dialog; the card in theemail adds that the original email or invitation attachment has more options. The card-stays put while the thread refreshes, and `Open in calendar` closes the dialog before the-calendar opens, including when Calendar is already open in another split. A calendar file with+stays put while the thread refreshes. Close, Escape, or dismissing the phone drawer returns+focus to the attachment chip; `Open in calendar` closes the dialog before the calendar opens,+including when Calendar is already open in another split, without pulling focus back to the+email. A calendar file withno card (nothing Macro could read) downloads directly, and an Outlook cloud-reference `.ics`opens its Outlook page like any other reference attachment.Related recurring components are grouped behind their own disclosure. Only mail synced