--- meta: title: Copy Button description: Copies data to the clipboard when the user clicks the button. layout: component --- ```html:preview ``` ```jsx:react import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button'; const App = () => ( ); ``` ## Examples ### Custom Labels Copy Buttons display feedback in a tooltip. You can customize the labels using the `copy-label`, `success-label`, and `error-label` attributes. ```html:preview ``` ```jsx:react import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button'; const App = () => ( ); ``` ### Custom Icons Use the `copy-icon`, `success-icon`, and `error-icon` slots to customize the icons that get displayed for each state. You can use [``](/components/icon) or your own images. ```html:preview ``` ```jsx:react import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button'; import { SlIcon } from '@shoelace-style/shoelace/dist/react/icon'; const App = () => ( <> ); ``` ### Copying Values From Other Elements Normally, the data that gets copied will come from the component's `value` attribute, but you can copy data from any element within the same document by providing its `id` to the `from` attribute. When using the `from` attribute, the element's [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) will be copied by default. Passing an attribute or property modifier will let you copy data from one of the element's attributes or properties instead. To copy data from an attribute, use `from="id[attr]"` where `id` is the id of the target element and `attr` is the name of the attribute you'd like to copy. To copy data from a property, use `from="id.prop"` where `id` is the id of the target element and `prop` is the name of the property you'd like to copy. ```html:preview +1 (234) 456-7890



Shoelace Website ``` ```jsx:react import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button'; import { SlInput } from '@shoelace-style/shoelace/dist/react/input'; const App = () => ( <> {/* Copies the span's textContent */} +1 (234) 456-7890

{/* Copies the input's "value" property */}

{/* Copies the link's "href" attribute */} Shoelace Website ); ``` ### Handling Errors A copy error will occur if the value is an empty string, if the `from` attribute points to an id that doesn't exist, or if the browser rejects the operation for any reason. When this happens, the `sl-error` event will be emitted. This example demonstrates what happens when a copy error occurs. You can customize the error label and icon using the `error-label` attribute and the `error-icon` slot, respectively. ```html:preview ``` ```jsx:react import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button'; const App = () => ( ); ``` ### Disabled Copy buttons can be disabled by adding the `disabled` attribute. ```html:preview ``` ```jsx:react import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button'; const App = () => ( ); ``` ### Changing Feedback Duration A success indicator is briefly shown after copying. You can customize the length of time the indicator is shown using the `feedback-duration` attribute. ```html:preview ``` ```jsx:react import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button'; const App = () => ( ); ``` ### Custom Styles You can customize the button to your liking with CSS. ```html:preview ``` ```jsx:react import { SlCopyButton } from '@shoelace-style/shoelace/dist/react/copy-button'; const css = ` .custom-styles { --success-color: white; --error-color: white; color: white; } .custom-styles::part(button) { background-color: #ff1493; border: solid 4px #ff7ac1; border-right-color: #ad005c; border-bottom-color: #ad005c; border-radius: 0; transition: 100ms scale ease-in-out, 100ms translate ease-in-out; } .custom-styles::part(button):hover { scale: 1.1; } .custom-styles::part(button):active { translate: 0 2px; } .custom-styles::part(button):focus-visible { outline: dashed 2px deeppink; outline-offset: 4px; } `; const App = () => ( <> ); ```