All files / apps/huella-legal/layers/reading/app/components/article ShareBar.vue

100% Statements 25/25
100% Branches 12/12
100% Functions 3/3
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76  20x                                     20x                                             20x   20x 20x 20x 20x 20x   20x 5x 4x 4x 1x 1x     3x       3x   2x 2x 2x     20x 1x 1x      
<template>
    <div class="flex items-center print:hidden">
        <UButton
            v-if="citeTo"
            variant="ghost"
            color="neutral"
            icon="i-lucide-quote"
            :label="$t('shareBar.cite')"
            :to="citeTo"
            class="px-3"
        />
        <ClientOnly>
            <UButton
                v-if="canNativeShare || canCopy"
                square
                variant="ghost"
                color="neutral"
                icon="i-lucide-share"
                :aria-label="$t('shareBar.share')"
                @click="share"
            />
        </ClientOnly>
        <UButton
            square
            variant="ghost"
            color="neutral"
            icon="i-lucide-printer"
            :aria-label="$t('shareBar.print')"
            @click="print"
        />
    </div>
</template>
 
<script setup lang="ts">
import { useClipboard, useShare } from "@vueuse/core";
import type { RouteLocationRaw } from "vue-router";
 
interface Props {
    title: string;
    url: string;
    citeTo?: RouteLocationRaw;
}
 
const props = defineProps<Props>();
 
const { t } = useI18n();
const toast = useToast();
const { share: nativeShare, isSupported: canNativeShare } = useShare();
const { copy, isSupported: canCopy } = useClipboard();
const track = useTrack();
 
const share = async (): Promise<void> => {
    if (canNativeShare.value) {
        try {
            await nativeShare({ title: props.title, url: props.url });
            track("share", { method: "native" });
            return;
        } catch (error) {
            // Dismissing the sheet is the reader's choice
            if (error instanceof DOMException && error.name === "AbortError") return;
        }
    }
 
    if (!canCopy.value) return;
 
    await copy(props.url);
    track("share", { method: "copy_link" });
    toast.add({ title: t("shareBar.linkCopied"), icon: "i-lucide-check" });
};
 
const print = (): void => {
    track("share", { method: "print" });
    window.print();
};
</script>