feat: add persist mode to the modal

This commit is contained in:
MAZE 2024-06-16 19:32:40 +03:30
parent 9d1d8f8035
commit 4c0f417469
2 changed files with 44 additions and 33 deletions

View File

@ -14,6 +14,7 @@ interface ModalProps {
children: React.ReactNode; children: React.ReactNode;
lockBody?: boolean; lockBody?: boolean;
onClose: () => void; onClose: () => void;
persist?: boolean;
show: boolean; show: boolean;
wide?: boolean; wide?: boolean;
} }
@ -22,6 +23,7 @@ export function Modal({
children, children,
lockBody = true, lockBody = true,
onClose, onClose,
persist = false,
show, show,
wide, wide,
}: ModalProps) { }: ModalProps) {
@ -50,40 +52,49 @@ export function Modal({
return () => document.removeEventListener('keydown', keyListener); return () => document.removeEventListener('keydown', keyListener);
}, [onClose, show]); }, [onClose, show]);
return ( const animationProps = persist
<Portal> ? {
<AnimatePresence> animate: show ? 'show' : 'hidden',
{show && ( }
<FocusTrap> : {
animate: 'show',
exit: 'hidden',
initial: 'hidden',
};
const content = (
<FocusTrap active={show}>
<div> <div>
<motion.div <motion.div
animate="show" {...animationProps}
className={styles.overlay} className={styles.overlay}
exit="hidden"
initial="hidden"
variants={variants.overlay} variants={variants.overlay}
onClick={onClose} onClick={onClose}
onKeyDown={onClose} onKeyDown={onClose}
/> />
<div className={styles.modal}> <div className={styles.modal}>
<motion.div <motion.div
animate="show" {...animationProps}
className={cn(styles.content, wide && styles.wide)} className={cn(styles.content, wide && styles.wide)}
exit="hidden"
initial="hidden"
variants={variants.modal} variants={variants.modal}
> >
<button className={styles.close} onClick={onClose}> <button className={styles.close} onClick={onClose}>
<IoClose /> <IoClose />
</button> </button>
{children} {children}
</motion.div> </motion.div>
</div> </div>
</div> </div>
</FocusTrap> </FocusTrap>
);
return (
<Portal>
{persist ? (
<div style={{ display: show ? 'block' : 'none' }}>{content}</div>
) : (
<AnimatePresence>{show && content}</AnimatePresence>
)} )}
</AnimatePresence>
</Portal> </Portal>
); );
} }

View File

@ -12,7 +12,7 @@ interface TimerProps {
export function CountdownTimer({ onClose, show }: TimerProps) { export function CountdownTimer({ onClose, show }: TimerProps) {
return ( return (
<Modal show={show} onClose={onClose}> <Modal persist show={show} onClose={onClose}>
<h2 className={styles.title}>Countdown Timer</h2> <h2 className={styles.title}>Countdown Timer</h2>
<Form /> <Form />
<Timers /> <Timers />