From 84d3211a1cbbf8f713d05ab7d036389cb3b53dfd Mon Sep 17 00:00:00 2001 From: Leon-JavaScript <70959426+Leon-JavaScript@users.noreply.github.com> Date: Sun, 15 Feb 2026 14:26:32 +0100 Subject: [PATCH] feat: enhance DialogContent with keyboard navigation support --- src/components/ui/dialog.tsx | 75 +++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/src/components/ui/dialog.tsx b/src/components/ui/dialog.tsx index eb875e5..8420cf1 100644 --- a/src/components/ui/dialog.tsx +++ b/src/components/ui/dialog.tsx @@ -32,25 +32,61 @@ DialogOverlay.displayName = DialogPrimitive.Overlay.displayName const DialogContent = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef ->(({ className, children, ...props }, ref) => ( - - - - {children} - - - Close - - - -)) +>(({ className, children, onKeyDown, ...props }, ref) => { + const handleKeyDown = React.useCallback( + (event: React.KeyboardEvent) => { + onKeyDown?.(event as React.KeyboardEvent) + if (event.defaultPrevented) return + if (event.key !== "Enter") return + if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return + + const target = event.target as HTMLElement | null + if (!target) return + if (target.isContentEditable || target.tagName === "TEXTAREA") return + + const content = event.currentTarget as HTMLElement + const explicitTarget = content.querySelector( + "[data-dialog-submit='true'], button[type='submit']" + ) + const footerButtons = Array.from( + content.querySelectorAll( + "[data-dialog-footer] button:not([disabled])" + ) + ) + const fallbackTarget = + footerButtons[footerButtons.length - 1] ?? + content.querySelector("button:not([disabled])") + + const actionTarget = explicitTarget ?? fallbackTarget + if (!actionTarget) return + + event.preventDefault() + actionTarget.click() + }, + [onKeyDown] + ) + + return ( + + + + {children} + + + Close + + + + ) +}) DialogContent.displayName = DialogPrimitive.Content.displayName const DialogHeader = ({ @@ -76,6 +112,7 @@ const DialogFooter = ({ "flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2", className )} + data-dialog-footer {...props} /> )