feat: add closing animation to all modal dialogs and fix drag-dismiss bug

This commit is contained in:
Xiaohan-Tian
2026-04-20 00:36:32 -07:00
parent 778c0236b6
commit 55861cf3cd
6 changed files with 154 additions and 35 deletions
+25
View File
@@ -35,6 +35,31 @@
} }
} }
@keyframes dialogFadeOut {
from {
opacity: 1;
transform: scale(1) translateY(0);
}
to {
opacity: 0;
transform: scale(0.95) translateY(-10px);
}
}
@keyframes overlayFadeOut {
from { opacity: 1; }
to { opacity: 0; }
}
.dialog-overlay-closing {
animation: overlayFadeOut 0.15s ease-in forwards;
}
.dialog-modal-closing {
animation: dialogFadeOut 0.15s ease-in forwards;
}
.dialog-header { .dialog-header {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
+14 -4
View File
@@ -66,11 +66,13 @@ interface DialogInfo {
const DialogProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const DialogProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [dialog, setDialog] = useState<DialogInfo | null>(null); const [dialog, setDialog] = useState<DialogInfo | null>(null);
const [isClosing, setIsClosing] = useState(false);
const [inputValue, setInputValue] = useState(''); const [inputValue, setInputValue] = useState('');
const [timeSigNumerator, setTimeSigNumerator] = useState(''); const [timeSigNumerator, setTimeSigNumerator] = useState('');
const [timeSigDenominator, setTimeSigDenominator] = useState(''); const [timeSigDenominator, setTimeSigDenominator] = useState('');
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const resolveRef = useRef<((value: any) => void) | null>(null); const resolveRef = useRef<((value: any) => void) | null>(null);
const pendingValueRef = useRef<unknown>(undefined);
const openAlert = useCallback((message: string): Promise<void> => { const openAlert = useCallback((message: string): Promise<void> => {
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
@@ -104,15 +106,23 @@ const DialogProvider: React.FC<{ children: React.ReactNode }> = ({ children }) =
}, []); }, []);
const close = useCallback((value: unknown) => { const close = useCallback((value: unknown) => {
pendingValueRef.current = value;
setIsClosing(true);
}, []);
const handleAnimationEnd = useCallback((e: React.AnimationEvent) => {
if (e.target !== e.currentTarget) return;
if (!isClosing) return;
setIsClosing(false);
setDialog(null); setDialog(null);
setInputValue(''); setInputValue('');
setTimeSigNumerator(''); setTimeSigNumerator('');
setTimeSigDenominator(''); setTimeSigDenominator('');
if (resolveRef.current) { if (resolveRef.current) {
resolveRef.current(value); resolveRef.current(pendingValueRef.current);
resolveRef.current = null; resolveRef.current = null;
} }
}, []); }, [isClosing]);
const mouseDownOnOverlay = useRef(false); const mouseDownOnOverlay = useRef(false);
@@ -161,8 +171,8 @@ const DialogProvider: React.FC<{ children: React.ReactNode }> = ({ children }) =
return ( return (
<> <>
{children} {children}
<div className="dialog-overlay" onMouseDown={handleOverlayMouseDown} onClick={handleOverlayClick}> <div className={`dialog-overlay${isClosing ? ' dialog-overlay-closing' : ''}`} onMouseDown={handleOverlayMouseDown} onClick={handleOverlayClick} onAnimationEnd={handleAnimationEnd}>
<div className="dialog-modal"> <div className={`dialog-modal${isClosing ? ' dialog-modal-closing' : ''}`}>
<div className="dialog-header"> <div className="dialog-header">
<h3 className="dialog-title">{title}</h3> <h3 className="dialog-title">{title}</h3>
<button <button
+24
View File
@@ -36,6 +36,30 @@
} }
} }
@keyframes fileImportFadeOut {
from {
opacity: 1;
transform: scale(1) translateY(0);
}
to {
opacity: 0;
transform: scale(0.95) translateY(-10px);
}
}
@keyframes fileImportOverlayFadeOut {
from { opacity: 1; }
to { opacity: 0; }
}
.file-import-overlay-closing {
animation: fileImportOverlayFadeOut 0.15s ease-in forwards;
}
.file-import-modal-closing {
animation: fileImportFadeOut 0.15s ease-in forwards;
}
.file-import-header { .file-import-header {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
+32 -16
View File
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react'; import React, { useCallback, useRef, useState } from 'react';
import './FileImportModal.css'; import './FileImportModal.css';
import { FaTimes } from 'react-icons/fa'; import { FaTimes } from 'react-icons/fa';
import { showAlert } from './DialogProvider'; import { showAlert } from './DialogProvider';
@@ -21,6 +21,17 @@ const FileImportModal: React.FC<FileImportModalProps> = ({
description = 'Drag and drop your project file here' description = 'Drag and drop your project file here'
}) => { }) => {
const [isDragOver, setIsDragOver] = useState(false); const [isDragOver, setIsDragOver] = useState(false);
const [isClosing, setIsClosing] = useState(false);
const mouseDownOnOverlay = useRef(false);
const startClose = useCallback(() => setIsClosing(true), []);
const handleAnimationEnd = useCallback((e: React.AnimationEvent) => {
if (e.target !== e.currentTarget) return;
if (!isClosing) return;
setIsClosing(false);
onClose();
}, [isClosing, onClose]);
const handleDragEnter = useCallback((e: React.DragEvent) => { const handleDragEnter = useCallback((e: React.DragEvent) => {
e.preventDefault(); e.preventDefault();
@@ -31,7 +42,6 @@ const FileImportModal: React.FC<FileImportModalProps> = ({
const handleDragLeave = useCallback((e: React.DragEvent) => { const handleDragLeave = useCallback((e: React.DragEvent) => {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
// Only set drag over to false if we're leaving the drop zone entirely
if (e.currentTarget === e.target) { if (e.currentTarget === e.target) {
setIsDragOver(false); setIsDragOver(false);
} }
@@ -50,45 +60,51 @@ const FileImportModal: React.FC<FileImportModalProps> = ({
const files = Array.from(e.dataTransfer.files); const files = Array.from(e.dataTransfer.files);
if (files.length > 0) { if (files.length > 0) {
const file = files[0]; const file = files[0];
// Check if file type is accepted
const fileExtension = '.' + file.name.split('.').pop()?.toLowerCase(); const fileExtension = '.' + file.name.split('.').pop()?.toLowerCase();
if (acceptedTypes.includes(fileExtension)) { if (acceptedTypes.includes(fileExtension)) {
onFileImport(file); onFileImport(file);
onClose(); startClose();
} else { } else {
await showAlert(`Invalid file type. Please select a file with one of these extensions: ${acceptedTypes.join(', ')}`); await showAlert(`Invalid file type. Please select a file with one of these extensions: ${acceptedTypes.join(', ')}`);
} }
} }
}, [acceptedTypes, onFileImport, onClose]); }, [acceptedTypes, onFileImport, startClose]);
const handleFileSelect = useCallback((e: React.ChangeEvent<HTMLInputElement>) => { const handleFileSelect = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const files = e.target.files; const files = e.target.files;
if (files && files.length > 0) { if (files && files.length > 0) {
onFileImport(files[0]); onFileImport(files[0]);
onClose(); startClose();
} }
}, [onFileImport, onClose]); }, [onFileImport, startClose]);
const handleOverlayMouseDown = useCallback((e: React.MouseEvent) => {
mouseDownOnOverlay.current = e.target === e.currentTarget;
}, []);
const handleOverlayClick = useCallback((e: React.MouseEvent) => { const handleOverlayClick = useCallback((e: React.MouseEvent) => {
// Only close if clicking on the overlay itself, not the modal content if (e.target === e.currentTarget && mouseDownOnOverlay.current) {
if (e.target === e.currentTarget) { startClose();
onClose();
} }
}, [onClose]); }, [startClose]);
if (!isVisible) { if (!isVisible && !isClosing) {
return null; return null;
} }
return ( return (
<div className="file-import-overlay" onClick={handleOverlayClick}> <div
<div className="file-import-modal"> className={`file-import-overlay${isClosing ? ' file-import-overlay-closing' : ''}`}
onMouseDown={handleOverlayMouseDown}
onClick={handleOverlayClick}
onAnimationEnd={handleAnimationEnd}
>
<div className={`file-import-modal${isClosing ? ' file-import-modal-closing' : ''}`}>
<div className="file-import-header"> <div className="file-import-header">
<h3 className="file-import-title">{title}</h3> <h3 className="file-import-title">{title}</h3>
<button <button
className="file-import-close-btn" className="file-import-close-btn"
onClick={onClose} onClick={startClose}
aria-label="Close import modal" aria-label="Close import modal"
> >
<FaTimes /> <FaTimes />
@@ -39,6 +39,30 @@
} }
} }
@keyframes openProjectFadeOut {
from {
opacity: 1;
transform: scale(1) translateY(0);
}
to {
opacity: 0;
transform: scale(0.95) translateY(-10px);
}
}
@keyframes openProjectOverlayFadeOut {
from { opacity: 1; }
to { opacity: 0; }
}
.open-project-overlay-closing {
animation: openProjectOverlayFadeOut 0.15s ease-in forwards;
}
.open-project-panel-closing {
animation: openProjectFadeOut 0.15s ease-in forwards;
}
/* Header */ /* Header */
.open-project-header { .open-project-header {
display: flex; display: flex;
+31 -11
View File
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useCallback } from 'react'; import React, { useState, useEffect, useCallback, useRef } from 'react';
import { FaTimes, FaSortUp, FaSortDown, FaCopy, FaTrash, FaUndo } from 'react-icons/fa'; import { FaTimes, FaSortUp, FaSortDown, FaCopy, FaTrash, FaUndo } from 'react-icons/fa';
import { KGProjectStorage, type ProjectMeta } from '../../core/io/KGProjectStorage'; import { KGProjectStorage, type ProjectMeta } from '../../core/io/KGProjectStorage';
import { isValidProjectName } from '../../util/projectNameUtil'; import { isValidProjectName } from '../../util/projectNameUtil';
@@ -48,12 +48,23 @@ const OpenProjectModal: React.FC<OpenProjectModalProps> = ({ onClose, onOpenProj
const [projects, setProjects] = useState<ProjectMeta[]>([]); const [projects, setProjects] = useState<ProjectMeta[]>([]);
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const [filter, setFilter] = useState(''); const [filter, setFilter] = useState('');
const [isClosing, setIsClosing] = useState(false);
const mouseDownOnOverlay = useRef(false);
const [sortField, setSortField] = useState<SortField>('updatedAt'); const [sortField, setSortField] = useState<SortField>('updatedAt');
const [sortDirection, setSortDirection] = useState<SortDirection>('desc'); const [sortDirection, setSortDirection] = useState<SortDirection>('desc');
const [viewMode, setViewMode] = useState<ViewMode>('projects'); const [viewMode, setViewMode] = useState<ViewMode>('projects');
const startClose = useCallback(() => setIsClosing(true), []);
const handleAnimationEnd = useCallback((e: React.AnimationEvent) => {
if (e.target !== e.currentTarget) return;
if (!isClosing) return;
setIsClosing(false);
onClose();
}, [isClosing, onClose]);
const fetchProjects = useCallback(async (mode: ViewMode) => { const fetchProjects = useCallback(async (mode: ViewMode) => {
setIsLoading(true); setIsLoading(true);
try { try {
@@ -78,11 +89,11 @@ const OpenProjectModal: React.FC<OpenProjectModalProps> = ({ onClose, onOpenProj
useEffect(() => { useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => { const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') onClose(); if (e.key === 'Escape') startClose();
}; };
window.addEventListener('keydown', handleKeyDown); window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown);
}, [onClose]); }, [startClose]);
const handleSortClick = useCallback((field: SortField) => { const handleSortClick = useCallback((field: SortField) => {
if (field === sortField) { if (field === sortField) {
@@ -111,7 +122,7 @@ const OpenProjectModal: React.FC<OpenProjectModalProps> = ({ onClose, onOpenProj
const handleOpen = async (projectName: string) => { const handleOpen = async (projectName: string) => {
await onOpenProject(projectName); await onOpenProject(projectName);
onClose(); startClose();
}; };
const handleDuplicate = async (e: React.MouseEvent, projectName: string) => { const handleDuplicate = async (e: React.MouseEvent, projectName: string) => {
@@ -130,7 +141,7 @@ const OpenProjectModal: React.FC<OpenProjectModalProps> = ({ onClose, onOpenProj
const finalName = await storage.resolveUniqueName(trimmed); const finalName = await storage.resolveUniqueName(trimmed);
await storage.duplicate(projectName, finalName); await storage.duplicate(projectName, finalName);
await onOpenProject(finalName); await onOpenProject(finalName);
onClose(); startClose();
} catch (error) { } catch (error) {
console.error('Error duplicating project:', error); console.error('Error duplicating project:', error);
await showAlert(`Failed to duplicate project: ${error}`); await showAlert(`Failed to duplicate project: ${error}`);
@@ -187,11 +198,15 @@ const OpenProjectModal: React.FC<OpenProjectModalProps> = ({ onClose, onOpenProj
setFilter(''); setFilter('');
}; };
const handleOverlayMouseDown = useCallback((e: React.MouseEvent) => {
mouseDownOnOverlay.current = e.target === e.currentTarget;
}, []);
const handleOverlayClick = useCallback((e: React.MouseEvent) => { const handleOverlayClick = useCallback((e: React.MouseEvent) => {
if (e.target === e.currentTarget) { if (e.target === e.currentTarget && mouseDownOnOverlay.current) {
onClose(); startClose();
} }
}, [onClose]); }, [startClose]);
const SortIcon: React.FC<{ field: SortField }> = ({ field }) => { const SortIcon: React.FC<{ field: SortField }> = ({ field }) => {
if (sortField !== field) return null; if (sortField !== field) return null;
@@ -199,11 +214,16 @@ const OpenProjectModal: React.FC<OpenProjectModalProps> = ({ onClose, onOpenProj
}; };
return ( return (
<div className="open-project-overlay" onClick={handleOverlayClick}> <div
<div className="open-project-panel"> className={`open-project-overlay${isClosing ? ' open-project-overlay-closing' : ''}`}
onMouseDown={handleOverlayMouseDown}
onClick={handleOverlayClick}
onAnimationEnd={handleAnimationEnd}
>
<div className={`open-project-panel${isClosing ? ' open-project-panel-closing' : ''}`}>
<div className="open-project-header"> <div className="open-project-header">
<h3 className="open-project-title">Open Project</h3> <h3 className="open-project-title">Open Project</h3>
<button className="open-project-close-btn" onClick={onClose} aria-label="Close"> <button className="open-project-close-btn" onClick={startClose} aria-label="Close">
<FaTimes /> <FaTimes />
</button> </button>
</div> </div>