feat: add Escape key handling to close modals and dialogs in App and UserProfile components

This commit is contained in:
Daniel Volz
2025-12-28 03:07:08 +01:00
parent f3da765f7c
commit 0ecb892a15
2 changed files with 20 additions and 1 deletions
+9 -1
View File
@@ -379,8 +379,16 @@ function AppContent() {
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === "Escape") {
// Close modals in order of priority (topmost first)
if (showImageLightbox) {
setShowImageLightbox(false);
} else if (showEditModal) {
setShowEditModal(false);
resetForm();
} else if (showShareDialog) {
setShowShareDialog(false);
} else if (showProfile) {
setShowProfile(false);
} else if (selectedUser) {
setSelectedUser(null);
} else if (selectedMed) {
@@ -390,7 +398,7 @@ function AppContent() {
};
document.addEventListener("keydown", handleEscape);
return () => document.removeEventListener("keydown", handleEscape);
}, [selectedMed, showImageLightbox, selectedUser]);
}, [selectedMed, showImageLightbox, selectedUser, showProfile, showShareDialog, showEditModal]);
// Check if settings have changed
const settingsChanged = settings.emailEnabled !== savedSettings.emailEnabled ||
+11
View File
@@ -499,6 +499,17 @@ export function UserProfile({ onClose }: { onClose?: () => void }) {
const [avatarLoading, setAvatarLoading] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
// Close on Escape key
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === "Escape" && onClose) {
onClose();
}
};
document.addEventListener("keydown", handleEscape);
return () => document.removeEventListener("keydown", handleEscape);
}, [onClose]);
async function handleAvatarUpload(e: React.ChangeEvent<HTMLInputElement>) {
const file = e.target.files?.[0];
if (!file) return;