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]);
const animationProps = persist
? {
animate: show ? 'show' : 'hidden',
}
: {
animate: 'show',
exit: 'hidden',
initial: 'hidden',
};
const content = (
<FocusTrap active={show}>
<div>
<motion.div
{...animationProps}
className={styles.overlay}
variants={variants.overlay}
onClick={onClose}
onKeyDown={onClose}
/>
<div className={styles.modal}>
<motion.div
{...animationProps}
className={cn(styles.content, wide && styles.wide)}
variants={variants.modal}
>
<button className={styles.close} onClick={onClose}>
<IoClose />
</button>
{children}
</motion.div>
</div>
</div>
</FocusTrap>
);
return ( return (
<Portal> <Portal>
<AnimatePresence> {persist ? (
{show && ( <div style={{ display: show ? 'block' : 'none' }}>{content}</div>
<FocusTrap> ) : (
<div> <AnimatePresence>{show && content}</AnimatePresence>
<motion.div )}
animate="show"
className={styles.overlay}
exit="hidden"
initial="hidden"
variants={variants.overlay}
onClick={onClose}
onKeyDown={onClose}
/>
<div className={styles.modal}>
<motion.div
animate="show"
className={cn(styles.content, wide && styles.wide)}
exit="hidden"
initial="hidden"
variants={variants.modal}
>
<button className={styles.close} onClick={onClose}>
<IoClose />
</button>
{children}
</motion.div>
</div>
</div>
</FocusTrap>
)}
</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 />