Sha256: ae3590acdf49f15e1ab220c4e3f676a856ecbffdebb9bc2c16ef4b785673c95f

Contents?: true

Size: 775 Bytes

Versions: 10

Compression:

Stored size: 775 Bytes

Contents

import { Controller } from "@hotwired/stimulus"
import { debounce } from "helpers/timing_helpers"

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

10 entries across 10 versions & 1 rubygems

Version Path
css-zero-0.0.50 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.49 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.48 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.47 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.46 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.45 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.44 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.43 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.42 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.41 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js