Skip to content

Commit bfd59c0

Browse files
authored
Move CopyToClipboard to its own component (#1845)
* Extract CopyToClipboard to own component * Update aria-label for better screenreader support
1 parent ba335d4 commit bfd59c0

4 files changed

Lines changed: 75 additions & 26 deletions

File tree

‎libs/ui/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export * from './lib/avatar/Avatar'
1818
export * from './lib/badge/Badge'
1919
export * from './lib/button/Button'
2020
export * from './lib/checkbox/Checkbox'
21+
export * from './lib/copy-to-clipboard/CopyToClipboard'
2122
export * from './lib/date-picker/DateRangePicker'
2223
export * from './lib/divider/Divider'
2324
export * from './lib/dropdown-menu/DropdownMenu'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
import { CopyToClipboard } from './CopyToClipboard'
9+
10+
export const Default = () => (
11+
<div>
12+
<div className="flex items-center gap-2">
13+
<span>This is the text to be copied.</span>
14+
<CopyToClipboard text="This is the text to be copied." />
15+
</div>
16+
<p>
17+
Note that the text rendered on the screen is independent of the text copied to the
18+
clipboard …
19+
</p>
20+
<div className="flex items-center gap-2">
21+
<span>This text, for example, is different from the text that’ll be copied …</span>
22+
<CopyToClipboard
23+
text="Clicking this button will copy the text you are currently reading"
24+
ariaLabel="You can customize the ariaLabel for the prompt to copy"
25+
/>
26+
</div>
27+
</div>
28+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
9+
import { useState } from 'react'
10+
11+
import { Clipboard16Icon, Success12Icon, useTimeout } from '@oxide/ui'
12+
13+
export const CopyToClipboard = ({
14+
ariaLabel = 'Click to copy this text',
15+
text,
16+
}: {
17+
ariaLabel?: string
18+
text: string
19+
}) => {
20+
const [hasCopied, setHasCopied] = useState(false)
21+
22+
useTimeout(() => setHasCopied(false), hasCopied ? 2000 : null)
23+
24+
const handleCopy = () => {
25+
window.navigator.clipboard.writeText(text).then(() => {
26+
setHasCopied(true)
27+
})
28+
}
29+
30+
return (
31+
<button
32+
className="text-tertiary hover:text-accent-secondary"
33+
onClick={handleCopy}
34+
type="button"
35+
aria-label={hasCopied ? 'Copied' : ariaLabel}
36+
>
37+
{hasCopied ? (
38+
<Success12Icon className="text-accent-secondary" />
39+
) : (
40+
<Clipboard16Icon className="h-3 w-3" />
41+
)}
42+
</button>
43+
)
44+
}

‎libs/ui/lib/truncate/Truncate.tsx‎

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8-
import { useState } from 'react'
98

10-
import { Clipboard16Icon, Success12Icon } from '@oxide/design-system/icons/react'
11-
12-
import useTimeout from '../hooks/use-timeout'
9+
import { CopyToClipboard } from '../copy-to-clipboard/CopyToClipboard'
1310
import { Tooltip } from '../tooltip/Tooltip'
1411

1512
type TruncatePosition = 'middle' | 'end'
@@ -29,38 +26,17 @@ export const Truncate = ({
2926
hasCopyButton,
3027
tooltipDelay = 300,
3128
}: TruncateProps) => {
32-
const [hasCopied, setHasCopied] = useState(false)
33-
34-
useTimeout(() => setHasCopied(false), hasCopied ? 2000 : null)
35-
3629
if (text.length <= maxLength) {
3730
return <div>{text}</div>
3831
}
3932

40-
const handleCopy = () => {
41-
window.navigator.clipboard.writeText(text).then(() => {
42-
setHasCopied(true)
43-
})
44-
}
45-
4633
// Only use the tooltip if the text is longer than maxLength
4734
return (
4835
<div className="flex items-center space-x-2">
4936
<Tooltip content={text} delay={tooltipDelay}>
5037
<div aria-label={text}>{truncate(text, maxLength, position)}</div>
5138
</Tooltip>
52-
{hasCopyButton &&
53-
(hasCopied ? (
54-
<Success12Icon className="text-accent-secondary" />
55-
) : (
56-
<button
57-
className="text-tertiary hover:text-accent-secondary"
58-
onClick={handleCopy}
59-
type="button"
60-
>
61-
<Clipboard16Icon className="h-3 w-3" />
62-
</button>
63-
))}
39+
{hasCopyButton && <CopyToClipboard text={text} />}
6440
</div>
6541
)
6642
}

0 commit comments

Comments
 (0)