diff --git a/src/app/components/CodeSnippet.tsx b/src/app/components/CodeSnippet.tsx index bc5b1fc..901b5b0 100644 --- a/src/app/components/CodeSnippet.tsx +++ b/src/app/components/CodeSnippet.tsx @@ -3,12 +3,17 @@ import React, { Children, MouseEvent, + ReactElement, ReactNode, useState, } from 'react'; import styles from './CodeSnippet.module.css'; -function CodeSnippetCopyButton() { +function CodeSnippetCopyButton({ + copyText, +}: { + copyText: string; +}) { const [copyButtonTooltip, setCopyButtonTooltip] = useState(false); @@ -21,13 +26,7 @@ function CodeSnippetCopyButton() { setCopyButtonTooltip(false); }, 1000); - const target = e.currentTarget; - const code = - target.parentElement?.parentElement?.parentElement?.parentElement?.querySelector( - 'pre' - )?.innerText ?? ''; - - navigator.clipboard.writeText(code); + navigator.clipboard.writeText(copyText ?? ''); }; return ( @@ -130,6 +129,17 @@ export default function CodeSnippet({ setSelectedLanguage(e.target.value); }; + const code = Children.toArray(children) + .filter( + child => + React.isValidElement(child) && + child.props.className === + `language-${selectedLanguage}` + ) + .map(child => { + return child as ReactElement; + })[0]; + const snippetLanguages = Children.toArray(children).map( child => { if (React.isValidElement(child)) { @@ -165,22 +175,13 @@ export default function CodeSnippet({
) : null} - +
-
-          {Children.toArray(children)
-            .filter(
-              child =>
-                React.isValidElement(child) &&
-                child.props.className ===
-                  `language-${selectedLanguage}`
-            )
-            .map(child => {
-              return child;
-            })}
-        
+
{code}
); diff --git a/src/app/components/Fence.tsx b/src/app/components/Fence.tsx index 243c768..1fe0631 100644 --- a/src/app/components/Fence.tsx +++ b/src/app/components/Fence.tsx @@ -21,6 +21,7 @@ export default function Fence({ ); }