2022-06-03 08:09:47 -06:00
|
|
|
import React, { FC } from 'react';
|
|
|
|
|
|
2023-03-23 17:34:16 +02:00
|
|
|
import { Button, IconButton, Tooltip } from '@grafana/ui';
|
2022-06-03 08:09:47 -06:00
|
|
|
import cn from 'classnames/bind';
|
|
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
|
|
|
|
|
2024-02-13 10:53:18 +02:00
|
|
|
import { openNotification } from 'utils/utils';
|
2022-06-03 08:09:47 -06:00
|
|
|
|
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-09-14 15:35:39 +03:00
|
|
|
showClipboardIconOnly?: boolean;
|
2022-08-26 17:40:27 +03:00
|
|
|
showCopyToClipboard?: boolean;
|
2022-09-07 13:48:55 +03:00
|
|
|
children?: any;
|
2023-05-03 16:51:45 +02:00
|
|
|
className?: string;
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
2024-02-13 10:53:18 +02:00
|
|
|
export const SourceCode: FC<SourceCodeProps> = (props) => {
|
2023-05-03 16:51:45 +02:00
|
|
|
const { children, noMaxHeight = false, showClipboardIconOnly = false, showCopyToClipboard = true, className } = props;
|
2022-09-14 15:35:39 +03:00
|
|
|
const showClipboardCopy = showClipboardIconOnly || showCopyToClipboard;
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cx('root')}>
|
2022-09-14 15:35:39 +03:00
|
|
|
{showClipboardCopy && (
|
2022-08-26 17:40:27 +03:00
|
|
|
<CopyToClipboard
|
|
|
|
|
text={children as string}
|
|
|
|
|
onCopy={() => {
|
|
|
|
|
openNotification('Copied!');
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-09-14 15:35:39 +03:00
|
|
|
{showClipboardIconOnly ? (
|
2023-03-23 17:34:16 +02:00
|
|
|
<Tooltip placement="top" content="Copy to Clipboard">
|
2023-11-29 14:11:31 +02:00
|
|
|
<IconButton
|
|
|
|
|
aria-label="Copy"
|
|
|
|
|
className={cx('copyIcon')}
|
|
|
|
|
size={'lg'}
|
|
|
|
|
name="copy"
|
|
|
|
|
data-testid="test__copyIcon"
|
|
|
|
|
/>
|
2023-03-23 17:34:16 +02:00
|
|
|
</Tooltip>
|
2022-09-14 15:35:39 +03:00
|
|
|
) : (
|
2022-10-24 14:27:03 +02:00
|
|
|
<Button
|
|
|
|
|
className={cx('copyButton')}
|
|
|
|
|
variant="primary"
|
|
|
|
|
size="xs"
|
|
|
|
|
icon="copy"
|
|
|
|
|
data-testid="test__copyIconWithText"
|
|
|
|
|
>
|
2022-09-14 15:35:39 +03:00
|
|
|
Copy
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2022-08-26 17:40:27 +03:00
|
|
|
</CopyToClipboard>
|
|
|
|
|
)}
|
2022-06-03 08:09:47 -06:00
|
|
|
<pre
|
2023-05-03 16:51:45 +02:00
|
|
|
className={cx(
|
|
|
|
|
'scroller',
|
|
|
|
|
{
|
|
|
|
|
'scroller--maxHeight': !noMaxHeight,
|
|
|
|
|
},
|
|
|
|
|
className
|
|
|
|
|
)}
|
2022-06-03 08:09:47 -06:00
|
|
|
>
|
|
|
|
|
<code>{children}</code>
|
|
|
|
|
</pre>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|