Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

ui-nit: modals focus the primary action on open, arrow keys move between footer buttons

aashu6911ba412067
diff --git a/packages/react/src/Modal.tsx b/packages/react/src/Modal.tsx
index addf55dbbc..5a8089b106 100644
--- a/packages/react/src/Modal.tsx
+++ b/packages/react/src/Modal.tsx
@@ -6,7 +6,8 @@ import { cva } from "class-variance-authority";
import type {
ComponentPropsWithoutRef,
ElementRef,
- HTMLAttributes
+ HTMLAttributes,
+ KeyboardEvent
} from "react";
import { forwardRef } from "react";
@@ -41,6 +42,42 @@ const ModalOverlay = forwardRef<
));
ModalOverlay.displayName = DialogPrimitive.Overlay.displayName;
+const FOCUSABLE =
+ 'button:not([disabled]), input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
+
+function getFooterActions(footer: Element) {
+ return Array.from(
+ footer.querySelectorAll<HTMLElement>("button:not([disabled]), a[href]")
+ ).filter((el) => el.offsetParent !== null);
+}
+
+// When nothing before the footer can take focus (a plain confirm modal), start
+// on the primary action instead of Cancel.
+function focusPrimaryAction(event: Event) {
+ const content = event.currentTarget as HTMLElement;
+ const first = Array.from(
+ content.querySelectorAll<HTMLElement>(FOCUSABLE)
+ ).find((el) => el.offsetParent !== null);
+ const footer = first?.closest("[data-modal-footer]");
+ if (!footer) return;
+ const actions = getFooterActions(footer);
+ const primary = actions[actions.length - 1];
+ if (!primary) return;
+ event.preventDefault();
+ primary.focus();
+}
+
+function moveBetweenFooterActions(event: KeyboardEvent<HTMLDivElement>) {
+ if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
+ if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
+ const actions = getFooterActions(event.currentTarget);
+ const index = actions.indexOf(event.target as HTMLElement);
+ if (index === -1) return;
+ event.preventDefault();
+ const step = event.key === "ArrowRight" ? 1 : -1;
+ actions[(index + step + actions.length) % actions.length].focus();
+}
+
const ModalContentVariants = cva(
cn(
"px-0 pt-6",
@@ -79,32 +116,48 @@ const ModalContent = forwardRef<
VariantProps<typeof ModalContentVariants> & {
withCloseButton?: boolean;
}
->(({ className, children, size, withCloseButton = true, ...props }, ref) => (
- <ClientOnly fallback={null}>
- {() => (
- <ModalPortal>
- <ModalOverlay>
- <DialogPrimitive.Content
- ref={ref}
- className={cn(ModalContentVariants({ size }), className)}
- {...props}
- >
- {children}
- {withCloseButton && (
- <DialogPrimitive.Close
- type="button"
- className="absolute right-4 top-4 rounded-full opacity-70 transition-opacity hover:opacity-100 outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-foreground-muted p-3 hover:bg-accent/80"
- >
- <LuX className="h-4 w-4" />
- <span className="sr-only">Close</span>
- </DialogPrimitive.Close>
- )}
- </DialogPrimitive.Content>
- </ModalOverlay>
- </ModalPortal>
- )}
- </ClientOnly>
-));
+>(
+ (
+ {
+ className,
+ children,
+ size,
+ withCloseButton = true,
+ onOpenAutoFocus,
+ ...props
+ },
+ ref
+ ) => (
+ <ClientOnly fallback={null}>
+ {() => (
+ <ModalPortal>
+ <ModalOverlay>
+ <DialogPrimitive.Content
+ ref={ref}
+ className={cn(ModalContentVariants({ size }), className)}
+ onOpenAutoFocus={(event) => {
+ onOpenAutoFocus?.(event);
+ if (!event.defaultPrevented) focusPrimaryAction(event);
+ }}
+ {...props}
+ >
+ {children}
+ {withCloseButton && (
+ <DialogPrimitive.Close
+ type="button"
+ className="absolute right-4 top-4 rounded-full opacity-70 transition-opacity hover:opacity-100 outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-foreground-muted p-3 hover:bg-accent/80"
+ >
+ <LuX className="h-4 w-4" />
+ <span className="sr-only">Close</span>
+ </DialogPrimitive.Close>
+ )}
+ </DialogPrimitive.Content>
+ </ModalOverlay>
+ </ModalPortal>
+ )}
+ </ClientOnly>
+ )
+);
ModalContent.displayName = DialogPrimitive.Content.displayName;
const ModalHeader = ({
@@ -135,9 +188,15 @@ ModalBody.displayName = "ModalBody";
const ModalFooter = ({
className,
+ onKeyDown,
...props
}: HTMLAttributes<HTMLDivElement>) => (
<div
+ data-modal-footer=""
+ onKeyDown={(event) => {
+ onKeyDown?.(event);
+ if (!event.defaultPrevented) moveBetweenFooterActions(event);
+ }}
className={cn(
"flex flex-col-reverse shrink-0 sm:flex-row sm:justify-end gap-2 px-6 py-3 border-t border-border bg-muted/40 sm:rounded-b-2xl",
className