From d3313a202b02109a50ab7addf6a2db8a2fb0c59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Mon, 9 Jan 2023 17:35:20 +0800 Subject: [PATCH] Add a clipboard controller and wire up the copy button --- .../controllers/clipboard_controller.js | 16 ++++++++++++++++ app/views/icons/_check.html.erb | 2 +- app/views/settings/profile/index.html.erb | 10 ++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 app/javascript/controllers/clipboard_controller.js diff --git a/app/javascript/controllers/clipboard_controller.js b/app/javascript/controllers/clipboard_controller.js new file mode 100644 index 0000000..e641010 --- /dev/null +++ b/app/javascript/controllers/clipboard_controller.js @@ -0,0 +1,16 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static targets = ["source", "trigger"] + + copy (event) { + event.preventDefault(); + navigator.clipboard.writeText(this.sourceTarget.value); + this.triggerTarget.querySelector('.content-initial').classList.add('hidden'); + this.triggerTarget.querySelector('.content-active').classList.remove('hidden'); + setTimeout(() => { + this.triggerTarget.querySelector('.content-initial').classList.remove('hidden'); + this.triggerTarget.querySelector('.content-active').classList.add('hidden'); + }, 2000) + } +} diff --git a/app/views/icons/_check.html.erb b/app/views/icons/_check.html.erb index 1c20989..d1b699c 100644 --- a/app/views/icons/_check.html.erb +++ b/app/views/icons/_check.html.erb @@ -1 +1 @@ - \ No newline at end of file + diff --git a/app/views/settings/profile/index.html.erb b/app/views/settings/profile/index.html.erb index 7a8dd33..b166cee 100644 --- a/app/views/settings/profile/index.html.erb +++ b/app/views/settings/profile/index.html.erb @@ -6,15 +6,17 @@

<%= label :user_address, 'User address', class: 'font-bold' %>

-

+

disabled="disabled" /> + value=<%= @user.address %> disabled="disabled" + data-clipboard-target="source" />