Sha256: 22337cad7c1f98dc7d8b9f9906a92258f4f295c6c736f62136745c22886e2d5b

Contents?: true

Size: 1.29 KB

Versions: 16

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', function ({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

16 entries across 16 versions & 1 rubygems

Version Path
primer_view_components-0.0.93 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.73 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.72 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.71 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.70 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.69 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.68 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.67 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.66 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.65 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.64 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.63 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.62 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.61 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.60 app/components/primer/clipboard_copy_component.ts
primer_view_components-0.0.59 app/components/primer/clipboard_copy_component.ts