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

95.34% Statements 41/43
100% Branches 25/25
90% Functions 18/20
94.73% Lines 36/38

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113  37x                   5x 3x             3x     1x       5x 5x                   5x 72x                 72x                                   15x   15x   29x 15x   15x 26x 47x 2x   26x     15x 15x   15x 15x   22x 22x       15x 26x 26x 26x   26x           15x 15x 15x     22x 15x   15x    
<template>
    <nav v-if="sections.length" :aria-label="title ?? $t('articleToc.title')" class="print:hidden">
        <UAccordion
            :items="[{ label: title ?? $t('articleToc.title'), slot: 'toc' as const }]"
            :ui="{
                trigger: 'min-h-12 font-sans text-sm font-semibold text-highlighted',
                body: 'pb-4',
            }"
            class="max-w-measure rounded-xs border border-default bg-muted px-4 lg:hidden"
        >
            <template #toc-body>
                <ol class="flex flex-col border-l border-default">
                    <li v-for="item in sections" :key="item.id">
                        <ULink
                            :to="`#${item.id}`"
                            raw
                            @click="track('toc_click', {})"
                            class="-ml-px flex min-h-11 items-center border-l-2 border-transparent pl-4 font-sans text-sm text-toned focus-visible:outline-offset-0"
                        >
                            {{ item.label }}
                        </ULink>
                    </li>
                </ol>
            </template>
        </UAccordion>
 
        <div class="sticky top-8 hidden lg:block">
            <p class="text-kicker">
                {{ title ?? $t("articleToc.title") }}
            </p>
            <div ref="rail" class="relative mt-3">
                <span
                    v-if="indicator"
                    aria-hidden="true"
                    class="absolute top-0 -left-px w-0.5 rounded-full bg-primary transition-[translate,height] duration-200 ease-out motion-reduce:transition-none"
                    :style="{ height: `${indicator.height}px`, translate: `0 ${indicator.top}px` }"
                />
                <ol class="flex flex-col border-l border-default">
                    <li v-for="item in sections" :key="item.id">
                        <ULink
                            :to="`#${item.id}`"
                            raw
                            @click="track('toc_click', {})"
                            :data-lit="lit.includes(item.id) || undefined"
                            :aria-current="lit[0] === item.id ? 'location' : undefined"
                            class="flex min-h-11 items-center py-1 pl-4 font-sans text-sm/snug text-muted transition-colors hover:text-highlighted focus-visible:outline-offset-0 data-lit:font-semibold data-lit:text-highlighted"
                        >
                            {{ item.label }}
                        </ULink>
                    </li>
                </ol>
            </div>
        </div>
    </nav>
</template>
 
<script setup lang="ts">
import { useScrollspy } from "@nuxt/ui/composables/useScrollspy";
import { useResizeObserver } from "@vueuse/core";
 
interface Props {
    items: TocItem[];
    title?: string;
}
 
const props = defineProps<Props>();
 
const track = useTrack();
 
const sections = computed(() => props.items.filter(({ level }) => level === 2));
const { activeHeadings, updateHeadings } = useScrollspy();
 
const lit = computed(() => {
    const visible = sections.value
        .filter(({ id }) => activeHeadings.value.includes(id))
        .map(({ id }) => id);
 
    return visible.length ? visible : sections.value.slice(0, 1).map(({ id }) => id);
});
 
const rail = useTemplateRef("rail");
const indicator = shallowRef<{ top: number; height: number }>();
 
const observeHeadings = (): void => {
    updateHeadings(
        sections.value
            .map(({ id }) => document.getElementById(id))
            .filter((heading) => heading !== null),
    );
};
 
const measure = (): void => {
    const links = [...(rail.value?.querySelectorAll<HTMLElement>("[data-lit]") ?? [])];
    const first = links.at(0);
    const last = links.at(-1);
 
    indicator.value =
        first && last
            ? { top: first.offsetTop, height: last.offsetTop + last.offsetHeight - first.offsetTop }
            : undefined;
};
 
onMounted(() => {
    observeHeadings();
    measure();
});
 
watch(() => sections.value.map(({ id }) => id), observeHeadings, { flush: "post" });
watch(lit, measure, { flush: "post" });
 
useResizeObserver(rail, measure);
</script>