| 7 | import styles from './code.module.css'; |
| 8 | |
| 9 | export const Code = props => { |
| 10 | const [copied, setCopied] = useState(false); |
| 11 | const { theme } = useTheme(); |
| 12 | const elementRef = useRef(); |
| 13 | const copyTimeout = useRef(); |
| 14 | const lang = props.className?.split('-')[1]; |
| 15 | |
| 16 | const handleCopy = () => { |
| 17 | clearTimeout(copyTimeout); |
| 18 | navigator.clipboard.writeText(elementRef.current.textContent); |
| 19 | |
| 20 | setCopied(true); |
| 21 | |
| 22 | setTimeout(() => { |
| 23 | setCopied(false); |
| 24 | }, 2000); |
| 25 | }; |
| 26 | |
| 27 | return ( |
| 28 | <div className={styles.code} data-theme={theme}> |
| 29 | {!!lang && ( |
| 30 | <Text secondary size="s" className={styles.lang}> |
| 31 | {lang} |
| 32 | </Text> |
| 33 | )} |
| 34 | <pre ref={elementRef} {...props} /> |
| 35 | <div className={styles.actions}> |
| 36 | <Button iconOnly onClick={handleCopy} aria-label="Copy"> |
| 37 | <span className={styles.copyIcon}> |
| 38 | <Transition in={!copied}> |
| 39 | {({ visible, nodeRef }) => ( |
| 40 | <Icon ref={nodeRef} icon="copy" data-visible={visible} /> |
| 41 | )} |
| 42 | </Transition> |
| 43 | <Transition in={copied}> |
| 44 | {({ visible, nodeRef }) => ( |
| 45 | <Icon ref={nodeRef} icon="check" data-visible={visible} /> |
| 46 | )} |
| 47 | </Transition> |
| 48 | </span> |
| 49 | </Button> |
| 50 | </div> |
| 51 | </div> |
| 52 | ); |
| 53 | }; |
nothing calls this directly
no test coverage detected