import copy from 'copy-to-clipboard'; import React, { createRef, useContext } from 'react'; import CodeOptsContext from '../../contexts/CodeOpts'; import ThemeContext from '../../contexts/Theme'; import useHydrated from '../../hooks/useHydrated'; // Only load CodeMirror in the browser, for SSR // apps. Necessary because of people like this: // https://github.com/codemirror/CodeMirror/issues/3701#issuecomment-164904534 let syntaxHighlighter; // eslint-disable-next-line @typescript-eslint/no-unused-vars let canonicalLanguage = lang => ''; if (typeof window !== 'undefined') { // @ts-expect-error this library does not have any types defined import('@readme/syntax-highlighter').then(module => { syntaxHighlighter = module.default; canonicalLanguage = module.canonical; }); } function CopyCode({ codeRef, rootClass = 'rdmd-code-copy', className = '', }: { className?: string; codeRef: React.RefObject; rootClass?: string; }) { const copyClass = `${rootClass}_copied`; const buttonRef = createRef(); const copier = () => { const code = codeRef.current.textContent; if (copy(code)) { const el = buttonRef.current; el.classList.add(copyClass); setTimeout(() => el.classList.remove(copyClass), 1500); } }; return