-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathindex.tsx
84 lines (76 loc) · 2.41 KB
/
index.tsx
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
import type { FC } from 'react';
import { useMemo } from 'react';
import BreadcrumbHomeLink from '@node-core/ui-components/Common/Breadcrumbs/BreadcrumbHomeLink';
import BreadcrumbItem from '@node-core/ui-components/Common/Breadcrumbs/BreadcrumbItem';
import BreadcrumbLink from '@node-core/ui-components/Common/Breadcrumbs/BreadcrumbLink';
import BreadcrumbRoot from '@node-core/ui-components/Common/Breadcrumbs/BreadcrumbRoot';
import BreadcrumbTruncatedItem from '@node-core/ui-components/Common/Breadcrumbs/BreadcrumbTruncatedItem';
import type {
FormattedMessage,
LinkLike,
} from '@node-core/ui-components/types';
export type BreadcrumbLink = {
label: FormattedMessage;
href: string | undefined;
};
type BreadcrumbsProps = {
links: Array<BreadcrumbLink>;
maxLength?: number;
hideHome?: boolean;
as: LinkLike;
homeLinkAriaLabel?: string;
};
const Breadcrumbs: FC<BreadcrumbsProps> = ({
links = [],
maxLength = 5,
hideHome = false,
as = 'a',
homeLinkAriaLabel,
}) => {
const totalLength = links.length + +!hideHome;
const lengthOffset = maxLength - totalLength;
const isOverflow = lengthOffset < 0;
const items = useMemo(
() =>
links.map((link, index, items) => {
const position = index + 1;
const isLastItem = index === items.length - 1;
const hidden =
// We add 1 here to take into account of the truncated breadcrumb.
position <= Math.abs(lengthOffset) + 1 && isOverflow && !isLastItem;
return (
<BreadcrumbItem
key={link.label.toString()}
hidden={hidden}
hideSeparator={isLastItem}
position={position + +!hideHome}
>
{link.href || isLastItem ? (
<BreadcrumbLink
as={as}
href={link.href || undefined}
active={isLastItem}
>
{link.label}
</BreadcrumbLink>
) : (
<span className="opacity-70">{link.label}</span>
)}
</BreadcrumbItem>
);
}),
[hideHome, isOverflow, lengthOffset, links]
);
return (
<BreadcrumbRoot>
{!hideHome && (
<BreadcrumbItem position={1}>
<BreadcrumbHomeLink as={as} aria-label={homeLinkAriaLabel} />
</BreadcrumbItem>
)}
{isOverflow && <BreadcrumbTruncatedItem />}
{items}
</BreadcrumbRoot>
);
};
export default Breadcrumbs;