oncall-engine/grafana-plugin/src/components/SourceCode/SourceCode.tsx

53 lines
1.1 KiB
TypeScript
Raw Normal View History

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';
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;
}
const SourceCode: FC<SourceCodeProps> = (props) => {
2022-09-12 16:57:37 +03:00
const { children, noMaxHeight = false, showCopyToClipboard = true } = props;
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>
)}
<pre
className={cx('scroller', {
2022-09-07 13:48:55 +03:00
'scroller--maxHeight': !noMaxHeight,
})}
>
<code>{children}</code>
</pre>
</div>
);
};
export default SourceCode;