Skip to content

Commit

Permalink
Add alchemy-clipboard-button component
Browse files Browse the repository at this point in the history
Encapsulates ClipboardJS into a web component.
This makes sure it sets up and tears down correctly
without any imperative code even in dialogs.
  • Loading branch information
tvdeyen committed Jan 11, 2024
1 parent bb89883 commit 0125ec7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/javascript/alchemy_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import PagePublicationFields from "alchemy_admin/page_publication_fields"
// Web Components
import "alchemy_admin/components/button"
import "alchemy_admin/components/char_counter"
import "alchemy_admin/components/clipboard_button"
import "alchemy_admin/components/datepicker"
import "alchemy_admin/components/dialog_link"
import "alchemy_admin/components/element_editor"
Expand All @@ -33,7 +34,6 @@ import "alchemy_admin/components/page_select"
import "alchemy_admin/components/select"
import "alchemy_admin/components/spinner"
import "alchemy_admin/components/tinymce"
import "alchemy_admin/clipboard"

import { setDefaultAnimation } from "shoelace"

Expand Down
16 changes: 0 additions & 16 deletions app/javascript/alchemy_admin/clipboard.js

This file was deleted.

28 changes: 28 additions & 0 deletions app/javascript/alchemy_admin/components/clipboard_button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "clipboard"

class ClipboardButton extends HTMLElement {
constructor() {
super()

this.innerHTML = `
<i class="icon ri-clipboard-line ri-fw"></i>
`

this.clipboard = new ClipboardJS(this, {
target: this,
text: () => {
return this.getAttribute("content")
}
})

this.clipboard.on("success", (evt) => {
Alchemy.growl(this.getAttribute("success-text"))
})
}

disconnectedCallback() {
this.clipboard.destroy()
}
}

customElements.define("alchemy-clipboard-button", ClipboardButton)
16 changes: 10 additions & 6 deletions app/views/alchemy/admin/attachments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
<div class="value with-icon">
<label><%= Alchemy::Attachment.human_attribute_name(:url) %></label>
<p><%= @attachment.url %></p>
<a data-clipboard-text="<%= @attachment.url %>" data-clipboard-success-text="<%= Alchemy.t("Copied to clipboard") %>" class="icon_button--right">
<%= render_icon(:clipboard) %>
</a>
<alchemy-clipboard-button
content="<%= @attachment.url %>"
success-text="<%= Alchemy.t("Copied to clipboard") %>"
class="icon_button--right"
></alchemy-clipboard-button>
</div>
<div class="value with-icon">
<label><%= Alchemy::Attachment.human_attribute_name(:download_url) %></label>
<p><%= @attachment.url(download: true) %></p>
<a data-clipboard-text="<%= @attachment.url(download: true) %>" data-clipboard-success-text="<%= Alchemy.t("Copied to clipboard") %>" class="icon_button--right">
<%= render_icon(:clipboard) %>
</a>
<alchemy-clipboard-button
content="<%= @attachment.url(download: true) %>"
success-text="<%= Alchemy.t("Copied to clipboard") %>"
class="icon_button--right"
></alchemy-clipboard-button>
</div>
</div>

Expand Down

0 comments on commit 0125ec7

Please sign in to comment.