mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 12:50:09 +00:00
fix modal
This commit is contained in:
@@ -5,7 +5,8 @@
|
|||||||
|
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import React, { useEffect, useRef, useCallback } from 'react';
|
import React, { useEffect, useRef, useCallback, useState } from 'react';
|
||||||
|
import { createPortal } from 'react-dom';
|
||||||
import { cn } from '@/lib/utils/cn';
|
import { cn } from '@/lib/utils/cn';
|
||||||
|
|
||||||
interface ModalProps {
|
interface ModalProps {
|
||||||
@@ -25,6 +26,12 @@ export function Modal({
|
|||||||
size = 'md',
|
size = 'md',
|
||||||
showCloseButton = true,
|
showCloseButton = true,
|
||||||
}: ModalProps) {
|
}: ModalProps) {
|
||||||
|
const [mounted, setMounted] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setMounted(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Use ref to avoid re-running effect when onClose changes
|
// Use ref to avoid re-running effect when onClose changes
|
||||||
const onCloseRef = useRef(onClose);
|
const onCloseRef = useRef(onClose);
|
||||||
onCloseRef.current = onClose;
|
onCloseRef.current = onClose;
|
||||||
@@ -53,7 +60,7 @@ export function Modal({
|
|||||||
};
|
};
|
||||||
}, [isOpen, handleClose]);
|
}, [isOpen, handleClose]);
|
||||||
|
|
||||||
if (!isOpen) return null;
|
if (!isOpen || !mounted) return null;
|
||||||
|
|
||||||
const sizeClasses = {
|
const sizeClasses = {
|
||||||
sm: 'max-w-md',
|
sm: 'max-w-md',
|
||||||
@@ -63,8 +70,8 @@ export function Modal({
|
|||||||
full: 'max-w-[95vw]',
|
full: 'max-w-[95vw]',
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
const content = (
|
||||||
<div className="fixed inset-0 z-50 overflow-y-auto">
|
<div className="fixed inset-0 z-[100] overflow-y-auto">
|
||||||
{/* Backdrop */}
|
{/* Backdrop */}
|
||||||
<div
|
<div
|
||||||
className="fixed inset-0 bg-black bg-opacity-50 transition-opacity"
|
className="fixed inset-0 bg-black bg-opacity-50 transition-opacity"
|
||||||
@@ -77,7 +84,7 @@ export function Modal({
|
|||||||
className={cn(
|
className={cn(
|
||||||
'relative w-full bg-white dark:bg-gray-800 rounded-lg shadow-xl',
|
'relative w-full bg-white dark:bg-gray-800 rounded-lg shadow-xl',
|
||||||
'transform transition-all',
|
'transform transition-all',
|
||||||
sizeClasses[size]
|
sizeClasses[size],
|
||||||
)}
|
)}
|
||||||
onClick={(e) => e.stopPropagation()}
|
onClick={(e) => e.stopPropagation()}
|
||||||
>
|
>
|
||||||
@@ -116,4 +123,6 @@ export function Modal({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return createPortal(content, document.body);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user