Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Rewrite and move breadcrumbs #2604 #2605

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions web/components/src/Breadcrumbs/BreadcrumbList.tsx

This file was deleted.

39 changes: 0 additions & 39 deletions web/components/src/Breadcrumbs/BreadcrumbListItem.tsx

This file was deleted.

1 change: 0 additions & 1 deletion web/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ export * from './Heading'
export * from './Table'
export * from './Tabs'
export * from './Form'
export * from './Breadcrumbs'
15 changes: 15 additions & 0 deletions web/core/Breadcrumbs/BreadcrumbList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { forwardRef, HTMLAttributes } from 'react'
import { twMerge } from 'tailwind-merge'

export type BreadcrumbsProps = HTMLAttributes<HTMLOListElement>

export const BreadcrumbsList = forwardRef<HTMLOListElement, BreadcrumbsProps>(function Breadcrumbs(
{ children, className = '' },
ref
) {
return (
<ol ref={ref} className={twMerge('p-0 list-none text-sm leading-8', className)}>
{children}
</ol>
)
})
23 changes: 23 additions & 0 deletions web/core/Breadcrumbs/BreadcrumbListItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { forwardRef, HTMLAttributes } from 'react'
import { twMerge } from 'tailwind-merge'

export type BreadcrumbsListItemProps = HTMLAttributes<HTMLLIElement> & {
active?: boolean
}

export const BreadcrumbsListItem = forwardRef<HTMLLIElement, BreadcrumbsListItemProps>(
({ children, active, ...rest }, ref) => {
return (
<li
ref={ref}
{...rest}
className={twMerge(
'inline-block pr-2 text-grey-90 after:content-[">"] after:pl-2 last:after:content-[""]',
active ? 'font-medium text-slate-blue-90' : 'font-bold'
)}
>
{children}
</li>
)
}
)
79 changes: 79 additions & 0 deletions web/core/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { BreadcrumbsList } from './index'
import { BackgroundContainer, BackgroundContainerProps } from '@components'
import { BreadcrumbJsonLd } from 'next-seo'
import { useRouter } from 'next/router'
import { twMerge } from 'tailwind-merge'
import NextLink from 'next/link'
import type { Breadcrumb } from '../../types'

type BreadcrumbsProps = {
slug: string
useCustomBreadcrumbs: boolean
defaultBreadcrumbs: Breadcrumb[]
customBreadcrumbs: Breadcrumb[]
className?: string
} & BackgroundContainerProps

const buildJsonLdElements = (crumbs: Breadcrumb[], router: ReturnType<typeof useRouter>) => {
const { pathname, locale } = router

return crumbs.map((item, index) => ({
position: index + 1,
name: item.label,
item: `${pathname}/${item.slug}`,
}))
}

const capitalize = (text: string): string => text[0].toUpperCase() + text.slice(1)

const parseBreadcrumbs = (crumbs: Breadcrumb[]) => {
return crumbs.map((item) => ({
...item,
label: capitalize(item.label),
}))
}

export const Breadcrumbs = ({
slug,
useCustomBreadcrumbs,
defaultBreadcrumbs,
customBreadcrumbs,
background,
className = '',
}: BreadcrumbsProps) => {
const router = useRouter()
const crumbs =
useCustomBreadcrumbs && customBreadcrumbs.length >= 3
? parseBreadcrumbs(customBreadcrumbs)
: parseBreadcrumbs(defaultBreadcrumbs)

if (crumbs.length < 2) return null

return (
<BackgroundContainer background={background} renderFragmentWhenPossible className="mx-auto max-w-viewport">
<nav aria-label="Breadcrumbs">
<BreadcrumbsList className={twMerge(`py-10 px-layout-lg`, className)}>
{crumbs.map((item) => {
const isActive = item.slug === slug
const label = item.label
return (
<BreadcrumbsList.BreadcrumbsListItem key={item.slug} active={isActive}>
{isActive ? (
<span aria-current="page">{label}</span>
) : (
<NextLink
href={item.slug}
className="hover:underline no-underline focus:outline-none focus-visible:envis-outline dark:focus-visible:envis-outline-invert"
>
{label}
</NextLink>
)}
</BreadcrumbsList.BreadcrumbsListItem>
)
})}
</BreadcrumbsList>
</nav>
<BreadcrumbJsonLd itemListElements={buildJsonLdElements(crumbs, router)} />
</BackgroundContainer>
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BreadcrumbsList as Wrapper } from './BreadcrumbList'
import { BreadcrumbsListItem } from './BreadcrumbListItem'
import { Breadcrumbs } from './Breadcrumbs'

type BreadcrumbsListCompoundProps = typeof Wrapper & {
BreadcrumbsListItem: typeof BreadcrumbsListItem
Expand All @@ -9,4 +10,4 @@ const BreadcrumbsList = Wrapper as BreadcrumbsListCompoundProps

BreadcrumbsList.BreadcrumbsListItem = BreadcrumbsListItem

export { BreadcrumbsList }
export { Breadcrumbs, BreadcrumbsList }
2 changes: 1 addition & 1 deletion web/pageComponents/pageTemplates/TopicPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Seo from '../shared/Seo'
import { SharedBanner } from './shared/SharedBanner'
import { PageContent } from './shared/SharedPageContent'
import SharedTitle from './shared/SharedTitle'
import { Breadcrumbs } from '../topicPages/Breadcrumbs'
import { Breadcrumbs } from '@core/Breadcrumbs'

type TopicPageProps = {
data: TopicPageSchema
Expand Down
5 changes: 3 additions & 2 deletions web/pageComponents/topicPages/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { default as NextLink } from 'next/link'
import { BackgroundContainer, BackgroundContainerProps, BreadcrumbsList } from '@components'
import { BreadcrumbsList } from '@core/Breadcrumbs'
import { BackgroundContainer, BackgroundContainerProps } from '@components'
import { BreadcrumbJsonLd } from 'next-seo'
import { useRouter } from 'next/router'
import type { NextRouter } from 'next/router'
import type { NextRouter } from 'next/router'
import { getFullUrl } from '../../common/helpers/getFullUrl'
import { Breadcrumb } from '../../types'
import { twMerge } from 'tailwind-merge'
Expand Down
Loading