Skip to content

Commit

Permalink
Upwards Menu (#213)
Browse files Browse the repository at this point in the history
* add direction prop to menu component

* make downwards menu list absolute
  • Loading branch information
devgioele authored Jun 26, 2023
1 parent d3f6ede commit 8babc27
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/components/menu/Menu.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs'
import { action } from '@storybook/addon-actions'
import { Theme } from '../../../.storybook/components'
import { Menu } from './Menu'
import { Menu, MenuDirection } from './Menu'
import { MenuItem } from './MenuItem'

<Meta title="Components/Menu/Menu" component={Menu} />
Expand Down Expand Up @@ -32,6 +32,7 @@ export const Template = ({ menuLabel, className, ...args }) => {
args={{
menuLabel: 'Menu',
menuButtonContent: 'Menu',
direction: MenuDirection.down,
}}
>
{Template.bind({})}
Expand Down
26 changes: 21 additions & 5 deletions src/components/menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { ReactNode } from 'react'
import { useTheme } from '../../framework'
import { ClassNameProps } from '../types'

export enum MenuDirection {
up = 'up',
down = 'down',
}

export type MenuProps = ClassNameProps & {
/**
* Defines the accessibility label for the menu.
Expand All @@ -19,6 +24,7 @@ export type MenuProps = ClassNameProps & {
**/
menuButtonId?: string
children?: ReactNode
direction: MenuDirection
}

export function Menu({
Expand All @@ -27,13 +33,25 @@ export function Menu({
menuButtonContent,
children,
menuButtonId,
direction,
}: MenuProps) {
const { menu } = useTheme()

const items = (
<HeadlessMenu.Items
className={classNames(
menu.menuList.base,
menu.menuList.direction[direction]
)}
>
{children}
</HeadlessMenu.Items>
)
return (
<HeadlessMenu>
{({ open }) => (
<>
<div className="relative">
{direction === MenuDirection.up && items}
<HeadlessMenu.Button
id={menuButtonId}
aria-label={menuLabel}
Expand All @@ -49,10 +67,8 @@ export function Menu({
/>
</span>
</HeadlessMenu.Button>
<HeadlessMenu.Items className={menu.menuList.base}>
{children}
</HeadlessMenu.Items>
</>
{direction === MenuDirection.down && items}
</div>
)}
</HeadlessMenu>
)
Expand Down
6 changes: 5 additions & 1 deletion src/components/menu/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export default {
active: 'bg-primary-100',
},
menuList: {
base: 'py-2 mb-1 w-32 shadow focus:outline-none bg-white',
base: 'py-2 mb-2 w-32 shadow focus:outline-none bg-white absolute',
direction: {
up: '-mt-2 -translate-y-full',
down: '',
},
},
}

0 comments on commit 8babc27

Please sign in to comment.