-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(web): refactor breadcrumb (#1008)
- Loading branch information
1 parent
2f0c3a8
commit 5b50d83
Showing
12 changed files
with
498 additions
and
5 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
web/src/beta/lib/reearth-ui/components/Breadcrumb/index.stories.tsx
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,120 @@ | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { PopupMenu, PopupMenuItem as PopupItems } from "@reearth/beta/lib/reearth-ui"; | ||
|
||
import { Breadcrumb, BreadcrumbProp, BreadcrumbItem } from "./"; | ||
|
||
const meta: Meta<BreadcrumbProp> = { | ||
component: Breadcrumb, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<BreadcrumbProp>; | ||
|
||
const defaultItems: BreadcrumbItem[] = [ | ||
{ | ||
title: "First", | ||
}, | ||
{ | ||
title: "Second", | ||
}, | ||
{ | ||
title: "Third", | ||
}, | ||
]; | ||
|
||
const multiLevelItems: BreadcrumbItem[] = [ | ||
{ | ||
title: "First Item", | ||
icon: "addStyle", | ||
subItem: [ | ||
{ | ||
id: "one", | ||
title: "Sub Item one", | ||
subItem: [ | ||
{ | ||
id: "sub-one", | ||
title: "Item one", | ||
}, | ||
{ | ||
id: "sub-two", | ||
title: "Item two", | ||
}, | ||
], | ||
}, | ||
{ | ||
id: "two", | ||
title: "Sub Item two", | ||
}, | ||
{ | ||
id: "three", | ||
title: "Sub Item three", | ||
}, | ||
], | ||
}, | ||
{ | ||
title: "Second Item", | ||
}, | ||
]; | ||
|
||
const itemMenu: BreadcrumbItem[] = [ | ||
{ | ||
title: "First Item", | ||
subItem: [ | ||
{ | ||
id: "one-1", | ||
title: "Sub Menu 1", | ||
}, | ||
{ | ||
id: "two-2", | ||
title: "Sub Menu 2", | ||
}, | ||
], | ||
}, | ||
{ | ||
title: "Second Item", | ||
}, | ||
{ | ||
title: "Third Item", | ||
}, | ||
]; | ||
|
||
const renderPopupMenu = (items: BreadcrumbItem[], level: number) => { | ||
return items.map(item => { | ||
if (item.subItem) { | ||
return { | ||
...item, | ||
title: ( | ||
<PopupMenu label={item.title} menu={item.subItem as PopupItems[]} nested={level > 0} /> | ||
), | ||
subItem: undefined, | ||
}; | ||
} | ||
return item; | ||
}); | ||
}; | ||
|
||
export const Default: Story = { | ||
render: () => ( | ||
<div style={{ margin: "5px", height: "10vh" }}> | ||
<Breadcrumb items={defaultItems} /> | ||
</div> | ||
), | ||
}; | ||
|
||
export const DropdownMenu: Story = { | ||
render: () => ( | ||
<div style={{ margin: "5px", height: "10vh" }}> | ||
<Breadcrumb items={renderPopupMenu(itemMenu, 0)} /> | ||
</div> | ||
), | ||
}; | ||
|
||
export const MultiLevelMenu: Story = { | ||
render: () => ( | ||
<div style={{ margin: "5px", height: "10vh" }}> | ||
<Breadcrumb items={renderPopupMenu(multiLevelItems, 0)} /> | ||
</div> | ||
), | ||
}; |
68 changes: 68 additions & 0 deletions
68
web/src/beta/lib/reearth-ui/components/Breadcrumb/index.tsx
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,68 @@ | ||
import { FC, ReactNode } from "react"; | ||
|
||
import { Icon, IconName, Typography } from "@reearth/beta/lib/reearth-ui"; | ||
import { styled, useTheme } from "@reearth/services/theme"; | ||
|
||
import { PopupMenuItem } from "../PopupMenu"; | ||
|
||
export type BreadcrumbItem = { | ||
title: string | ReactNode; | ||
icon?: IconName; | ||
subItem?: PopupMenuItem[]; | ||
}; | ||
|
||
export type BreadcrumbProp = { | ||
items: BreadcrumbItem[]; | ||
separator?: ReactNode; | ||
onClick?: () => void; | ||
}; | ||
|
||
export const Breadcrumb: FC<BreadcrumbProp> = ({ items = [], separator = " / ", onClick }) => { | ||
const theme = useTheme(); | ||
return ( | ||
<Wrapper> | ||
{items.map((item, index) => ( | ||
<ItemWrapper key={index}> | ||
<Item onClick={onClick}> | ||
{typeof item.title === "string" ? ( | ||
<> | ||
{item.icon && <Icon icon={item.icon} size="small" color={theme.content.weak} />} | ||
<Typography weight="bold" size="body"> | ||
{item.title} | ||
</Typography> | ||
</> | ||
) : ( | ||
item.title | ||
)} | ||
</Item> | ||
{index < items.length - 1 && <Separator>{separator}</Separator>} | ||
</ItemWrapper> | ||
))} | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
const Wrapper = styled("div")(({ theme }) => ({ | ||
display: "flex", | ||
gap: theme.spacing.smallest, | ||
})); | ||
|
||
const ItemWrapper = styled("div")(({ theme }) => ({ | ||
display: "flex", | ||
alignItems: "center", | ||
gap: theme.spacing.smallest, | ||
cursor: "pointer", | ||
})); | ||
|
||
const Item = styled("div")(({ theme }) => ({ | ||
padding: `${theme.spacing.micro}px ${theme.spacing.small}px`, | ||
borderRadius: theme.radius.smallest, | ||
display: "flex", | ||
gap: theme.spacing.smallest, | ||
alignItems: "center", | ||
})); | ||
|
||
const Separator = styled("div")(({ theme }) => ({ | ||
color: theme.content.weak, | ||
userSelect: "none", | ||
})); |
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
3 changes: 3 additions & 0 deletions
3
web/src/beta/lib/reearth-ui/components/Icon/Icons/CaretRight.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.