Skip to content

Commit

Permalink
use floating portal to make menu items list always visible
Browse files Browse the repository at this point in the history
  • Loading branch information
devgioele committed Oct 20, 2023
1 parent 80f845e commit bc9bc84
Show file tree
Hide file tree
Showing 2 changed files with 27 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
13 changes: 9 additions & 4 deletions src/examples/Menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ 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>
Nested container
<div className="overflow-hidden border-2 p-10">
<div ref={elementRef}>
<Story />
</div>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit bc9bc84

Please sign in to comment.