-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
5 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,82 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
|
||
// @components | ||
import { Button } from "@/components/button"; | ||
|
||
// @icons | ||
import { RiCheckLine, RiFileCopyLine } from "@remixicon/react"; | ||
|
||
// @hooks | ||
import { useTheme } from "next-themes"; | ||
import { useCopyToClipboard } from "usehooks-ts"; | ||
|
||
// @utils | ||
import prettier from "prettier/standalone"; | ||
import parserBabel from "prettier/plugins/babel"; | ||
import prettierPluginEstree from "prettier/plugins/estree"; | ||
|
||
export function CodeBlock({ | ||
children, | ||
...props | ||
}: React.ComponentPropsWithoutRef<"div">) { | ||
const { resolvedTheme } = useTheme(); | ||
const [, copy] = useCopyToClipboard(); | ||
const codeRef = React.useRef<any>(null); | ||
|
||
const [isClient, setIsClient] = React.useState(false); | ||
const [isCopied, setIsCopied] = React.useState(false); | ||
|
||
React.useEffect(() => { | ||
setIsClient(true); | ||
}, []); | ||
|
||
if (!isClient) { | ||
return null; | ||
} | ||
|
||
async function copyCode() { | ||
setIsCopied(true); | ||
|
||
const code = codeRef.current.innerText; | ||
const cleanedCode = code | ||
.split("\n") | ||
.map((line: string) => line.replace(/^\d+\s*/, "")) // Removes leading numbers | ||
.join("\n"); | ||
|
||
const formattedCode = await prettier.format(cleanedCode, { | ||
parser: "babel", | ||
plugins: [parserBabel, prettierPluginEstree], | ||
}); | ||
|
||
copy(formattedCode); | ||
} | ||
|
||
function resetCopy() { | ||
setIsCopied(false); | ||
} | ||
|
||
return ( | ||
<div | ||
{...props} | ||
ref={codeRef} | ||
data-theme={resolvedTheme} | ||
className="code-block border-secondary [&_pre_span[data-bright-ln]]:!text-foreground/50 relative rounded-2xl border [&_>_div]:!my-0 [&_pre]:!bg-transparent [&_pre]:!px-2 [&_pre]:!py-4 [&_pre]:font-mono [&_pre]:text-sm [&_pre]:leading-relaxed sm:[&_pre]:text-base [&_pre_span[data-bright-ln]]:!mr-4" | ||
> | ||
<Button | ||
variant="secondary" | ||
onClick={copyCode} | ||
onMouseLeave={resetCopy} | ||
className="absolute top-1 right-1 size-8 border-0 p-1.5 hover:bg-transparent" | ||
> | ||
{isCopied ? ( | ||
<RiCheckLine className="size-4" /> | ||
) : ( | ||
<RiFileCopyLine className="size-4" /> | ||
)} | ||
</Button> | ||
{children} | ||
</div> | ||
); | ||
} |
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,7 +1,5 @@ | ||
/** @type {import('postcss-load-config').Config} */ | ||
const config = { | ||
export default { | ||
plugins: { | ||
"@tailwindcss/postcss": {}, | ||
}, | ||
}; | ||
export default config; |