-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make MenuItem not depend on Button * fix arrow selection of menu items * show only one story of MenuItem and let the user pick the tone * make MenuItem button of type 'button'
- Loading branch information
Showing
6 changed files
with
74 additions
and
43 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
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 |
---|---|---|
@@ -1,64 +1,71 @@ | ||
import { Menu as HeadlessMenu } from '@headlessui/react' | ||
import { Fragment, ReactNode } from 'react' | ||
import { ClassNameProps, Size, Tone } from '../types' | ||
import { Button, ButtonLink, ButtonVariant } from '../button' | ||
import classNames from 'classnames' | ||
import { ClassNameProps, Tone } from '../types' | ||
import { useLinkComponent, useTheme } from '../../framework' | ||
|
||
export type MenuItemProps = ClassNameProps & { | ||
children: ReactNode | ||
disabled?: boolean | ||
} & ( | ||
| { | ||
onClick: () => void | ||
href?: never | ||
} | ||
| { | ||
onClick?: never | ||
href: string | ||
} | ||
) | ||
|
||
const sharedProps = { | ||
variant: ButtonVariant.Transparent, | ||
size: Size.Sm, | ||
tone: Tone.Neutral, | ||
role: 'menuitem', | ||
tone?: Tone.Neutral | Tone.Critical | ||
onClick?: () => void | ||
href?: string | ||
} | ||
|
||
const ROLE = 'menuitem' | ||
|
||
/** | ||
* This component is used to add an item to the [Menu](/docs/components-menu-menu--docs). | ||
* | ||
* It leverages on the `Menu` component of [HeadlessUI](https://headlessui.com/react/menu). | ||
*/ | ||
export function MenuItem({ | ||
className, | ||
disabled, | ||
onClick, | ||
href, | ||
children, | ||
disabled, | ||
tone = Tone.Neutral, | ||
}: MenuItemProps) { | ||
const LinkComponent = useLinkComponent() | ||
const { | ||
menu: { item: theme }, | ||
} = useTheme() | ||
|
||
return ( | ||
<HeadlessMenu.Item as={Fragment}> | ||
{() => { | ||
return href === undefined ? ( | ||
<Button | ||
className={className} | ||
{({ active }) => | ||
href === undefined ? ( | ||
<button | ||
type="button" | ||
className={classNames( | ||
theme.base, | ||
active && theme.active.tone[tone], | ||
disabled ? theme.tone.disabled : theme.tone[tone], | ||
className, | ||
)} | ||
disabled={disabled} | ||
{...sharedProps} | ||
onClick={onClick} | ||
role={ROLE} | ||
> | ||
{children} | ||
</Button> | ||
</button> | ||
) : ( | ||
<ButtonLink | ||
className={className} | ||
<LinkComponent | ||
className={classNames( | ||
theme.base, | ||
active && theme.active.tone[tone], | ||
disabled ? theme.tone.disabled : theme.tone[tone], | ||
className, | ||
)} | ||
disabled={disabled} | ||
{...sharedProps} | ||
role={ROLE} | ||
href={href} | ||
> | ||
{children} | ||
</ButtonLink> | ||
</LinkComponent> | ||
) | ||
}} | ||
} | ||
</HeadlessMenu.Item> | ||
) | ||
} |
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