Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Use popper instead of portal for meatball menu #3663

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 76 additions & 76 deletions src/components/v5/shared/MeatBallMenu/MeatBallMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DotsThree, X } from '@phosphor-icons/react';
import clsx from 'clsx';
import React, { type FC } from 'react';
import { usePopperTooltip } from 'react-popper-tooltip';

import useRelativePortalElement from '~hooks/useRelativePortalElement.ts';
import { useMobile } from '~hooks';
import useToggle from '~hooks/useToggle/index.ts';
import { formatText } from '~utils/intl.ts';
import Portal from '~v5/shared/Portal/index.ts';

import HoverWidthWrapper from '../HoverWidthWrapper/index.ts';
import MenuContainer from '../MenuContainer/index.ts';
Expand All @@ -23,28 +23,32 @@ const MeatBallMenu: FC<MeatBallMenuProps> = ({
renderItemWrapper = DEFAULT_ITEM_WRAPPER_RENDERER,
withVerticalIcon,
contentWrapperClassName,
dropdownPlacementProps,
iconSize = 16,
}) => {
const [
isMenuOpen,
{ toggle: toggleMenu, toggleOff: toggleMenuOff, registerContainerRef },
] = useToggle();
const { portalElementRef, relativeElementRef } = useRelativePortalElement<
HTMLButtonElement,
HTMLDivElement
>([isMenuOpen], dropdownPlacementProps);
const [isMenuOpen, { toggle: toggleMenu, toggleOff: toggleMenuOff }] =
useToggle();

const isMobile = useMobile();

const { getTooltipProps, setTooltipRef, setTriggerRef } = usePopperTooltip({
delayShow: 100,
delayHide: 100,
placement: 'auto-end',
trigger: ['click'],
interactive: true,
visible: isMenuOpen,
onVisibleChange: toggleMenu,
});

if (!items.length) {
throw new Error('There are no items added to the menu.');
}

return (
<div className={clsx(className, 'md:relative')} ref={registerContainerRef}>
<div className={clsx(className, 'relative')}>
<button
type="button"
ref={relativeElementRef}
onClick={disabled ? undefined : toggleMenu}
ref={setTriggerRef}
aria-label={formatText({ id: 'ariaLabel.openMenu' })}
className={clsx(
typeof buttonClassName === 'function'
Expand All @@ -67,55 +71,52 @@ const MeatBallMenu: FC<MeatBallMenuProps> = ({
/>
</button>
{isMenuOpen && (
<Portal>
<MenuContainer
className={clsx(
contentWrapperClassName,
'absolute z-sidebar overflow-y-auto px-6 py-4',
)}
hasShadow
rounded="s"
ref={(ref) => {
registerContainerRef(ref);
portalElementRef.current = ref;
}}
>
<div className="mb-3 flex items-center justify-between sm:hidden">
<p className="uppercase text-gray-400 text-4">
{formatText({ id: 'meatballMenu.selectAction' })}
</p>
<button
type="button"
className="text-gray-400"
onClick={toggleMenuOff}
>
<X size={18} />
</button>
</div>
<ul>
{items.map(
({
key,
label,
onClick,
icon: Icon,
renderItemWrapper: itemRenderItemWrapper,
className: itemClassName,
}) => (
<li
key={key}
className={clsx(itemClassName, 'flex-shrink-0')}
>
<HoverWidthWrapper hoverClassName="w-full md:font-medium">
{(itemRenderItemWrapper || renderItemWrapper)(
{
className: `
<MenuContainer
className={clsx(
contentWrapperClassName,
'absolute !right-0 z-sidebar w-fit min-w-fit overflow-y-auto px-6 py-4',
{
'-translate-x-full': isMobile,
},
)}
hasShadow
rounded="s"
{...getTooltipProps()}
ref={setTooltipRef}
>
<div className="mb-3 flex items-center justify-between sm:hidden">
<p className="uppercase text-gray-400 text-4">
{formatText({ id: 'meatballMenu.selectAction' })}
</p>
<button
type="button"
className="text-gray-400"
onClick={toggleMenuOff}
>
<X size={18} />
</button>
</div>
<ul>
{items.map(
({
key,
label,
onClick,
icon: Icon,
renderItemWrapper: itemRenderItemWrapper,
className: itemClassName,
}) => (
<li key={key} className={clsx(itemClassName, 'flex-shrink-0')}>
<HoverWidthWrapper hoverClassName="w-full md:font-medium">
{(itemRenderItemWrapper || renderItemWrapper)(
{
className: `
flex
items-center
text-md
transition-colors
duration-normal
md:hover:text-gray-900
text-gray-900
md:hover:bg-gray-50
md:hover:font-medium
rounded
Expand All @@ -125,26 +126,25 @@ const MeatBallMenu: FC<MeatBallMenuProps> = ({
flex-grow
-mx-4
`,
onClick: () => {
if (onClick?.() === false) {
return;
}
onClick: () => {
if (onClick?.() === false) {
return;
}

toggleMenuOff();
},
toggleMenuOff();
},
<>
{Icon ? <Icon size={16} /> : null}
<span>{label}</span>
</>,
)}
</HoverWidthWrapper>
</li>
),
)}
</ul>
</MenuContainer>
</Portal>
},
<>
{Icon ? <Icon size={16} /> : null}
<span className="whitespace-nowrap">{label}</span>
</>,
)}
</HoverWidthWrapper>
</li>
),
)}
</ul>
</MenuContainer>
)}
</div>
);
Expand Down
Loading