Skip to content

Commit 7bc4def

Browse files
committed
Add code copy
- move indent editor func
1 parent 9c6c39e commit 7bc4def

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

apps/landing/src/app/(detail)/components/MdxCard.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export default async function MdxCard({
3030
// extract comment
3131
const comment = content.match(/\/\*\*[\s\S]*?\*\//)?.[0]
3232
const code = content.replace('\n' + comment!, '')
33+
const normalizedCode = code
34+
.split('\n')
35+
.map((line) => line.replaceAll(' ', ' '))
36+
.join('\n')
3337
const normalizedComment = comment
3438
?.replace(/\/\*\*|\*\//g, '')
3539
?.replace(/^\s*\*\s*/gm, '')
@@ -64,8 +68,8 @@ export default async function MdxCard({
6468
{normalizedComment ?? ''}
6569
</ReactMarkdown>
6670
</VStack>
67-
<MdxCardFooter>
68-
<Code language="typescript" value={code} />
71+
<MdxCardFooter code={normalizedCode}>
72+
<Code language="typescript" value={normalizedCode} />
6973
</MdxCardFooter>
7074
</Card>
7175
)

apps/landing/src/app/(detail)/components/MdxCardFooter.tsx

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
'use client'
22

3-
import { Box, Center, Flex, Text, VStack } from '@devup-ui/react'
3+
import { Box, Center, Flex, Image, Text, VStack } from '@devup-ui/react'
44
import { useState } from 'react'
55

66
import IconCode from '@/components/icons/IconCode'
77

88
export default function MdxCardFooter({
9+
code,
910
children,
1011
}: {
12+
code: string
1113
children: React.ReactNode
1214
}) {
1315
const [isOpen, setIsOpen] = useState(false)
16+
const [copied, setCopied] = useState(false)
17+
18+
const handleCopy = () => {
19+
navigator.clipboard
20+
.writeText(code)
21+
.then(() => setCopied(true))
22+
.catch(() => setCopied(false))
23+
}
24+
1425
return (
1526
<VStack justifyContent="flex-end" maxH="600px" maxW="100%">
1627
<Flex
@@ -41,7 +52,46 @@ export default function MdxCardFooter({
4152
</Center>
4253
</Flex>
4354
{isOpen && (
44-
<Box borderTop="1px solid $border" overflow="auto" px="24px" py="16px">
55+
<Box
56+
borderTop="1px solid $border"
57+
h="100%"
58+
overflow="auto"
59+
pos="relative"
60+
px="24px"
61+
py="16px"
62+
>
63+
<Center
64+
_active={{
65+
borderColor: '$primary',
66+
bg: '$menuActive',
67+
}}
68+
_hover={{
69+
borderColor: '$primary',
70+
bg: '$menuHover',
71+
}}
72+
bg="$containerBackground"
73+
border="1px solid transparent"
74+
borderRadius="4px"
75+
boxShadow="0 2px 6px 0 $shadow"
76+
cursor="pointer"
77+
gap="8px"
78+
onClick={handleCopy}
79+
p="8px"
80+
pos="absolute"
81+
right="24px"
82+
top="16px"
83+
transition="all 0.125s ease-in-out"
84+
>
85+
<Image
86+
aspectRatio="1"
87+
boxSize="20px"
88+
src={copied ? '/icons/copied.svg' : '/icons/copy-code.svg'}
89+
/>
90+
<Text color="$captionBold" typography="caption">
91+
Copy
92+
</Text>
93+
</Center>
94+
4595
{children}
4696
</Box>
4797
)}

apps/landing/src/components/Code.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ export const Code = ({
1010
language: string
1111
value: string
1212
}) => {
13-
const normalizedValue = value
14-
.split('\n')
15-
.map((line) => line.replaceAll(' ', ' '))
16-
.join('\n')
17-
1813
return (
1914
<>
2015
<Box
@@ -23,7 +18,7 @@ export const Code = ({
2318
}}
2419
>
2520
<SyntaxHighlighter language={language} showLineNumbers style={Light}>
26-
{normalizedValue}
21+
{value}
2722
</SyntaxHighlighter>
2823
</Box>
2924
<Box

0 commit comments

Comments
 (0)