Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(evendrive): describe and restore focus in the parent-role dialog (R1-L2)

M1d974d5da2fa
diff --git a/desktop-app/ui/src/gfs/GfsInheritedAccessDialog/index.tsx b/desktop-app/ui/src/gfs/GfsInheritedAccessDialog/index.tsx
index c556e521a..94256820d 100644
--- a/desktop-app/ui/src/gfs/GfsInheritedAccessDialog/index.tsx
+++ b/desktop-app/ui/src/gfs/GfsInheritedAccessDialog/index.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useId, useState } from 'react'
+import { useEffect, useId, useRef, useState } from 'react'
import { Button } from '@components/Common'
import type { GfsInheritedAccessDialogProps, GfsInheritedAccessRole } from './types'
@@ -28,11 +28,29 @@ export function GfsInheritedAccessDialog({
onCancel,
}: GfsInheritedAccessDialogProps) {
const titleId = useId()
+ const bodyId = useId()
+ const cancelButtonRef = useRef<HTMLButtonElement | null>(null)
+ const previousActiveElementRef = useRef<HTMLElement | null>(null)
const [showHelp, setShowHelp] = useState(false)
+ // Keyed on open/close transitions, not the request object identity: the
+ // parent rebuilds the request object on every render, and refiring per
+ // render would reset the help view and steal focus mid-dialog.
+ const open = request !== null
useEffect(() => {
setShowHelp(false)
- }, [request])
+ }, [open])
+
+ useEffect(() => {
+ if (!open) return
+ const previousActiveElement =
+ document.activeElement instanceof HTMLElement ? document.activeElement : null
+ previousActiveElementRef.current = previousActiveElement
+ cancelButtonRef.current?.focus()
+ return () => {
+ previousActiveElementRef.current?.focus()
+ }
+ }, [open])
if (!request) return null
@@ -59,6 +77,7 @@ export function GfsInheritedAccessDialog({
}}
>
<section
+ aria-describedby={bodyId}
aria-labelledby={titleId}
aria-modal="true"
className="da-gfs-parent-update-dialog"
@@ -67,7 +86,7 @@ export function GfsInheritedAccessDialog({
<header className="da-gfs-parent-update-dialog__header">
<h3 id={titleId}>{showHelp ? 'How sharing works in EvenDrive' : title}</h3>
</header>
- <div className="da-gfs-parent-update-dialog__body">
+ <div className="da-gfs-parent-update-dialog__body" id={bodyId}>
{showHelp ? (
<ul className="da-gfs-parent-update-dialog__help">
{HELP_POINTS.map(point => (
@@ -124,7 +143,13 @@ export function GfsInheritedAccessDialog({
</Button>
) : (
<>
- <Button autoFocus disabled={busy} onClick={onCancel} type="button" variant="ghost">
+ <Button
+ ref={cancelButtonRef}
+ disabled={busy}
+ onClick={onCancel}
+ type="button"
+ variant="ghost"
+ >
Cancel
</Button>
<Button
diff --git a/desktop-app/ui/src/gfs/__tests__/GfsInheritedAccessDialog.test.tsx b/desktop-app/ui/src/gfs/__tests__/GfsInheritedAccessDialog.test.tsx
index ec3ed6d7f..d0f7a17a0 100644
--- a/desktop-app/ui/src/gfs/__tests__/GfsInheritedAccessDialog.test.tsx
+++ b/desktop-app/ui/src/gfs/__tests__/GfsInheritedAccessDialog.test.tsx
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest'
-import { cleanup, fireEvent, render, screen, within } from '@testing-library/react'
+import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import { GfsInheritedAccessDialog } from '../GfsInheritedAccessDialog'
afterEach(() => {
@@ -104,4 +104,41 @@ describe('GfsInheritedAccessDialog', () => {
)
expect(container.firstElementChild).toBeNull()
})
+
+ // R1-L2 — the dialog describes itself and restores focus on close, matching
+ // the control-ui counterpart.
+ it('is described by its body and restores focus to the opener on close', async () => {
+ function Harness({ request }: { request: typeof changeRequest | null }) {
+ return (
+ <div>
+ <button type="button">Open share dialog</button>
+ <GfsInheritedAccessDialog
+ busy={false}
+ request={request}
+ onCancel={vi.fn()}
+ onConfirm={vi.fn()}
+ />
+ </div>
+ )
+ }
+
+ const { rerender } = render(<Harness request={null} />)
+ const opener = screen.getByRole('button', { name: 'Open share dialog' })
+ opener.focus()
+ expect(document.activeElement).toBe(opener)
+
+ rerender(<Harness request={changeRequest} />)
+ const dialog = screen.getByRole('alertdialog')
+ // The body copy is programmatically associated with the dialog…
+ const describedBy = dialog.getAttribute('aria-describedby')
+ expect(describedBy).toBeTruthy()
+ const describedBody = document.getElementById(describedBy ?? '')
+ expect(describedBody?.textContent).toContain('will also change permissions on a parent folder')
+ // …focus lands on the safe cancel action while open…
+ await waitFor(() => expect(document.activeElement?.textContent).toBe('Cancel'))
+
+ // …and returns to the invoking element once the request clears.
+ rerender(<Harness request={null} />)
+ await waitFor(() => expect(document.activeElement).toBe(opener))
+ })
})