-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from ivalshamkya/development
feat: show toast after copycode clicked
- Loading branch information
Showing
7 changed files
with
80 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,66 @@ | ||
'use client'; | ||
'use client' | ||
import React, { useState } from "react"; | ||
import Button from "./neobruu/Button"; | ||
import { IoCheckmark, IoCopy } from "react-icons/io5"; | ||
|
||
export default function CopyCode({ code }: { code: string }) { | ||
const [isClicked, setIsClicked] = useState(false); | ||
|
||
const handleClick = () => { | ||
navigator.clipboard | ||
.writeText(code) | ||
.then(() => { | ||
setIsClicked(true); | ||
setTimeout(() => { | ||
setIsClicked(false); | ||
}, 2500); | ||
}) | ||
.catch(() => { | ||
alert( | ||
"Your device doesn't support navigator api, so you'll have to manually select and copy the code.", | ||
); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Button rounded="md" variant="yellow" onClick={handleClick}> | ||
{isClicked ? <IoCheckmark /> : <IoCopy />} | ||
</Button> | ||
); | ||
import { IoCopy } from "react-icons/io5"; | ||
import { IoMdCheckmark, IoMdCheckmarkCircle } from "react-icons/io"; | ||
import Toast from "./neobruu/Toast"; | ||
|
||
export interface ButtonProps | ||
extends React.ButtonHTMLAttributes<HTMLButtonElement> { | ||
code: string, | ||
} | ||
|
||
|
||
const CopyCode = React.forwardRef<HTMLButtonElement, ButtonProps>( | ||
({ className, children, code, ...props }, ref) => { | ||
const [isClicked, setIsClicked] = useState(false); | ||
const [showToast, setShowToast] = useState(false); | ||
|
||
const handleShowToast = () => { | ||
setShowToast(true); | ||
}; | ||
|
||
const handleClick = () => { | ||
handleShowToast(); | ||
navigator.clipboard | ||
.writeText(code) | ||
.then(() => { | ||
setIsClicked(true); | ||
setTimeout(() => { | ||
setIsClicked(false); | ||
}, 2500); | ||
}) | ||
.catch(() => { | ||
alert( | ||
"Your device doesn't support navigator api, so you'll have to manually select and copy the code.", | ||
); | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button rounded="md" variant="yellow" onClick={handleClick}> | ||
{isClicked ? <IoMdCheckmark className="w-5 h-5" /> : <IoCopy />} | ||
</Button> | ||
|
||
{showToast && ( | ||
<Toast | ||
variant="primary" | ||
duration={3000} | ||
active={showToast} | ||
setActive={setShowToast} | ||
> | ||
<div className="flex items-center gap-3 p-1"> | ||
<IoMdCheckmarkCircle className="w-6 h-6"/> | ||
<p className="text-sm">Copied to clipboard</p> | ||
</div> | ||
</Toast> | ||
)} | ||
</> | ||
); | ||
} | ||
) | ||
|
||
CopyCode.displayName = "Copy Code"; | ||
|
||
export default CopyCode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters