Sha256: 78c04253657e694e4ba00fbad8ada604ea0629fe602717ae87f122254f95dcab

Contents?: true

Size: 920 Bytes

Versions: 19

Compression:

Stored size: 920 Bytes

Contents

import { Controller } from "@hotwired/stimulus"

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
  }
}

// Helpers

function debounce(fn, delay = 1000) {
  let timeoutId = null

  return (...args) => {
    clearTimeout(timeoutId)
    timeoutId = setTimeout(() => fn.apply(this, args), delay)
  }
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
css-zero-0.0.69 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.68 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.67 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.66 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.65 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.64 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.63 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.62 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.61 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.60 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.59 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.58 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.57 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.56 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.55 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.54 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.53 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.52 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js
css-zero-0.0.51 lib/generators/css_zero/add/templates/app/javascript/controllers/copyable_input_controller.js