diff --git a/src/app/globals.css b/src/app/globals.css index 33a46d20..e2b124f9 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -45,7 +45,6 @@ body { code[class*='language-'], pre[class*='language-'] { - background: none; text-align: left; white-space: pre; word-spacing: normal; @@ -70,13 +69,6 @@ pre[class*='language-'] { --falsy: #f087bd; --linenumber-border-width: theme('space.1'); --pad: theme('space.6'); - - @apply my-5 overflow-auto rounded-lg p-[--pad] font-mono text-sm; -} - -:not(pre) > code[class*='language-'], -pre[class*='language-'] { - @apply bg-inverse-surface-light; } /* Inline code */ diff --git a/src/components/mdx/Code/Code.tsx b/src/components/mdx/Code/Code.tsx new file mode 100644 index 00000000..89a10bc6 --- /dev/null +++ b/src/components/mdx/Code/Code.tsx @@ -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 ( +
+ {children} ++ +
React component
+ }
+ })
+ }
+}
diff --git a/src/components/mdx/index.tsx b/src/components/mdx/index.tsx
index aefd7275..5e75739f 100644
--- a/src/components/mdx/index.tsx
+++ b/src/components/mdx/index.tsx
@@ -1,3 +1,4 @@
+export * from './Code'
export * from './Codesandbox'
export * from './Details'
export * from './Gha'
diff --git a/src/utils/docs.tsx b/src/utils/docs.tsx
index 6f60af76..e505160c 100644
--- a/src/utils/docs.tsx
+++ b/src/utils/docs.tsx
@@ -1,5 +1,6 @@
import type { Doc, DocToC } from '@/app/[...slug]/DocsContext'
import * as components from '@/components/mdx'
+import { rehypeCode } from '@/components/mdx/Code/rehypeCode'
import { Codesandbox } from '@/components/mdx/Codesandbox'
import { fetchCSB } from '@/components/mdx/Codesandbox/fetchCSB'
import { rehypeCodesandbox } from '@/components/mdx/Codesandbox/rehypeCodesandbox'
@@ -156,6 +157,7 @@ async function _getDocs(
rehypeSummary,
rehypeGha,
rehypePrismPlus,
+ rehypeCode(),
rehypeCodesandbox(boxes), // 1. put all Codesandbox[id] into `doc.boxes`
rehypeToc(tableOfContents, url, title), // 2. will populate `doc.tableOfContents`
],