Skip to content

Commit

Permalink
Adding renderLink optional prop to Breadcrumbs so we can override the…
Browse files Browse the repository at this point in the history
… default anchor and pass a Link component
  • Loading branch information
corbanbrook committed Nov 28, 2023
1 parent 362ad5f commit 376e43c
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/components/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ReactNode } from 'react'

import { Box, BoxProps } from '~/components/Box'
import { Divider } from '~/components/Divider'
import { Text } from '~/components/Text'
Expand All @@ -10,10 +12,11 @@ interface Path {
type BreadcrumbsProps = BoxProps & {
excludeDivider?: boolean
paths: Path[]
renderLink?: (path: Path, children: ReactNode) => JSX.Element
}

export const Breadcrumbs = (props: BreadcrumbsProps) => {
const { paths, excludeDivider = false, ...rest } = props
const { paths, excludeDivider = false, renderLink, ...rest } = props

return (
<Box {...rest}>
Expand All @@ -22,6 +25,7 @@ export const Breadcrumbs = (props: BreadcrumbsProps) => {
key={idx}
path={path}
active={idx === paths.length - 1}
renderLink={renderLink}
/>
))}

Expand All @@ -33,10 +37,15 @@ export const Breadcrumbs = (props: BreadcrumbsProps) => {
interface BreadcrumbSegmentProps {
path: Path
active?: boolean
renderLink?: (path: Path, children: ReactNode) => JSX.Element
}

const defaultRenderLink = (path: Path, children: ReactNode) => (
<a href={path.url}>{children}</a>
)

const BreadcrumbSegment = (props: BreadcrumbSegmentProps) => {
const { path, active } = props
const { path, active, renderLink = defaultRenderLink } = props

return active ? (
<Text
Expand All @@ -49,17 +58,18 @@ const BreadcrumbSegment = (props: BreadcrumbSegmentProps) => {
{path.label}
</Text>
) : (
<Text
as="a"
href={path.url}
variant="small"
fontWeight="medium"
color="text50"
whiteSpace="nowrap"
capitalize
>
{path.label}
{' / '}
</Text>
renderLink(
path,
<Text
variant="small"
fontWeight="medium"
color="text50"
whiteSpace="nowrap"
capitalize
>
{path.label}
{' / '}
</Text>
)
)
}

0 comments on commit 376e43c

Please sign in to comment.