Sha256: 28120592369c73ff1e79680b5d01f29aa5deb983f9b47d525618d1160c260528

Contents?: true

Size: 1.29 KB

Versions: 87

Compression:

Stored size: 1.29 KB

Contents

import '@github/clipboard-copy-element'

const CLIPBOARD_COPY_TIMER_DURATION = 2000

function showSVG(svg: SVGElement) {
  svg.style.display = 'inline-block'
}

function hideSVG(svg: SVGElement) {
  svg.style.display = 'none'
}

// Toggle a copy button.
function showCopy(button: HTMLElement) {
  const [copyIcon, checkIcon] = button.querySelectorAll<SVGElement>('.octicon')

  if (!copyIcon || !checkIcon) return

  showSVG(copyIcon)
  hideSVG(checkIcon)
}

// Toggle a copy button.
function showCheck(button: HTMLElement) {
  const [copyIcon, checkIcon] = button.querySelectorAll<SVGElement>('.octicon')

  if (!copyIcon || !checkIcon) return

  hideSVG(copyIcon)
  showSVG(checkIcon)
}

const clipboardCopyElementTimers = new WeakMap<HTMLElement, number>()

document.addEventListener('clipboard-copy', ({target}) => {
  if (!(target instanceof HTMLElement)) return
  if (!target.hasAttribute('data-view-component')) return

  const currentTimeout = clipboardCopyElementTimers.get(target)

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

  clipboardCopyElementTimers.set(
    target,
    setTimeout(() => {
      showCopy(target)
      clipboardCopyElementTimers.delete(target)
    }, CLIPBOARD_COPY_TIMER_DURATION)
  )
})

Version data entries

87 entries across 87 versions & 3 rubygems

Version Path
primer_view_components-0.0.100 app/components/primer/clipboard_copy.ts
primer_view_components-0.0.99 app/components/primer/clipboard_copy.ts
primer_view_components-0.0.98 app/components/primer/clipboard_copy.ts
primer_view_components-0.0.97 app/components/primer/clipboard_copy.ts
primer_view_components-0.0.96 app/components/primer/clipboard_copy.ts
primer_view_components-0.0.95 app/components/primer/clipboard_copy.ts
primer_view_components-0.0.94 app/components/primer/clipboard_copy.ts