Skip to content

Commit ae5c402

Browse files
Merge pull request #62 from vtexdocs/feat/copy-link-button
feat: CopyLinkButton - components migration
2 parents ee716d8 + 0e37961 commit ae5c402

File tree

7 files changed

+116
-4
lines changed

7 files changed

+116
-4
lines changed

src/components/code-block/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import CopyButton from 'components/copy-button'
2+
import CopyCode from 'components/copy-code'
33
import { Box, Text } from '@vtex/brand-ui'
44
import { childrenToString } from 'utils/string-utils'
55
import styles from './styles'
@@ -14,7 +14,7 @@ const CodeBlock = ({ ...props }: Component) => {
1414
return (
1515
<Box sx={styles.CodeBlock}>
1616
<pre>
17-
<CopyButton code={childrenToString(props.children)} />
17+
<CopyCode code={childrenToString(props.children)} />
1818
<Text sx={styles.CodeContent} {...props} />
1919
</pre>
2020
</Box>

src/components/copy-button/index.tsx renamed to src/components/copy-code/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useState, useRef, useEffect } from 'react'
1+
import { useCallback, useState, useRef, useEffect } from 'react'
22
import copy from 'copy-text-to-clipboard'
33
import { Box, Button, Icon } from '@vtex/brand-ui'
44
import type { SxStyleProp } from '@vtex/brand-ui'
@@ -10,7 +10,7 @@ interface CopyButtonProps {
1010
sx?: SxStyleProp
1111
}
1212

13-
export default function CopyButton({ code, sx }: CopyButtonProps): JSX.Element {
13+
export default function CopyCode({ code, sx }: CopyButtonProps): JSX.Element {
1414
const [isCopied, setIsCopied] = useState(false)
1515
const copyTimeout = useRef<number | undefined>(undefined)
1616
const handleCopyCode = useCallback(() => {
File renamed without changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { Meta, StoryObj } from '@storybook/react'
2+
import CopyLinkButton from '.'
3+
import LibraryContextProvider from 'utils/context/libraryContext'
4+
import { ThemeProvider } from '@vtex/brand-ui'
5+
import { exampleContextProps } from 'utils/storybook-constants'
6+
7+
const meta = {
8+
title: 'Example/CopyLinkButton',
9+
component: CopyLinkButton,
10+
parameters: {
11+
layout: 'centered',
12+
},
13+
tags: ['autodocs'],
14+
argTypes: {
15+
onAccept: () => alert('Accept cookies'),
16+
},
17+
decorators: [
18+
(Story) => (
19+
<ThemeProvider>
20+
<LibraryContextProvider {...exampleContextProps}>
21+
<Story />
22+
</LibraryContextProvider>
23+
</ThemeProvider>
24+
),
25+
],
26+
} satisfies Meta<typeof CopyLinkButton>
27+
28+
export default meta
29+
type Story = StoryObj<typeof meta>
30+
31+
export const SimpleCookieBar: Story = {
32+
args: {},
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Button } from '@vtex/brand-ui'
2+
import Tooltip from 'components/tooltip'
3+
import CopyIcon from '../icons/copy-icon'
4+
import { useState } from 'react'
5+
import styles from './styles'
6+
7+
export default function CopyLinkButton() {
8+
const [tooltipText, setTooltipText] = useState('Copy')
9+
10+
const handleCopy = () => {
11+
navigator.clipboard.writeText(window.location.href)
12+
setTooltipText('Copy')
13+
14+
setTimeout(() => {
15+
setTooltipText('Copied!')
16+
}, 2000)
17+
}
18+
19+
return (
20+
<Tooltip label={tooltipText} placement="bottom">
21+
<Button onClick={handleCopy} sx={styles.copyLinkButton}>
22+
<CopyIcon sx={styles.copyIcon} size={16} />
23+
</Button>
24+
</Tooltip>
25+
)
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { SxStyleProp } from '@vtex/brand-ui'
2+
3+
const copyIcon: SxStyleProp = {
4+
marginRight: '4px',
5+
}
6+
7+
const copyLinkButton: SxStyleProp = {
8+
textTransform: 'none',
9+
color: '#3F3F3F',
10+
fontWeight: '100',
11+
fontSize: '12px',
12+
lineHeight: '18px',
13+
height: 'none',
14+
padding: '8px 5px 8px 8px',
15+
border: '1px solid #A1AAB7',
16+
backgroundColor: 'white',
17+
':hover': {
18+
backgroundColor: '#EFEFEF',
19+
},
20+
}
21+
22+
export default {
23+
copyIcon,
24+
copyLinkButton,
25+
}

src/components/icons/copy-icon.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { IconProps } from '@vtex/brand-ui'
2+
import { Icon } from '@vtex/brand-ui'
3+
4+
const CopyIcon = (props: IconProps) => (
5+
<Icon
6+
{...props}
7+
viewBox="0 0 15 15"
8+
fill="none"
9+
xmlns="http://www.w3.org/2000/svg"
10+
>
11+
<path
12+
d="M12.5 5.625H6.875C6.18464 5.625 5.625 6.18464 5.625 6.875V12.5C5.625 13.1904 6.18464 13.75 6.875 13.75H12.5C13.1904 13.75 13.75 13.1904 13.75 12.5V6.875C13.75 6.18464 13.1904 5.625 12.5 5.625Z"
13+
stroke="currentcolor"
14+
strokeWidth="1.5"
15+
strokeLinecap="round"
16+
strokeLinejoin="round"
17+
/>
18+
<path
19+
d="M3.125 9.375H2.5C2.16848 9.375 1.85054 9.2433 1.61612 9.00888C1.3817 8.77446 1.25 8.45652 1.25 8.125V2.5C1.25 2.16848 1.3817 1.85054 1.61612 1.61612C1.85054 1.3817 2.16848 1.25 2.5 1.25H8.125C8.45652 1.25 8.77446 1.3817 9.00888 1.61612C9.2433 1.85054 9.375 2.16848 9.375 2.5V3.125"
20+
stroke="currentcolor"
21+
strokeWidth="1.5"
22+
strokeLinecap="round"
23+
strokeLinejoin="round"
24+
/>
25+
</Icon>
26+
)
27+
28+
export default CopyIcon

0 commit comments

Comments
 (0)