feat: breadcrumbs component

- <SeoBreadcrumbs> (UI-SPEC L1): the resolve answer's trail as nav > ol
  with server-rendered links and the last item as aria-current text;
  nothing with fewer than two items, with page meta seo.breadcrumbs false
  or with the option seo.breadcrumbs false; props items, label,
  hideCurrent, locale
- one trail per page: the first rendered instance claims it
  (seoClaimBreadcrumbs, kept per path)
- seoSitePath: same-origin CMS URLs become paths, non-http(s) URLs are
  never links; useSeoLabels takes an optional locale
- L1 default styles with CSS-generated separators
This commit is contained in:
Jakub Zych
2026-11-04 11:59:17 +01:00
parent 3abc986f42
commit ebb0c8084e
9 changed files with 222 additions and 6 deletions

View File

@@ -0,0 +1,80 @@
<script setup lang="ts">
// The visible breadcrumb trail (UI-SPEC L1, D-86): the resolve answer's
// breadcrumbs, the same list the CMS puts in BreadcrumbList, as nav > ol
// with server-rendered links and the current page as text. Renders nothing
// with fewer than two items (the home page), when the page meta sets
// seo: { breadcrumbs: false } or when the option seo.breadcrumbs is false.
// Only the first instance rendered for a page shows its trail, so an app
// may place one in its layout while a layer's page renders one too.
// Separators are CSS-generated (seo-base.css), never DOM characters. Links
// use NuxtLink's custom slot, so only the last item carries aria-current.
import { seoClaimBreadcrumbs, seoReleaseBreadcrumbs, type SeoBreadcrumbClaim } from '../../utils/breadcrumbClaim.ts'
import { seoSitePath } from '../../utils/sitePath.ts'
import type { SeoBreadcrumb } from '../../utils/types.ts'
const props = withDefaults(
defineProps<{
/** Overrides the resolve answer's trail. */
items?: SeoBreadcrumb[] | null
/** Overrides the breadcrumbs.label label (the nav's aria-label). */
label?: string
/** Leaves the last item (the current page) out of the visible list. */
hideCurrent?: boolean
/** The labels' locale (default: the page's content locale). */
locale?: string
}>(),
{ items: null, label: '', hideCurrent: false, locale: '' },
)
const config = useRuntimeConfig()
const route = useRoute()
const answer = useSeoAnswer()
const labels = useSeoLabels(() => props.locale)
const claim = useState<SeoBreadcrumbClaim | null>('seo-layer:breadcrumbs', () => null)
const id = useId() ?? 'seo-breadcrumbs'
const trail = computed(() => {
const source = props.items ?? (answer.value?.action === 'ok' ? answer.value.breadcrumbs : null) ?? []
const origin = answer.value?.url
return (Array.isArray(source) ? source : [])
.filter((item) => item && typeof item.name === 'string' && item.name !== '')
.map((item) => ({ name: item.name, to: (typeof item.path === 'string' && item.path.startsWith('/') && !item.path.startsWith('//') ? item.path : '') || seoSitePath(item.url, origin) }))
})
const enabled = computed(() => {
const option = (config.public.seo as { breadcrumbs?: unknown } | undefined)?.breadcrumbs
const meta = (route.meta as { seo?: { breadcrumbs?: unknown } }).seo?.breadcrumbs
return option !== false && option !== 'false' && meta !== false
})
const shown = computed(() => enabled.value && trail.value.length >= 2)
const owns = ref(false)
const take = () => {
if (!shown.value) {
claim.value = seoReleaseBreadcrumbs(claim.value, id)
owns.value = false
return
}
const next = seoClaimBreadcrumbs(claim.value, route.path, id)
claim.value = next.state
owns.value = next.owns
}
take()
watch([() => route.path, shown], take)
const visible = computed(() => (props.hideCurrent ? trail.value.slice(0, -1) : trail.value))
</script>
<template>
<nav v-if="owns && shown" class="seo-breadcrumbs" :aria-label="label || labels.breadcrumbs.label">
<ol class="seo-breadcrumbs__list">
<li v-for="(item, i) in visible" :key="i" class="seo-breadcrumbs__item">
<span v-if="!hideCurrent && i === visible.length - 1" class="seo-breadcrumbs__current" aria-current="page">{{ item.name }}</span>
<NuxtLink v-else-if="item.to" v-slot="link" :to="item.to" custom>
<a class="seo-breadcrumbs__link" :href="link.href" @click="link.navigate">{{ item.name }}</a>
</NuxtLink>
<span v-else class="seo-breadcrumbs__text">{{ item.name }}</span>
</li>
</ol>
</nav>
</template>