Skip to content

Commit a1d8b1d

Browse files
committed
fix(useBreadcrumbItems): allow overrides, append and prepend
1 parent 6b0d7fd commit a1d8b1d

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

src/runtime/nuxt/composables/useBreadcrumbItems.ts

+46-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { withoutTrailingSlash } from 'ufo'
22
import type { RouteMeta } from 'vue-router'
33
import type { MaybeRefOrGetter } from 'vue'
44
import type { BreadcrumbLink } from '@nuxt/ui/dist/runtime/types'
5+
import { defu } from 'defu'
56
import { pathBreadcrumbSegments } from '../../pure/breadcrumbs'
67
import {
78
computed,
@@ -15,8 +16,33 @@ import {
1516
} from '#imports'
1617

1718
export interface BreadcrumbProps {
19+
/**
20+
* Generate the breadcrumbs based on a different path than the current route.
21+
*/
1822
path?: MaybeRefOrGetter<string>
23+
/**
24+
* The id of the breadcrumb list. It's recommended to provide a unique
25+
* id when adding multiple breadcrumb lists to the same page.
26+
*/
1927
id?: string
28+
/**
29+
* Append additional breadcrumb items to the end of the list. This is applied
30+
* after the `overrides` option.
31+
*/
32+
append?: BreadcrumbItemProps[]
33+
/**
34+
* Prepend additional breadcrumb items to the start of the list. This is applied
35+
* after the `overrides` option.
36+
*/
37+
prepend?: BreadcrumbItemProps[]
38+
/**
39+
* Override any of the breadcrumb items based on the index.
40+
*/
41+
overrides?: (BreadcrumbItemProps | false | undefined)[]
42+
/**
43+
* Should the schema.org breadcrumb be generated.
44+
* @default true
45+
*/
2046
schemaOrg?: boolean
2147
/**
2248
* The Aria Label for the breadcrumbs.
@@ -83,10 +109,26 @@ export function useBreadcrumbItems(options: BreadcrumbProps = {}) {
83109
rootNode = `/${i18n.defaultLocale.value}`
84110
}
85111
const current = withoutQuery(withoutTrailingSlash(toValue(options.path || useRoute().path) || rootNode))
86-
return pathBreadcrumbSegments(current, rootNode)
87-
.map(path => ({
88-
to: path,
89-
}) as BreadcrumbItemProps)
112+
// apply overrides
113+
const overrides = options.overrides || []
114+
const segments = pathBreadcrumbSegments(current, rootNode)
115+
.map((path, index) => {
116+
let item = <BreadcrumbItemProps> {
117+
to: path,
118+
}
119+
if (typeof overrides[index] !== 'undefined') {
120+
if (overrides[index] === false)
121+
return false
122+
item = defu(overrides[index] as any as BreadcrumbItemProps, item)
123+
}
124+
return item
125+
})
126+
// apply prepends and appends
127+
if (options.prepend)
128+
segments.unshift(...options.prepend)
129+
if (options.append)
130+
segments.push(...options.append)
131+
return (segments.filter(Boolean) as BreadcrumbItemProps[])
90132
.map((item) => {
91133
const route = routes.find(r => withoutTrailingSlash(r.path) === withoutTrailingSlash(item.to))
92134
const routeMeta = (route?.meta || {}) as RouteMeta & { title?: string, breadcrumbLabel: string }

0 commit comments

Comments
 (0)