A button component that copies text to the clipboard with visual feedback.
Basic Usage
Code
<m-copy-button value="Hello World">
Copy Text
</m-copy-button> Custom Feedback
Code
<m-copy-button value="npm install @maxhill/components" feedback="Command copied!">
Copy Install Command
</m-copy-button> Without Icon
Code
<m-copy-button value="code snippet example" show-icon="false">
Copy
</m-copy-button> Custom Icon
Code
<m-copy-button value="custom icon text">
<span slot="icon">📋</span>
Copy with Custom Icon
</m-copy-button> Event Listening
Click the button to see events...
Code
<div class="stack" data-gap="2">
<m-copy-button id="event-copy" value="Event example text">
Copy Text
</m-copy-button>
<div id="copy-event-output" class="box" style="padding: var(--size-2); background: var(--color-surface-raised);">
Click the button to see events...
</div>
</div>
<script>
const eventCopy = document.getElementById('event-copy');
const copyEventOutput = document.getElementById('copy-event-output');
eventCopy.addEventListener('copy-success', (e) => {
copyEventOutput.textContent = `✓ Copied: "${e.detail.value}"`;
});
eventCopy.addEventListener('copy-error', (e) => {
copyEventOutput.textContent = `✗ Error: ${e.detail.error.message}`;
});
</script> Copying Code Snippets
const greeting = 'Hello, World!';
console.log(greeting);
Code
<div class="stack" data-gap="2">
<pre class="box" style="position: relative; padding: var(--size-3); background: var(--color-surface-raised);">
<m-copy-button
value="const greeting = 'Hello, World!';\nconsole.log(greeting);"
style="position: absolute; top: var(--size-2); right: var(--size-2);"
show-icon="true">
</m-copy-button>
<code>const greeting = 'Hello, World!';
console.log(greeting);</code>
</pre>
</div> - value string ""
- The text to copy to clipboard (required)
- showIcon boolean true
- Show/hide the copy icon (default: true)
- feedback string "Copied!"
- Custom feedback message shown on successful copy (default: "Copied!")
- render void
- attachEventListeners void
- define void
- copy-success CustomEvent
- Fired when text is successfully copied (detail: { value: string })
- copy-error CustomEvent
- Fired when copy fails (detail: { error: Error })
- (default)
- Default slot for button label/content
- icon
- Optional slot for custom icon (overrides default copy icon)
- ::part(button)
- The button element
- ::part(icon)
- Icon wrapper
- ::part(feedback)
- Feedback tooltip