Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(ui): focus alert dialog inputs on open (#8243)

Andrew Huang22066358f376
diff --git a/frontend/src/components/v3/generic/AlertDialog/AlertDialog.stories.tsx b/frontend/src/components/v3/generic/AlertDialog/AlertDialog.stories.tsx
index 65df4cd4ac..4d867004a7 100644
--- a/frontend/src/components/v3/generic/AlertDialog/AlertDialog.stories.tsx
+++ b/frontend/src/components/v3/generic/AlertDialog/AlertDialog.stories.tsx
@@ -1,9 +1,11 @@
import { useState } from "react";
import type { Meta, StoryObj } from "@storybook/react-vite";
import { LogOutIcon, TrashIcon } from "lucide-react";
+import { expect, userEvent, within } from "storybook/test";
import { Alert, AlertDescription } from "../Alert";
import { Button } from "../Button";
+import { Input } from "../Input";
import {
AlertDialog,
AlertDialogAction,
@@ -75,7 +77,101 @@ export const Default: Story = {
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
- )
+ ),
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.click(canvas.getByRole("button", { name: "Continue" }));
+
+ await expect(body.getByRole("button", { name: "Cancel" })).toHaveFocus();
+ }
+};
+
+export const OpenFocusOverride: Story = {
+ name: "Example: Open Focus Override",
+ parameters: {
+ docs: {
+ description: {
+ story:
+ "Prevent `onOpenAutoFocus` and move focus explicitly when a workflow needs a control other than the first input or Cancel to receive focus."
+ }
+ }
+ },
+ render: () => (
+ <AlertDialog>
+ <AlertDialogTrigger asChild>
+ <Button variant="outline">Open with custom focus</Button>
+ </AlertDialogTrigger>
+ <AlertDialogContent
+ onOpenAutoFocus={(event) => {
+ event.preventDefault();
+ (event.currentTarget as HTMLElement | null)
+ ?.querySelector<HTMLElement>("[data-slot='alert-dialog-action']")
+ ?.focus();
+ }}
+ >
+ <AlertDialogHeader>
+ <AlertDialogTitle>Continue with custom focus?</AlertDialogTitle>
+ <AlertDialogDescription>
+ This example moves focus directly to the Continue action when the dialog opens.
+ </AlertDialogDescription>
+ </AlertDialogHeader>
+ <AlertDialogFooter>
+ <AlertDialogCancel>Cancel</AlertDialogCancel>
+ <AlertDialogAction>Continue</AlertDialogAction>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
+ ),
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.click(canvas.getByRole("button", { name: "Open with custom focus" }));
+
+ await expect(body.getByRole("button", { name: "Continue" })).toHaveFocus();
+ }
+};
+
+export const InputFocus: Story = {
+ name: "Example: Input Focus",
+ parameters: {
+ docs: {
+ description: {
+ story:
+ "When an alert dialog contains inputs, the first enabled, visible input receives focus when the dialog opens."
+ }
+ }
+ },
+ render: () => (
+ <AlertDialog>
+ <AlertDialogTrigger asChild>
+ <Button variant="outline">Open input dialog</Button>
+ </AlertDialogTrigger>
+ <AlertDialogContent>
+ <AlertDialogHeader>
+ <AlertDialogTitle>Confirm account name?</AlertDialogTitle>
+ <AlertDialogDescription>Enter the account name before continuing.</AlertDialogDescription>
+ </AlertDialogHeader>
+ <Input hidden aria-label="Hidden input" />
+ <Input disabled aria-label="Disabled input" />
+ <Input aria-label="Account name" />
+ <AlertDialogFooter>
+ <AlertDialogCancel>Cancel</AlertDialogCancel>
+ <AlertDialogAction>Continue</AlertDialogAction>
+ </AlertDialogFooter>
+ </AlertDialogContent>
+ </AlertDialog>
+ ),
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.click(canvas.getByRole("button", { name: "Open input dialog" }));
+
+ await expect(body.getByRole("textbox", { name: "Account name" })).toHaveFocus();
+ }
};
export const DestructiveAction: Story = {
diff --git a/frontend/src/components/v3/generic/AlertDialog/AlertDialog.tsx b/frontend/src/components/v3/generic/AlertDialog/AlertDialog.tsx
index 2097c67a14..38f05ec807 100644
--- a/frontend/src/components/v3/generic/AlertDialog/AlertDialog.tsx
+++ b/frontend/src/components/v3/generic/AlertDialog/AlertDialog.tsx
@@ -90,6 +90,7 @@ function AlertDialogOverlay({
function AlertDialogContent({
className,
size = "default",
+ onOpenAutoFocus,
...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Content> & {
size?: "default" | "sm";
@@ -105,6 +106,21 @@ function AlertDialogContent({
DIALOG_CONTENT_WIDTH_CLASSNAME,
className
)}
+ onOpenAutoFocus={(event) => {
+ onOpenAutoFocus?.(event);
+ if (event.defaultPrevented) return;
+
+ const inputs = (
+ event.currentTarget as HTMLElement | null
+ )?.querySelectorAll<HTMLInputElement>("input:not([type='hidden']):not([disabled])");
+
+ const focusedInput = Array.from(inputs ?? []).find((input) => {
+ input.focus({ preventScroll: true });
+ return input.ownerDocument.activeElement === input;
+ });
+
+ if (focusedInput) event.preventDefault();
+ }}
{...props}
/>
</AlertDialogPortal>