Skip to content

Commit

Permalink
feat(ui-breadcrumb): add backLink, Link & improve the mobile version
Browse files Browse the repository at this point in the history
  • Loading branch information
AliKdhim87 committed Feb 8, 2024
1 parent a006948 commit 103c458
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
6 changes: 6 additions & 0 deletions packages/ui/src/components/Breadcrumb/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.utrecht-breadcrumb-nav__item-wrapper {
--utrecht-icon-inset-block-start: 2px;

align-items: baseline;
display: inline-flex;
}
74 changes: 46 additions & 28 deletions packages/ui/src/components/Breadcrumb/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { BreadcrumbNav, BreadcrumbNavLink, BreadcrumbNavSeparator } from '@utrecht/component-library-react';
import {
BreadcrumbNav,
BreadcrumbNavLink,
BreadcrumbNavSeparator,
Link as UtrechtLink,
} from '@utrecht/component-library-react';
import { UtrechtIconChevronLeft, UtrechtIconChevronRight } from '@utrecht/web-component-library-react';
import Link from 'next/link';
import { Fragment } from 'react';
import classnames from 'classnames/bind';
import { ComponentType } from 'react';
import styles from './index.module.scss';
import { useScreenSize } from '../../hooks';

type BreadcrumbLinkType = {
Expand All @@ -11,33 +17,38 @@ type BreadcrumbLinkType = {
};
interface BreadcrumbProps {
links: BreadcrumbLinkType[];
Link?: ComponentType<any>;
backLink?: {
href: string;
label: string;
current: boolean;
};
breakpoint?: number;
}
const css = classnames.bind(styles);

export const Breadcrumbs = ({ links }: BreadcrumbProps) => {
export const Breadcrumbs = ({ links, Link = UtrechtLink, backLink, breakpoint = 360 }: BreadcrumbProps) => {
const screenSize = useScreenSize();

const currentIndex = links.findIndex((link) => link.current);
const smallScreen = Number(screenSize) <= breakpoint;

const previousLink = links[currentIndex - 1];

const smallScreen = Number(screenSize) <= 360;

if (smallScreen) {
if (smallScreen && backLink?.href && backLink.label) {
return (
<BreadcrumbNav>
<BreadcrumbNavSeparator>
<UtrechtIconChevronLeft />
</BreadcrumbNavSeparator>
<BreadcrumbNavLink
className="utrecht-link utrecht-link--html-a"
href={previousLink.href}
rel={previousLink.href === '/' ? 'home' : 'up'}
index={currentIndex}
current={previousLink.current}
Link={Link}
>
{previousLink.label}
</BreadcrumbNavLink>
<div className={css('utrecht-breadcrumb-nav__item-wrapper')}>
<BreadcrumbNavSeparator>
<UtrechtIconChevronLeft />
</BreadcrumbNavSeparator>
<BreadcrumbNavLink
className="utrecht-link utrecht-link--html-a"
href={backLink.href}
rel={backLink.href === '/' ? 'home' : 'up'}
current={backLink.current}
Link={Link}
>
{backLink.label}
</BreadcrumbNavLink>
</div>
</BreadcrumbNav>
);
}
Expand All @@ -47,7 +58,12 @@ export const Breadcrumbs = ({ links }: BreadcrumbProps) => {
links.length > 0 &&
links.map(({ href, label, current }: any, index: number) =>
label ? (
<Fragment key={`${href}-${index}`}>
<div key={`${href}-${index}`} className={css('utrecht-breadcrumb-nav__item-wrapper')}>
{links.length === 1 && (
<BreadcrumbNavSeparator>
<UtrechtIconChevronLeft />
</BreadcrumbNavSeparator>
)}
<BreadcrumbNavLink
className="utrecht-link utrecht-link--html-a"
href={href}
Expand All @@ -58,10 +74,12 @@ export const Breadcrumbs = ({ links }: BreadcrumbProps) => {
>
{label}
</BreadcrumbNavLink>
<BreadcrumbNavSeparator>
<UtrechtIconChevronRight />
</BreadcrumbNavSeparator>
</Fragment>
{links.length > 1 && (
<BreadcrumbNavSeparator>
<UtrechtIconChevronRight />
</BreadcrumbNavSeparator>
)}
</div>
) : null,
)}
</BreadcrumbNav>
Expand Down

0 comments on commit 103c458

Please sign in to comment.