-
Notifications
You must be signed in to change notification settings - Fork 0
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 #17 from tuzkituan/dev/menu
New: Menu component
- Loading branch information
Showing
9 changed files
with
251 additions
and
12 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
File renamed without changes.
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,67 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { Text } from "../text/text"; | ||
import { Menu } from "./menu"; | ||
|
||
const meta = { | ||
title: "OVERLAY/Menu", | ||
component: Menu, | ||
tags: ["autodocs"], | ||
parameters: { | ||
layout: "centered", | ||
}, | ||
argTypes: { | ||
children: { | ||
type: "string", | ||
}, | ||
placement: { | ||
description: "Menu placement", | ||
options: [ | ||
"top-center", | ||
"top-start", | ||
"top-end", | ||
"left-start", | ||
"left-center", | ||
"left-end", | ||
"right-start", | ||
"right-center", | ||
"right-end", | ||
"bottom-start", | ||
"bottom-center", | ||
"bottom-end", | ||
], | ||
control: { type: "radio" }, | ||
}, | ||
items: {}, | ||
}, | ||
} satisfies Meta<typeof Menu>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Primary: Story = { | ||
render: ({ | ||
children, | ||
items = [ | ||
{ | ||
key: 1, | ||
title: "Option 1", | ||
}, | ||
{ | ||
key: 2, | ||
title: "Option 2", | ||
}, | ||
{ | ||
key: 3, | ||
title: "Option 3", | ||
}, | ||
], | ||
...rest | ||
}) => ( | ||
<Menu items={items} {...rest}> | ||
<Text className="underline cursor-pointer">{children}</Text> | ||
</Menu> | ||
), | ||
args: { | ||
children: "Open menu", | ||
}, | ||
}; |
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,99 @@ | ||
import { AnimatePresence, motion } from "framer-motion"; | ||
import { useMemo, useState } from "react"; | ||
import { Arrow, useLayer } from "react-laag"; | ||
import { twMerge } from "tailwind-merge"; | ||
import { useComponentStyle } from "../../customization/styles/theme.context"; | ||
import { IMenu, IMenuItem, IMenuKey } from "./menu.types"; | ||
|
||
export const Menu = ({ | ||
children, | ||
placement, | ||
open, | ||
onOpenChange, | ||
dropdownClassName, | ||
items = [], | ||
onMenuClick, | ||
}: IMenu) => { | ||
const [isOpen, setOpen] = useState(false); | ||
|
||
const theme = useComponentStyle("Menu"); | ||
|
||
const { triggerProps, layerProps, arrowProps, renderLayer } = useLayer({ | ||
isOpen: open || isOpen, | ||
placement, | ||
auto: true, | ||
triggerOffset: 10, | ||
onOutsideClick: () => { | ||
onOpenChange && onOpenChange(false); | ||
setOpen(false); | ||
}, | ||
}); | ||
|
||
const classes = useMemo(() => { | ||
return twMerge(theme.base(), dropdownClassName); | ||
}, [dropdownClassName, theme]); | ||
|
||
const itemClasses = useMemo(() => { | ||
return twMerge(theme.item()); | ||
}, [theme]); | ||
|
||
const arrowClasses = useMemo(() => { | ||
return twMerge(theme.arrow()); | ||
}, [theme]); | ||
|
||
const onMenuItemClick = (value: IMenuKey) => { | ||
if (value) { | ||
onMenuClick?.(value); | ||
} | ||
}; | ||
|
||
const renderMenuItems = () => { | ||
return items.map((x: IMenuItem) => ( | ||
<li | ||
key={x.key} | ||
value={x.key} | ||
className={itemClasses} | ||
onClick={(e) => onMenuItemClick((e.target as HTMLInputElement).value)} | ||
> | ||
{x.title} | ||
</li> | ||
)); | ||
}; | ||
|
||
return ( | ||
<> | ||
<span | ||
{...triggerProps} | ||
onClick={() => { | ||
const _isOpen = !isOpen; | ||
setOpen(_isOpen); | ||
onOpenChange && onOpenChange(_isOpen); | ||
}} | ||
> | ||
{children} | ||
</span> | ||
{(open || isOpen) && | ||
renderLayer( | ||
<AnimatePresence> | ||
<motion.ul | ||
initial={{ opacity: 0 }} | ||
animate={{ opacity: 1 }} | ||
exit={{ opacity: 0 }} | ||
className={classes} | ||
{...layerProps} | ||
> | ||
{renderMenuItems()} | ||
<Arrow | ||
{...arrowProps} | ||
backgroundColor="var(--background-sec)" | ||
borderColor="var(--line-primary)" | ||
className={arrowClasses} | ||
borderWidth={1} | ||
size={8} | ||
/> | ||
</motion.ul> | ||
</AnimatePresence> | ||
)} | ||
</> | ||
); | ||
}; |
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,27 @@ | ||
export type IMenuKey = number | string; | ||
export interface IMenu { | ||
placement?: | ||
| "top-center" | ||
| "top-start" | ||
| "top-end" | ||
| "left-start" | ||
| "left-center" | ||
| "left-end" | ||
| "right-start" | ||
| "right-center" | ||
| "right-end" | ||
| "bottom-start" | ||
| "bottom-center" | ||
| "bottom-end"; | ||
open?: boolean; | ||
onOpenChange?: (open: boolean) => void; | ||
children?: React.ReactNode; | ||
dropdownClassName?: string; | ||
onMenuClick?: (key: IMenuKey) => void; | ||
items?: IMenuItem[]; | ||
} | ||
|
||
export interface IMenuItem { | ||
title?: React.ReactNode; | ||
key: IMenuKey; | ||
} |
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 |
---|---|---|
@@ -1,17 +1,18 @@ | ||
export * from "./button.styles"; | ||
export * from "./alert.styles"; | ||
export * from "./avatar.styles"; | ||
export * from "./box.styles"; | ||
export * from "./text.styles"; | ||
export * from "./tooltip.styles"; | ||
export * from "./popover.styles"; | ||
export * from "./button.styles"; | ||
export * from "./center.styles"; | ||
export * from "./checkbox.styles"; | ||
export * from "./flex.styles"; | ||
export * from "./avatar.styles"; | ||
export * from "./input.styles"; | ||
export * from "./number-input.styles"; | ||
export * from "./checkbox.styles"; | ||
export * from "./menu.styles"; | ||
export * from "./modal.styles"; | ||
export * from "./alert.styles"; | ||
export * from "./tabs.styles"; | ||
export * from "./number-input.styles"; | ||
export * from "./popover.styles"; | ||
export * from "./radio.styles"; | ||
export * from "./toast.styles"; | ||
export * from "./select.styles"; | ||
export * from "./tabs.styles"; | ||
export * from "./text.styles"; | ||
export * from "./toast.styles"; | ||
export * from "./tooltip.styles"; |
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,28 @@ | ||
import { cva } from "class-variance-authority"; | ||
|
||
const base = cva([ | ||
"rounded-lg", | ||
"p-1", | ||
"bg-sec-background", | ||
"text-primary-text", | ||
"text-base", | ||
"border", | ||
"border-line-primary", | ||
"shadow-sm", | ||
]); | ||
const item = cva([ | ||
"w-full", | ||
"px-2", | ||
"py-1.5", | ||
"rounded", | ||
"hover:bg-line-primary", | ||
"cursor-pointer", | ||
]); | ||
const arrow = cva([]); | ||
const menuStyles = { | ||
base, | ||
arrow, | ||
item, | ||
}; | ||
|
||
export { menuStyles }; |
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