File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ export * from './lib/avatar/Avatar'
1818export * from './lib/badge/Badge'
1919export * from './lib/button/Button'
2020export * from './lib/checkbox/Checkbox'
21+ export * from './lib/copy-to-clipboard/CopyToClipboard'
2122export * from './lib/date-picker/DateRangePicker'
2223export * from './lib/divider/Divider'
2324export * from './lib/dropdown-menu/DropdownMenu'
Original file line number Diff line number Diff line change 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+ )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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'
1310import { Tooltip } from '../tooltip/Tooltip'
1411
1512type 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}
You can’t perform that action at this time.
0 commit comments