fix(ui): keep the photo confirmation open until the removal settles

The dialog closed before the request, focus went back to the remove button, then the button vanished and focus fell on the page. The confirmation now closes with the photo gone, Escape and Cancel wait for it, and ModalDialog restores focus after the commit so its fallback sees the opener removed. The sr-only file input leaves the tab order: it was an invisible tab stop.
This commit is contained in:
bchanot committed 2026-10-25 22:46:18 +01:00
1 parent 72ee9f9971
commit 8371aa0a2a
3 files changed
+50 -8

No files matched your search

@@ -90,6 +90,7 @@ export default function AvatarSection() {
avatar?.inFlight ? { kind: 'processing', long: false } : { kind: 'idle' },
)
const [confirmOpen, setConfirmOpen] = useState(false)
const [removing, setRemoving] = useState(false)
const inputRef = useRef<HTMLInputElement>(null)
const pollRef = useRef<number | null>(null)
@@ -171,16 +172,27 @@ export default function AvatarSection() {
}
}
const closeConfirm = () => !removing && setConfirmOpen(false)
// The confirmation stays open until the removal settles: it closes with the
// photo gone, so focus moves past the vanished button (ModalDialog fallback).
async function onRemove() {
const client = getSupabaseClient()
setConfirmOpen(false)
if (!client) return
if (removing) return
if (!client) {
setConfirmOpen(false)
return
}
setRemoving(true)
try {
await removeAvatar(client)
clearAvatar()
setPhase({ kind: 'removed' })
} catch (error) {
setPhase({ kind: 'failed', failure: classifyAssetError(error) })
} finally {
setRemoving(false)
setConfirmOpen(false)
}
}
@@ -211,6 +223,8 @@ export default function AvatarSection() {
accept={ACCEPT}
onChange={onPick}
className="sr-only"
// Opened by the visible button: an invisible tab stop would hide the focus.
tabIndex={-1}
aria-label={t('account.avatar.change')}
disabled={busy}
/>
@@ -258,13 +272,13 @@ export default function AvatarSection() {
{rejected && <Notice tone="error">{t(`account.avatar.rejected.${rejected}`)}</Notice>}
<Dialog
open={confirmOpen}
onClose={() => setConfirmOpen(false)}
onClose={closeConfirm}
title={t('account.avatar.remove_title')}
description={<p className="text-sm text-[var(--color-ink-muted)]">{t('account.avatar.remove_body')}</p>}
size="sm"
>
<div className="mt-4 flex justify-end gap-2">
<Button type="button" variant="ghost" onClick={() => setConfirmOpen(false)}>
<Button type="button" variant="ghost" onClick={closeConfirm}>
{t('common.cancel')}
</Button>
<Button type="button" variant="danger" onClick={onRemove}>
+13 -4
View File
@@ -17,15 +17,20 @@ export interface ModalDialogProps {
children: ReactNode
}
/** Controls that can take focus: the first one on open, a fallback when the opener is gone. */
/**
* Controls in the tab order: the first one takes focus on open, and serves as a
* fallback when the opener is gone.
*/
const FOCUSABLE = [
'a[href]',
'button:not([disabled])',
'input:not([disabled]):not([type="hidden"])',
'select:not([disabled])',
'textarea:not([disabled])',
'[tabindex]:not([tabindex="-1"])',
].join(', ')
'[tabindex]',
]
.map((selector) => `${selector}:not([tabindex="-1"])`)
.join(', ')
/** Full-viewport transparent container: the consumer draws its own backdrop and panel. */
const SURFACE =
@@ -94,7 +99,11 @@ export default function ModalDialog({
dialog.querySelector<HTMLElement>(FOCUSABLE)?.focus()
return () => {
if (dialog.open) dialog.close()
restoreFocus(opener, ancestors)
// After the commit: the render that closes the dialog may remove the opener
// too. A dialog still on screen was only remounted (React StrictMode).
queueMicrotask(() => {
if (!dialog.isConnected) restoreFocus(opener, ancestors)
})
}
}, [])
@@ -157,6 +157,25 @@ describe('AvatarSection', () => {
expect(screen.queryByRole('img')).toBeNull()
})
it('keeps the confirmation open until the removal settles, then focuses the photo control', async () => {
useAuthStore.setState({ avatarUrl: 'blob:current', avatar: { clean: fakeAssetRow(), inFlight: null, lastRejected: null } })
let settle: (value: { data: number; error: null }) => void = () => {}
vi.spyOn(fake.client, 'rpc').mockImplementationOnce(
() => new Promise((resolve) => (settle = resolve)) as unknown as ReturnType<typeof fake.client.rpc>,
)
render(<AvatarSection />)
await userEvent.click(screen.getByRole('button', { name: fr['account.avatar.remove'] }))
const dialog = await screen.findByRole('dialog', { name: fr['account.avatar.remove_title'] })
await userEvent.click(screen.getByRole('button', { name: fr['account.avatar.remove_confirm'] }))
fireEvent.keyDown(dialog, { key: 'Escape' })
await userEvent.click(screen.getByRole('button', { name: fr['common.cancel'] }))
expect(dialog).toBeInTheDocument()
await act(async () => settle({ data: 1, error: null }))
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
expect(screen.queryByRole('button', { name: fr['account.avatar.remove'] })).toBeNull()
await waitFor(() => expect(screen.getByRole('button', { name: fr['account.avatar.change'] })).toHaveFocus())
})
it('resumes the processing state for a row in flight at mount', async () => {
useAuthStore.setState({ avatar: { clean: null, inFlight: fakeAssetRow({ status: 'uploaded' }), lastRejected: null } })
render(<AvatarSection />)