Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add outsideClick to close modals and increase X size #17

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions app/components/modals/ImageModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import useImageModal from '@/app/hooks/useImageModal';
import { X } from '@phosphor-icons/react';
import { useEffect, useState } from 'react';
import { useOutsideClickModal } from '@/app/hooks/useOutsideClickModal';

const ImageModal = () => {
const imageModal = useImageModal();
const thisRef = useOutsideClickModal(() => {
handleClose();
});

const [showModal, setShowModal] = useState(imageModal.isOpen);

Expand All @@ -14,6 +18,7 @@ const ImageModal = () => {
}, [imageModal.isOpen]);

const handleClose = () => {
setShowModal(false);
setTimeout(() => {
imageModal.onClose();
}, 300);
Expand All @@ -24,9 +29,8 @@ const ImageModal = () => {
}

return (
<>
<div
className="
<div
className="
justify-center
items-center
flex
Expand All @@ -39,9 +43,9 @@ const ImageModal = () => {
focus:outline-none
bg-neutral-800/70
"
>
<div
className="
>
<div
className="
relative
w-full
md:w-4/5
Expand All @@ -52,19 +56,19 @@ const ImageModal = () => {
lg:h-auto
md:h-auto
"
>
<div
className={`
>
<div
className={`
translate
duration-300
h-full
h-full
${showModal ? 'translate-y-0' : 'translate-y-full'}
${showModal ? 'opacity-100' : 'opacity-0'}
`}
>
<div
className="
>
<div
className="
translate
h-full
md:h-auto
Expand All @@ -79,9 +83,10 @@ const ImageModal = () => {
outline-none
focus:outline-none
"
>
<div
className="
ref={thisRef}
>
<div
className="
flex
items-center
p-6
Expand All @@ -90,29 +95,30 @@ const ImageModal = () => {
relative
border-b-[1px]
"
>
<button
onClick={handleClose}
className="
>
<button
onClick={handleClose}
className="
p-1
border=0
hover:opacity-70
transition
absolute
right-9
right-7
hover:bg-neutral-200
rounded-full
"
>
<X size={18} />
</button>
</div>
<div className="flex items-center justify-center h-auto lg:h-[600px] xl:h-[800px] w-auto">
<img src={imageModal.image} alt="modal" className="h-auto lg:h-auto w-auto lg:w-full" />
</div>
>
<X size={24} />
</button>
</div>
<div className="flex items-center justify-center h-auto lg:h-[600px] xl:h-[800px] w-auto">
<img src={imageModal.image} alt="modal" className="h-auto lg:h-auto w-auto lg:w-full" />
</div>
</div>
</div>
</div>
</>
</div>
);
};

Expand Down
11 changes: 9 additions & 2 deletions app/components/modals/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React, { useCallback, useEffect, useState } from 'react';
import { X } from '@phosphor-icons/react';
import Button from '../Button';
import { useOutsideClickModal } from '@/app/hooks/useOutsideClickModal';

interface ModalProps {
isOpen?: boolean;
Expand Down Expand Up @@ -30,6 +31,9 @@ const Modal: React.FC<ModalProps> = ({
secondaryActionLabel,
}) => {
const [showModal, setShowModal] = useState(isOpen);
const thisRef = useOutsideClickModal(() => {
handleClose();
});

useEffect(() => {
setShowModal(isOpen);
Expand Down Expand Up @@ -115,6 +119,7 @@ const Modal: React.FC<ModalProps> = ({
outline-none
focus:outline-none
"
ref={thisRef}
>
<div
className="
Expand All @@ -135,10 +140,12 @@ const Modal: React.FC<ModalProps> = ({
hover:opacity-70
transition
absolute
right-9
right-7
hover:bg-neutral-200
rounded-full
"
>
<X size={18} />
<X size={24} />
</button>
<div className="text-lg font-semibold">{title}</div>
</div>
Expand Down
21 changes: 21 additions & 0 deletions app/hooks/useOutsideClickModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useEffect, useRef } from 'react';

export const useOutsideClickModal = (callback: () => void) => {
const ref = useRef<HTMLDivElement>(null);

useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
callback();
}
};

document.addEventListener('mousedown', handleClickOutside);

return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [callback]);

return ref;
};
Loading