-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for copy button (#314)
* Add support for copy button * Minor fixes * Clean up global styles * Update CopyButton components * Update rehypeCopyButton helper * Update CopyButton entrypoint * Minor changes * Code --------- Co-authored-by: Antoine BERNIER <[email protected]>
- Loading branch information
1 parent
ba69c0b
commit 87f08b7
Showing
6 changed files
with
82 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use client' | ||
|
||
import cn from '@/lib/cn' | ||
import { ComponentProps, ReactNode, useEffect, useState } from 'react' | ||
import { TbClipboard, TbClipboardCheck } from 'react-icons/tb' | ||
|
||
export const Code = ({ children, className, ...props }: ComponentProps<'pre'>) => { | ||
const [copied, setCopied] = useState(false) | ||
|
||
const handleClick = async () => { | ||
const textToCopy = extractTextFromChildren(children) | ||
|
||
await navigator.clipboard.writeText(textToCopy) | ||
setCopied(true) | ||
} | ||
|
||
useEffect(() => { | ||
if (!copied) return | ||
const int = setTimeout(() => setCopied(false), 2000) | ||
return () => clearTimeout(int) | ||
}, [copied]) | ||
|
||
return ( | ||
<div className={cn('relative')}> | ||
<pre | ||
{...props} | ||
className={cn( | ||
className, | ||
'bg-inverse-surface-light my-5 overflow-auto rounded-lg p-[--pad] font-mono text-sm', | ||
)} | ||
> | ||
{children} | ||
</pre> | ||
<button | ||
className="absolute right-0 top-0 m-4 flex size-8 items-center justify-center rounded-md text-outline-variant transition-colors hover:text-outline" | ||
onClick={handleClick} | ||
> | ||
{copied ? <TbClipboardCheck className="size-6" /> : <TbClipboard className="size-6" />} | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
// Recursive function to extract text content from React nodes | ||
const extractTextFromChildren = (children: ReactNode): string => { | ||
if (typeof children === 'string') { | ||
return children | ||
} | ||
|
||
if (Array.isArray(children)) { | ||
return children.map(extractTextFromChildren).join('') | ||
} | ||
|
||
if (typeof children === 'object' && children !== null && 'props' in children) { | ||
return extractTextFromChildren(children.props.children) | ||
} | ||
|
||
return '' | ||
} |
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 @@ | ||
export * from './Code' |
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,19 @@ | ||
import type { Root } from 'hast' | ||
import { visit } from 'unist-util-visit' | ||
|
||
export function rehypeCode() { | ||
return () => (tree: Root) => { | ||
visit(tree, null, function (node) { | ||
// console.log('node', node) | ||
|
||
const isMDPre = | ||
'tagName' in node && | ||
node.tagName === 'pre' && | ||
node.properties?.className?.toString()?.includes('language-') | ||
|
||
if (isMDPre) { | ||
node.tagName = 'Code' // map to <Code> React component | ||
} | ||
}) | ||
} | ||
} |
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,3 +1,4 @@ | ||
export * from './Code' | ||
export * from './Codesandbox' | ||
export * from './Details' | ||
export * from './Gha' | ||
|
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