2022-06-03 08:09:47 -06:00
|
|
|
import React, { FC } from 'react';
|
|
|
|
|
|
|
|
|
|
import { Button } from '@grafana/ui';
|
|
|
|
|
import cn from 'classnames/bind';
|
|
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
|
|
|
|
|
|
|
|
|
import { openNotification } from 'utils';
|
|
|
|
|
|
2022-09-07 13:48:55 +03:00
|
|
|
import styles from './SourceCode.module.scss';
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
const cx = cn.bind(styles);
|
|
|
|
|
|
|
|
|
|
interface SourceCodeProps {
|
|
|
|
|
noMaxHeight?: boolean;
|
2022-08-26 17:40:27 +03:00
|
|
|
showCopyToClipboard?: boolean;
|
2022-09-07 13:48:55 +03:00
|
|
|
children?: any;
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SourceCode: FC<SourceCodeProps> = (props) => {
|
2022-09-12 16:57:37 +03:00
|
|
|
const { children, noMaxHeight = false, showCopyToClipboard = true } = props;
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cx('root')}>
|
2022-08-26 17:40:27 +03:00
|
|
|
{showCopyToClipboard && (
|
|
|
|
|
<CopyToClipboard
|
|
|
|
|
text={children as string}
|
|
|
|
|
onCopy={() => {
|
|
|
|
|
openNotification('Copied!');
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-09-07 13:48:55 +03:00
|
|
|
<Button
|
2022-09-12 16:57:37 +03:00
|
|
|
className={cx('button')}
|
2022-09-07 13:48:55 +03:00
|
|
|
variant="primary"
|
2022-09-12 16:57:37 +03:00
|
|
|
size='xs'
|
2022-09-07 13:48:55 +03:00
|
|
|
icon="copy"
|
|
|
|
|
>
|
2022-08-26 17:40:27 +03:00
|
|
|
Copy
|
|
|
|
|
</Button>
|
|
|
|
|
</CopyToClipboard>
|
|
|
|
|
)}
|
2022-06-03 08:09:47 -06:00
|
|
|
<pre
|
|
|
|
|
className={cx('scroller', {
|
2022-09-07 13:48:55 +03:00
|
|
|
'scroller--maxHeight': !noMaxHeight,
|
2022-06-03 08:09:47 -06:00
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
<code>{children}</code>
|
|
|
|
|
</pre>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SourceCode;
|