Copy Button
<mo-copy-button> | MOCopyButton
Copies data to the clipboard when the user clicks the button.
<mo-copy-button value="This text will be copied."></mo-copy-button>
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.
<mo-copy-button value="Custom labels are easy" copy-label="Click me to copy text." success-label="Successfully copied text." error-label="Looks like your browser doesn't support this!" ></mo-copy-button>
import { MOCopyButton } from '@metsooutotec/modes-web-components/dist/react/copy-button'; const App = () => ( <MOCopyButton value="Custom labels are easy" copy-label="Click me to copy text." success-label="Successfully copied text." error-label="Looks like your browser doesn't support this!" /> );
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
<mo-icon>
or your own images.
<mo-copy-button value="Copied from a custom button"> <mo-icon slot="copy-icon" name="paste"></mo-icon> <mo-icon slot="success-icon" name="ok-circle-fill"></mo-icon> <mo-icon slot="error-icon" name="cross-circle-fill"></mo-icon> </mo-copy-button>
import { MOCopyButton } from '@metsooutotec/modes-web-components/dist/react/copy-button'; import { MOIcon } from '@metsooutotec/modes-web-components/dist/react/icon'; const App = () => ( <> <MOCopyButton value="Copied from a custom button"> <MOIcon slot="copy-icon" name="paste" /> <MOIcon slot="success-icon" name="ok-circle-fill" /> <MOIcon slot="error-icon" name="cross-circle-fill" /> </MOCopyButton> </> );
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
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.
<!-- Copies the span's textContent --> <span id="my-email">john.doe@email.com</span> <mo-copy-button copy-label="Copy email" from="my-email"></mo-copy-button> <br /><br /> <!-- Copies the input's "value" property --> <mo-input id="my-input" type="text" value="User input" style="display: inline-block; max-width: 300px;"></mo-input> <mo-copy-button copy-label="Copy input" from="my-input.value"></mo-copy-button> <br /><br /> <!-- Copies the link's "href" attribute --> <a id="my-link" href="https://modes-web.metso.com/">Modes UI Website</a> <mo-copy-button copy-label="Copy website URL" from="my-link[href]"></mo-copy-button>
import { MOCopyButton } from '@metsooutotec/modes-web-components/dist/react/copy-button'; import { MOInput } from '@metsooutotec/modes-web-components/dist/react/input'; const App = () => ( <> {/* Copies the span's textContent */} <span id="my-email">john.doe@email.com</span> <MOCopyButton copy-label="Copy email" from="my-email" /> <br /> <br /> {/* Copies the input's "value" property */} <MOInput id="my-input" type="text" /> <MOCopyButton copy-label="Copy input" from="my-input.value" /> <br /> <br /> {/* Copies the link's "href" attribute */} <a id="my-link" href="https://modes-web.metso.com/"> Modes UI Website </a> <MOCopyButton copy-label="Copy website URL" from="my-link[href]" /> </> );
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
mo-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.
<mo-copy-button from="i-do-not-exist"></mo-copy-button>
import { MOCopyButton } from '@metsooutotec/modes-web-components/dist/react/copy-button'; const App = () => <MOCopyButton from="i-do-not-exist" />;
Sizing
The copy button is sized relative to the current font size (like the icon), it defaults to 16px. To change its size, set the font-size property on the copy button itself or on a parent element.
<mo-copy-button style="font-size: var(--mo-font-size-medium);" value="1rem (16px)"></mo-copy-button> <mo-copy-button style="font-size: var(--mo-font-size-x-large);" value="1.25rem (20px)"></mo-copy-button> <mo-copy-button style="font-size: var(--mo-font-size-2x-large);" value="1.5rem (24px)"></mo-copy-button>
import { MOCopyButton } from '@metsooutotec/modes-web-components/dist/react/copy-button'; const App = () => ( <> <MOCopyButton style={{ fontSize: 'var(--mo-font-size-medium)' }} value="1rem (16px)" /> <MOCopyButton style={{ fontSize: 'var(--mo-font-size-x-large)' }} value="1.25rem (20px)" /> <MOCopyButton style={{ fontSize: 'var(--mo-font-size-2x-large)' }} value="1.5rem (24px)" /> </> );
Disabled
Copy buttons can be disabled by adding the disabled
attribute.
<mo-copy-button value="You can't copy me" disabled></mo-copy-button>
import { MOCopyButton } from '@metsooutotec/modes-web-components/dist/react/copy-button'; const App = () => <MOCopyButton value="You can't copy me" disabled />;
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. The default duration is one second (1000 ms).
<mo-copy-button value="This text will be copied." feedback-duration="250"></mo-copy-button>
import { MOCopyButton } from '@metsooutotec/modes-web-components/dist/react/copy-button'; const App = () => <MOCopyButton value="This text will be copied." feedback-duration={250} />;
Localized
If the user does not provide custom labels, the copy button uses the localized terms copy
,
copied
, and error
for the tooltip messages. This example uses the Finnish
translations. Note that only Finnish and English are provided out of the box, and English is the default.
<mo-copy-button lang="fi" value="This text will be copied." feedback-duration="3000"></mo-copy-button>
import { MOCopyButton } from '@metsooutotec/modes-web-components/dist/react/copy-button'; const App = () => <MOCopyButton lang="fi" value="This text will be copied." feedback-duration={3000} />;
See the localization page for more information.
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component using a bundler:
import '@metsooutotec/modes-web-components/dist/components/copy-button/copy-button.js';
To import this component as a React component:
import MOCopyButton from '@metsooutotec/modes-web-components/dist/react/copy-button/';
To import this component using a script tag:
<script type="module" src="https://modes-web.metso.com/dist/components/cdn/components/copy-button/copy-button.js"></script>
Slots
Name | Description |
---|---|
copy-icon
|
The icon to show in the default copy state. Works best with <mo-icon> . |
success-icon
|
The icon to show when the content is copied. Works best with <mo-icon> . |
error-icon
|
The icon to show when a copy error occurs. Works best with <mo-icon> . |
Learn more about using slots.
Properties
Name | Description | Reflects | Type | Default |
---|---|---|---|---|
value
|
The text value to copy. |
string
|
''
|
|
from
|
An id that references an element in the same document from which data will be copied. If both this
and value are present, this value will take precedence. By default, the target
element’s textContent will be copied. To copy an attribute, append the attribute name
wrapped in square brackets, e.g. from="el[value]" . To copy a property, append a dot and
the property name, e.g. from="el.value" .
|
string
|
''
|
|
disabled
|
Disables the copy button. |
|
boolean
|
false
|
copyLabel
copy-label
|
A custom label to show in the tooltip. |
string
|
''
|
|
successLabel
success-label
|
A custom label to show in the tooltip after copying. |
string
|
''
|
|
errorLabel
error-label
|
A custom label to show in the tooltip when a copy error occurs. |
string
|
''
|
|
feedbackDuration
feedback-duration
|
The length of time to show feedback before restoring the default trigger. |
number
|
1000
|
|
tooltipPlacement
tooltip-placement
|
The preferred placement of the tooltip. |
'top' | 'right' | 'bottom' | 'left'
|
'top'
|
|
hoist
|
Enable this option to prevent the tooltip from being clipped when the component is placed inside a
container with
overflow: auto|hidden|scroll . Hoisting uses a fixed positioning strategy that works in
many, but not all, scenarios.
|
boolean
|
false
|
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Events
Name | React Event | Description | Event Detail |
---|---|---|---|
mo-copy |
onMoCopy |
Emitted when the data has been copied. | - |
mo-error |
onMoError |
Emitted when the data could not be copied. | - |
Learn more about events.
Custom Properties
Name | Description | Default |
---|---|---|
--success-color |
The color to use for success feedback. | |
--error-color |
The color to use for error feedback. |
Learn more about customizing CSS custom properties.
Parts
Name | Description |
---|---|
button |
The internal <button> element. |
copy-icon |
The container that holds the copy icon. |
success-icon |
The container that holds the success icon. |
error-icon |
The container that holds the error icon. |
tooltip__base |
The tooltip’s exported base part. |
tooltip__base__popup |
The tooltip’s exported popup part. |
tooltip__base__arrow |
The tooltip’s exported arrow part. |
tooltip__body |
The tooltip’s exported body part. |
Learn more about customizing CSS parts.
Animations
Name | Description |
---|---|
copy.in |
The animation to use when feedback icons animate in. |
copy.out |
The animation to use when feedback icons animate out. |
Learn more about customizing animations.
Dependencies
This component automatically imports the following dependencies.