add copy button for code

This commit is contained in:
Andrei Canta 2023-03-22 18:27:40 +02:00
parent b29205c9fb
commit 9d1e906323
3 changed files with 43 additions and 24 deletions

View file

@ -93,6 +93,17 @@ export function App() {
},
},
},
Code: {
styles: (theme) => ({
root: {
fontSize: theme.fontSizes.sm,
backgroundColor:
theme.colorScheme == "dark"
? theme.colors.dark[7]
: theme.colors.gray[1],
},
}),
},
},
}}
>

View file

@ -2,6 +2,8 @@ import {
ActionIcon,
Box,
Card,
Code,
CopyButton,
Flex,
Table,
Text,
@ -41,9 +43,38 @@ export function MessageItem({ message }: { message: Message }) {
children={message.content}
remarkPlugins={[remarkGfm]}
components={{
table: (props) => (
table: ({ node, ...props }) => (
<Table verticalSpacing="sm" highlightOnHover {...props} />
),
code: ({ node, inline, ...props }) =>
inline ? (
<Code {...props} />
) : (
<Box sx={{ position: "relative" }}>
<Code block {...props} />
<CopyButton
value={
typeof props.children === "string"
? props.children
: ""
}
>
{({ copied, copy }) => (
<Tooltip
label={copied ? "Copied" : "Copy"}
position="left"
>
<ActionIcon
sx={{ position: "absolute", top: 4, right: 4 }}
onClick={copy}
>
<IconCopy opacity={0.5} size={20} />
</ActionIcon>
</Tooltip>
)}
</CopyButton>
</Box>
),
}}
/>
{message.role === "assistant" && (

View file

@ -7,11 +7,6 @@
margin-bottom: 0;
}
}
pre {
overflow-x: auto;
padding: 8px 12px;
border-radius: 8px;
}
ul {
padding-left: 20px;
}
@ -19,21 +14,3 @@
padding-left: 4px;
}
}
.dark-theme {
.markdown {
pre {
background-color: var(--mantine-color-dark-7);
color: var(--mantine-color-white);
}
}
}
.light-theme {
.markdown {
pre {
background-color: var(--mantine-color-gray-1);
color: var(--mantine-color-dark-4);
}
}
}