-
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.
- Loading branch information
Nemobot
committed
Feb 9, 2022
1 parent
751310d
commit 2f9aaa8
Showing
8 changed files
with
251 additions
and
5 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
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,37 @@ | ||
import { Breadcrumb, unsafeLocalizedString } from "../"; | ||
import { createComponentStories } from "../util"; | ||
|
||
const { defaultExport, createStory } = createComponentStories({ | ||
component: Breadcrumb, | ||
args: {}, | ||
}); | ||
|
||
export default defaultExport; | ||
|
||
export const breadcrumb = createStory({ | ||
items: [ | ||
{ | ||
label: unsafeLocalizedString("Root"), | ||
href: "https://www.example.com", | ||
}, | ||
{ | ||
label: unsafeLocalizedString("1st Level"), | ||
href: "https://www.example.com", | ||
}, | ||
{ | ||
label: unsafeLocalizedString("2nd Level"), | ||
href: "https://www.example.com", | ||
}, | ||
{ | ||
label: unsafeLocalizedString("3rd Level"), | ||
href: "https://www.example.com", | ||
}, | ||
{ | ||
label: unsafeLocalizedString("4th Level"), | ||
href: "https://www.example.com", | ||
}, | ||
{ | ||
label: unsafeLocalizedString("5th Level"), | ||
}, | ||
], | ||
}); |
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