Sha256: 8ef6472de2474ecd1086e4582dcba07c60994d2dc03a4342869358de1693d8d6

Contents?: true

Size: 798 Bytes

Versions: 2

Compression:

Stored size: 798 Bytes

Contents

import { Controller } from "@hotwired/stimulus"
import { debounce } from "https://cdn.skypack.dev/lodash-es@4.17.21?min"

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

2 entries across 2 versions & 1 rubygems

Version Path
css-zero-0.0.72 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.71 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js