Files
archie/ui/src/components/topbar/Nav.vue
T

46 lines
1.7 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import { useRoute } from "vue-router";
import { activeNavPath, navTree, type NavEntry } from "@/lib/nav";
const props = defineProps<{ hidden: string[] }>();
const route = useRoute();
const root = ref<HTMLElement | null>(null);
const links = computed(() =>
navTree(props.hidden).flatMap((node): NavEntry[] => (node.kind === "link" ? [node.entry] : [])),
);
const activePath = computed(() => activeNavPath(route.path));
// The strip scrolls sideways on a narrow window, so the current section can sit
// outside it. Centre it after each route change; block:"nearest" keeps it from
// scrolling the page too.
function revealActive(): void {
root.value
?.querySelector('[aria-current="page"]')
?.scrollIntoView({ inline: "center", block: "nearest" });
}
onMounted(revealActive);
watch(activePath, revealActive, { flush: "post" });
</script>
<template>
<nav
ref="root"
aria-label="Sections"
class="scroll-fade-x flex min-w-0 flex-1 items-center gap-0.5 overflow-x-auto [scrollbar-width:none]"
>
<RouterLink
v-for="entry in links"
:key="entry.path"
:to="entry.path"
:aria-current="activePath === entry.path ? 'page' : undefined"
:data-current="activePath === entry.path ? '' : undefined"
class="text-fg-muted hover:text-foreground hover:bg-accent data-current:text-foreground data-current:bg-accent focus-visible:ring-ring/50 inline-flex h-8 shrink-0 items-center rounded-md px-2.5 text-label font-medium whitespace-nowrap transition-colors outline-none focus-visible:ring-[3px]"
>{{ entry.label }}</RouterLink
>
</nav>
</template>