import { Text } from "./text"; import { Box, Code, CodeProps, Icon, IconButton, useClipboard, useColorModeValue, } from "@chakra-ui/react"; import { IoMdCheckmark } from "@react-icons/all-files/io/IoMdCheckmark"; import { Highlight, Prism, themes } from "prism-react-renderer"; import { useEffect } from "react"; import { FiCopy } from "react-icons/fi"; const darkThemeDefault = themes.vsDark; const lightThemeDefault = themes.vsLight; type PrismTheme = typeof darkThemeDefault; // add solidity lang support for code (typeof global !== "undefined" ? global : window).Prism = Prism; require("prismjs/components/prism-solidity"); // end add solidity support export interface CodeBlockProps extends Omit { code: string; codeValue?: string; language?: string; canCopy?: boolean; wrap?: boolean; prefix?: string; darkTheme?: PrismTheme; lightTheme?: PrismTheme; } export const CodeBlock: React.FC = ({ code, codeValue, language = "sh", px = 4, py = 2, w = "full", borderRadius = "md", borderColor = "borderColor", borderWidth = "1px", fontFamily = "mono", backgroundColor, prefix, canCopy = true, wrap = true, darkTheme, lightTheme, ...restCodeProps }) => { const theme = useColorModeValue( lightTheme || lightThemeDefault, darkTheme || darkThemeDefault, ); const { onCopy, hasCopied, setValue } = useClipboard(codeValue || code); useEffect(() => { if (code) { setValue(codeValue || code); } }, [code, codeValue, setValue]); return ( {({ className, style, tokens, getLineProps, getTokenProps }) => ( {canCopy && code && ( } /> )} {tokens.map((line, i) => ( {line.map((token, key) => ( ))} ))} )} ); };