Skip to content

Commit

Permalink
feat(ui): make code blocks copyable
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Oct 12, 2023
1 parent 5fd1598 commit eb88436
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
31 changes: 17 additions & 14 deletions src/components/CopyableBlock/CopyableBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Box, { BoxProps } from '@mui/material/Box'
import Tooltip from '@mui/material/Tooltip'
import Fab from '@mui/material/Fab'
import ContentCopy from '@mui/icons-material/ContentCopy'
import { useContext, useRef } from 'react'
Expand Down Expand Up @@ -31,20 +32,22 @@ export const CopyableBlock = ({ children }: CopyableBlockProps) => {
}}
>
{children}
<Fab
color="default"
size="small"
onClick={handleCopyClick}
sx={theme => ({
position: 'absolute',
top: '1em',
right: '1em',
opacity: 0,
transition: theme.transitions.create(['opacity', 'transform']),
})}
>
<ContentCopy />
</Fab>
<Tooltip title="Copy to clipboard">
<Fab
color="default"
size="small"
onClick={handleCopyClick}
sx={theme => ({
position: 'absolute',
top: '1em',
right: '1em',
opacity: 0,
transition: theme.transitions.create(['opacity', 'transform']),
})}
>
<ContentCopy />
</Fab>
</Tooltip>
</Box>
)
}
19 changes: 12 additions & 7 deletions src/components/Message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import {
isInlineMedia,
} from 'models/chat'
import { PeerNameDisplay } from 'components/PeerNameDisplay'
import { CopyableBlock } from 'components/CopyableBlock/CopyableBlock'

import { SyntaxHighlighter } from '../../components/SyntaxHighlighter'

import { InlineMedia } from './InlineMedia'

import './Message.sass'

export interface MessageProps {
Expand Down Expand Up @@ -65,14 +67,17 @@ const componentMap = {
// https://github.com/remarkjs/react-markdown#use-custom-components-syntax-highlight
code({ node, inline, className, children, style, ...props }: CodeProps) {
const match = /language-(\w+)/.exec(className || '')

return !inline && match ? (
<SyntaxHighlighter
children={String(children).replace(/\n$/, '')}
language={match[1]}
style={materialDark}
PreTag="div"
{...props}
/>
<CopyableBlock>
<SyntaxHighlighter
children={String(children).replace(/\n$/, '')}
language={match[1]}
style={materialDark}
PreTag="div"
{...props}
/>
</CopyableBlock>
) : (
<code className={className} {...props}>
{children}
Expand Down

0 comments on commit eb88436

Please sign in to comment.