Corex. Clipboard
(Corex v0.2.0)
View Source
Copy-to-clipboard control for Phoenix LiveView. Behavior follows Zag.js Clipboard.
Set value for the initial string to copy. Use Corex.Clipboard.set_value/2 or corex:clipboard:set-value to change it on the client.
Anatomy
Minimal
<.clipboard class="clipboard" value="hello@example.com">
<:label>Email</:label>
<:copy>
<.heroicon name="hero-clipboard" />
</:copy>
<:copied>
<.heroicon name="hero-check" />
</:copied>
</.clipboard>Trigger only
<.clipboard
class="clipboard"
value="https://example.com/share"
input={false}
trigger_aria_label="Copy link"
>
<:copy>
<.heroicon name="hero-clipboard" />
</:copy>
<:copied>
<.heroicon name="hero-check" />
</:copied>
</.clipboard>API
Requires a stable id on <.clipboard>.
| Function | Action | Returns |
|---|---|---|
copy/1 | Copy current value (client) | %Phoenix.LiveView.JS{} |
copy/2 | Copy current value (server) | socket |
set_value/2 | Set value to copy (client) | %Phoenix.LiveView.JS{} |
set_value/3 | Set value to copy (server) | socket |
Events
Pick an event name and pass it to on_* on <.clipboard>.
Server events
| Event | When | Payload |
|---|---|---|
on_copy="clipboard_copied" | User copies | %{"id" => id, "value" => value} |
on_copy
<.clipboard
class="clipboard"
value="info@netoum.com"
on_copy="clipboard_copied"
>
<:label>Copy</:label>
<:copy>
<.heroicon name="hero-clipboard" />
</:copy>
<:copied>
<.heroicon name="hero-check" />
</:copied>
</.clipboard>def handle_event("clipboard_copied", %{"value" => value, "id" => _id}, socket) do
{:noreply, assign(socket, :last_copied, value)}
endClient events
| Event | When | event.detail |
|---|---|---|
on_copy_client="clipboard-copied" | User copies | id, value |
Style
Target parts with data-scope and data-part, or use Corex Design: import tokens and clipboard.css, then set class="clipboard" on <.clipboard>.
[data-scope="clipboard"][data-part="root"] {}
[data-scope="clipboard"][data-part="trigger"] {}
[data-scope="clipboard"][data-part="control"] {}
[data-scope="clipboard"][data-part="input"] {}
[data-scope="clipboard"][data-part="copy"] {}
[data-scope="clipboard"][data-part="copied"] {}@import "../corex/corex.css";Stack modifiers on the host (class on <.clipboard>). Combine axes, for example clipboard ui-accent ui-size-lg or clipboard ui-info ui-solid.
Axes: Semantic (ui-accent, ui-brand, ui-alert, ui-info, ui-success), Variant (ui-solid), Size (ui-size-sm … ui-size-xl), Radius (ui-rounded-*). See the modifier guide.
Semantic modifiers set palette variables on the trigger. Variant modifiers control trigger surface treatment. Default is subtle. Copied feedback tints the input and trigger border and text only (surface stays on the UI tokens). Default copied palette is success; add a semantic modifier to override.
Semantic
Palette variables for clipboard ink and fill. Does not change surface treatment by itself.
| Modifier | Classes |
|---|---|
| Default (success on copy) | clipboard |
| Accent | clipboard ui-accent |
| Brand | clipboard ui-brand |
| Alert | clipboard ui-alert |
| Info | clipboard ui-info |
| Success | clipboard ui-success |
Variant
Visual treatment of the copy trigger. Combine with a semantic modifier for palette-driven ink and fill.
| Modifier | Classes |
|---|---|
| Subtle (default) | clipboard or clipboard ui-accent |
| Solid | clipboard ui-accent ui-solid |
Size
| Modifier | Classes |
|---|---|
| SM | clipboard ui-size-sm |
| MD | clipboard ui-size-md |
| LG | clipboard ui-size-lg |
| XL | clipboard ui-size-xl |
Summary
Components
Renders a clipboard component.
API
Copy the component's current value from a control (phx-click).
Copy the current value from handle_event.
Set the string to copy from a control (phx-click).
Set the value to copy from handle_event.
Components
Renders a clipboard component.
Attributes
id(:string) - The id of the clipboard, useful for API to identify the clipboard.value(:string) - The value shown in the input and copied (initial or after client set_value). Defaults tonil.timeout(:integer) - The timeout in milliseconds before resetting the copied state. Defaults tonil.dir(:string) - The direction of the clipboard. When nil, derived from document (html lang + config :rtl_locales). Defaults tonil. Must be one ofnil,"ltr", or"rtl".orientation(:string) - Layout orientation for CSS. Defaults to"horizontal". Must be one of"horizontal", or"vertical".on_copy(:string) - The server event name when the value is copied. Defaults tonil.on_copy_client(:string) - The client event name when the value is copied. Defaults tonil.trigger_aria_label(:string) - Accessible name for the trigger button when it contains only an icon (e.g. "Copy to clipboard"). Defaults tonil.input_aria_label(:string) - Accessible name for the input when it's not associated with a visible label (e.g. "Value to copy"). Defaults tonil.input(:boolean) - Whether to render the input element. Set to false when using only the trigger to copy. Defaults totrue.input_class(:string) - Defaults tonil.control_class(:string) - Defaults tonil.root_class(:string) - Defaults tonil.- Global attributes are accepted.
Slots
label- Accepts attributes:class(:string)
copy- Accepts attributes:class(:string)
copied- Accepts attributes:class(:string)
trigger- Accepts attributes:class(:string)
API
@spec copy(String.t()) :: Phoenix.LiveView.JS.t()
Copy the component's current value from a control (phx-click).
<.action phx-click={Corex.Clipboard.copy("my-clipboard")}>Copy</.action>
<.clipboard id="my-clipboard" class="clipboard" value="hello@example.com">
<:label>Email</:label>
<:copy><.heroicon name="hero-clipboard" /></:copy>
<:copied><.heroicon name="hero-check" /></:copied>
</.clipboard>document.getElementById("my-clipboard")?.dispatchEvent(
new CustomEvent("corex:clipboard:copy", { bubbles: false })
);
@spec copy(Phoenix.LiveView.Socket.t(), String.t()) :: Phoenix.LiveView.Socket.t()
Copy the current value from handle_event.
<.action phx-click="copy_email">Copy</.action>
<.clipboard id="my-clipboard" class="clipboard" value="hello@example.com">
<:label>Email</:label>
<:copy><.heroicon name="hero-clipboard" /></:copy>
<:copied><.heroicon name="hero-check" /></:copied>
</.clipboard>def handle_event("copy_email", _, socket) do
{:noreply, Corex.Clipboard.copy(socket, "my-clipboard")}
end
@spec set_value(String.t(), String.t()) :: Phoenix.LiveView.JS.t()
Set the string to copy from a control (phx-click).
<.action phx-click={Corex.Clipboard.set_value("my-clipboard", "next@example.com")}>Load</.action>
<.clipboard id="my-clipboard" class="clipboard" value="hello@example.com">
<:label>Email</:label>
<:copy><.heroicon name="hero-clipboard" /></:copy>
<:copied><.heroicon name="hero-check" /></:copied>
</.clipboard>document.getElementById("my-clipboard")?.dispatchEvent(
new CustomEvent("corex:clipboard:set-value", {
bubbles: false,
detail: { value: "next@example.com" },
})
);
@spec set_value(Phoenix.LiveView.Socket.t(), String.t(), String.t()) :: Phoenix.LiveView.Socket.t()
Set the value to copy from handle_event.
<.action phx-click="load_clipboard" phx-value-value="next@example.com">Load</.action>
<.clipboard id="my-clipboard" class="clipboard" value="hello@example.com">
<:label>Email</:label>
<:copy><.heroicon name="hero-clipboard" /></:copy>
<:copied><.heroicon name="hero-check" /></:copied>
</.clipboard>def handle_event("load_clipboard", %{"value" => value}, socket) do
{:noreply, Corex.Clipboard.set_value(socket, "my-clipboard", value)}
end