-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* created separate heading component * changed to heading props * added generate URL function * functional copy heading link button * added copied notification * remove default param for tag
- Loading branch information
1 parent
0ddb311
commit 217250f
Showing
2 changed files
with
55 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { useState } from "react" | ||
import CopyToClipboard from "react-copy-to-clipboard" | ||
import { FaLink } from "react-icons/fa" | ||
|
||
interface HeadingProps { | ||
content: React.ReactNode | ||
section: string | ||
tag: string | ||
} | ||
|
||
const Heading: React.FC<HeadingProps> = ({ content, section, tag }) => { | ||
const Tag = tag as keyof JSX.IntrinsicElements | ||
const headingContent = content?.toString().replaceAll(" ", "-") | ||
const [isCopied, setIsCopied] = useState(false) | ||
|
||
const generateHeadingURL = () => { | ||
const href: string = typeof window !== "undefined" ? window.location.href.split("#")[0] : "" | ||
return href + "#" + headingContent ?? "" | ||
} | ||
|
||
const onCopyHandler = () => { | ||
setIsCopied(true) | ||
setTimeout(() => { | ||
setIsCopied(false) | ||
}, 1500) | ||
} | ||
|
||
return ( | ||
<> | ||
<Tag id={headingContent} className="flex gap-3"> | ||
{content} | ||
<CopyToClipboard text={generateHeadingURL()}> | ||
<button className="text-xs align-top" onClick={onCopyHandler}> | ||
<FaLink className="group-hover:text-white" /> | ||
</button> | ||
</CopyToClipboard> | ||
{isCopied && <span className="text-xs text-green-500 flex items-center">Copied to clipboard!</span>} | ||
</Tag> | ||
</> | ||
) | ||
} | ||
|
||
export default Heading |