Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): silence Radix DialogContent missing Description warning (#11035) (#14576)

Nicholas Tindlebdd3594b503f
diff --git a/autogpt_platform/frontend/src/components/__legacy__/ui/__tests__/dialog.test.tsx b/autogpt_platform/frontend/src/components/__legacy__/ui/__tests__/dialog.test.tsx
new file mode 100644
index 0000000000..e59bc152d3
--- /dev/null
+++ b/autogpt_platform/frontend/src/components/__legacy__/ui/__tests__/dialog.test.tsx
@@ -0,0 +1,68 @@
+import { describe, expect, test } from "vitest";
+import { render, screen } from "@testing-library/react";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogHeader,
+ DialogTitle,
+} from "../dialog";
+
+describe("legacy DialogContent", () => {
+ test("injects an sr-only Description when none is provided", () => {
+ render(
+ <Dialog open>
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>Untitled dialog</DialogTitle>
+ </DialogHeader>
+ <p>Body</p>
+ </DialogContent>
+ </Dialog>,
+ );
+
+ const dialog = screen.getByRole("dialog");
+ const descriptionId = dialog.getAttribute("aria-describedby");
+ expect(descriptionId).toBeTruthy();
+ const description = document.getElementById(descriptionId ?? "");
+ expect(description?.textContent).toBe("Dialog");
+ expect(description?.classList.contains("sr-only")).toBe(true);
+ });
+
+ test("keeps an explicit DialogDescription without injecting a fallback", () => {
+ render(
+ <Dialog open>
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>Confirm</DialogTitle>
+ <DialogDescription>This action cannot be undone.</DialogDescription>
+ </DialogHeader>
+ </DialogContent>
+ </Dialog>,
+ );
+
+ const dialog = screen.getByRole("dialog");
+ const descriptionId = dialog.getAttribute("aria-describedby");
+ expect(descriptionId).toBeTruthy();
+ const description = document.getElementById(descriptionId ?? "");
+ expect(description?.textContent).toBe("This action cannot be undone.");
+ expect(screen.queryByText("Dialog", { selector: "p.sr-only" })).toBeNull();
+ });
+
+ test("respects a caller-provided aria-describedby without injecting a fallback", () => {
+ render(
+ <Dialog open>
+ <DialogContent aria-describedby="custom-description">
+ <DialogHeader>
+ <DialogTitle>Export</DialogTitle>
+ </DialogHeader>
+ <p id="custom-description">Downloads a JSON file.</p>
+ </DialogContent>
+ </Dialog>,
+ );
+
+ const dialog = screen.getByRole("dialog");
+ expect(dialog.getAttribute("aria-describedby")).toBe("custom-description");
+ expect(screen.queryByText("Dialog", { selector: "p.sr-only" })).toBeNull();
+ });
+});
diff --git a/autogpt_platform/frontend/src/components/__legacy__/ui/dialog.tsx b/autogpt_platform/frontend/src/components/__legacy__/ui/dialog.tsx
index 10af4fa3c8..8a44158a73 100644
--- a/autogpt_platform/frontend/src/components/__legacy__/ui/dialog.tsx
+++ b/autogpt_platform/frontend/src/components/__legacy__/ui/dialog.tsx
@@ -37,6 +37,26 @@ const DialogOverlay = React.forwardRef<
));
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
+// Walks the static JSX tree only: a Description returned from a custom child
+// component is invisible here and would get a duplicate fallback. Such callers
+// should pass their own `aria-describedby` to DialogContent instead.
+function hasDialogDescription(node: React.ReactNode): boolean {
+ return React.Children.toArray(node).some((child) => {
+ if (!React.isValidElement(child)) return false;
+ const type = child.type as { displayName?: string } | string;
+ if (type === DialogPrimitive.Description) return true;
+ if (
+ typeof type === "object" &&
+ type !== null &&
+ type.displayName === DialogPrimitive.Description.displayName
+ ) {
+ return true;
+ }
+ const childProps = child.props as { children?: React.ReactNode };
+ return hasDialogDescription(childProps.children);
+ });
+}
+
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
@@ -85,6 +105,11 @@ const DialogContent = React.forwardRef<
{...props}
>
{children}
+ {!hasDialogDescription(children) && !("aria-describedby" in props) ? (
+ <DialogPrimitive.Description className="sr-only">
+ Dialog
+ </DialogPrimitive.Description>
+ ) : null}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-white transition-opacity data-[state=open]:bg-neutral-100 data-[state=open]:text-neutral-500 hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-neutral-950 focus:ring-offset-2 disabled:pointer-events-none dark:ring-offset-neutral-950 dark:data-[state=open]:bg-neutral-800 dark:data-[state=open]:text-neutral-400 dark:focus:ring-neutral-300">
<Cross2Icon className="h-4 w-4" />
<span className="sr-only">Close</span>
diff --git a/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/Dialog.test.tsx b/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/Dialog.test.tsx
index c043d8027d..8dd041b117 100644
--- a/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/Dialog.test.tsx
+++ b/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/Dialog.test.tsx
@@ -34,7 +34,7 @@ describe("Dialog", () => {
renderDialog({ title: "My Title" });
const dialog = screen.getByRole("dialog");
- const heading = within(dialog).getByText("My Title");
+ const heading = within(dialog).getByRole("heading", { name: "My Title" });
expect(heading).toBeDefined();
expect(heading.classList.contains("sr-only")).toBe(false);
});
@@ -43,7 +43,7 @@ describe("Dialog", () => {
renderDialog({});
const dialog = screen.getByRole("dialog");
- const fallback = within(dialog).getByText("Dialog");
+ const fallback = within(dialog).getByRole("heading", { name: "Dialog" });
expect(fallback).toBeDefined();
expect(fallback.classList.contains("sr-only")).toBe(true);
});
@@ -52,7 +52,7 @@ describe("Dialog", () => {
renderDialog({ title: "" });
const dialog = screen.getByRole("dialog");
- const fallback = within(dialog).getByText("Dialog");
+ const fallback = within(dialog).getByRole("heading", { name: "Dialog" });
expect(fallback).toBeDefined();
expect(fallback.classList.contains("sr-only")).toBe(true);
});
@@ -116,4 +116,26 @@ describe("Dialog", () => {
expect(screen.queryByRole("dialog")).toBeNull();
});
+
+ test("associates an sr-only Description with constant text Dialog", () => {
+ renderDialog({ title: "My Title" });
+
+ const dialog = screen.getByRole("dialog");
+ const descriptionId = dialog.getAttribute("aria-describedby");
+ expect(descriptionId).toBeTruthy();
+ const description = document.getElementById(descriptionId ?? "");
+ expect(description?.textContent).toBe("Dialog");
+ expect(description?.classList.contains("sr-only")).toBe(true);
+ });
+
+ test("associates the same default sr-only Description when title is missing", () => {
+ renderDialog({});
+
+ const dialog = screen.getByRole("dialog");
+ const descriptionId = dialog.getAttribute("aria-describedby");
+ expect(descriptionId).toBeTruthy();
+ const description = document.getElementById(descriptionId ?? "");
+ expect(description?.textContent).toBe("Dialog");
+ expect(description?.classList.contains("sr-only")).toBe(true);
+ });
});
diff --git a/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/DialogDrawer.test.tsx b/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/DialogDrawer.test.tsx
index 42ca98d15c..a7f7250a0a 100644
--- a/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/DialogDrawer.test.tsx
+++ b/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/DialogDrawer.test.tsx
@@ -31,7 +31,9 @@ describe("Dialog as Drawer (small screen)", () => {
renderDrawerDialog({ title: "Drawer Title" });
const dialog = screen.getByRole("dialog");
- const heading = within(dialog).getByText("Drawer Title");
+ const heading = within(dialog).getByRole("heading", {
+ name: "Drawer Title",
+ });
expect(heading).toBeDefined();
expect(heading.classList.contains("sr-only")).toBe(false);
});
@@ -40,7 +42,7 @@ describe("Dialog as Drawer (small screen)", () => {
renderDrawerDialog({});
const dialog = screen.getByRole("dialog");
- const fallback = within(dialog).getByText("Dialog");
+ const fallback = within(dialog).getByRole("heading", { name: "Dialog" });
expect(fallback).toBeDefined();
expect(fallback.classList.contains("sr-only")).toBe(true);
});
@@ -49,7 +51,7 @@ describe("Dialog as Drawer (small screen)", () => {
renderDrawerDialog({ title: "" });
const dialog = screen.getByRole("dialog");
- const fallback = within(dialog).getByText("Dialog");
+ const fallback = within(dialog).getByRole("heading", { name: "Dialog" });
expect(fallback).toBeDefined();
expect(fallback.classList.contains("sr-only")).toBe(true);
});
diff --git a/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/Drawer.test.tsx b/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/Drawer.test.tsx
index bb5fe1ec99..f1b6591af6 100644
--- a/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/Drawer.test.tsx
+++ b/autogpt_platform/frontend/src/components/molecules/Dialog/__tests__/Drawer.test.tsx
@@ -63,4 +63,15 @@ describe("Dialog rendered as a drawer", () => {
expect(set).toHaveBeenCalledWith(false);
});
+
+ test("associates an sr-only Description with constant text Dialog", () => {
+ renderDrawer({ set: vi.fn() });
+
+ const dialog = screen.getByRole("dialog");
+ const descriptionId = dialog.getAttribute("aria-describedby");
+ expect(descriptionId).toBeTruthy();
+ const description = document.getElementById(descriptionId ?? "");
+ expect(description?.textContent).toBe("Dialog");
+ expect(description?.classList.contains("sr-only")).toBe(true);
+ });
});
diff --git a/autogpt_platform/frontend/src/components/molecules/Dialog/components/DialogWrap.tsx b/autogpt_platform/frontend/src/components/molecules/Dialog/components/DialogWrap.tsx
index b7ace22735..7f3ed833bc 100644
--- a/autogpt_platform/frontend/src/components/molecules/Dialog/components/DialogWrap.tsx
+++ b/autogpt_platform/frontend/src/components/molecules/Dialog/components/DialogWrap.tsx
@@ -109,7 +109,6 @@ export function DialogWrap({
onPointerDownOutside={handlePointerDownOutside}
onFocusOutside={handleFocusOutside}
onEscapeKeyDown={handleEscapeKeyDown}
- aria-describedby={undefined}
className={cn(
modalStyles.content,
isCompact && compactStyles.content,
@@ -151,6 +150,7 @@ export function DialogWrap({
</Button>
)}
</div>
+ <RXDialog.Description className="sr-only">Dialog</RXDialog.Description>
<div className="flex min-h-0 flex-1 flex-col">
<div
ref={scrollRef}
diff --git a/autogpt_platform/frontend/src/components/molecules/Dialog/components/DrawerWrap.tsx b/autogpt_platform/frontend/src/components/molecules/Dialog/components/DrawerWrap.tsx
index 3363ce703a..5feb96b438 100644
--- a/autogpt_platform/frontend/src/components/molecules/Dialog/components/DrawerWrap.tsx
+++ b/autogpt_platform/frontend/src/components/molecules/Dialog/components/DrawerWrap.tsx
@@ -51,7 +51,6 @@ export function DrawerWrap({
<Drawer.Portal>
<Drawer.Overlay className={drawerStyles.overlay} />
<Drawer.Content
- aria-describedby={undefined}
className={cn(
drawerStyles.content,
isCompact && compactStyles.drawerContent,
@@ -93,6 +92,7 @@ export function DrawerWrap({
)
) : null}
</div>
+ <Drawer.Description className="sr-only">Dialog</Drawer.Description>
<div className="flex min-h-0 flex-1 flex-col overflow-hidden">
<div
className={cn(