Skip to content

Commit 9750bdf

Browse files
committed
feat: add copy button to CodeModal
1 parent 9def832 commit 9750bdf

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/components/CodeModal.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1+
import { Children, type ReactElement } from "react"
12
import { useTranslation } from "next-i18next"
2-
import type { ReactNode } from "react"
3+
import { IoMdCopy } from "react-icons/io"
4+
import { MdCheck } from "react-icons/md"
35
import { Modal, ModalBody, ModalContent, ModalOverlay } from "@chakra-ui/react"
46

57
import { Button } from "./ui/buttons/Button"
68

9+
import { useClipboard } from "@/hooks/useClipboard"
10+
711
type CodeModalProps = {
812
title: string
9-
children: ReactNode
13+
children?: ReactElement
1014
isOpen: boolean
1115
setIsOpen: (isOpen: boolean) => void
1216
}
1317

1418
const CodeModal = ({ children, isOpen, setIsOpen, title }: CodeModalProps) => {
1519
const { t } = useTranslation()
20+
const codeSnippet = (Children.toArray(children)[0] as ReactElement).props
21+
.children.props.children
22+
23+
const { onCopy, hasCopied } = useClipboard(codeSnippet, {
24+
timeout: 1500,
25+
})
1626

1727
return (
1828
<Modal
@@ -44,6 +54,21 @@ const CodeModal = ({ children, isOpen, setIsOpen, title }: CodeModalProps) => {
4454
</Button>
4555
</div>
4656
<ModalBody p="0">{children}</ModalBody>
57+
<Button
58+
variant="outline"
59+
onClick={onCopy}
60+
className="absolute end-4 top-20"
61+
>
62+
{hasCopied ? (
63+
<>
64+
<MdCheck /> {t("copied")}
65+
</>
66+
) : (
67+
<>
68+
<IoMdCopy /> {t("copy")}
69+
</>
70+
)}
71+
</Button>
4772
</ModalContent>
4873
</Modal>
4974
)

0 commit comments

Comments
 (0)