({
children,
language,
className,
copyButtonLabel,
copiedButtonLabel,
as = 'a',
}: PropsWithChildren<CodeBoxProps>)
| 75 | }; |
| 76 | |
| 77 | const BaseCodeBox: FC<PropsWithChildren<CodeBoxProps>> = ({ |
| 78 | children, |
| 79 | language, |
| 80 | className, |
| 81 | copyButtonLabel, |
| 82 | copiedButtonLabel, |
| 83 | as = 'a', |
| 84 | }: PropsWithChildren<CodeBoxProps>) => { |
| 85 | const [copied, copy] = useCopy(); |
| 86 | |
| 87 | const containerRef = useRef<HTMLPreElement>(null); |
| 88 | |
| 89 | const handleCopy = () => copy(containerRef.current?.textContent); |
| 90 | |
| 91 | const ButtonIcon = copied ? DocumentDuplicateIcon : CodeBracketIcon; |
| 92 | |
| 93 | const hideFooter = className?.includes('no-footer'); |
| 94 | |
| 95 | return ( |
| 96 | <div className={styles.root}> |
| 97 | <pre |
| 98 | ref={containerRef} |
| 99 | className={classNames(styles.content, className)} |
| 100 | tabIndex={0} |
| 101 | > |
| 102 | {transformCode(children as ReactElement<PropsWithChildren>, language)} |
| 103 | </pre> |
| 104 | |
| 105 | {!language || hideFooter || ( |
| 106 | <div className={styles.footer}> |
| 107 | <span className={styles.language}>{language}</span> |
| 108 | |
| 109 | <BaseButton |
| 110 | as={as} |
| 111 | className={styles.action} |
| 112 | kind="neutral" |
| 113 | onClick={handleCopy} |
| 114 | > |
| 115 | <ButtonIcon className="size-4" /> |
| 116 | {copied ? copiedButtonLabel : copyButtonLabel} |
| 117 | </BaseButton> |
| 118 | </div> |
| 119 | )} |
| 120 | </div> |
| 121 | ); |
| 122 | }; |
| 123 | |
| 124 | export default BaseCodeBox; |
nothing calls this directly
no test coverage detected