Skip to content

Commit

Permalink
feat: Breadcrumb improvements (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmar authored Apr 10, 2023
1 parent 542df72 commit ad0c45a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/components/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {
ReactNode,
forwardRef,
useCallback,
useContext,
useEffect,
useId,
useRef,
Expand All @@ -19,7 +20,7 @@ import usePrevious from '../hooks/usePrevious'
import { Select } from './Select'
import { ListBoxItem } from './ListBoxItem'
import { useNavigationContext } from './contexts/NavigationContext'
import { Breadcrumb, useBreadcrumbs } from './contexts/BreadcrumbsContext'
import { Breadcrumb, BreadcrumbsContext } from './contexts/BreadcrumbsContext'

function getCrumbKey(crumb: Breadcrumb) {
const maybeKey = crumb?.key
Expand Down Expand Up @@ -239,6 +240,7 @@ type BreadcrumbsProps = {
minLength?: number
maxLength?: number
collapsible?: boolean
breadcrumbs?: Breadcrumb[]
} & FlexProps

export function BreadcrumbsInside({
Expand Down Expand Up @@ -338,9 +340,17 @@ export function Breadcrumbs({
minLength = 0,
maxLength = Infinity,
collapsible = true,
breadcrumbs: propsCrumbs,
...props
}: BreadcrumbsProps) {
const { breadcrumbs } = useBreadcrumbs()
const { breadcrumbs: contextCrumbs } = useContext(BreadcrumbsContext)
const breadcrumbs = propsCrumbs || contextCrumbs

if (!breadcrumbs) {
throw Error(
"<Breadcrumbs> must be provided a 'breadcrumbs' prop or used inside a <BreadcrumbProvider>"
)
}
const prevBreadcrumbs = usePrevious(breadcrumbs)
const transitionKey = useRef<number>(0)

Expand Down
24 changes: 21 additions & 3 deletions src/components/contexts/BreadcrumbsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {
ReactNode,
useContext,
useEffect,
useMemo,
useState,
} from 'react'

Expand All @@ -28,14 +29,31 @@ export type Breadcrumb = BreadcrumbBase &
}
)

const BreadcrumbsContext = React.createContext<BreadcrumbsContextT | null>(null)
export const BreadcrumbsContext =
React.createContext<BreadcrumbsContextT | null>(null)

export function BreadcrumbsProvider({ children }: PropsWithChildren) {
export function BreadcrumbsProvider({
children,
breadcrumbsTransform,
}: PropsWithChildren<{
breadcrumbsTransform?: (breadcrumbs: Breadcrumb[]) => Breadcrumb[]
}>) {
const [breadcrumbs, setBreadcrumbs] = useState<Breadcrumb[]>([])

const contextVal = useMemo(
() => ({
breadcrumbs:
typeof breadcrumbsTransform === 'function'
? breadcrumbsTransform(breadcrumbs)
: breadcrumbs,
setBreadcrumbs,
}),
[breadcrumbsTransform, breadcrumbs]
)

return (
// eslint-disable-next-line react/jsx-no-constructed-context-values
<BreadcrumbsContext.Provider value={{ breadcrumbs, setBreadcrumbs }}>
<BreadcrumbsContext.Provider value={contextVal}>
{children}
</BreadcrumbsContext.Provider>
)
Expand Down

0 comments on commit ad0c45a

Please sign in to comment.