The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/client/components/basic/TextCopy.js

53 lines
1.3 KiB

import { Box, Icon, Button, Scrollable } from '@rocket.chat/fuselage';
import React, { useCallback, useMemo } from 'react';
import { useTranslation } from '../../contexts/TranslationContext';
import { useToastMessageDispatch } from '../../contexts/ToastMessagesContext';
const TextCopy = ({ text, wordBreak = 'break-all', ...props }) => {
const t = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();
const style = useMemo(() => ({ wordBreak }), [wordBreak]);
const onClick = useCallback(() => {
try {
navigator.clipboard.writeText(text);
dispatchToastMessage({ type: 'success', message: t('Copied') });
} catch (e) {
dispatchToastMessage({ type: 'error', message: e });
}
}, [dispatchToastMessage, t, text]);
return <Box
display='flex'
flexDirection='row'
justifyContent='stretch'
alignItems='flex-start'
flexGrow={1}
padding='x16'
backgroundColor='neutral-100'
width='full'
{...props}
>
<Scrollable vertical>
<Box
fontFamily='mono'
alignSelf='center'
fontScale='p1'
style={style}
mie='x4'
flexGrow={1}
maxHeight='x108'
>
{text}
</Box>
</Scrollable>
<Button ghost square small flexShrink={0} onClick={onClick} title={t('Copy')}>
<Icon name='copy' size='x20' />
</Button>
</Box>;
};
export default TextCopy;