Sha256: 01df9b2b2ba59a56e36b463d224ad338d84b6bd22c82a13ca51f82a81ee2ce6b

Contents?: true

Size: 1.38 KB

Versions: 41

Compression:

Stored size: 1.38 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,
  },
}) {
  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('hidden')
      this.confirmedTarget.classList.add('inline-block')
      this.initialTarget.classList.add('hidden')
    }
  }

  showInitial() {
    this.initialTarget.classList.remove('hidden')
    this.initialTarget.classList.add('inline-block')
    if (this.hasConfirmedTarget) {
      this.confirmedTarget.classList.add('hidden')
    }
  }
}

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.88 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.87 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86.9 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86.8 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86.7 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86.6 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86.5 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86.4 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86.3 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86.2 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86.1 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.86 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.85 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.84 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.83.3 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.83.2 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.83.1 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.83 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.82 app/components/ariadne/ui/clipboard_copy/component.ts
ariadne_view_components-0.0.81 app/components/ariadne/ui/clipboard_copy/component.ts