Fixed context menu on index page (#9316)

Fixes https://github.com/twentyhq/twenty/issues/8970

The context menu wasn't working because of wrong architecture with the
Dropdown component, as it's a unique behavior (no clickable component
and portal) it also required refactoring a bit the Dropdown component.

- Context menu now uses a portal
- Fixed dropdown offset without clickable component (now using a
fallback anchor component)
- Fixed React array key props
This commit is contained in:
Lucas Bordeau
2025-01-03 11:11:33 +01:00
committed by GitHub
parent 0674388426
commit 8333892647
4 changed files with 63 additions and 56 deletions

View File

@ -5,30 +5,22 @@ import { ActionMenuDropdownHotkeyScope } from '@/action-menu/types/ActionMenuDro
import { getActionMenuDropdownIdFromActionMenuId } from '@/action-menu/utils/getActionMenuDropdownIdFromActionMenuId'; import { getActionMenuDropdownIdFromActionMenuId } from '@/action-menu/utils/getActionMenuDropdownIdFromActionMenuId';
import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown'; import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer'; import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown';
import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow'; import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2'; import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { extractComponentState } from '@/ui/utilities/state/component-state/utils/extractComponentState'; import { extractComponentState } from '@/ui/utilities/state/component-state/utils/extractComponentState';
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { useRecoilValue } from 'recoil'; import { useRecoilValue } from 'recoil';
import { MenuItem } from 'twenty-ui'; import { MenuItem } from 'twenty-ui';
import { PositionType } from '../types/PositionType';
type StyledContainerProps = { const StyledDropdownMenuContainer = styled.div`
position: PositionType; width: 100%;
}; height: 100%;
const StyledContainerActionMenuDropdown = styled.div<StyledContainerProps>`
align-items: flex-start;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: center;
left: ${(props) => `${props.position.x}px`}; align-items: center;
position: fixed;
top: ${(props) => `${props.position.y}px`};
transform: translateX(-50%);
width: 0;
height: 0;
`; `;
export const RecordIndexActionMenuDropdown = () => { export const RecordIndexActionMenuDropdown = () => {
@ -40,10 +32,13 @@ export const RecordIndexActionMenuDropdown = () => {
ActionMenuComponentInstanceContext, ActionMenuComponentInstanceContext,
); );
const dropdownId = getActionMenuDropdownIdFromActionMenuId(actionMenuId);
const { closeDropdown } = useDropdown(dropdownId);
const actionMenuDropdownPosition = useRecoilValue( const actionMenuDropdownPosition = useRecoilValue(
extractComponentState( extractComponentState(
recordIndexActionMenuDropdownPositionComponentState, recordIndexActionMenuDropdownPositionComponentState,
getActionMenuDropdownIdFromActionMenuId(actionMenuId), dropdownId,
), ),
); );
@ -55,32 +50,37 @@ export const RecordIndexActionMenuDropdown = () => {
: undefined; : undefined;
return ( return (
<StyledContainerActionMenuDropdown <Dropdown
position={actionMenuDropdownPosition} dropdownId={dropdownId}
className="action-menu-dropdown" dropdownHotkeyScope={{
> scope: ActionMenuDropdownHotkeyScope.ActionMenuDropdown,
<Dropdown }}
dropdownId={getActionMenuDropdownIdFromActionMenuId(actionMenuId)} data-select-disable
dropdownHotkeyScope={{ dropdownMenuWidth={width}
scope: ActionMenuDropdownHotkeyScope.ActionMenuDropdown, dropdownPlacement="bottom-start"
}} dropdownStrategy="absolute"
data-select-disable dropdownOffset={{
dropdownMenuWidth={width} x: actionMenuDropdownPosition.x ?? 0,
dropdownComponents={ y: actionMenuDropdownPosition.y ?? 0,
}}
dropdownComponents={
<StyledDropdownMenuContainer className="action-menu-dropdown">
<DropdownMenuItemsContainer> <DropdownMenuItemsContainer>
{actionMenuEntries.map((item, index) => ( {actionMenuEntries.map((item) => (
<MenuItem <MenuItem
key={index} key={item.key}
LeftIcon={item.Icon} LeftIcon={item.Icon}
onClick={item.onClick} onClick={() => {
item.onClick?.();
closeDropdown();
}}
accent={item.accent} accent={item.accent}
text={item.label} text={item.label}
/> />
))} ))}
</DropdownMenuItemsContainer> </DropdownMenuItemsContainer>
} </StyledDropdownMenuContainer>
avoidPortal }
/> />
</StyledContainerActionMenuDropdown>
); );
}; };

View File

@ -120,13 +120,5 @@ export const WithInteractions: Story = {
const deleteButton = await canvas.findByText('Delete'); const deleteButton = await canvas.findByText('Delete');
await userEvent.click(deleteButton); await userEvent.click(deleteButton);
expect(deleteMock).toHaveBeenCalled(); expect(deleteMock).toHaveBeenCalled();
const markAsDoneButton = await canvas.findByText('Mark as done');
await userEvent.click(markAsDoneButton);
expect(markAsDoneMock).toHaveBeenCalled();
const addToFavoritesButton = await canvas.findByText('Add to favorites');
await userEvent.click(addToFavoritesButton);
expect(addToFavoritesMock).toHaveBeenCalled();
}, },
}; };

View File

@ -10,8 +10,12 @@ type ArrayDisplayProps = {
export const ArrayDisplay = ({ value }: ArrayDisplayProps) => { export const ArrayDisplay = ({ value }: ArrayDisplayProps) => {
return ( return (
<ExpandableList> <ExpandableList>
{value?.map((item) => ( {value?.map((item, index) => (
<Chip variant={ChipVariant.Highlighted} label={item} /> <Chip
key={`${item}-${index}`}
variant={ChipVariant.Highlighted}
label={item}
/>
))} ))}
</ExpandableList> </ExpandableList>
); );

View File

@ -19,10 +19,17 @@ import { DropdownUnmountEffect } from '@/ui/layout/dropdown/components/DropdownU
import { DropdownComponentInstanceContext } from '@/ui/layout/dropdown/contexts/DropdownComponeInstanceContext'; import { DropdownComponentInstanceContext } from '@/ui/layout/dropdown/contexts/DropdownComponeInstanceContext';
import { dropdownMaxHeightComponentStateV2 } from '@/ui/layout/dropdown/states/dropdownMaxHeightComponentStateV2'; import { dropdownMaxHeightComponentStateV2 } from '@/ui/layout/dropdown/states/dropdownMaxHeightComponentStateV2';
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2'; import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';
import styled from '@emotion/styled';
import { flushSync } from 'react-dom'; import { flushSync } from 'react-dom';
import { isDefined } from 'twenty-ui'; import { isDefined } from 'twenty-ui';
import { DropdownOnToggleEffect } from './DropdownOnToggleEffect'; import { DropdownOnToggleEffect } from './DropdownOnToggleEffect';
const StyledDropdownFallbackAnchor = styled.div`
left: 0;
position: absolute;
top: 0;
`;
type DropdownProps = { type DropdownProps = {
className?: string; className?: string;
clickableComponent?: ReactNode; clickableComponent?: ReactNode;
@ -53,7 +60,7 @@ export const Dropdown = ({
dropdownHotkeyScope, dropdownHotkeyScope,
dropdownPlacement = 'bottom-end', dropdownPlacement = 'bottom-end',
dropdownStrategy = 'absolute', dropdownStrategy = 'absolute',
dropdownOffset = { x: 0, y: 0 }, dropdownOffset,
onClickOutside, onClickOutside,
onClose, onClose,
onOpen, onOpen,
@ -61,24 +68,27 @@ export const Dropdown = ({
}: DropdownProps) => { }: DropdownProps) => {
const { isDropdownOpen, toggleDropdown } = useDropdown(dropdownId); const { isDropdownOpen, toggleDropdown } = useDropdown(dropdownId);
const offsetMiddlewares = [];
const setDropdownMaxHeight = useSetRecoilComponentStateV2( const setDropdownMaxHeight = useSetRecoilComponentStateV2(
dropdownMaxHeightComponentStateV2, dropdownMaxHeightComponentStateV2,
dropdownId, dropdownId,
); );
if (isDefined(dropdownOffset.x)) { const isUsingOffset =
offsetMiddlewares.push(offset({ crossAxis: dropdownOffset.x })); isDefined(dropdownOffset?.x) || isDefined(dropdownOffset?.y);
}
if (isDefined(dropdownOffset.y)) { const offsetMiddleware = isUsingOffset
offsetMiddlewares.push(offset({ mainAxis: dropdownOffset.y })); ? [
} offset({
crossAxis: dropdownOffset?.x ?? 0,
mainAxis: dropdownOffset?.y ?? 0,
}),
]
: [];
const { refs, floatingStyles, placement } = useFloating({ const { refs, floatingStyles, placement } = useFloating({
placement: dropdownPlacement, placement: dropdownPlacement,
middleware: [ middleware: [
...offsetMiddleware,
flip(), flip(),
size({ size({
padding: 32, padding: 32,
@ -89,7 +99,6 @@ export const Dropdown = ({
}, },
boundary: document.querySelector('#root') ?? undefined, boundary: document.querySelector('#root') ?? undefined,
}), }),
...offsetMiddlewares,
], ],
whileElementsMounted: autoUpdate, whileElementsMounted: autoUpdate,
strategy: dropdownStrategy, strategy: dropdownStrategy,
@ -109,13 +118,15 @@ export const Dropdown = ({
> >
<DropdownScope dropdownScopeId={getScopeIdFromComponentId(dropdownId)}> <DropdownScope dropdownScopeId={getScopeIdFromComponentId(dropdownId)}>
<> <>
{clickableComponent && ( {isDefined(clickableComponent) ? (
<div <div
ref={refs.setReference} ref={refs.setReference}
onClick={handleClickableComponentClick} onClick={handleClickableComponentClick}
> >
{clickableComponent} {clickableComponent}
</div> </div>
) : (
<StyledDropdownFallbackAnchor ref={refs.setReference} />
)} )}
{isDropdownOpen && ( {isDropdownOpen && (
<DropdownContent <DropdownContent
@ -124,7 +135,7 @@ export const Dropdown = ({
dropdownMenuWidth={dropdownMenuWidth} dropdownMenuWidth={dropdownMenuWidth}
dropdownComponents={dropdownComponents} dropdownComponents={dropdownComponents}
dropdownId={dropdownId} dropdownId={dropdownId}
dropdownPlacement={placement ?? 'bottom-end'} dropdownPlacement={placement}
floatingUiRefs={refs} floatingUiRefs={refs}
hotkeyScope={dropdownHotkeyScope} hotkeyScope={dropdownHotkeyScope}
hotkey={hotkey} hotkey={hotkey}