Skip to content

Commit

Permalink
Fix cropped menu items list (#296)
Browse files Browse the repository at this point in the history
* use floating portal to make menu items list always visible

* clarify example of Menu
  • Loading branch information
devgioele authored Oct 23, 2023
1 parent 80f845e commit 7ef7499
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
26 changes: 18 additions & 8 deletions src/components/menu/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Menu as HeadlessMenu } from '@headlessui/react'
import { Fragment, ReactElement, ReactNode, ReactPortal } from 'react'
import { autoUpdate, useFloating, flip, offset } from '@floating-ui/react'
import {
autoUpdate,
useFloating,
flip,
offset,
FloatingPortal,
} from '@floating-ui/react'
import classNames from 'classnames'
import { useTheme } from '../../framework'
import { remToPx } from '../util/remToPx'
Expand Down Expand Up @@ -74,13 +80,17 @@ export function Menu({ className, children, button, placement }: MenuProps) {
})
: button}
</HeadlessMenu.Button>
<HeadlessMenu.Items
ref={refs.setFloating}
className={menu.list.base}
style={floatingStyles}
>
{children}
</HeadlessMenu.Items>
{open && (
<FloatingPortal>
<HeadlessMenu.Items
ref={refs.setFloating}
className={menu.list.base}
style={floatingStyles}
>
{children}
</HeadlessMenu.Items>
</FloatingPortal>
)}
</>
)}
</HeadlessMenu>
Expand Down
15 changes: 11 additions & 4 deletions src/examples/Menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ const meta = {
}, [])

return (
<div className="h-96 overflow-y-scroll border-2" tabIndex={-1}>
<div className="flex h-[70rem] items-center justify-center">
<div ref={elementRef}>
<Story />
<div className="h-screen overflow-y-scroll border-2" tabIndex={-1}>
<div className="flex h-[100rem] items-center justify-center">
<div>
<div className="px-4">
Container with &quot;overflow: hidden&quot;
</div>
<div className="mx-4 flex items-center justify-center overflow-hidden border-2 p-8">
<div ref={elementRef}>
<Story />
</div>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 7ef7499

Please sign in to comment.