<script>
  import { Tooltip } from 'sveltestrap';
  const uuid=Math.random().toString(36).replace('0.','ctc');
  const canCopy = navigator.clipboard && navigator.clipboard.writeText;
  export let text;
  export let force = false;
  const click = ()=> {
    navigator.clipboard.writeText(text)
      .then(()=>{
	justCopied = true;
      })
  }
  let justCopied=false;
  let isOpen;
  $: if (justCopied && !isOpen) justCopied = false;
</script>

{#if canCopy||force}
  <slot id="{uuid}" action="{click}"/>
  {#if canCopy}
    <Tooltip target={uuid} bind:isOpen>
      {#if justCopied}
	copied!
      {:else}
	click to copy
      {/if}  
    </Tooltip>
  {/if}
{/if}
