Skip to content

Commit

Permalink
feat(fuselage): Adds detached prop to render Menu on the document…
Browse files Browse the repository at this point in the history
… body (#1087)

Co-authored-by: dougfabris <devfabris@gmail.com>
  • Loading branch information
ggazzo and dougfabris authored Jan 4, 2024
1 parent 91e502a commit 4cd9553
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-mails-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/fuselage": minor
---

feat(fuselage): Adds detached prop to render Menu on the document body
25 changes: 15 additions & 10 deletions packages/fuselage/src/components/Menu/V2/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ComponentProps, ElementType } from 'react';
import React, { cloneElement, useRef } from 'react';
import type { AriaMenuProps } from 'react-aria';
import { useButton, useMenuTrigger } from 'react-aria';
import { createPortal } from 'react-dom';
import type { MenuTriggerProps } from 'react-stately';
import { useMenuTriggerState } from 'react-stately';

Expand All @@ -21,6 +22,7 @@ interface MenuButtonProps<T> extends AriaMenuProps<T>, MenuTriggerProps {
mini?: boolean;
placement?: UsePositionOptions['placement'];
title?: string;
detached?: boolean;
/**
* A component that renders an IconButton
*/
Expand All @@ -39,6 +41,7 @@ const Menu = <T extends object>({
pressed,
maxWidth = 'x250',
button,
detached,
...props
}: MenuButtonProps<T>) => {
const state = useMenuTriggerState(props);
Expand All @@ -55,6 +58,17 @@ const Menu = <T extends object>({
const sizes = { large, medium, tiny, mini };
const defaultSmall = !large && !medium && !tiny && !mini;

const popover = state.isOpen && (
<MenuPopover
state={state}
triggerRef={ref}
placement={getPlacement(placement)}
maxWidth={maxWidth}
>
<MenuDropDown {...props} {...menuProps} />
</MenuPopover>
);

return (
<>
{button ? (
Expand All @@ -78,16 +92,7 @@ const Menu = <T extends object>({
{...sizes}
/>
)}
{state.isOpen && (
<MenuPopover
state={state}
triggerRef={ref}
placement={getPlacement(placement)}
maxWidth={maxWidth}
>
<MenuDropDown {...props} {...menuProps} />
</MenuPopover>
)}
{detached ? createPortal(popover, document.body) : popover}
</>
);
};
Expand Down

0 comments on commit 4cd9553

Please sign in to comment.