Corex. Clipboard
(Corex v0.1.0-rc.0)
View Source
Phoenix implementation of 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/main.css";
@import "../corex/tokens/themes/neo/light.css";
@import "../corex/components/clipboard.css";Stack modifiers on the host (class on <.clipboard>).
Color
| Modifier | Classes |
|---|---|
| Default | clipboard |
| Accent | clipboard clipboard--accent |
| Brand | clipboard clipboard--brand |
| Alert | clipboard clipboard--alert |
| Info | clipboard clipboard--info |
| Success | clipboard clipboard--success |
Size
| Modifier | Classes |
|---|---|
| SM | clipboard clipboard--sm |
| MD | clipboard clipboard--md |
| LG | clipboard clipboard--lg |
| XL | clipboard clipboard--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
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 })
);
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
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" },
})
);
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