Skip to content

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

TypeScript

fix(ui): rp-020 trap the focus in the move confirmation dialogs

Mc99275e8c4c7
diff --git a/control-ui/components/GfsMoveConfirmDialog/index.tsx b/control-ui/components/GfsMoveConfirmDialog/index.tsx
index 0a3b7b0cc..a67aad786 100644
--- a/control-ui/components/GfsMoveConfirmDialog/index.tsx
+++ b/control-ui/components/GfsMoveConfirmDialog/index.tsx
@@ -1,6 +1,6 @@
'use client'
-import { useEffect, useState } from 'react'
+import { useEffect, useRef, useState } from 'react'
import { Button } from '@components/ui'
export interface GfsMoveConfirmRequest {
@@ -34,6 +34,65 @@ export function GfsMoveConfirmDialog({
onCancel,
}: GfsMoveConfirmDialogProps): React.JSX.Element {
const [allowNumberedRename, setAllowNumberedRename] = useState(false)
+ const dialogRef = useRef<HTMLElement | null>(null)
+ // The element that had focus before the dialog opened. It is captured during
+ // the FIRST render — before the dialog moves focus into itself — so the
+ // restore can hand the keyboard back where it came from (RP-020).
+ const openerRef = useRef<HTMLElement | null>(null)
+ if (openerRef.current === null && typeof document !== 'undefined') {
+ openerRef.current =
+ document.activeElement instanceof HTMLElement ? document.activeElement : null
+ }
+
+ /**
+ * Focus returns to the opener when the dialog closes: a keyboard user must
+ * never be left on a removed node, and the surface behind a modal dialog
+ * must not be reachable while it is open (RP-020).
+ */
+ useEffect(() => {
+ return () => {
+ const opener = openerRef.current
+ if (opener?.isConnected) opener.focus()
+ }
+ }, [])
+
+ /**
+ * Tab stays inside the dialog. The panel is a modal surface: without the
+ * wrap, a keyboard user tabs straight out of the confirmation and into the
+ * page behind it, which the modal has just taken over (RP-020).
+ */
+ function handleDialogKeyDown(event: React.KeyboardEvent<HTMLElement>) {
+ if (event.key !== 'Tab') return
+ const panel = dialogRef.current
+ if (!panel) return
+ const focusable = Array.from(
+ panel.querySelectorAll<HTMLElement>(
+ 'a[href], button:not(:disabled), input:not(:disabled):not([type="hidden"]), select:not(:disabled), textarea:not(:disabled), [tabindex]:not([tabindex="-1"])'
+ )
+ ).filter(element => !element.closest('[hidden], [aria-hidden="true"]'))
+ if (focusable.length === 0) {
+ event.preventDefault()
+ panel.focus()
+ return
+ }
+ const first = focusable[0]
+ const last = focusable[focusable.length - 1]
+ if (!first || !last) {
+ event.preventDefault()
+ panel.focus()
+ return
+ }
+ const active = document.activeElement
+ if (event.shiftKey && (active === first || !panel.contains(active))) {
+ event.preventDefault()
+ last.focus()
+ return
+ }
+ if (!event.shiftKey && (active === last || !panel.contains(active))) {
+ event.preventDefault()
+ first.focus()
+ }
+ }
useEffect(() => {
if (busy) return
@@ -56,7 +115,10 @@ export function GfsMoveConfirmDialog({
aria-label={`Move folder ${request.sourceName} to ${request.destinationName}`}
aria-modal="true"
className="cu-modal-panel cu-gfs-move-confirm"
+ onKeyDown={handleDialogKeyDown}
+ ref={dialogRef}
role="dialog"
+ tabIndex={-1}
>
<header className="cu-gfs-move-confirm__header">
<h3>
diff --git a/control-ui/components/__tests__/GfsMoveConfirmDialog.test.tsx b/control-ui/components/__tests__/GfsMoveConfirmDialog.test.tsx
new file mode 100644
index 000000000..57343e12a
--- /dev/null
+++ b/control-ui/components/__tests__/GfsMoveConfirmDialog.test.tsx
@@ -0,0 +1,57 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { GfsMoveConfirmDialog } from '../GfsMoveConfirmDialog'
+
+afterEach(() => {
+ cleanup()
+ vi.restoreAllMocks()
+})
+
+function request(canRename = true) {
+ return {
+ sourceName: 'Team folder',
+ destinationName: 'Archive',
+ canRename,
+ }
+}
+
+describe('GfsMoveConfirmDialog — keyboard containment (RP-020)', () => {
+ it('focuses the cancel action when it opens', () => {
+ render(<GfsMoveConfirmDialog request={request()} onCancel={vi.fn()} onConfirm={vi.fn()} />)
+ expect(document.activeElement).toBe(screen.getByRole('button', { name: 'Cancel' }))
+ })
+
+ it('keeps Tab inside the dialog', () => {
+ render(<GfsMoveConfirmDialog request={request(false)} onCancel={vi.fn()} onConfirm={vi.fn()} />)
+ const dialog = screen.getByRole('dialog', { name: 'Move folder Team folder to Archive' })
+ const cancel = screen.getByRole('button', { name: 'Cancel' })
+ const move = screen.getByRole('button', { name: 'Move' })
+
+ // The last focusable control wraps back to the first one.
+ move.focus()
+ fireEvent.keyDown(dialog, { key: 'Tab' })
+ expect(document.activeElement).toBe(cancel)
+
+ // …and Shift+Tab from the first wraps to the last.
+ fireEvent.keyDown(dialog, { key: 'Tab', shiftKey: true })
+ expect(document.activeElement).toBe(move)
+ })
+
+ it('returns focus to the opener when the dialog closes', () => {
+ const opener = document.createElement('button')
+ opener.textContent = 'Open the tree row'
+ document.body.append(opener)
+ opener.focus()
+ expect(document.activeElement).toBe(opener)
+
+ const { unmount } = render(
+ <GfsMoveConfirmDialog request={request()} onCancel={vi.fn()} onConfirm={vi.fn()} />
+ )
+ expect(document.activeElement).not.toBe(opener)
+
+ unmount()
+
+ expect(document.activeElement).toBe(opener)
+ opener.remove()
+ })
+})
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 99dd39c39..3ea631711 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.746",
+ "version": "0.1.747",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.746",
+ "version": "0.1.747",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/frontend-components": "file:../packages/frontend-components",
diff --git a/control-ui/package.json b/control-ui/package.json
index 342a94f15..78f8d639e 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.746",
+ "version": "0.1.747",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",
diff --git a/desktop-app/ui/src/gfs/__tests__/moveConfirmDialog.test.tsx b/desktop-app/ui/src/gfs/__tests__/moveConfirmDialog.test.tsx
new file mode 100644
index 000000000..7fee8942f
--- /dev/null
+++ b/desktop-app/ui/src/gfs/__tests__/moveConfirmDialog.test.tsx
@@ -0,0 +1,59 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { GfsMoveConfirmDialog } from '../moveConfirmDialog'
+
+afterEach(() => {
+ cleanup()
+ vi.restoreAllMocks()
+})
+
+function request(canRename = true) {
+ return {
+ sourceName: 'Team folder',
+ destinationName: 'Archive',
+ canRename,
+ }
+}
+
+describe('GfsMoveConfirmDialog — keyboard containment (RP-020)', () => {
+ it('focuses the cancel action when it opens', () => {
+ render(<GfsMoveConfirmDialog request={request()} onCancel={vi.fn()} onConfirm={vi.fn()} />)
+ expect(document.activeElement).toBe(screen.getByRole('button', { name: 'Cancel' }))
+ })
+
+ it('keeps Tab inside the dialog', () => {
+ // No rename option: the dialog's focusables are exactly Cancel and Move.
+ render(<GfsMoveConfirmDialog request={request(false)} onCancel={vi.fn()} onConfirm={vi.fn()} />)
+ const dialog = screen.getByRole('dialog', { name: 'Move folder Team folder to Archive' })
+ const cancel = screen.getByRole('button', { name: 'Cancel' })
+ const move = screen.getByRole('button', { name: 'Move' })
+
+ // The last focusable control wraps back to the first one.
+ move.focus()
+ fireEvent.keyDown(dialog, { key: 'Tab' })
+ expect(document.activeElement).toBe(cancel)
+
+ // …and Shift+Tab from the first wraps to the last.
+ fireEvent.keyDown(dialog, { key: 'Tab', shiftKey: true })
+ expect(document.activeElement).toBe(move)
+ })
+
+ it('returns focus to the opener when the dialog closes', () => {
+ const opener = document.createElement('button')
+ opener.textContent = 'Open the tree row'
+ document.body.append(opener)
+ opener.focus()
+ expect(document.activeElement).toBe(opener)
+
+ const { unmount } = render(
+ <GfsMoveConfirmDialog request={request()} onCancel={vi.fn()} onConfirm={vi.fn()} />
+ )
+ expect(document.activeElement).not.toBe(opener)
+
+ unmount()
+
+ expect(document.activeElement).toBe(opener)
+ opener.remove()
+ })
+})
diff --git a/desktop-app/ui/src/gfs/moveConfirmDialog.tsx b/desktop-app/ui/src/gfs/moveConfirmDialog.tsx
index 5bad992e2..65b9718de 100644
--- a/desktop-app/ui/src/gfs/moveConfirmDialog.tsx
+++ b/desktop-app/ui/src/gfs/moveConfirmDialog.tsx
@@ -33,9 +33,20 @@ export function GfsMoveConfirmDialog({
}: GfsMoveConfirmDialogProps) {
const [allowNumberedRename, setAllowNumberedRename] = useState(false)
const cancelButtonRef = useRef<HTMLButtonElement | null>(null)
+ const dialogRef = useRef<HTMLElement | null>(null)
+ /**
+ * Focus enters the dialog and returns to the opener when it closes: a
+ * keyboard user must never be left on a removed node, and the surface
+ * behind a modal dialog must not be reachable while it is open (RP-020).
+ */
useEffect(() => {
+ const previousActiveElement =
+ document.activeElement instanceof HTMLElement ? document.activeElement : null
cancelButtonRef.current?.focus()
+ return () => {
+ if (previousActiveElement?.isConnected) previousActiveElement.focus()
+ }
}, [])
useEffect(() => {
@@ -47,6 +58,44 @@ export function GfsMoveConfirmDialog({
return () => window.removeEventListener('keydown', handleKeyDown)
}, [busy, onCancel])
+ /**
+ * Tab stays inside the dialog. The panel is a modal surface: without the
+ * wrap, a keyboard user tabs straight out of the confirmation and into the
+ * page behind it, which the modal has just taken over (RP-020).
+ */
+ function handleDialogKeyDown(event: React.KeyboardEvent<HTMLElement>) {
+ if (event.key !== 'Tab') return
+ const panel = dialogRef.current
+ if (!panel) return
+ const focusable = Array.from(
+ panel.querySelectorAll<HTMLElement>(
+ 'a[href], button:not(:disabled), input:not(:disabled):not([type="hidden"]), select:not(:disabled), textarea:not(:disabled), [tabindex]:not([tabindex="-1"])'
+ )
+ ).filter(element => !element.closest('[hidden], [aria-hidden="true"]'))
+ if (focusable.length === 0) {
+ event.preventDefault()
+ panel.focus()
+ return
+ }
+ const first = focusable[0]
+ const last = focusable[focusable.length - 1]
+ if (!first || !last) {
+ event.preventDefault()
+ panel.focus()
+ return
+ }
+ const active = document.activeElement
+ if (event.shiftKey && (active === first || !panel.contains(active))) {
+ event.preventDefault()
+ last.focus()
+ return
+ }
+ if (!event.shiftKey && (active === last || !panel.contains(active))) {
+ event.preventDefault()
+ first.focus()
+ }
+ }
+
return (
<div
className="da-gfs-manage-modal"
@@ -59,7 +108,10 @@ export function GfsMoveConfirmDialog({
aria-label={`Move folder ${request.sourceName} to ${request.destinationName}`}
aria-modal="true"
className="da-gfs-manage-dialog da-gfs-move-confirm-dialog"
+ onKeyDown={handleDialogKeyDown}
+ ref={dialogRef}
role="dialog"
+ tabIndex={-1}
>
<header className="da-gfs-manage-dialog__header">
<span className="da-gfs-manage-dialog__heading">