Sorry for the noob response, I’m not THAT proficient in Next, but I’m having the same issue. Codemirror shows up beautifully in dev, but doesn’t style in production. How should I change my code using your advice (I tried any possible LLM… to no avail). Any help appreciated.
import React, { useEffect, useRef, useState } from "react";
import { EditorView, basicSetup } from "codemirror";
import { cpp } from "@codemirror/lang-cpp";
const CopyBlock = ({ code, maxHeight, maxWidth, fontSize = 14, language }) => {
const editorRef = useRef(null);
const [copied, setCopied] = useState(false);
useEffect(() => {
if (editorRef.current) {
const extensions = [
basicSetup,
EditorView.editable.of(false),
EditorView.theme({
"&": { fontSize: `${fontSize}px` },
}),
];
if (language) {
switch (language.toLowerCase()) {
case "cpp":
extensions.push(cpp());
break;
}
}
const view = new EditorView({
doc: code,
extensions: extensions,
parent: editorRef.current,
});
return () => {
view.destroy();
};
}
}, [code, fontSize, language]);
const copyToClipboard = () => {
navigator.clipboard.writeText(code).then(() => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
});
};
return (
<div
style={{
position: "relative",
maxHeight: maxHeight || "none",
maxWidth: maxWidth || "none",
overflow: "hidden",
border: "1px solid #ccc",
borderRadius: "4px",
margin: "auto",
}}
>
<div
style={{
position: "absolute",
top: "10px",
right: "10px",
zIndex: 1000,
}}
>
<button
onClick={copyToClipboard}
style={{
background: "white",
border: "1px solid #ccc",
borderRadius: "4px",
padding: "5px 10px",
cursor: "pointer",
}}
>
{copied ? (
"Copied!"
) : (
<>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
style={{ marginRight: "5px", verticalAlign: "middle" }}
>
<rect x="9" y="9" width="13" height="13" rx="2" ry="2" />
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" />
</svg>
Copy
</>
)}
</button>
</div>
<div
style={{
maxHeight: maxHeight || "none",
maxWidth: maxWidth || "none",
overflow: "auto",
// paddingTop: "40px", // Add padding to prevent overlap with the button
}}
>
<div ref={editorRef} style={{ padding: "0px" }} />
</div>
</div>
);
};
export default CopyBlock;