-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from buildo/3105850-implement_breadcrumbs
#3105850: Implement Breadcrumbs
- Loading branch information
Showing
16 changed files
with
419 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { useBreadcrumbItem, useBreadcrumbs } from "@react-aria/breadcrumbs"; | ||
import { useRef, Fragment, FunctionComponent } from "react"; | ||
import { IconProps } from "src/Icons/IconProps"; | ||
import { IconChevronRight, Label, LinkProps, LocalizedString } from "../"; | ||
import { Box, Inline, BentoSprinkles } from "../internal"; | ||
|
||
type LastItem = { | ||
label: LocalizedString; | ||
}; | ||
|
||
type Item = LastItem & { | ||
href: string; | ||
}; | ||
|
||
export type BreadcrumbProps = { | ||
items: [...Item[], LastItem]; | ||
}; | ||
|
||
type BreadcrumbItemProps = LastItem & Partial<Item> & { isCurrent: boolean }; | ||
|
||
type BreadcrumbConfig = { | ||
separator: FunctionComponent<IconProps>; | ||
separatorSize: IconProps["size"]; | ||
space: BentoSprinkles["gap"]; | ||
}; | ||
|
||
export function createBreadcrumb( | ||
Link: FunctionComponent<LinkProps>, | ||
config: BreadcrumbConfig = { | ||
separator: IconChevronRight, | ||
separatorSize: "8", | ||
space: "16", | ||
} | ||
) { | ||
const BreadcrumbItem = createBreadcrumbItem(Link); | ||
const Separator = config.separator; | ||
return function Breadcrumb(props: BreadcrumbProps) { | ||
const children = ( | ||
<Box as="ol"> | ||
<Inline space={config.space} alignY="center"> | ||
{props.items.map((item, idx) => { | ||
const isCurrent = idx === props.items.length - 1; | ||
return ( | ||
<Fragment key={idx}> | ||
<BreadcrumbItem isCurrent={isCurrent} {...item} /> | ||
{!isCurrent && ( | ||
<Box as="span" aria-hidden="true"> | ||
<Separator size={config.separatorSize} /> | ||
</Box> | ||
)} | ||
</Fragment> | ||
); | ||
})} | ||
</Inline> | ||
</Box> | ||
); | ||
const { navProps } = useBreadcrumbs({ children }); | ||
return ( | ||
<Box as="nav" {...navProps} color={undefined}> | ||
{children} | ||
</Box> | ||
); | ||
}; | ||
} | ||
|
||
function createBreadcrumbItem(Link: FunctionComponent<LinkProps>) { | ||
return function BreadcrumbItem({ isCurrent, label, href = "" }: BreadcrumbItemProps) { | ||
const ref = useRef(null); | ||
const { itemProps } = useBreadcrumbItem( | ||
{ children: label, isCurrent, elementType: "div" }, | ||
ref | ||
); | ||
|
||
return ( | ||
<Box as="li" ref={ref}> | ||
{isCurrent ? ( | ||
<Label size="large" {...itemProps} color={undefined}> | ||
{label} | ||
</Label> | ||
) : ( | ||
<Link | ||
label={label} | ||
href={href} | ||
title={label} | ||
// NOTE(gabro): we need the cast due to a minor inconsistency in the callback type of FocusEventHandler | ||
{...itemProps} | ||
/> | ||
)} | ||
</Box> | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IconProps } from "./IconProps"; | ||
import { svgIconProps } from "./svgIconProps"; | ||
|
||
export function IconChevronRight(props: IconProps) { | ||
return ( | ||
<svg {...svgIconProps(props)}> | ||
<path d="M5.627 23.372a2.138 2.138 0 0 0 3.029 0l9.858-9.858a2.14 2.14 0 0 0 0-3.029L8.656.627a2.141 2.141 0 0 0-3.029 3.029l8.334 8.354-8.334 8.334a2.158 2.158 0 0 0 0 3.028Z" /> | ||
</svg> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { bentoSprinkles } from "../internal/sprinkles.css"; | ||
|
||
export const link = bentoSprinkles({ | ||
cursor: { | ||
default: "pointer", | ||
disabled: "notAllowed", | ||
}, | ||
outline: "none", | ||
}); |
Oops, something went wrong.