Sha256: f81ab0d83920d7781a3a7640479d5109dd6043ef6be47f57d412e5ab72d2f992

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

import {controllerFactory} from '@utils/createController'

import '@github/clipboard-copy-element'

const CLIPBOARD_COPY_TIMER_DURATION = 2000

export default class ClipboardCopyController extends controllerFactory<HTMLDetailsElement>()({
  targets: {
    initial: HTMLElement,
    confirmed: null,
  },
  values: {
    hiddenClass: String,
    showClass: String,
  },
}) {
  declare clipboardCopyElementTimers: WeakMap<HTMLElement, number>

  connect() {
    this.clipboardCopyElementTimers = new WeakMap()
  }

  copy(event: Event) {
    const target = event.target as HTMLElement
    const currentTimeout = this.clipboardCopyElementTimers.get(target)

    if (currentTimeout) {
      clearTimeout(currentTimeout)
      this.clipboardCopyElementTimers.delete(target)
    } else {
      this.showConfirm()
    }

    this.clipboardCopyElementTimers.set(
      target,
      window.setTimeout(() => {
        this.showInitial()
        this.clipboardCopyElementTimers.delete(target)
      }, CLIPBOARD_COPY_TIMER_DURATION),
    )
  }

  showConfirm() {
    if (this.hasConfirmedTarget) {
      this.confirmedTarget.classList.remove(this.hiddenClassValue)
      this.confirmedTarget.classList.add(this.showClassValue)
      this.initialTarget.classList.add(this.hiddenClassValue)
    }
  }

  showInitial() {
    this.initialTarget.classList.remove(this.hiddenClassValue)
    this.initialTarget.classList.add(this.showClassValue)
    if (this.hasConfirmedTarget) {
      this.confirmedTarget.classList.add(this.hiddenClassValue)
    }
  }
}

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.93.2 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.93.1 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.93 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.92 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.91 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.90 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.89.1 app/components/ariadne/ui/clipboard_copy/component.ts