Sha256: 67b8701922dbef9600ff6fb3d7ceab9722ed99dfc8cc08249e6378ee8854dd99

Contents?: true

Size: 796 Bytes

Versions: 16

Compression:

Stored size: 796 Bytes

Contents

import { Controller } from "@hotwired/stimulus"
import debounce from "https://esm.sh/lodash.debounce@4.0.8?standalone"

export default class extends Controller {
  static targets = [ "input", "copyIcon", "successIcon" ]
  static values  = { copied: Boolean }

  initialize() {
    this.reset = debounce(this.reset.bind(this), 2500)
  }

  copiedValueChanged() {
    this.#update()
  }

  copy() {
    this.#copyToClipboard()
    this.reset()
  }

  reset() {
    this.copiedValue = false
  }

  #copyToClipboard() {
    try {
      navigator.clipboard.writeText(this.inputTarget.value)
      this.copiedValue = true
    } catch {
      this.copiedValue = false
    }
  }

  #update() {
    this.copyIconTarget.hidden = this.copiedValue
    this.successIconTarget.hidden = !this.copiedValue
  }
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
css-zero-1.0.4 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-1.0.3 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-1.0.2 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-1.0.1 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-1.0.0 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.98 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.97 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.96 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.95 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.94 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.93 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.92 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.91 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.90 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.89 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.88 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js