Clipboard
v0.0.10
mix mishka.ui.gen.component clipboard
Overview of the Phoenix and LiveView Clipboard Component
The Phoenix LiveView Clipboard component provides an elegant and customizable interface for copying text content to the user's clipboard. It's designed for various use-cases like copying static text, dynamic values, or text from external inputs and code blocksو all with visual feedback and support for success and error states..
Overview of the Phoenix and LiveView Clipboard Component
This is your private API key. Click the button to copy it and keep it secure.
1 defmodule MyApp do
2 def hello do
3 "world"
4 end
5 endExperience the innovative capabilities of the Clipboard component and hook from Mishka Chelekom, seamlessly integrated into Phoenix LiveView to empower your web applications with dynamic copy functionality. This feature ensures that essential information from documentation links to code snippets can be effortlessly copied, enhancing both accessibility and user engagement. To instantly copy the complete guide URL for further insights, simply copy and explore our comprehensive resources.
Text prop
The text prop allows you to define a static string that will be copied to the user's clipboard when the trigger is activated. When this prop is set, it takes priority over both target_selector and the slot content.
This is ideal for simple copy actions like copying an email, token, or URL where the content is known ahead of time and doesn’t rely on the DOM.
Text prop
<.clipboard text="[email protected]">
<:trigger>
<button>Copy Email</button>
</:trigger>
</.clipboard>Target selector prop
The target_selector prop accepts a valid CSS selector pointing to another DOM element. When the copy trigger is activated, the content of the targeted element will be copied to the clipboard.
This is useful when the content to be copied exists elsewhere on the page and you do not want to duplicate it. If both text and target_selector are provided, text takes precedence.
Target selector prop
<div id="copy-me">https://mishka.tools/chelekom/docs/clipboard</div>
<.clipboard target_selector="#copy-me">
<:trigger>
<button>Copy Link</button>
</:trigger>
</.clipboard>Timeout prop
The timeout prop sets how long (in milliseconds) the visual feedback (success or error state) should remain visible after a copy attempt. The default value is 2000, which means the status message and any temporary styling will reset after 2 seconds.
You can customize this duration to better fit your UX. For example, a shorter timeout might be preferred in fast-paced interfaces, while a longer one gives users more time to notice the feedback.
Timeout prop
<.clipboard text="https://mishka.tools" timeout={4000}>
<:trigger>
<button>Copy URL</button>
</:trigger>
</.clipboard>Success class prop
The success_class prop defines the CSS class that will be temporarily added to the copy trigger element when the copy operation is successful. By default, it uses "clipboard-success".
This allows you to apply custom styles such as background changes, icons, or animations to indicate that the copy worked. The class is automatically removed after the duration defined by the timeout prop.
Success class prop
<.clipboard text="Invite Code: CHELEKOM123" success_class="bg-green-500 text-white">
<:trigger>
<button class="px-3 py-1 border rounded">Copy Code</button>
</:trigger>
</.clipboard>
<!--Use it inside inline-->
<p class="text-[14px] leading-8">
Mishka Chelekom is a set of Phoenix LiveView components for any project
<.clipboard class=""
text="Text you wanna copy"
show_status_text={false}
success_class="text-emerald-500 dark:text-emerald-800"
>
<:trigger>
<button class="">
<span class="mr-1">copy</span>
<.icon name="hero-clipboard-document-list" class="size-4" />
</button>
</:trigger>
</.clipboard> Copy the install command to add any Chelekom component to your app
</p>Error class prop
The error_class prop defines the CSS class that will be temporarily added to the trigger element when the copy action fails. The default value is "clipboard-error".
You can use this to provide immediate visual feedback, such as a red background or shake animation, when something goes wrong (e.g. clipboard permissions denied). The class is automatically removed after the duration specified by the timeout prop.
Error class prop
<.clipboard text="this-will-fail!" error_class="bg-red-500 text-white">
<:trigger>
<button class="px-3 py-1 border rounded">Copy Text</button>
</:trigger>
</.clipboard>Copy success text prop
The copy_success_text prop defines the message that appears in the status element when the copy operation is successful. This text is visible to screen readers and any live UI feedback components.
By default, it uses a localized value from Gettext: "Copied!". You can override it with a custom string or use translated strings to support localization in your app.
Copy success text prop
<.clipboard text="https://chelekom.tools" copy_success_text="Link copied successfully!">
<:trigger>
<button>Copy Link</button>
</:trigger>
</.clipboard>Copy error text prop
The copy_error_text prop defines the message shown in the status element when the copy operation fails. This message is announced to assistive technologies and can also be styled or animated as part of your feedback system.
By default, the message is pulled from your Gettext module and displays "Copy failed". You can customize this message with a static string or localized translation to match your tone or brand voice.
Copy error text prop
<.clipboard text={@maybe_invalid_value} copy_error_text="Oops! Something went wrong.">
<:trigger>
<button>Try Copy</button>
</:trigger>
</.clipboard>Text description prop
The text_description prop provides an optional description of the content being copied. This text is visually hidden using a sr-only class but announced by screen readers when the user focuses on the trigger.
It’s helpful for improving accessibility when the content being copied is not clearly explained by visible UI elements, like when using just an icon or symbol as the trigger.
Text description prop
<.clipboard
text="https://mishka.tools/chelekom/docs"
text_description="This button copies the documentation link to your clipboard"
>
<:trigger>
<button aria-hidden="true">🔗</button>
</:trigger>
</.clipboard>Content slot
The :content slot allows you to provide fallback content to be copied if neither text nor target_selector are defined.
This is useful for inline or dynamic content that exists within the component itself. The rendered content inside this slot will automatically be used as the copy source.
Content slot
<.clipboard>
<:content>
Invite code: <span class="font-mono">CHELEKOM2025</span>
</:content>
<:trigger>
<button>Copy Code</button>
</:trigger>
</.clipboard>Trigger slot
The :trigger slot defines the element that activates the copy action when clicked (or focused + "Enter" key pressed). This slot is required and must wrap a clickable element like a button, icon, or link.
The trigger is automatically enhanced with accessibility attributes and copy behavior through a JavaScript hook. You can style or structure it however you'd like. The slot gives you full control over the UI.
Trigger slot
<.clipboard text="https://mishka.tools/chelekom">
<:trigger>
<button class="bg-blue-500 text-white px-3 py-1 rounded">Copy Link</button>
</:trigger>
</.clipboard>Status class prop
The status_class prop allows you to apply custom CSS classes to the status message element that appears after a copy action (either success or failure).
By default, the component adds a clipboard-status class for base styling. You can extend or override the styles by providing additional classes using status_class, allowing full control over appearance (e.g. color, size, animation).
Status class prop
<.clipboard
text="mishka.tools"
copy_success_text="Copied successfully!"
status_class="text-green-600 font-semibold text-sm mt-1"
>
<:trigger>
<button class="px-3 py-1 border rounded">Copy</button>
</:trigger>
</.clipboard>Show status text prop in Phoenix LiveView clipboard component
The show_status_text prop determines whether the visual feedback message (such as “Copied!” or “Copy failed”) should be displayed in the DOM.
By default, it is set to true, which means the component will render a <span> element containing the status message. If you set it to false, the message will not appear visibly, but accessibility support (via aria-live) and visual feedback (like success/error classes) still work.
Show status text prop in Phoenix LiveView clipboard component
<.clipboard
text="my-secret-token"
show_status_text={false}
success_class="bg-green-500 text-white"
copy_success_text="Copied!"
>
<:trigger>
<button class="btn btn-primary">Copy</button>
</:trigger>
</.clipboard>Dynamic Label Prop in Phoenix LiveView Clipboard Component
The dynamic_label prop determines whether the text inside the clipboard label should change based on copy success or failure.
By default, it is set to false, which means the clipboard label text remains static. If you set it to true, the text inside the label will dynamically update when the clipboard operation succeeds or fails, providing real-time feedback to the user.
Dynamic Label Prop in Phoenix LiveView Clipboard Component
<.clipboard
text="https://mishka.tools/chelekom/docs"
show_status_text={false}
success_class="bg-green-500 text-white rounde"
copy_success_text="✅ Copied!"
copy_error_text="❌ Failed"
dynamic_label={true}
>
<:trigger>
<button>
📋 Copy
</button>
</:trigger>
</.clipboard>Content class Prop
The content_class prop defines the CSS class applied to the container of the status message shown after a copy operation.
This allows you to control the visual styling of the feedback area where messages like copy_success_text or copy_error_text are rendered.
Content class Prop
<.clipboard
content_class="block mt-2"
>
<:trigger>
<button class="btn btn-primary">Copy</button>
</:trigger>
<:content>
Invite code: <span class="font-mono">CHELEKOM2025</span>
</:content>
</.clipboard>Attributes
| Name | Type | Default | Values | Required | Description |
|---|---|---|---|---|---|
id
|
string
| — | — | No | The unique identifier for the clipboard component element. |
class
|
any
| — | — | No | CSS classes to apply to the clipboard component container. |
text
|
string
| — | — | No | The text to copy to the clipboard. If not provided, it will look for the text content or target selector. |
target_selector
|
string
| — | — | No | The CSS selector for the target element to copy from, if no text is provided. |
timeout
|
integer
|
2000
| — | No | The timeout duration (in milliseconds) before the clipboard operation is considered failed. |
success_class
|
string
|
clipboard-success
| — | No | CSS class applied to the component when the clipboard copy is successful. |
error_class
|
string
|
clipboard-error
| — | No | CSS class applied to the component when the clipboard copy fails. |
copy_success_text
|
string
| — | — | No | The success message to display after a successful copy operation. |
copy_error_text
|
string
| — | — | No | The error message to display after a failed copy operation. |
copy_button_label
|
string
| — | — | No | Label for the button used to trigger the copy operation. |
text_description
|
string
| — | — | No | Optional description text for screen readers, providing more context about the clipboard functionality. |
status_class
|
string
|
block mt-2
| — | No | CSS class for styling the status message shown after a copy operation. |
content_class
|
string
|
block mb-2
| — | No | CSS class for styling the content shown in the clipboard container. |
trigger_class
|
string
| — | — | No | CSS class for styling the trigger wrapper. |
show_status_text
|
boolean
|
true
| — | No | If true, displays the visual clipboard status text (e.g., 'Copied!'). Default: true. |
dynamic_label
|
boolean
|
false
| — | No | If true, replaces text inside `.clipboard-label` on copy success/failure. Default: false. |
rest
|
global
| — | — | No | Global attributes can define defaults which are merged with attributes provided by the caller |
Slots
| Name | Required | Description |
|---|---|---|
content
| No | Slot for custom content to display inside the clipboard container. |
trigger
| Yes | The slot for the button or trigger element that initiates the copy operation. This is a required slot. |
inner_block
| No | Slot for additional content or inner components that should be rendered inside the clipboard container. |
Options
Each option takes one or more of its values, separated by commas.
| Option | Values |
|---|---|
--module
| A module name of your own |
What it generates
mix mishka.ui.gen.component clipboard