diff --git a/src/components/common/CopiedNotice.tsx b/src/components/common/CopiedNotice.tsx index 96ff899b..9f79cecb 100644 --- a/src/components/common/CopiedNotice.tsx +++ b/src/components/common/CopiedNotice.tsx @@ -1,8 +1,17 @@ import CheckSvg from '@/components/svg-component/CheckSvg.tsx'; -export function CopiedNotice() { +interface Props { + isOpen: boolean; + handleClose: () => void; +} +export function CopiedNotice({ isOpen, handleClose }: Props) { + if (!isOpen) return null; + return ( -
+
diff --git a/src/components/common/ShareModal.tsx b/src/components/common/ShareModal.tsx index baeffbd0..8b9afc44 100644 --- a/src/components/common/ShareModal.tsx +++ b/src/components/common/ShareModal.tsx @@ -12,7 +12,7 @@ export default function ShareModal() { const { handleShare } = useLoadKakaoScript(); const router = useRouter(); const shareURL = window.location.href; - const { isCopied, onCopyClipboard } = useCopyClipboard({ + const { isCopied, onCopyClipboard, onCloseCopyClipboard } = useCopyClipboard({ url: shareURL, }); @@ -44,7 +44,7 @@ export default function ShareModal() {
- {isCopied && } +
); } diff --git a/src/components/pages/detail/URLShareButton.tsx b/src/components/pages/detail/URLShareButton.tsx index 33d3fd1b..2eb82c01 100644 --- a/src/components/pages/detail/URLShareButton.tsx +++ b/src/components/pages/detail/URLShareButton.tsx @@ -3,14 +3,15 @@ import useCopyClipboard from '@/hooks/useCopyClipboard.ts'; import { CopiedNotice } from '@/components/common/CopiedNotice.tsx'; export default function URLShareButton() { - const { isCopied, onCopyClipboard } = useCopyClipboard(); + const { isCopied, onCopyClipboard, onCloseCopyClipboard } = + useCopyClipboard(); return (
- - {isCopied && } + {}
); } diff --git a/src/hooks/useCopyClipboard.ts b/src/hooks/useCopyClipboard.ts index bde4b6f8..7d1c919b 100644 --- a/src/hooks/useCopyClipboard.ts +++ b/src/hooks/useCopyClipboard.ts @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; async function copyURL(url?: string) { const targetUrl = url ? url : window.document.location.href; @@ -28,10 +28,14 @@ export default function useCopyClipboard(props?: Props) { }; }, [isCopied, props?.ms]); - const onCopyClipboard = async () => { + const onCopyClipboard = useCallback(async () => { await copyURL(props?.url); setIsCopied(true); - }; + }, [props?.url]); - return { isCopied, onCopyClipboard }; + const onCloseCopyClipboard = useCallback(() => { + setIsCopied(false); + }, []); + + return { isCopied, setIsCopied, onCopyClipboard, onCloseCopyClipboard }; }